def get(): """ API: GET: Retrun: { hostname: string error: bool, err_code: int, err_reason: string } """ _err_reason = [ '', # err_code: 0 'cmd excute error', # err_code: 1 ] obj = {'hostname': '', 'error': False, 'err_code': 0, 'err_reason': ''} status, arr = vcmd.get_status_arr( 'cdbctl read/cdb/sys/sys_global/hostname/') if status != 0: obj['error'] = True obj['err_code'] = 1 obj['err_reason'] = ''.join(arr) else: obj['hostname'] = arr[0].get('hostname') return obj
def log_cfg_get(): """ API: DELETE: { vlan_ids: [int, int, ....] } Retrun: { error: bool, err_code: int, err_reason: string } """ _err_reason = [ '', # err_code: 0 'bad request', # err_code: 1 'cannot remove vlan 1', # err_code: 2 'excute error', # err_code: 3 ] obj = {'error': False, 'err_code': 0, 'err_reason': ''} status, arr = vcmd.get_status_arr('cdbctl read/cdb/app/syslog_cfg/') # param check if status != 0: obj['error'] = True obj['err_code'] = 2 obj['err_reason'] = '; '.join(arr) return obj else: # '64,1.1.1.1;64,1.1.1.2;0,1.1.1.3;' -> {1.1.1.1:64, 1.1.1.2:64, 1.1.1.3:0} temp = arr[0].get('server_addr') if len(temp) != 0: temp = temp[:(len(temp) - 1)] #temp = temp.replace('64,', '') temp = temp.split(';') addMap = {} for entry in temp: entry = entry.split(',') addMap[entry[1]] = entry[0] arr[0]['server_addr'] = addMap else: temp = [] obj['cfg'] = arr[0] return obj
def radius_server_get(): """ TBD """ _err_reason = [ '', # err_code: 0 'log is null', # err_code: 1 ] obj = { 'auth_cfg': [], 'dot1x_radius': [], 'error': False, 'err_code': 0, 'err_reason': '' } status, arr = vcmd.get_status_arr('cdbctl read/cdb/app/auth_cfg') status1, arr1 = vcmd.get_status_arr('cdbctl read/cdb/app/auth_server') if (status != 0) and (status1 != 0): obj['error'] = True obj['err_code'] = 2 obj['err_reason'] = '; '.join(arr) return obj else: for cfg in arr1: cfg['host'], cfg['type'], _ = cfg['key'].split('#') if cfg['is_inband'] == "0": cfg['is_inband'] = "mgmt" else: cfg['is_inband'] = "default" del cfg['key'] obj['auth_cfg'] = arr obj['dot1x_radius'] = arr1 #print obj return obj
def dot1x_auth_ctl_enable_get(): """ API: Retrun: { if /: group_arr: [group_dict, group_dict, ...], elif /<int:git>/: group: group_dict error: bool, err_code: int, err_reason: string } group_dict { lag_name: string, protocol: 'static' | 'LACP', group_state: 'L2' | 'L3', ports_in_bundle: int, member_ports: ['eth-0-1', ...], max_ports_count: int, } """ _err_reason = [ '', # err_code: 0 'bad request', # err_code: 1 'cmd excute error', # err_code: 2 'group is not exist', # err_code: 3 ] obj = {'error': False, 'err_code': 0, 'err_reason': ''} # excute status, arr = vcmd.get_status_arr('cdbctl read/cdb/l2/dot1x_global/global_auth_enable') if status != 0: obj['error'] = True obj['err_code'] = 2 obj['err_reason'] = '; '.join(arr) return obj else: obj['dhcp_snooping_enable']= arr[0].get('global_auth_enable') return obj
def get(): """ API: GET: Retrun: { ip: string, eg: '10.10.10.2/24' gateway: string, eg: '10.10.10.1' error: bool, err_code: int, err_reason: string } """ _err_reason = [ '', # err_code: 0 'cmd excute error', # err_code: 1 ] obj = { 'ip': '', 'gateway': '', 'error': False, 'err_code': 0, 'err_reason': '' } status, arr = vcmd.get_status_arr('cdbctl read/cdb/sys/manage_if') if status != 0: obj['error'] = True obj['err_code'] = 1 obj['err_reason'] = ''.join(arr) else: obj['ip'] = arr[0].get('addr').replace('#', '/') obj['gateway'] = arr[0].get('gateway') return obj
def gratuitous_arp_get(): """ API: Retrun: { gratuitous_arp_learn_en:1 or 0 error: bool, err_code: int, err_reason: string } """ _err_reason = [ '', # err_code: 0 'bad request', # err_code: 1 'cmd excute error', # err_code: 2 ] obj = {'error': False, 'err_code': 0, 'err_reason': ''} # excute status, arr = vcmd.get_status_arr('cdbctl read/cdb/l3/route_global') if status != 0: obj['error'] = True obj['err_code'] = 2 obj['err_reason'] = '; '.join(arr) return obj else: obj['gratuitous_arp_learn_en'] = arr[0].get('gratuitous_arp_learn_en') #print obj return obj
def channel_group_get(gid): """ API: GET: / or /<int:gid>/ @param gid: group id Retrun: { if /: group_arr: [group_dict, group_dict, ...], elif /<int:git>/: group: group_dict error: bool, err_code: int, err_reason: string } group_dict { lag_name: string, protocol: 'static' | 'LACP', group_state: 'L2' | 'L3', ports_in_bundle: int, member_ports: ['eth-0-1', ...], max_ports_count: int, } """ _err_reason = [ '', # err_code: 0 'bad request', # err_code: 1 'cmd excute error', # err_code: 2 'group is not exist', # err_code: 3 ] obj = {'error': False, 'err_code': 0, 'err_reason': ''} # param check if gid != 'all' and base.parse.parse_int(gid) == None: obj['error'] = True obj['err_code'] = 1 obj['err_reason'] = _err_reason[1] return obj # excute if gid == 'all': status, arr = vcmd.get_status_arr( 'cdbctl read/cdb/interface/lag//type/summary') else: status, arr = vcmd.get_status_arr( 'cdbctl read/cdb/interface/lag/%d/type/summary' % int(gid)) if status != 0: obj['error'] = True obj['err_code'] = 2 obj['err_reason'] = '; '.join(arr) return obj max_ports_count = 16 status, version = vcmd.get_status_arr('cdbctl read/cdb/sys/version') if status != 0: obj['error'] = True obj['err_code'] = 2 obj['err_reason'] = '; '.join(arr) return obj for info in version: product = info.get('product') if (product.find('580') != -1 or product.find('550') != -1 or product.find('530') != -1): max_ports_count = 64 # arr shaping arr_shaping = [] for i in arr: lag_name = i.get('key') protocol = 'None' if i.get('lag') == None else 'static' if i.get( 'lag').get('mode') == 'static' else 'LACP' group_state = 'L2' if i.get('mode') == 'switch' else 'L3' ports_in_bundle = 'None' if i.get( 'lag') == None else base.parse.parse_int( i.get('lag').get('bundle_ports_count')) member_ports = 'None' if i.get('lag') == None else i.get('lag').get( 'member_ports').split(',') for mem_item in range(len(member_ports)): member_ports[mem_item] = portMap.Mapping(member_ports[mem_item]) member_ports, err = base.check.array(member_ports, ['string', '!=', '', '', 1], True) arr_shaping.append({ 'lag_name': lag_name, 'protocol': protocol, 'group_state': group_state, 'ports_in_bundle': ports_in_bundle, 'member_ports': member_ports, 'max_ports_count': max_ports_count }) # Insert "load_balance_mode" fild to arr status, lines = vcmd.get_status_arr( "cdbctl read/cdb/sys/lag_global/load_balance_mode") for lag in arr_shaping: lag_name = lag.get('lag_name') replace = lag_name.replace('agg', '') if (replace.isdigit()): lagId = int(replace) else: continue mode = lines[0].get('load_balance_mode').split(',') m = mode[lagId] lag['mode'] = m # return if gid == 'all': obj['group_arr'] = arr_shaping elif len(arr_shaping) >= 1: obj['group'] = arr_shaping[0] else: obj['error'] = True obj['err_code'] = 3 # 不要随便�? obj['err_reason'] = _err_reason[3] return obj return obj
def load_balance_set(lb_type, lb_state): """ API: PUT: /<lb_type>/<lb_state>/ @param lb_type: src-mac | dst-mac | src-ip | dst-ip | ip-protocol | src-port-l4 | dst-port-l4 @param lb_state: set | unset Retrun: { error: bool, err_code: int, err_reason: string } """ _err_reason = [ '', # err_code: 0 'bad request', # err_code: 1 'cmd excute error', # err_code: 2 ] obj = {'error': False, 'err_code': 0, 'err_reason': ''} # param check lb_type = str(lb_type) lb_state = str(lb_state) #if not lb_type or not lb_type in ['src-mac', 'dst-mac', 'src-ip', 'dst-ip', 'ip-protocol', 'src-port-l4', 'dst-port-l4'] or not lb_state in ['set', 'unset']: # obj['error'] = True # obj['err_code'] = 1 # obj['err_reason'] = _err_reason[1] # return obj cmd = 'cdbctl read/cdb/sys/chip' status, arr = vcmd.get_status_arr(cmd) type = arr[0]['type'] if type == 'Greatbelt': if lb_state == 'set': cmd = 'cdbctl update/cdb/sys/lag_global/load_balance/overwrite/%s' % ( lb_type) status, lines = vcmd.get_status_lines(cmd) if status != 0: obj['error'] = True obj['err_code'] = 2 obj['err_reason'] = '; '.join(lines) return obj else: cmd = 'cdbctl update/cdb/sys/lag_global/load_balance/set/default' status, lines = vcmd.get_status_lines(cmd) if status != 0: obj['error'] = True obj['err_code'] = 2 obj['err_reason'] = '; '.join(lines) return obj else: # excute cmd = 'cdbctl update/cdb/sys/lag_global/load_balance/%s/%s' % ( lb_state, lb_type) status, lines = vcmd.get_status_lines(cmd) if status != 0: obj['error'] = True obj['err_code'] = 2 obj['err_reason'] = '; '.join(lines) return obj return obj
def load_balance_get(): """ API: GET: Retrun: { state: { src-mac bool, dst-mac bool, src-ip bool, dst-ip bool, ip-protocol bool, src-port-l4 bool, dst-port-l4 bool, }, error: bool, err_code: int, err_reason: string } """ _err_reason = [ '', # err_code: 0 'cmd excute error', # err_code: 1 ] cmd = 'cdbctl read/cdb/sys/chip' status, arr = vcmd.get_status_arr(cmd) type = arr[0]['type'] l2_hash_field = [] ip_hash_field = [] ipv6_hash_field = [] gre_hash_field = [] vxlan_hash_field = [] nvgre_hash_field = [] mpls_hash_field = [] if type == 'Greatbelt': obj = { 'state': { 'src-mac': False, 'dst-mac': False, 'src-ip': False, 'dst-ip': False, 'ip-protocol': False, 'src-port-l4': False, 'dst-port-l4': False, 'vxlan-vni': False, 'inner-src-mac': False, 'inner-dst-mac': False, 'inner-src-ip': False, 'inner-dst-ip': False, 'gre-key': False, 'nvgre-vsid': False, 'nvgre-flow-id': False, 'src-dst-ip': False, 'src-dst-ip-src-dst-port': False, 'src-dst-mac': False, 'src-dst-port': False, }, 'error': False, 'err_code': 0, 'err_reason': '' } elif type == 'Tsingma': obj = { 'state_tap530': { 'l2-eth-type': False, 'l2-macda': False, 'l2-macsa': False, 'l2-src-interface': False, 'l2-vlan': False, 'mpls-2nd-label': False, 'mpls-3rd-label': False, 'mpls-source-interface': False, 'mpls-top-label': False, 'ip-l4-destport': False, 'ip-l4-sourceport': False, 'ip-ipda': False, 'ip-ipsa': False, 'ip-ip-protocol': False, 'ip-src-interface': False, 'ipv6-l4-destport': False, 'ipv6-l4-sourceport': False, 'ipv6-ipda': False, 'ipv6-ipsa': False, 'ipv6-ip-protocol': False, 'ipv6-src-interface': False, 'gre-gre-key': False, 'gre-gre-protocol': False, 'gre-ipda': False, 'gre-ipsa': False, 'gre-src-interface': False, 'nvgre-inner2-eth-type': False, 'nvgre-inner2-macda': False, 'nvgre-inner2-macsa': False, 'nvgre-inner3-ip-protocol': False, 'nvgre-inner3-ipda': False, 'nvgre-inner3-ipsa': False, 'nvgre-outer-gre-protocol': False, 'nvgre-outer-ipda': False, 'nvgre-outer-ipsa': False, 'nvgre-vsid': False, 'nvgre-src-interface': False, 'vxlan-inner2-eth-type': False, 'vxlan-inner2-macda': False, 'vxlan-inner2-macsa': False, 'vxlan-inner3-l4-destport': False, 'vxlan-inner3-ip-protocol': False, 'vxlan-inner3-ipda': False, 'vxlan-inner3-ipsa': False, 'vxlan-inner3-l4-sourceport': False, 'vxlan-outer-l4-destport': False, 'vxlan-outer-ipda': False, 'vxlan-outer-ipsa': False, 'vxlan-outer-l4-sourceport': False, 'vxlan-outer-vlan': False, 'vxlan-src-interface': False, 'vxlan-vni': False, }, 'setip_enable': True, 'setipv6_enable': True, 'setmpls_enable': True, 'error': False, 'err_code': 0, 'err_reason': '', } else: obj = { 'state': { 'src-mac': False, 'dst-mac': False, 'src-ip': False, 'dst-ip': False, 'ip-protocol': False, 'src-port-l4': False, 'dst-port-l4': False, 'vxlan-vni': False, 'inner-src-mac': False, 'inner-dst-mac': False, 'inner-src-ip': False, 'inner-dst-ip': False, 'inner-ip-protocol': False, 'inner-src-port-l4': False, 'inner-dst-port-l4': False, 'gre-key': False, 'nvgre-vsid': False, 'nvgre-flow-id': False, }, 'error': False, 'err_code': 0, 'err_reason': '' } (status, lines ) = vcmd.get_status_lines('cdbctl show/cdb/sys/lag_global/load-balance') if status != 0 or len(lines) <= 2: obj['error'] = True obj['err_code'] = 1 obj['err_reason'] = ''.join(lines) return obj if type == 'Greatbelt': lines = lines[2:] if 'src-ip' in lines and 'dst-ip' in lines and 'src-port-l4' in lines and 'dst-port-l4' in lines: obj['state']['src-dst-ip-src-dst-port'] = True if 'src-ip' in lines and 'dst-ip' in lines: obj['state']['src-dst-ip'] = True if 'src-mac' in lines and 'dst-mac' in lines: obj['state']['src-dst-mac'] = True if 'src-port-l4' in lines and 'dst-port-l4' in lines: obj['state']['src-dst-port'] = True for key in lines: if obj['state'].has_key(key): obj['state'][key] = True elif type == 'Tsingma': (status, lines) = vcmd.get_status_lines( 'cdbctl show/cdb/l2/show_hash_field/port-channel') lines1 = lines[13:] str1 = ','.join(lines1) str1 = str1.replace(',', ' ') str1 = str1.replace(':', ' ') arr1 = str1.split() sub_arr = arr1[arr1.index('l2'):arr1.index('ip')] for filed_ in sub_arr: filed_ = 'l2-' + filed_ l2_hash_field.append(filed_) l2_hash = l2_hash_field[1:] sub_arr = arr1[arr1.index('ip'):arr1.index('ipv6')] for filed_ in sub_arr: filed_ = 'ip-' + filed_ ip_hash_field.append(filed_) ip_hash = ip_hash_field[1:] sub_arr = arr1[arr1.index('ipv6'):arr1.index('gre')] for filed_ in sub_arr: filed_ = 'ipv6-' + filed_ ipv6_hash_field.append(filed_) ipv6_hash = ipv6_hash_field[1:] sub_arr = arr1[arr1.index('gre'):arr1.index('vxlan')] for filed_ in sub_arr: filed_ = 'gre-' + filed_ gre_hash_field.append(filed_) gre_hash = gre_hash_field[1:] sub_arr = arr1[arr1.index('vxlan'):arr1.index('nvgre')] for filed_ in sub_arr: filed_ = 'vxlan-' + filed_ vxlan_hash_field.append(filed_) vxlan_hash = vxlan_hash_field[1:] sub_arr = arr1[arr1.index('nvgre'):arr1.index('mpls')] for filed_ in sub_arr: filed_ = 'nvgre-' + filed_ nvgre_hash_field.append(filed_) nvgre_hash = nvgre_hash_field[1:] sub_arr = arr1[arr1.index('mpls'):] for filed_ in sub_arr: if filed_ != 'inner-ipda' and filed_ != 'inner-ipsa': filed_ = 'mpls-' + filed_ mpls_hash_field.append(filed_) mpls_hash = mpls_hash_field[1:] for key in gre_hash: obj['state_tap530'][key] = True for key in l2_hash: obj['state_tap530'][key] = True for key in ip_hash: obj['state_tap530'][key] = True for key in ipv6_hash: obj['state_tap530'][key] = True for key in nvgre_hash: obj['state_tap530'][key] = True for key in mpls_hash: obj['state_tap530'][key] = True for key in vxlan_hash: obj['state_tap530'][key] = True cmd = 'cdbctl read/cdb/l2/hash_field_profile/port-channel' (status, lines) = vcmd.get_status_lines(cmd) bitmapstr = lines[0][lines[0].find('bitmap_disable=') + 15:] if len(bitmapstr) <= 2: bitmapval = int(bitmapstr) else: bitmapval = int(bitmapstr[0:bitmapstr.find('/')]) if (bitmapval & 1): obj['setip_enable'] = False else: obj['setip_enable'] = True if (bitmapval & 2): obj['setipv6_enable'] = False else: obj['setipv6_enable'] = True if (bitmapval & 8): obj['setmpls_enable'] = False else: obj['setmpls_enable'] = True return obj else: lines = lines[2:] for key in lines: if obj['state'].has_key(key): obj['state'][key] = True return obj