Skip to content

Commit

Permalink
- Added following support to process raw CLI XMLs to produce exanded …
Browse files Browse the repository at this point in the history
…XMLs

  that Klish parser engine can consume to create the UI :
   1.) For each view and nested views add default commands like endi,exit
   2.) Substitute macros and nested macros refrenced in CLI xml with actual contents
   3.) Proper substitution of arguments passed to macros
   4.) Add pipe option to all show/get commands
   5.) Entity/Enumeration substitutions
- Reorganized source as well as target directory structure
  • Loading branch information
amrutasali committed Jun 12, 2019
1 parent e676b18 commit 3e2fa10
Show file tree
Hide file tree
Showing 12 changed files with 1,438 additions and 0 deletions.
215 changes: 215 additions & 0 deletions src/CLI/actioner/sonic-cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
#!/usr/bin/python
import sys
import time
import json
import ast
import pdb
import swagger_client
from swagger_client.rest import ApiException
from pprint import pprint


plugins = dict()

def register(func):
"""Register sdk client method as a plug-in"""
plugins[func.__name__] = func
return func


def call_method(name, args):
method = plugins[name]
return method(args)

def generate_body(func, args):
body = None
# Get the rules of all ACL table entries.
if func.__name__ == 'get_list_base_acl_sets_acl_set':
keypath = []

# Get Interface binding to ACL table info
elif func.__name__ == 'get_acl_interfaces':
keypath = []

# Get all the rules specific to an ACL table.
elif func.__name__ == 'get_acl_set_acl_entries':
keypath = [ args[0], args[1] ]

# Configure ACL table
elif func.__name__ == 'post_acl_set_config' :
keypath = [ args[0], args[1] ]
body = { "config": {
"name": args[0],
"type": args[1],
"description": ""
}
}

# Configure ACL rule specific to an ACL table
elif func.__name__ == 'post_list_base_acl_entries_acl_entry' :
keypath = [ args[0], args[1] ]
forwarding_action = "ACCEPT" if args[4] == 'permit' else 'DROP'
if args[3] == 'ip' :
protocol = "IP"
elif args[3] == 'tcp' :
protocol = "IP_TCP"
else :
protocol = "IP_UDP"
if (len(args) <= 7):
body = { "acl-entry": [ {
"sequence-id": int(args[2]),
"config": {
"sequence-id": int(args[2]),
"description": ""
},
"ipv4": {
"config": {
"source-address": args[5],
"destination-address": args[6],
"protocol": protocol
}
},
"actions": {
"config": {
"forwarding-action": forwarding_action
}
}
} ] }
else:
body = { "acl-entry": [ {
"sequence-id": int(args[2]),
"config": {
"sequence-id": int(args[2]),
"description": ""
},
"ipv4": {
"config": {
"source-address": args[5],
"destination-address": args[7],
"protocol": protocol
}
},
"transport": {
"config": {
"source-port": int(args[6]),
"destination-port": int(args[8])
}
},
"actions": {
"config": {
"forwarding-action": forwarding_action
}
}
} ] }

# Add the ACL table binding to an Interface(Ingress / Egress).
elif func.__name__ == 'post_list_base_interfaces_interface':
keypath = []
if args[3] == "ingress":
body = { "interface": [ {
"id": args[2],
"config": {
"id": args[2]
},
"interface-ref": {
"config": {
"interface": args[2]
}
},
"ingress-acl-sets": {
"ingress-acl-set": [
{
"set-name": args[0],
"type": args[1],
"config": {
"set-name": args[0],
"type": args[1]
}
} ] }
} ] }
else:
body = { "interface": [ {
"id": args[2],
"config": {
"id": args[2]
},
"interface-ref": {
"config": {
"interface": args[2]
}
},
"egress-acl-sets": {
"egress-acl-set": [
{
"set-name": args[0],
"type": args[1],
"config": {
"set-name": args[0],
"type": args[1]
}
} ] }
} ] }
# Remove the ACL table binding to an Ingress interface.
elif func.__name__ == 'delete_interface_ingress_acl_sets_ingress_acl_set':
keypath = [args[0], args[1], args[2]]

# Remove the ACL table binding to an Egress interface.
elif func.__name__ == 'delete_interface_egress_acl_sets_egress_acl_set':
keypath = [args[0], args[1], args[2]]

# Remove all the rules and delete the ACL table.
elif func.__name__ == 'delete_list_base_acl_entries_acl_entry':
keypath = [args[0], args[1]]
elif func.__name__ == 'delete_acl_set_acl_entries_acl_entry':
keypath = [args[0], args[1], args[2]]
else:
body = {}

if body is not None:
body = json.dumps(body,ensure_ascii=False)
return keypath, ast.literal_eval(body)
else:
return keypath,body

def run(func, args):

# create a body block
keypath, body = generate_body(func, args)

try:
if body is not None:
api_response = getattr(swagger_client.OpenconfigAclApi(),func.__name__)(*keypath, body)
else :
api_response = getattr(swagger_client.OpenconfigAclApi(),func.__name__)(*keypath)
#print(api_response)
if api_response is None:
print ("Success")
else:
response = api_response.to_dict()
if 'openconfig_aclacl_entry' in response.keys():
value = response['openconfig_aclacl_entry']
if value is None:
print("Success")
else:
print ("Failed")
elif 'openconfig_aclacl_entries' in response.keys():
value = response['openconfig_aclacl_entries']
# Call the template
elif 'openconfig_aclacl_set' in response.keys():
value = response['openconfig_aclacl_set']
# Call the template
elif 'openconfig_aclinterfaces' in response.keys():
value = response['openconfig_aclinterfaces']
# Call the template
else:
print("Failed")
#pprint(api_response)

except ApiException as e:
print("Exception when calling OpenconfigAclApi->%s : %s\n" %(func.__name__, e))

if __name__ == '__main__':

#pdb.set_trace()
func = eval(sys.argv[1], globals(), swagger_client.OpenconfigAclApi.__dict__)
run(func, sys.argv[2:])
11 changes: 11 additions & 0 deletions src/CLI/clicfg/mgmt_clish_entities.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- Print the ENTITY definition string, looking at the input XML file -->
<xsl:output method ="text"/>
<xsl:template match="/">
<xsl:for-each select="/PLATFORMMODULE/ENTITYLIST/ENTITYNAME">
&lt;!ENTITY <xsl:value-of select="."/> "<xsl:value-of select="./@value"/>"&gt;</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
16 changes: 16 additions & 0 deletions src/CLI/clicfg/mgmt_clish_entities_macro.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- Write the selected entities of a platform into an xml output file -->
<xsl:output method ="xml" version="1.0" indent="yes"/>
<xsl:template match="/">
<xsl:element name="root">
<xsl:for-each select="/PLATFORMMODULE/ENTITYLIST/ENTITYNAME">
<xsl:element name="{.}">
<xsl:value-of select="./@value"/>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Loading

0 comments on commit 3e2fa10

Please sign in to comment.