def rest_add_dhcp_relay(self, tenant, vnsname, dhcpserverip): '''Create dhcp server on tenant VNS" Input: `tenant` tenant name `vnsname` name of vns interface `dhcpserverip` IP address of dhcp server Return: true if configuration is successful, false otherwise REST-POST: PATCH http://127.0.0.1:8080/api/v1/data/controller/applications/bvs/tenant[name="C"]/virtual-router/vns-interfaces[vns-name="C1"] {"dhcp-server-ip": "10.2.1.1"} REST-POST: http://127.0.0.1:8080/api/v1/data/controller/applications/bvs/tenant[name="C"]/virtual-router/vns-interfaces[vns-name="C1"] reply: ''' t = test.Test() c = t.controller('master') helpers.test_log( "Input arguments: tenant = %s vns name = %s relay-ip = %s" % (tenant, vnsname, dhcpserverip)) url = '/api/v1/data/controller/applications/bvs/tenant[name="%s"]/virtual-router/vns-interfaces[vns-name="%s"]/dhcp-relay' % ( tenant, vnsname) try: c.rest.patch(url, {"dhcp-server-ip": dhcpserverip}) except: helpers.test_failure(c.rest.error()) else: helpers.test_log("Output: %s" % c.rest.result_json()) return c.rest.content()
def rest_add_tacacs_authorization(self): ''' Objective: Add TACACS configuration Input: | tacacs_server | Tacacs Server | Return Value: True on Success False on Failure ''' try: t = test.Test() except: return False else: c = t.controller('master') # Configure AAA/authorization try: url = '/api/v1/data/controller/core/aaa/authorizer' c.rest.put(url, [{ "priority": 1, "name": "tacacs" }, { "priority": 2, "name": "local" }]) except: helpers.test_log(c.rest.error()) return False else: return True
def rest_add_tacacs_server(self, tacacs_server, key, timeout=90): try: t = test.Test() except: return False else: c = t.controller('master') # Get encoded password from key try: url = '/api/v1/data/controller/core/aaa/tacacs/encode-password[password="******"]' % str( key) c.rest.get(url) content = c.rest.content() except: helpers.test_log(c.rest.error()) return False else: # Configure tacacse server encoded_password = content[0]['encoded-password'] try: url = '/api/v1/data/controller/core/aaa/tacacs/server[server-address="%s"]' % str( tacacs_server) c.rest.put( url, { "server-address": str(tacacs_server), "secret": str(encoded_password), "timeout": int(timeout) }) except: helpers.test_log(c.rest.error()) return False else: return True
def rest_verify_interface_is_up(self, node, interface_name): '''Verify if a given interface on a given switch is up Input: `switch_dpid` DPID of the Switch `interface_name` Interface Name e.g. ethernet13 Returns: True if the interface is up, false otherwise ''' try: t = test.Test() except: return False else: c = t.controller('master') try: switch_dpid = self.rest_return_switch_dpid_from_ip(node) url = '/api/v1/data/controller/core/switch[interface/name="%s"][dpid="%s"]?select=interface[name="%s"]' % ( interface_name, switch_dpid, interface_name) c.rest.get(url) except: helpers.test_log(c.rest.error()) return False else: if not c.rest.status_code_ok(): helpers.test_log(c.rest.error()) return False content = c.rest.content() if (content[0]['interface'][0]['state-flags'] == 0): return True else: return False
def rest_delete_syslog(self, syslog_server, log_level): '''Delete Syslog server Inputs: syslog_server: Name of Syslog server log_level : Logging Level, 0-9 Returns: True if configuration is successful, false otherwise ''' try: t = test.Test() except: return False else: c = t.controller('master') try: url = '/rest/v1/model/syslog-server/?logging-enabled=True&logging-server=%s&logging-level=%d' % ( str(syslog_server), int(log_level)) c.rest.delete(url, {}) except: helpers.test_log(c.rest.error()) return False else: helpers.test_log(c.rest.content_json()) return True
def rest_add_switch_datacenter(self,datacenter_name,switch_dpid,zone_name): '''Add switch to a datacenter Input: datacenter_name Datacenter Name switch_dpid DPID of switch zone_name Zone to which switch belongs Returns: True if configuration is successful, false otherwise ''' try: t = test.Test() except: return False else: c= t.controller('master') try: url='/api/v1/data/controller/applications/bigwire/datacenter[name="%s"]/member-switch[dpid="%s"]' % (str(datacenter_name),str(switch_dpid)) c.rest.put(url, {"zone": str(zone_name), "dpid": str(switch_dpid)}) except: helpers.test_failure(c.rest.error()) return False else: if not c.rest.status_code_ok(): helpers.test_failure(c.rest.error()) return False else: helpers.test_log(c.rest.content_json()) return True
def rest_show_switch(self): '''Return dictionary containing DPID,IP Addresses for every switch connected to current controller Input: N/A Returns: Dictionary of Switch DPID and IP Addresses ''' try: t = test.Test() except: return False else: c = t.controller('master') try: url = '/api/v1/data/controller/core/switch' c.rest.get(url) except: helpers.test_log(c.rest.error()) return False else: content = c.rest.content() switch_dict = {} for x in range(0, len(content)): switch_dict[str(content[x]['inet-address']['ip'])] = str( content[x]['dpid']) return switch_dict
def rest_bigtap_add_policy(self, view_name, policy_name, policy_action='inactive'): try: t = test.Test() except: return False else: c = t.controller('master') try: url = '/api/v1/data/controller/applications/bigtap/view[name="%s"]/policy[name="%s"]' % ( str(view_name), str(policy_name)) c.rest.put(url, {'name': str(policy_name)}) except: helpers.test_failure(c.rest.error()) return False else: if not c.rest.status_code_ok(): helpers.test_failure(c.rest.error()) try: c.rest.patch(url, {"action": str(policy_action)}) except: helpers.test_failure(c.rest.error()) return False else: if not c.rest.status_code_ok(): helpers.test_failure(c.rest.error()) return False else: helpers.test_log(c.rest.content_json()) return True
def rest_bigtap_add_service(self, service_name, intf_pre_nick, intf_post_nick): try: t = test.Test() except: return False else: c = t.controller('master') try: url = '/api/v1/data/controller/applications/bigtap/service[name="%s"]' % ( str(service_name)) c.rest.put(url, {"name": str(service_name)}) except: helpers.test_failure(c.rest.error()) return False else: #Add Pre-Service Interface try: url_add_intf = '/api/v1/data/controller/applications/bigtap/service[name="%s"]/pre-group[name="%s"]' % ( str(service_name), str(intf_pre_nick)) c.rest.put(url_add_intf, {"name": str(intf_pre_nick)}) except: helpers.test_failure(c.rest.error()) return False #Add Post-Service Interface try: url_add_intf = '/api/v1/data/controller/applications/bigtap/service[name="%s"]/post-group[name="%s"]' % ( str(service_name), str(intf_post_nick)) c.rest.put(url_add_intf, {"name": str(intf_post_nick)}) except: helpers.test_failure(c.rest.error()) return False else: helpers.test_log(c.rest.content_json()) return True
def rest_add_tenant_routers_to_system(self, tenant): '''Attach tenant router to system tenant" Input: `tenant` tenant name Return: true if configuration is successful, false otherwise REST-POST: PUT http://127.0.0.1:8080/api/v1/data/controller/applications/bvs/tenant[name="system"]/virtual-router/tenant-interfaces[tenant-name="A"] {"tenant-name": "A"} REST-POST: http://127.0.0.1:8080/api/v1/data/controller/applications/bvs/tenant[name="system"]/virtual-router/tenant-interfaces[tenant-name="A"] reply: "" ''' t = test.Test() c = t.controller('master') helpers.test_log("Input arguments: tenant = %s " % (tenant)) url = '/api/v1/data/controller/applications/bvs/tenant[name="system"]/virtual-router/tenant-interfaces[tenant-name="%s"]' % ( tenant) try: c.rest.post(url, {"tenant-name": tenant, "active": True}) except: helpers.test_failure(c.rest.error()) else: helpers.test_log("Output: %s" % c.rest.result_json()) return c.rest.content()
def rest_add_vns_ip(self, tenant, vns, ipaddr, netmask): '''Create vns router interface via command "virtual-router vns interface" Input: `tenant` tenant name `vns` vns interface name which must be similar to VNS `ipaddr` interface ip address `netmask` vns subnet mask PATCH http://127.0.0.1:8080/api/v1/data/controller/applications/bvs/tenant[name="A"]/virtual-router/vns-interfaces[vns-name="A2"] {"ip-cidr": "10.10.12.1/24"} Return: true if configuration is successful, false otherwise ''' t = test.Test() c = t.controller('master') helpers.test_log( "Input arguments: tenant = %s vns = %s ipaddr = %s netmask = %s " % (tenant, vns, ipaddr, netmask)) #url = '/api/v1/data/controller/applications/bvs/tenant[name="%s"]/virtual-router/vns-interfaces' % (tenant) url = '/api/v1/data/controller/applications/bvs/tenant[name="%s"]/virtual-router/vns-interfaces[vns-name="%s"]' % ( tenant, vns) ip_addr = ipaddr + "/" + netmask try: #c.rest.patch(url, {"ip-cidr": str(ip_addr)}) #c.rest.post(url, {"vns-name": vns, "ip-cidr": str(ip_addr), "active": True}) c.rest.put(url, {"vns-name": vns, "ip-cidr": str(ip_addr)}) except: #helpers.test_failure(c.rest.error()) return False else: #helpers.test_log("Output: %s" % c.rest.result_json()) #return c.rest.content() return True
def rest_delete_dhcp_relay(self, tenant, vnsname, dhcpserverip): '''Delete dhcp server " Input: `tenant` tenant name `vnsname` name of vns interface `dhcpserverip` DHCP server IP, can be anything since it will delete everything under the vns Return: true if configuration is successful, false otherwise REST-POST: DELETE http://127.0.0.1:8080/api/v1/data/controller/applications/bvs/tenant[name="B"]/virtual-router/vns-interfaces[vns-name="B1"]/dhcp-server-ip {} ''' t = test.Test() c = t.controller('master') helpers.test_log( "Input arguments: tenant = %s vns name = %s dhcp server ip = %s" % (tenant, vnsname, dhcpserverip)) url = '/api/v1/data/controller/applications/bvs/tenant[name="%s"]/virtual-router/vns-interfaces[vns-name="%s"]/dhcp-relay/dhcp-server-ip' % ( tenant, vnsname) try: c.rest.delete(url, {}) except: helpers.test_failure(c.rest.error()) else: helpers.test_log("Output: %s" % c.rest.result_json()) return c.rest.content()
def rest_add_dhcprelay_circuitid(self, tenant, vnsname, circuitid): '''Set dhcp relay circuit id" Input: `tenant` tenant name `vnsname` name of vns interface `circuitid` Circuit id, can be a string upto 15 characters Return: true if configuration is successful, false otherwise REST-POST: PATCH http://127.0.0.1:8080/api/v1/data/controller/applications/bvs/tenant[name="C"]/virtual-router/vns-interfaces[vns-name="C1"] {"dhcp-circuit-id": "this is a test"} REST-POST: http://127.0.0.1:8080/api/v1/data/controller/applications/bvs/tenant[name="C"]/virtual-router/vns-interfaces[vns-name="C1"] reply: "" ''' t = test.Test() c = t.controller('master') helpers.test_log( "Input arguments: tenant = %s vns name = %s circuit id = %s" % (tenant, vnsname, circuitid)) url = '/api/v1/data/controller/applications/bvs/tenant[name="%s"]/virtual-router/vns-interfaces[vns-name="%s"]/dhcp-relay' % ( tenant, vnsname) try: c.rest.patch(url, {"dhcp-circuit-id": circuitid}) except: helpers.test_failure(c.rest.error()) else: helpers.test_log("Output: %s" % c.rest.result_json()) return c.rest.content()
def rest_disable_dhcp_relay(self, tenant, vnsname): '''Enable dhcp relay on tenant VNS" Input: `tenant` tenant name `vnsname` name of vns interface Return: true if configuration is successful, false otherwise REST-POST: PATCH http://127.0.0.1:8080/api/v1/data/controller/applications/bvs/tenant[name="C"]/virtual-router/vns-interfaces[vns-name="C1"] {"dhcp-relay-enable": true} REST-POST: http://127.0.0.1:8080/api/v1/data/controller/applications/bvs/tenant[name="C"]/virtual-router/vns-interfaces[vns-name="C1"] reply: "" ''' t = test.Test() c = t.controller('master') helpers.test_log("Input arguments: tenant = %s vns name = %s " % (tenant, vnsname)) url = '/api/v1/data/controller/applications/bvs/tenant[name="%s"]/virtual-router/vns-interfaces[vns-name="%s"]/dhcp-relay' % ( tenant, vnsname) try: c.rest.patch(url, {"dhcp-relay-enable": False}) except: helpers.test_failure(c.rest.error()) else: helpers.test_log("Output: %s" % c.rest.result_json()) return c.rest.content()
def rest_add_switch_to_tenant(self,tenant_name,switch_dpid,intf_name,vlan=0): '''Add switch to a tenant Input: tenant_name Tenant Name switch_dpid DPID of switch intf_name Interface Name Vlan Tenant Vlan Number Returns: True if configuration is successful, false otherwise ''' try: t = test.Test() except: return False else: c= t.controller('master') try: url='/api/v1/data/controller/applications/bigwire/tenant[name="%s"]/tenant-interface[interface="%s"][switch="%s"]' %(str(tenant_name),str(intf_name),str(switch_dpid)) if vlan == 0: c.rest.put(url,{"interface": str(intf_name), "switch": str(switch_dpid)}) else: c.rest.put(url,{"interface": str(intf_name), "tenant-vlan": [{"vlan": int(vlan)}], "switch": str(switch_dpid)}) except: helpers.test_failure(c.rest.error()) return False else: helpers.test_log(c.rest.content_json()) return True
def cli_controller_failover(self, node='slave'): ''' Function to trigger failover to slave controller via CLI. Input: None Output: True if successful, False otherwise ''' t = test.Test() c = t.controller(node) helpers.log("Failover") try: c.config("config") c.send("reauth") c.expect(r"Password:"******"adminadmin") c.send("system failover") options = c.expect([r'Currently: STANDBY.*', r'Failover to a standby controller*']) if options[0] == 0: c.send("yes") elif options[0] == 1: c.send("yes") # helpers.sleep(30) # helpers.sleep(90) except: helpers.test_log(c.cli_content()) return False else: return True
def rest_delete_pseudowire(self,pseudowire_name): '''Delete a pseudowire Input: pseudowire_name Pseudowire Name Returns: True if configuration is successful, false otherwise ''' try: t = test.Test() except: return False else: c= t.controller('master') try: url='/api/v1/data/controller/applications/bigwire/pseudo-wire[name="%s"]' % (str(pseudowire_name)) c.rest.delete(url, {}) except: helpers.test_failure(c.rest.error()) return False else: if not c.rest.status_code_ok(): helpers.test_failure(c.rest.error()) return False else: helpers.test_log(c.rest.content_json()) return True
def cli_copy_file_to_running_config(self, file_name): ''' Function to copy <file> to running-config via CLI Input: <file> Output: True if successful, False otherwise ''' helpers.test_log("Running command:\ncopy <file> running-config ") t = test.Test() c = t.controller('master') file_name = "snapshot://" + file_name if self.verify_file (file_name): c.config("config") c.send("copy %s running-config" % file_name) try: c.expect(timeout=180) except: helpers.test_log(c.cli_content()) helpers.log('copying to running config took more than 3 mins') return False else: helpers.log('copying to running completed successfully') return True else: helpers.log("INFO: FILE not found") return False
def rest_cleanconfig_bigtap_user_defined_offset(self): '''Get all the user-defined-groups and delete them Input: None show command used: show running-config bigtap user-defined-group Output: Return false if groups are not deleted properly ''' t = test.Test() try: c = t.controller('master') except: return False show_url = "/api/v1/data/controller/applications/bigtap/user-defined-offset?config=true" try: c.rest.get(show_url) show_data = c.rest.content() except: helpers.test_failure(c.rest.error()) return False delete_url = "/api/v1/data/controller/applications/bigtap/user-defined-offset/%s/anchor {}" if len(show_data) != 0: for ugrp in show_data: for grp in ugrp.keys(): helpers.test_log("Deleting the group %s" % (grp)) c.rest.delete(delete_url % (grp)) return True else: helpers.test_log("There are no user-defined offsets to delete") return True
def copy_config_from_server_to_snapshot(self, file_path, server, server_passwd, dest_file='cfg_file_from_server.cfg'): t = test.Test() c = t.controller('master') c.config('') # helpers.log("INFO: ****Getting config file from server") # string = "copy scp://root@%s:%s %s" % (server, file_path, dest_file) # helpers.log("copy string file is:%s" % string) # c.send(string) try: helpers.log("INFO: ****Getting config file from server") dest_file_format = "snapshot://" + dest_file string = "copy scp://root@%s:%s %s" % (server, file_path, dest_file_format) helpers.log("copy string file is:%s" % string) c.send (string) try: c.expect(r"Are you sure you want to continue connecting \(yes/no\)?") c.send("yes") except: helpers.test_log("Apparently already RSA key fingerprint stored") c.expect("password") c.send(server_passwd) c.expect() cli_content = c.cli_content() assert "100%" in cli_content assert "Error" not in cli_content except: helpers.test_log(c.cli_content()) return False else: return False
def restart_controller(self, controller_role): '''Restart a process on controller Input: processName Name of process to be restarted controller_role Where to execute the command. Accepted values are `Master` and `Slave` Return Value: True if the configuration is successful, false otherwise ''' try: t = test.Test() if (controller_role == 'Master'): c = t.controller('master') else: c = t.controller('slave') c.bash("echo '#!/bin/bash' > test_reboot.sh") c.bash("echo 'sleep 15' >> test_reboot.sh") c.bash("echo 'sudo reboot' >> test_reboot.sh") c.bash("sh test_reboot.sh &") helpers.sleep(300) except: helpers.test_log(c.rest.error()) return False else: return True
def rest_bigtap_delete_service(self, service_name): '''Delete a bigtap service. Input: `service_name` : Name of Service Returns: True if service deletion is successful, false otherwise ''' try: t = test.Test() except: return False else: c = t.controller('master') try: url = '/api/v1/data/controller/applications/bigtap/service[name="%s"]' % ( str(service_name)) c.rest.delete(url, {}) except: helpers.test_failure(c.rest.error()) return False else: if not c.rest.status_code_ok(): helpers.test_failure(c.rest.error()) return False else: helpers.test_log(c.rest.content_json()) return True
def rest_configure_syslog(self, syslog_server, log_level): '''Configure Syslog server Inputs: syslog_server: Name of Syslog server log_level : Logging Level, 0-9 Returns: True if configuration is successful, false otherwise ''' try: t = test.Test() except: return False else: c = t.controller('master') try: url = '/rest/v1/model/syslog-server/' c.rest.put( url, { "logging-enabled": True, "logging-server": str(syslog_server), "logging-level": int(log_level) }) except: helpers.test_log(c.rest.error()) return False else: helpers.test_log(c.rest.content_json()) return True
def rest_bigtap_delete_service_from_policy(self, rbac_view_name, policy_name, service_name): '''Delete a service from a policy. This is similar to executing CLI command "no use-service S1-LB7 sequence 1" Input: `rbac_view_name` : RBAC View Name for eg. admin-view `policy_name` : Policy Name `service_name` : Name of Service Returns: True if deletion of service from policy is successful, false otherwise ''' try: t = test.Test() except: return False else: c = t.controller('master') try: url = '/api/v1/data/controller/applications/bigtap/view[name="%s"]/policy[name="%s"]/service[name="%s"]' % ( str(rbac_view_name), str(policy_name), str(service_name)) c.rest.delete(url, {}) except: helpers.test_failure(c.rest.error()) return False else: if not c.rest.status_code_ok(): helpers.test_failure(c.rest.error()) return False else: helpers.test_log(c.rest.content_json()) return True
def rest_set_banner(self, banner_message): '''Set Banner on controller Inputs: banner_message: Message to be set Returns: True if configuration is successful, false otherwise ''' try: t = test.Test() except: return False else: c = t.controller('master') try: url = '/rest/v1/model/banner/' c.rest.put(url, { "message": str(banner_message), "id": "banner" }) except: return False else: helpers.test_log(c.rest.content_json()) return True
def rest_bigtap_enable_feature(self, feature_name): '''Enable a bigtap feature Input: `feature_name` : Bigtap Feature Name. Currently allowed feature names are `overlap`,`inport-mask`,`tracked-host`,`l3-l4-mode` Returns: True if feature is enabled successfully, false otherwise Examples: | rest enable feature | overlap | ''' try: t = test.Test() except: return False else: c = t.controller('master') try: url = '/api/v1/data/controller/applications/bigtap/feature' c.rest.patch(url, {str(feature_name): True}) except: helpers.test_failure(c.rest.error()) return False else: if not c.rest.status_code_ok(): helpers.test_failure(c.rest.error()) return False else: helpers.test_log(c.rest.content_json()) return True
def rest_delete_tacacs_authorization(self): ''' Objective: Add TACACS configuration Input: | tacacs_server | Tacacs Server | Return Value: True on Success False on Failure ''' try: t = test.Test() except: return False else: c = t.controller('master') # Configure AAA/authorization try: url = '/api/v1/data/controller/core/aaa/authorizer' c.rest.delete(url, {}) except: helpers.test_log(c.rest.error()) return False else: return True
def rest_create_bigwire_pseudowire(self,pseudowire_name,switch_dpid_1,intf_name_1,switch_dpid_2,intf_name_2,vlan=0): '''Create a bigwire pseudowire Input: pseudowire_name Name of bigwire pseudowire switch_dpid_1 DPID of first Switch intf_name_1 Uplink port/interface name for first Switch switch_dpid_2 DPID of second Switch intf_name_2 Uplink port/interface name for second Switch Vlan Vlan Number (in case of Vlan Mode, defaults to 0 for port-mode) Returns: True if configuration is successful, false otherwise ''' try: t = test.Test() except: return False else: c= t.controller('master') try: url='/api/v1/data/controller/applications/bigwire/pseudo-wire[name="%s"]' % (str(pseudowire_name)) if vlan == 0: c.rest.put(url, {"interface1": str(intf_name_1), "switch2": str(switch_dpid_2), "switch1": str(switch_dpid_1), "interface2": str(intf_name_2), "name": str(pseudowire_name)}) else: c.rest.put(url, {"interface1": str(intf_name_1), "switch2": str(switch_dpid_2), "switch1": str(switch_dpid_1), "interface2": str(intf_name_2), "name": str(pseudowire_name), "vlan": int(vlan) }) except: helpers.test_failure(c.rest.error()) return False else: if not c.rest.status_code_ok(): helpers.test_failure(c.rest.error()) return False else: helpers.test_log(c.rest.content_json()) return True
def cli_execute_show_command(self, command, user="******", password="******"): try: t = test.Test() except: return False else: c = t.controller('master') # Get encoded password from key try: if "admin" not in user: c_user = t.node_spawn(ip=c.ip(), user=str(user), password=password) c_user.enable(command) content = c_user.cli_content() c_user.close() else: c.enable(command) content = c.cli_content() except: helpers.test_log(c.rest.error()) return False else: return content
def rest_delete_vns_ip(self, tenant, vnsname, ipaddr, netmask): '''Create vns router interface via command "virtual-router vns interface" Input: `tenant` tenant name `vnsname` vns interface name which must be similar to VNS `ipaddr` interface ip address `netmask` vns subnet mask REST-POST: DELETE http://127.0.0.1:8080/api/v1/data/controller/applications/bvs/tenant[name="B"]/virtual-router/vns-interfaces[vns-name="B1"]/ip-cidr {} Return: true if configuration is successful, false otherwise ''' t = test.Test() c = t.controller('master') helpers.test_log( "Input arguments: tenant = %s vns = %s ipaddr = %s netmask = %s " % (tenant, vnsname, ipaddr, netmask)) url = '/api/v1/data/controller/applications/bvs/tenant[name="%s"]/virtual-router/vns-interfaces[vns-name="%s"]/ip-cidr' % ( tenant, vnsname) ip_addr = ipaddr + "/" + netmask try: c.rest.delete(url, {}) except: return False #helpers.test_failure(c.rest.error()) else: #helpers.test_log("Output: %s" % c.rest.result_json()) #return c.rest.content() return True