Skip to content

Commit

Permalink
Render template files for ACL CLI commands (Arlo POC)
Browse files Browse the repository at this point in the history
  • Loading branch information
ranjinidn committed Jun 12, 2019
1 parent d8bc76a commit ebc71bd
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/CLI/actioner/sonic-cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import swagger_client
from swagger_client.rest import ApiException
from pprint import pprint
from renderer.scripts.render_cli import show_cli_output
from scripts.render_cli import show_cli_output


plugins = dict()
Expand Down
2 changes: 2 additions & 0 deletions src/CLI/clitree/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ all:
(cd scripts;./klish_platform_features_process.sh ../../clicfg ${TGT_DIR})
python scripts/klish_preproc_cmdtree.py ${TGT_DIR}/command-tree ${TGT_DIR}/cli-macro 3
cp ./../actioner/*.py ${TGT_DIR}/.
cp ../renderer/scripts/*.py ${TGT_DIR}/scripts
cp ../renderer/templates/* ${TGT_DIR}/render-templates
########## remove following line when docker with correctly packaged Klish parser engine available
cp -r sonic-cli-xml ${TGT_DIR}/
rm -rf ${TGT_DIR}/cli-macro
Expand Down
21 changes: 21 additions & 0 deletions src/CLI/renderer/scripts/render_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python
from jinja2 import Template, Environment, FileSystemLoader
import os
import json

# Capture our current directory
THIS_DIR = os.path.dirname(os.path.abspath(__file__))

def show_cli_output(template_file, response):
# Create the jinja2 environment.
# Notice the use of trim_blocks, which greatly helps control whitespace.

template_path = os.path.abspath(os.path.join(THIS_DIR, "../render-templates"))

j2_env = Environment(loader=FileSystemLoader(template_path))
j2_env.trim_blocks = True
j2_env.lstrip_blocks = True
j2_env.rstrip_blocks = True

if response:
print (j2_env.get_template(template_file).render(json_output=response))
27 changes: 27 additions & 0 deletions src/CLI/renderer/templates/show_access_group.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{% if json_output %}
{% for key in json_output %}
{# This condition checks if the JSON response has data from the acl/interface list #}
{% if "interface" in key %}
{% for interface in json_output[key] %}
{% set if_id = interface["id"] %}
{% if interface["ingress_acl_sets"] %}
{% set direction = "ingress" %}
{% elif interface["egress_acl_sets"] %}
{% set direction = "egress" %}
{% endif %}
{% set acl_sets = direction + "_acl_sets" %}
{% set acl_set = direction + "_acl_set" %}
{% set acl_set_list = interface[acl_sets][acl_set] %}
{% for acl_set in acl_set_list %}
{% set acl_name = acl_set["set_name"] %}
{% if direction == "ingress" %}
{% set direction = "Ingress" %}
{% elif direction == "egress" %}
{% set direction = "Egress" %}
{% endif %}
{{- direction }} IP access-list {{ acl_name }} on {{ if_id }}
{% endfor %}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
58 changes: 58 additions & 0 deletions src/CLI/renderer/templates/show_access_list.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{% macro traverse_acl_entry(acl_entry_list) -%}
{% for seq in acl_entry_list %}
{# Get sequence id #}
{% set seqid = seq["sequence_id"] %}
{# Get forwarding action #}
{% set fwd_action = seq["actions"]["state"]["forwarding_action"] %}
{% if fwd_action == "ACCEPT" %}
{% set fwd_action = "permit" %}
{% endif %}
{% if fwd_action == "DROP" %}
{% set fwd_action = "deny" %}
{% endif %}
{# Get protocol #}
{% set proto = seq["ipv4"]["state"]["protocol"] %}
{% if proto == "6" %}
{% set proto = "tcp" %}
{% endif %}
{% if proto == "17" %}
{% set proto = "udp" %}
{% endif %}
{# Get Source IP #}
{% set src_ip = seq["ipv4"]["state"]["source_address"] %}
{# Get Destination IP #}
{% set dstn_ip = seq["ipv4"]["state"]["destination_address"] %}
{% set src_port = "" %}
{% set dstn_port = "" %}
{# include port number if available #}
{% if seq["transport"] %}
{% set src_port = "eq " + seq["transport"]["state"]["source_port"] %}
{% set dstn_port = "eq " + seq["transport"]["state"]["destination_port"] %}
{{- " " }} {{ seqid }} {{ fwd_action }} {{ proto }} {{ src_ip }} {{ src_port }} {{ dstn_ip }} {{ dstn_port }}
{% else %}
{{- " " }} {{ seqid }} {{ fwd_action }} {{ proto }} {{ src_ip }} {{ dstn_ip }}
{% endif %}
{% endfor %}
{%- endmacro %}
{% for key in json_output %}
{# This condition checks if the JSON response has data from the acl-entry list #}
{% if "acl_entry" in key -%}
{% set acl_entry = json_output[key] -%}
{{ traverse_acl_entry(acl_entry) }}
{%- endif %}
{% endfor %}
{% for acl_set in json_output -%}
{% if acl_set -%}
{# This condition checks if the JSON response has data from the acl-set list -#}
{% if acl_set["state"] -%}
ip access-list {{ acl_set["state"]["name"] }}
{% set acl_entry_list = acl_set["acl_entries"] %}
{% if acl_entry_list -%}
{% for each in acl_entry_list -%}
{% set acl_entry = acl_entry_list[each] -%}
{{ traverse_acl_entry(acl_entry) }}
{%- endfor %}
{%- endif %}
{%- endif %}
{%- endif %}
{%- endfor %}

0 comments on commit ebc71bd

Please sign in to comment.