def change_password(step, password): step("### Changing password to \"%s\" ###\n" % password) switch = switches[0] cfg_array = [] cfg_array.append("router bgp %s" % bgp1_asn) cfg_array.append("neighbor %s password %s" % (bgp1_neighbor, password)) SwitchVtyshUtils.vtysh_cfg_cmd(switch, cfg_array)
def verify_routes_received(step): step("### Verifying routes received... ###\n") switch = switches[0] # Network of BGP2 should be permitted by BGP1 network = "12.0.0.0/8" next_hop = "9.0.0.2" step("### Verifying route for switch %s ###\n" % switch.name) step("### Network: %s, Next-hop: %s - Should exist... ###\n" % (network, next_hop)) found = SwitchVtyshUtils.wait_for_route(switch, network, next_hop) assert found, "Route %s -> %s exists on %s" % \ (network, next_hop, switch.name) # Network of BGP3 should NOT be permitted by BGP1 network = "20.0.0.0/8" next_hop = "10.0.0.2" verify_route_exists = False step("### Verifying routes for switch %s ###\n" % switch.name) step("### Network: %s, Next-hop: %s - Should NOT exist... ###\n" % (network, next_hop)) found = SwitchVtyshUtils.wait_for_route(switch, network, next_hop, verify_route_exists) assert not found, "Route %s -> %s does not exist on %s" % \ (network, next_hop, switch.name) # Network of BGP1 should be permitted by BGP2 switch = switches[1] network = "15.0.0.0/8" next_hop = "9.0.0.1" step("### Verifying route for switch %s ###\n" % switch.name) step("### Network: %s, Next-hop: %s - Should exist... ###\n" % (network, next_hop)) found = SwitchVtyshUtils.wait_for_route(switch, network, next_hop) assert found, "Route %s -> %s exists on %s" % \ (network, next_hop, switch.name) # Network of BGP1 should be permitted by BGP3 switch = switches[2] network = "15.0.0.0/8" next_hop = "10.0.0.1" step("### Verifying route for switch %s ###\n" % switch.name) step("### Network: %s, Next-hop: %s - Should exist... ###\n" % (network, next_hop)) found = SwitchVtyshUtils.wait_for_route(switch, network, next_hop) assert found, "Route %s -> %s exists on %s" % \ (network, next_hop, switch.name)
def verify_no_route_map_match(step): step("\n########## Verifying no route-map match ##########\n") step("### Removing route-map configuration ###\n") switch = switches[0] routemap = bgp_config1.routemaps[0] prefixlist = routemap[1] action = routemap[3] cfg_array = [] cfg_array.append("route-map %s %s %d" % (prefixlist.name, action, prefixlist.seq_num)) cfg_array.append("no match ip address prefix-list %s" % prefixlist.name) SwitchVtyshUtils.vtysh_cfg_cmd(switch, cfg_array) reconfigure_neighbor(step) step("### Verifying first network of BGP1 received on BGP2 ###\n") switch = switches[1] network = bgp_config1.networks[0] next_hop = bgp_config1.routerid found = SwitchVtyshUtils.wait_for_route(switch, network, next_hop) assert found, "Route %s -> %s was not found on %s" % \ (network, next_hop, switch.name) step("### Previously denied network is now present in BGP2 ###\n")
def apply_bgp_config(step): global all_cfg_array step("\n########## Applying BGP configurations... ##########\n") all_cfg_array = [] i = 0 for bgp_cfg in bgpconfigarr: step("### Applying configurations for BGP: %s ###\n" % bgp_cfg.routerid) cfg_array = [] if i == 0: step("### Applying community list configurations for BGP1") cfg_array.append("ip community-list BGP_IN permit 2:0") cfg_array.append("ip community-list BGP_IN deny 3:0") step("### Applying route-map configurations for BGP1") cfg_array.append("route-map BGP_RMAP permit 10") cfg_array.append("match community BGP_IN") if i == 1: cfg_array.append("route-map BGP_2 permit 10") cfg_array.append("set community 2:0") if i == 2: cfg_array.append("route-map BGP_3 permit 10") cfg_array.append("set community 3:0") SwitchVtyshUtils.vtysh_cfg_cmd(switches[i], cfg_array) del cfg_array[:] # Initiate BGP configuration cfg_array.append("router bgp %s" % bgp_cfg.asn) cfg_array.append("bgp router-id %s" % bgp_cfg.routerid) # Add the networks this bgp will be advertising for network in bgp_cfg.networks: cfg_array.append("network %s/%s" % (network, default_pl)) if i == 0: cfg_array.append("neighbor 10.0.0.2 remote-as 3") cfg_array.append("neighbor 10.0.0.2 route-map BGP_RMAP in") cfg_array.append("neighbor 9.0.0.2 remote-as 2") cfg_array.append("neighbor 9.0.0.2 route-map BGP_RMAP in") if i == 1: cfg_array.append("neighbor 9.0.0.1 remote-as 1") cfg_array.append("neighbor 9.0.0.1 route-map BGP_2 out") if i == 2: cfg_array.append("neighbor 10.0.0.1 remote-as 1") cfg_array.append("neighbor 10.0.0.1 route-map BGP_3 out") SwitchVtyshUtils.vtysh_cfg_cmd(switches[i], cfg_array) # Add the configuration arrays to an array so that it can be used # for verification later. all_cfg_array.append(cfg_array) i += 1
def unconfigure_neighbor_bgp(step): step("\n########## Unconfiguring neighbor for BGP1... ##########\n") switch = switches[0] cfg_array = [] cfg_array.append("router bgp %s" % bgp1_asn) cfg_array.append("no neighbor %s" % bgp1_neighbor) SwitchVtyshUtils.vtysh_cfg_cmd(switch, cfg_array)
def configure_bgp(step): step("\n########## Applying BGP configurations... ##########\n") i = 0 for switch in switches: cfg_array = bgp_configs[i] i += 1 SwitchVtyshUtils.vtysh_cfg_cmd(switch, cfg_array)
def configure_bgp(step): step("\n########## Configuring BGP on all switches.. ##########\n") i = 0 for switch in switches: cfg_array = bgp_configs[i] i += 1 SwitchVtyshUtils.vtysh_cfg_cmd(switch, cfg_array)
def configure_bgp(step): step("\n########## Configuring BGP on all switches.. ##########\n") i = num_of_switches - 1 for iteration in range(num_of_switches): switch = switches[i] cfg_array = bgp_configs[i] i -= 1 SwitchVtyshUtils.vtysh_cfg_cmd(switch, cfg_array)
def unconfigure_remove_private_as(step): step("### Unconfiguring remove private AS for BGP2... ###\n") switch = switches[1] cfg_array = [] cfg_array.append("router bgp %s" % bgp2_asn) cfg_array.append("no neighbor %s remove-private-AS" % bgp2_neighbor) SwitchVtyshUtils.vtysh_cfg_cmd(switch, cfg_array)
def unconfigure_peer_group(step): switch = switches[0] step("\n########## Unconfiguring peer-group on %s ##########\n" % switch.name) cfg_array = [] cfg_array.append("router bgp %s" % bgp1_asn) cfg_array.append("no neighbor %s peer-group %s" % (bgp1_neighbor, bgp_peer_group)) SwitchVtyshUtils.vtysh_cfg_cmd(switch, cfg_array)
def change_to_no_neighbor_password(step): step("### Unsetting password ###\n") switch = switches[0] cfg_array = [] cfg_array.append("router bgp %s" % bgp1_asn) cfg_array.append("no neighbor %s password" % bgp1_neighbor) SwitchVtyshUtils.vtysh_cfg_cmd(switch, cfg_array) switch = switches[1] cfg_array = [] cfg_array.append("router bgp %s" % bgp2_asn) cfg_array.append("no neighbor %s password" % bgp2_neighbor) SwitchVtyshUtils.vtysh_cfg_cmd(switch, cfg_array)
def verify_routes_received(step): step("### Verifying routes received... ###\n") # Check route on switch 1 switch = switches[0] neighbor = bgp_config1.neighbors[0] network = neighbor.networks[0] next_hop = neighbor.routerid step("### Verifying route for switch %s ###\n" % switch.name) step("### Network: %s, Next-hop: %s - Should exist... ###\n" % (network, next_hop)) found = SwitchVtyshUtils.wait_for_route(switch, network, next_hop) assert found, "Route %s -> %s does not exist on %s" \ % (network, next_hop, switch.name) # Check routes on switch 2 switch = switches[1] neighbor = bgp_config2.neighbors[0] # Second network should not exist. network = neighbor.networks[1] next_hop = neighbor.routerid route_should_exist = False step("### Verifying routes for switch %s ###\n" % switch.name) step("### Network: %s, Next-hop: %s - Should NOT exist... ###\n" % (network, next_hop)) found = SwitchVtyshUtils.wait_for_route(switch, network, next_hop, route_should_exist) assert \ not found, "Route %s -> %s exists on %s" % (network, next_hop, switch.name) # First network should not exist. network = neighbor.networks[0] step("### Network: %s, Next-hop: %s - Should NOT exist... ###\n" % (network, next_hop)) found = SwitchVtyshUtils.wait_for_route(switch, network, next_hop, route_should_exist) assert not found, "Route %s -> %s exists on %s" \ % (network, next_hop, switch.name)
def verify_bgp_route(step, switch, network, next_hop): step("### Checking for route: %s --> %s ###\n" % (network, next_hop)) found = SwitchVtyshUtils.wait_for_route(switch, network, next_hop) assert found, "Could not find route (%s -> %s) on %s" % \ (network, next_hop, switch.name)
def verify_bgp_route_doesnt_exists(step, switch, network, next_hop): route_should_exist = False found = SwitchVtyshUtils.wait_for_route(switch, network, next_hop, route_should_exist) assert not found, "Route should not exist (%s -> %s) on %s" % \ (network, next_hop, switch.name)
def verify_no_set_metric_and_community_values(step): step("\n########## Verifying no community and no " "set metric values ##########\n") step("### Setting no set metric and no community on BGP1 ###\n") switch = switches[0] # Network 2 of BGP1 is the permitted route network = bgp_config1.networks[1] next_hop = bgp_config1.routerid routemap = bgp_config1.routemaps[0] prefixlist = routemap[1] action = routemap[3] metric = routemap[4] cfg_array = [] cfg_array.append("route-map %s %s %d" % (prefixlist.name, action, prefixlist.seq_num)) cfg_array.append("no set metric") cfg_array.append("no set community") SwitchVtyshUtils.vtysh_cfg_cmd(switch, cfg_array) reconfigure_neighbor(step) step("### Verifying no set metric. Metric should not exist ###\n") switch = switches[1] cmd = "show ip bgp" routes = switch(cmd).split("\r\n") metric_found = False for rte in routes: if (network in rte) and (next_hop in rte) and (metric in rte): metric_found = True break assert not metric_found, "Metric was found" step("### Metric was successfully unset on BGP1 ###\n") step("### Verifying no set community ###\n") cmd = "sh ip bgp %s" % network network_info = switch(cmd) assert rm_community not in network_info, "Community should not be set" step("### Community value was successfully unset ###\n")
def verify_route(step): step("### Verifying BGP route exists ###\n") switch = switches[0] found = SwitchVtyshUtils.wait_for_route(switch, bgp3_network, bgp2_router_id) assert found, "Route %s -> %s was not found" % (bgp3_network, bgp2_router_id)
def apply_bgp_config(step): global all_cfg_array step("\n########## Applying BGP configurations... ##########\n") all_cfg_array = [] i = 0 for bgp_cfg in bgpconfigarr: step("### Applying configurations for BGP: %s ###\n" % bgp_cfg.routerid) cfg_array = [] # Add any prefix-lists add_prefix_list_configs(bgp_cfg, cfg_array) print(cfg_array) SwitchVtyshUtils.vtysh_cfg_cmd(switches[i], cfg_array) del cfg_array[:] # Initiate BGP configuration cfg_array.append("router bgp %s" % bgp_cfg.asn) cfg_array.append("bgp router-id %s" % bgp_cfg.routerid) # Add the networks this bgp will be advertising for network in bgp_cfg.networks: cfg_array.append("network %s/%s" % (network, default_pl)) # Add the neighbors of this switch for neighbor in bgp_cfg.neighbors: cfg_array.append("neighbor %s remote-as %s" % (neighbor.routerid, neighbor.asn)) if bgp_cfg.asn is "1": # Add the neighbor prefix-list configs add_neighbor_prefix_list_configs(step, cfg_array) SwitchVtyshUtils.vtysh_cfg_cmd(switches[i], cfg_array) print(cfg_array) # Add the configuration arrays to an array so that it can be used # for verification later. all_cfg_array.append(cfg_array) i += 1
def verify_no_peer_group(step): step("\n########### Verifying no peer-group ##########\n") step("### Removing peer-group ###\n") switch = switches[0] cfg_array = [] cfg_array.append("router bgp %s" % bgp1_asn) peer_group_cfg = "neighbor %s" % bgp_peer_group cfg_array.append("no %s" % peer_group_cfg) SwitchVtyshUtils.vtysh_cfg_cmd(switch, cfg_array) step("### Verifying peer-group config removed ###\n") exists = SwitchVtyshUtils.verify_cfg_exist(switch, [peer_group_cfg]) assert not exists, "Peer-group was not unconfigured" step("### Peer-group unconfigured successfully ###\n")
def verify_configs(step): step("\n########## Verifying all configurations.. ##########\n") for i in range(0, len(bgp_configs)): bgp_cfg = bgp_configs[i] switch = switches[i] for cfg in bgp_cfg: res = SwitchVtyshUtils.verify_cfg_exist(switch, [cfg]) assert res, "Config \"%s\" was not correctly configured!" % cfg
def verify_neighbor_password(step): step("\n########## Verifying neighbor password ##########\n") switch = switches[1] found = SwitchVtyshUtils.wait_for_route(switch, bgp1_network, bgp1_router_id) assert found, "TCP connection not established(%s -> %s) on %s" % \ (bgp1_network, bgp1_router_id, switch.name) step("### Connection established succesfully ###\n")
def verify_no_connection(step): step("### Verifying no connection ###\n") switch = switches[1] verify_route_exists = False found = SwitchVtyshUtils.wait_for_route(switch, bgp1_network, bgp1_router_id, verify_route_exists) assert not found, "TCP connection should not be established"
def verify_no_ip_community(step): step("\n########## Verifying no ip prefix-list ##########\n") switch = switches[0] cmd = "no ip community-list BGP_IN" step("### Unconfiguring ip community-list BGP_IN ###\n") SwitchVtyshUtils.vtysh_cfg_cmd(switch, [cmd]) cfg = "ip community-list BGP_IN permit 2:0" step("### Checking ip community-list config ###\n") exists = SwitchVtyshUtils.verify_cfg_exist(switch, []) assert not exists, "Config \"%s\" was not removed" % cfg cfg = "ip community-list BGP_IN permit 3:0" step("### Checking ip community-list config ###\n") exists = SwitchVtyshUtils.verify_cfg_exist(switch, []) assert not exists, "Config \"%s\" was not removed" % cfg step("### ip community-list configs were successfully removed ###\n")
def verify_bgp_route_removed(step, switch, network, next_hop): step("\n########## Verifying route %s --> %s " "on switch %s removed... ##########\n" % (network, next_hop, switch.name)) verify_route_exists = False found = SwitchVtyshUtils.wait_for_route(switch, network, next_hop, verify_route_exists) assert not found, "Route (%s) was not successfully removed" % network step("### Route successfully removed ###\n")
def reconfigure_neighbor(step): step("### Reset connection from BGP2 via reconfiguring neighbor ###\n") switch = switches[1] neighbor = bgp_config2.neighbors[0] cfg_array = [] cfg_array.append("router bgp %s" % bgp_config2.asn) cfg_array.append("no neighbor %s" % neighbor.routerid) SwitchVtyshUtils.vtysh_cfg_cmd(switch, cfg_array) step("### Waiting for route to be removed ###\n") network = neighbor.networks[1] next_hop = neighbor.routerid route_should_exist = False found = SwitchVtyshUtils.wait_for_route(switch, network, next_hop, route_should_exist) assert not found, "Route %s -> %s exists on %s" \ % (network, next_hop, switch.name) step("### Reconfiguring neighbor (BGP1) on BGP2 ###\n") cfg_array = [] cfg_array.append("router bgp %s" % bgp_config2.asn) cfg_array.append("neighbor %s remote-as %s" % (neighbor.routerid, neighbor.asn)) SwitchVtyshUtils.vtysh_cfg_cmd(switch, cfg_array) step("### Waiting for route to be received again ###\n") found = SwitchVtyshUtils.wait_for_route(switch, network, next_hop) assert found, "Route %s -> %s was not found on %s" \ % (network, next_hop, switch.name)
def verify_advertised_routes(step): step("### Verifying advertised routes... ###\n") i = 0 for bgp_cfg in bgpconfigarr: switch = switches[i] next_hop = "0.0.0.0" for network in bgp_cfg.networks: found = SwitchVtyshUtils.wait_for_route(switch, network, next_hop) assert found, "Could not find route (%s -> %s) on %s" % \ (network, next_hop, switch.name) i += 1
def verify_bgp_configs(step): step("\n########## Verifying all configurations.. ##########\n") i = 0 for switch in switches: bgp_cfg_array = all_cfg_array[i] for cfg in bgp_cfg_array: res = SwitchVtyshUtils.verify_cfg_exist(switch, [cfg]) assert res, "Config \"%s\" was not correctly configured!" % cfg i += 1 step("### All configurations were verified successfully ###\n")
def reconfigure_neighbor(step): step("### Reconfiguring neighbor to refresh routes ###\n") switch = switches[0] cfg_array = [] cfg_array.append("router bgp %s" % bgp1_asn) cfg_array.append("no neighbor %s" % bgp1_neighbor) SwitchVtyshUtils.vtysh_cfg_cmd(switch, cfg_array) step("### Verifying route removed prior to proceeding ###\n") network = bgp3_network next_hop = bgp2_router_id route_should_exist = False found = SwitchVtyshUtils.wait_for_route(switch, network, next_hop, route_should_exist) assert not found, "Route %s -> %s still exists" % (network, next_hop) step("### Configuring neighbor again... ###\n") cfg_array = [] cfg_array.append("router bgp %s" % bgp1_asn) cfg_array.append("neighbor %s remote-as %s" % (bgp1_neighbor, bgp1_neighbor_asn)) SwitchVtyshUtils.vtysh_cfg_cmd(switch, cfg_array)
def verify_bgp_route_removed(step): step("\n########## Verifying route removed after " "peer removed from peer-group ##########\n") switch = switches[1] network = bgp1_network next_hop = bgp1_router_id verify_route_exists = False # Verify that the neighbor's route info should be removed. found = SwitchVtyshUtils.wait_for_route(switch, network, next_hop, verify_route_exists) assert not found, "Route still exists! (%s -> %s) on %s" % \ (network, next_hop, switch.name)
def verify_bgp_route_exists(step, switch, network, next_hop): found = SwitchVtyshUtils.wait_for_route(switch, network, next_hop) assert found, "Could not find route (%s -> %s) on %s" % \ (network, next_hop, switch.name)