input CLI's validated
This commit is contained in:
@@ -28,22 +28,7 @@ class CiscoType:
|
|||||||
|
|
||||||
def get_attributes(self):
|
def get_attributes(self):
|
||||||
return self.types
|
return self.types
|
||||||
'''
|
|
||||||
def set_ciscoType(self, str):
|
|
||||||
self.ciscoType = str
|
|
||||||
def get_ciscoType(self):
|
|
||||||
return self.ciscoType
|
|
||||||
|
|
||||||
def set_value(self, str):
|
|
||||||
self.value = str
|
|
||||||
def get_value(self):
|
|
||||||
return self.value
|
|
||||||
|
|
||||||
def set_puppetHeader(self, str):
|
|
||||||
self.puppetHeader = str
|
|
||||||
def get_puppetHeader(self):
|
|
||||||
return self.puppetHeader
|
|
||||||
'''
|
|
||||||
|
|
||||||
class Attribute:
|
class Attribute:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|||||||
@@ -73,7 +73,6 @@ def get_valid_values(s, values):
|
|||||||
match1 = re.search (r'range ([0-9]+)..([0-9]+)', s)
|
match1 = re.search (r'range ([0-9]+)..([0-9]+)', s)
|
||||||
match2 = re.search (r'between ([0-9]+) and ([0-9]+)', s)
|
match2 = re.search (r'between ([0-9]+) and ([0-9]+)', s)
|
||||||
match3 = re.search (r'from ([0-9]+)..([0-9]+)', s)
|
match3 = re.search (r'from ([0-9]+)..([0-9]+)', s)
|
||||||
#match4 = re.search(r'range of ([0-9]+) to ([0-9]+)', s)
|
|
||||||
if match:
|
if match:
|
||||||
first = match.group(1)
|
first = match.group(1)
|
||||||
last = match.group(2)
|
last = match.group(2)
|
||||||
@@ -94,7 +93,7 @@ def get_valid_values(s, values):
|
|||||||
match_values(match3, values)
|
match_values(match3, values)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
values.append('<integer>')
|
values.append(type(0)) #integer
|
||||||
|
|
||||||
if 'Speed' in s:
|
if 'Speed' in s:
|
||||||
s = s.split(".")[1]
|
s = s.split(".")[1]
|
||||||
@@ -107,16 +106,16 @@ def get_valid_values(s, values):
|
|||||||
values.append(match2.group(1))
|
values.append(match2.group(1))
|
||||||
|
|
||||||
if 'number' in s or 'Number' in s:
|
if 'number' in s or 'Number' in s:
|
||||||
values.append('<integer>')
|
values.append(type(0)) #integer
|
||||||
if ' port' in s or ' Port' in s:
|
if ' port' in s or ' Port' in s:
|
||||||
values.append('<integer>')
|
values.append(type(0)) #integer
|
||||||
|
|
||||||
if 'string' in s or 'String' in s:
|
if 'string' in s or 'String' in s:
|
||||||
values.append('<string>')
|
values.append(type('')) #string
|
||||||
if 'interface' in s or 'Interface' in s and 'Speed' not in s:
|
if 'interface' in s or 'Interface' in s and 'Speed' not in s:
|
||||||
values.append('<string>')
|
values.append(type('')) #string
|
||||||
if 'array' in s or 'Array' in s:
|
if 'array' in s or 'Array' in s:
|
||||||
values.append('<string>')
|
values.append(type('')) #string
|
||||||
|
|
||||||
if 'true' in s or 'True' in s:
|
if 'true' in s or 'True' in s:
|
||||||
values.append('true')
|
values.append('true')
|
||||||
@@ -145,14 +144,14 @@ def get_valid_values(s, values):
|
|||||||
for item in brackets:
|
for item in brackets:
|
||||||
val = (''.join(c for c in item if c not in string.punctuation))
|
val = (''.join(c for c in item if c not in string.punctuation))
|
||||||
if val.isdigit():
|
if val.isdigit():
|
||||||
values.append('<integer>')
|
values.append(type(0)) #integer
|
||||||
else:
|
else:
|
||||||
values.append(val)
|
values.append(val)
|
||||||
values = list(set(values))
|
values = list(set(values))
|
||||||
|
|
||||||
return values
|
return values
|
||||||
|
|
||||||
def parse_types():
|
def Parse_types():
|
||||||
|
|
||||||
url = urllib2.urlopen('https://github.com/cisco/cisco-network-puppet-module')
|
url = urllib2.urlopen('https://github.com/cisco/cisco-network-puppet-module')
|
||||||
html_doc = url.read()
|
html_doc = url.read()
|
||||||
@@ -187,25 +186,12 @@ def parse_types():
|
|||||||
if "Valid" in valid_values:
|
if "Valid" in valid_values:
|
||||||
valid_values = "Valid" + valid_values.split("Valid")[1]
|
valid_values = "Valid" + valid_values.split("Valid")[1]
|
||||||
valid_values_list = get_valid_values(valid_values, valid_list)
|
valid_values_list = get_valid_values(valid_values, valid_list)
|
||||||
attr.add_attributes(param, valid_values_list)
|
attr.add_attributes(get_name(param), valid_values_list)
|
||||||
feature.add_attribute(type[6:], attr)
|
feature.add_attribute(type[6:], attr)
|
||||||
#print "@@@@@@@@@@", param, valid_values_list
|
|
||||||
j += 1
|
j += 1
|
||||||
if j >= len(tags):
|
if j >= len(tags):
|
||||||
break
|
break
|
||||||
tag = str(tags[j])
|
tag = str(tags[j])
|
||||||
|
|
||||||
#feature_list.append(feature.typeName.replace('_',' '))
|
|
||||||
#print feature.typeName, feature.attributes
|
|
||||||
#print "\n"
|
|
||||||
|
|
||||||
|
|
||||||
return feature
|
return feature
|
||||||
|
|
||||||
|
|
||||||
cmd_types = parse_types()
|
|
||||||
|
|
||||||
print "List of Types: \n", cmd_types.types.keys()
|
|
||||||
|
|
||||||
print "Complete data structure including Types, attributes & its valid value list:\n", cmd_types.types
|
|
||||||
|
|
||||||
|
|||||||
34
backend/types.txt
Normal file
34
backend/types.txt
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
aaa authentication login;cisco_aaa_authentication_login;$ciscopuppet::aaa_authentication_login_data::aaa_authentication_login_instances.each |$aaa_authentication_login, $value| {;$aaa_authentication_login_instances = {
|
||||||
|
aaa authorization login cfg svc;cisco_aaa_authorization_login_cfg_svc;add description;$cisco_aaa_authorization_login_cfg_svc_instances = {
|
||||||
|
aaa authorization login exec svc;cisco_aaa_authorization_login_exec_svc;add description;$cisco_aaa_authorization_login_exec_svc_instances = {
|
||||||
|
aaa group tacacs;cisco_aaa_group_tacacs;add description;$cisco_aaa_group_tacacs_instances = {
|
||||||
|
acl;cisco_acl;add description;$cisco_acl_instances = {
|
||||||
|
bgp;cisco_bgp;$ciscopuppet::bgp_cfg_data::bgp_instances.each |$bgp, $value| {;$cisco_bgp_instances = {
|
||||||
|
bgp address-family;cisco_bgp_af;add description;$cisco_bgp_af_instances = {
|
||||||
|
bgp neighbor;cisco_bgp_neighbor;add description;$cisco_bgp_neighbor_instances = {
|
||||||
|
bgp neighbor address-family;cisco_bgp_neighbor_af;add description;$cisco_bgp_neighbor_af_instances = {
|
||||||
|
command config;cisco_command_config;$ciscopuppet::command_config_data::command_config_instances.each |$command, $value| {;$cisco_command_config_instances = {
|
||||||
|
interface;cisco_interface;$ciscopuppet::l3_interface_cfg_data::l3_interface_instances.each |$interface, $value| {;$l3_interface_instances = {
|
||||||
|
interface ospf;cisco_interface_ospf;add description;$cisco_interface_ospf_instances = {
|
||||||
|
interface portchannel;cisco_interface_portchannel;add description;$cisco_interface_portchannel_instances = {
|
||||||
|
interface service vni;cisco_interface_service_vni;add description;$cisco_interface_service_vni_instances = {
|
||||||
|
ospf;cisco_ospf;add description;$cisco_ospf_instances = {
|
||||||
|
ospf vrf;cisco_ospf_vrf;add description;$cisco_ospf_vrf_instances = {
|
||||||
|
portchannel global;cisco_portchannel_global;add description;$cisco_portchannel_global_instances = {
|
||||||
|
snmp community;cisco_snmp_community;add description;$cisco_snmp_community_instances = {
|
||||||
|
snmp group;cisco_snmp_group;add description;$cisco_snmp_group_instances = {
|
||||||
|
snmp server;cisco_snmp_server;add description;$cisco_snmp_server_instances = {
|
||||||
|
snmp user;cisco_snmp_user;add description;$cisco_snmp_user_instances = {
|
||||||
|
tacacs server;cisco_tacacs_server;add description;$cisco_tacacs_server_instances = {
|
||||||
|
tacacs server host;cisco_tacacs_server_host;add description;$cisco_tacacs_server_host_instances = {
|
||||||
|
vdc;cisco_vdc;add description;$cisco_vdc_instances = {
|
||||||
|
vlan;cisco_vlan;add description;$cisco_vlan_instances = {
|
||||||
|
vni;cisco_vni;add description;$cisco_vni_instances = {
|
||||||
|
vrf;cisco_vrf;add description;$cisco_vrf_instances = {
|
||||||
|
vrf address-family;cisco_vrf_af;add description;$cisco_vrf_af_instances = {
|
||||||
|
vtp;cisco_vtp;add description;$cisco_vtp_instances = {
|
||||||
|
vxlan global;cisco_vxlan_global;add description;$cisco_vxlan_global_instances = {
|
||||||
|
vxlan vtep;cisco_vxlan_vtep;add description;$cisco_vxlan_vtep_instances = {
|
||||||
|
interface nve;cisco_vxlan_vtep;add description;$cisco_vxlan_vtep_instances = {
|
||||||
|
router bgp;cisco_bgp;$ciscopuppet::bgp_cfg_data::bgp_instances.each |$bgp, $value| {;$cisco_bgp_instances = {
|
||||||
|
|
||||||
95
backend/validation.py
Normal file
95
backend/validation.py
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
import re
|
||||||
|
import parse_types as parse
|
||||||
|
|
||||||
|
def get_all_types():
|
||||||
|
type_dict = {}
|
||||||
|
with open('types.txt') as f:
|
||||||
|
lines = f.readlines()
|
||||||
|
for line in lines:
|
||||||
|
type_list = line.split(';')
|
||||||
|
type_dict[type_list[0]] = type_list
|
||||||
|
return type_dict
|
||||||
|
|
||||||
|
def validate_attr_value(ds_types, search_type, attr, value):
|
||||||
|
attr= parse.get_name(attr)
|
||||||
|
find_int =re.search(r'(^-?[0-9]+$)',value)
|
||||||
|
if find_int:
|
||||||
|
value = int(value)
|
||||||
|
print ds_types[search_type][attr]
|
||||||
|
if value in ds_types[search_type][attr] or type(value) in ds_types[search_type][attr]:
|
||||||
|
print "Provided value of the attribute is validated now, need to prepare manifest"
|
||||||
|
|
||||||
|
def type_check(type_dict, ds_types, index, cmd_list, search_type, value):
|
||||||
|
while (search_type):
|
||||||
|
if search_type in type_dict.keys():
|
||||||
|
print "Type:", search_type, "Value:", value
|
||||||
|
type_name = type_dict[search_type][1]
|
||||||
|
print ds_types[type_name][search_type]
|
||||||
|
if value in ds_types[type_name][search_type] or type(value) in ds_types[type_name][search_type]:
|
||||||
|
print "Provided value is validated now, need to prepare manifest"
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
value = search_type.rsplit(' ', 1)[1]
|
||||||
|
search_type = search_type.rsplit(' ', 1)[0]
|
||||||
|
|
||||||
|
def attr_check(type_dict, ds_types, index, cmd_list, search_type, value):
|
||||||
|
while(search_type):
|
||||||
|
if search_type in type_dict.keys():
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
search_type = search_type.rsplit(' ', 1)[0]
|
||||||
|
|
||||||
|
search_type = type_dict[search_type][1] #Get the type
|
||||||
|
|
||||||
|
cmd_list[index] = cmd_list[index].lstrip()
|
||||||
|
tmp_cmd_list = cmd_list[index].split(' ')
|
||||||
|
|
||||||
|
if tmp_cmd_list[0] == 'no':
|
||||||
|
value = 'false'
|
||||||
|
attr = cmd_list[index].split(' ', 1)[1]
|
||||||
|
if attr in ds_types[search_type].keys():
|
||||||
|
print "Attribute: ", attr, "Value: ", value
|
||||||
|
validate_attr_value(ds_types, search_type, attr, value)
|
||||||
|
|
||||||
|
else:
|
||||||
|
while (cmd_list[index]):
|
||||||
|
if cmd_list[index] in ds_types[search_type].keys():
|
||||||
|
if not value:
|
||||||
|
value = 'true'
|
||||||
|
print "Attribute: ", cmd_list[index], "value: ", value
|
||||||
|
validate_attr_value(ds_types, search_type, cmd_list[index], value)
|
||||||
|
break
|
||||||
|
|
||||||
|
else:
|
||||||
|
value = cmd_list[index].rsplit(' ', 1)[1]
|
||||||
|
cmd_list[index] = cmd_list[index].rsplit(' ', 1)[0]
|
||||||
|
|
||||||
|
|
||||||
|
def parse_data(data):
|
||||||
|
cmd_list = data.split('\n')
|
||||||
|
l_cmd_list = len(cmd_list)
|
||||||
|
for index in range(l_cmd_list):
|
||||||
|
type_dict = get_all_types()
|
||||||
|
ds_types = parse.Parse_types().types
|
||||||
|
value = ''
|
||||||
|
|
||||||
|
if not cmd_list[index]:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if cmd_list[index][0] != ' ':
|
||||||
|
search_type = cmd_list[index]
|
||||||
|
print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n", index, cmd_list[index]
|
||||||
|
type_check(type_dict, ds_types, index, cmd_list, search_type, value)
|
||||||
|
|
||||||
|
else:
|
||||||
|
print "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n", index, cmd_list[index]
|
||||||
|
attr_check(type_dict, ds_types, index, cmd_list, search_type, value)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def Convert(input_data):
|
||||||
|
parse_data(input_data)
|
||||||
|
return True
|
||||||
|
|
||||||
|
input_data = input_string = 'interface vlan14\n mtu 9100\n ip address 124.14.1.1/24\ninterface eth1/1\n shutdown\n no ip redirects'
|
||||||
|
Convert(input_data)
|
||||||
Reference in New Issue
Block a user