forked from sonic-net/sonic-mgmt-framework
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Render template files for ACL CLI commands (Arlo POC)
- Loading branch information
Showing
5 changed files
with
109 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |