Skip to content

Commit

Permalink
SNC-14953 Add show config to if-range config. (#138)
Browse files Browse the repository at this point in the history
* Add show config to if-range config.

* Show config for if-range and vrf using updated show_ci_output() with j2-template=None.

* Show config for if-range and vrf use new show_ci_rendered_output() (no jinja2).
  • Loading branch information
eddyatdell authored and GitHub Enterprise committed Aug 10, 2022
1 parent ccbcc03 commit f043b65
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 12 deletions.
17 changes: 15 additions & 2 deletions CLI/actioner/sonic_cli_if.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
from rpipe_utils import pipestr
import cli_client as cc
from netaddr import *
from scripts.render_cli import show_cli_output
from scripts.render_cli import show_cli_output, show_cli_rendered_output
import os
import syslog
import traceback
from natsort import natsorted
Expand Down Expand Up @@ -2104,6 +2105,18 @@ def invoke_api(func, args=[]):
show_args.append(view_key_str)
sonic_cli_show_config.run("show_view", show_args)
return
elif func == "showrun_if_range":
if not args[0] in [ 'configure-if', 'configure-lag', 'configure-vlan' ]:
return
iflistStr = args[1].split("=")[1]
iflist = iflistStr.rstrip().split(",")
viewid = 'views=' + args[0]
showrun_list = []
for intf in iflist:
showrun_list.append(('show_view', viewid, 'view_keys="name={}"'.format(intf)))
rcfgall = sonic_cli_show_config.showconfig_views_to_buffer(showrun_list)
show_cli_rendered_output(rcfgall)
return
return api.cli_not_implemented(func)


Expand Down Expand Up @@ -2274,7 +2287,7 @@ def run(func, args):
elif func == "get_sonic_port_table":
show_cli_output(args[0], response)
return
elif func == "showrun":
elif func == "showrun" or func == "showrun_if_range":
return

if response.ok():
Expand Down
36 changes: 31 additions & 5 deletions CLI/actioner/sonic_cli_show_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1017,16 +1017,23 @@ def parse_command_line(command_line):
cli_view.view_cmd_list.append(command_line)


def render_cli_config(view_name="", view_keys={}):
def render_cli_config(view_name="", view_keys={}, cacheAllKeys=False):
global CMDS_STRING, CMDS_VIEW, CMDS_VIEW_CHANGED, CMDS_SAVE_BUFFER
context = {"view_name": view_name, "view_keys": view_keys}

if cacheAllKeys:
# force DB_cache to get all table keys
context = {"view_name": view_name, "view_keys": None}
else:
context = {"view_name": view_name, "view_keys": view_keys}

# Call application
# set cache
try:
if view_name:
for module_cb in module_startup_cb.get(view_name, []):
mod_name = inspect.getmodule(module_cb).__name__
if cacheAllKeys and DB_Cache.get(mod_name) != None:
continue
DB_Cache[mod_name] = {}
module_cb(context, DB_Cache[mod_name])

Expand Down Expand Up @@ -1098,7 +1105,7 @@ def read_cli_format_file(format_file):
parse_command_line(command)


def show_views(func, args=[]):
def show_views(func, args=[], cacheAllKeys=False):

showrun_log(logging.DEBUG, "args {}", args)

Expand Down Expand Up @@ -1137,7 +1144,7 @@ def show_views(func, args=[]):
showrun_log(logging.DEBUG, "view-keys {}", viewKV)

for view_name in views:
if render_cli_config(view_name, viewKV):
if render_cli_config(view_name, viewKV, cacheAllKeys=cacheAllKeys):
# write() returns True if terminated
break

Expand All @@ -1161,8 +1168,27 @@ def showconfig_views_to_buffer(viewlist):
if not viewlist:
render_cli_config()
else:
# check repeating (ie. roughly first 5) show_view for the same viewid
# (used by show config within interface ranges with different keys)
# If repeating, force DB_cache to get all table keys once and use this
# cache to read each key, and not do multiple separate key fetches from DB.
sameviewid = False
viewid0 = viewlist[0][1]
vcnt = 0
for to_show in viewlist:
if 'show_view' == to_show[0] and viewid0 == to_show[1]:
vcnt += 1
if vcnt > 5:
sameviewid = True
break
else:
break
for to_show in viewlist:
show_views(to_show[0], to_show[1:])
if sameviewid and (('show_view' != to_show[0]) or
(viewid0 != to_show[1])):
# if nolonger same viewid (it changes) then turn off
sameviewid = False
show_views(to_show[0], to_show[1:], cacheAllKeys=sameviewid)

output = CMDS_SAVE_BUFFER

Expand Down
7 changes: 2 additions & 5 deletions CLI/actioner/sonic_cli_vrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import os
from rpipe_utils import pipestr
import cli_client as cc
from scripts.render_cli import show_cli_output, write
from scripts.render_cli import show_cli_output, show_cli_rendered_output
from sonic_cli_show_config import showconfig_views_to_buffer
import sonic_intf_utils as ifutils
import collections
Expand Down Expand Up @@ -430,10 +430,7 @@ def run(func, args):
gotSep = False
else:
vrfcfgs += "\n" + cfgl
full_cmd = os.getenv("USER_COMMAND", None)
if full_cmd is not None:
pipestr().write(full_cmd.split())
write(vrfcfgs.replace("\t", "\n"))
show_cli_rendered_output(vrfcfgs.replace("\t", "\n"))

else:
api_response = invoke_api(func, args)
Expand Down
3 changes: 3 additions & 0 deletions CLI/clitree/cli-xml/interface.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2270,6 +2270,7 @@ limitations under the License.
<MACRO name="INTF_ERR_DISABLE" arg="${iflist}"></MACRO>
<MACRO name="INTF_FEC" arg="${iflist}"></MACRO>
<MACRO name="INTF_DIAG_MODE" arg="${iflist}"></MACRO>
<MACRO name="SHOW_IF_RANGE" arg="${iflist},if"></MACRO>
<COMMAND
name="speed auto"
help="Enable auto-negotiation">
Expand Down Expand Up @@ -2310,6 +2311,7 @@ limitations under the License.
<MACRO name="MCLAG_PEER_GATEWAY_IF_RANGE" arg="${iflist}"></MACRO>
<MACRO name="INTF_SHUT" arg="${iflist}"></MACRO>
<MACRO name="INTF_RANGE_DESCR" arg="${iflist}"></MACRO>
<MACRO name="SHOW_IF_RANGE" arg="${iflist},vlan"></MACRO>

</VIEW>

Expand All @@ -2332,6 +2334,7 @@ limitations under the License.
<MACRO name="INTF_MTU" arg="${iflist}"></MACRO>
<MACRO name="INTF_RANGE_SHUT_DESCR" arg="${iflist}"></MACRO>
<MACRO name="INTF_RNG_AGGREGATE_SWITCHPORT" arg="${iflist}"></MACRO>
<MACRO name="SHOW_IF_RANGE" arg="${iflist},lag"></MACRO>

<COMMAND
name="graceful-shutdown"
Expand Down
6 changes: 6 additions & 0 deletions CLI/clitree/macro/interface_macro.xml
Original file line number Diff line number Diff line change
Expand Up @@ -584,5 +584,11 @@ limitations under the License.
</COMMAND>
</MACRODEF>

<MACRODEF name="SHOW_IF_RANGE">
<COMMAND name="show configuration" help = "show configuration">
<ACTION builtin="clish_pyobj">sonic_cli_if showrun_if_range configure-arg2 iflist=arg1 </ACTION>
</COMMAND>
</MACRODEF>

</ROOT>

14 changes: 14 additions & 0 deletions CLI/renderer/scripts/render_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,20 @@ def datetimeformat_ticks(timestring):
return ret


def show_cli_rendered_output(output_str, continuation=False, trim=True):
# Show already rendered output string (no jinja2).
if output_str is None:
return 0

full_cmd = os.getenv("USER_COMMAND", None)
if full_cmd is not None:
pipestr().write(full_cmd.split())

ret = write(output_str, continuation=continuation, trim=trim)
sys.stdout.flush()
return ret


def register_custom_filters(jinja2_env, filters):
if jinja2_env is not None:
# Add generic custom filters
Expand Down

0 comments on commit f043b65

Please sign in to comment.