+ if self.test:
+ return True
+ try:
+ ovs_veth_name = '{}-vethOD'.format(str(vlan))
+ ns_veth = '{}-vethDO'.format(str(vlan))
+ dhcp_namespace = '{}-dnsmasq'.format(str(vlan))
+
+ command = 'sudo ip netns add {}'.format(dhcp_namespace)
+ self.run_command(command)
+
+ command = 'sudo ip link add {} type veth peer name {}'.format(ns_veth, ovs_veth_name)
+ self.run_command(command)
+
+ command = 'sudo ip link set {} netns {}'.format(ns_veth, dhcp_namespace)
+ self.run_command(command)
+
+ command = 'sudo ip netns exec {} ip link set dev {} up'.format(dhcp_namespace, ns_veth)
+ self.run_command(command)
+
+ command = 'sudo ovs-vsctl add-port br-int {} tag={}'.format(ovs_veth_name, str(vlan))
+ self.run_command(command, ignore_exit_status=True)
+
+ command = 'sudo ip link set dev {} up'.format(ovs_veth_name)
+ self.run_command(command)
+
+ command = 'sudo ip netns exec {} ip link set dev lo up'.format(dhcp_namespace)
+ self.run_command(command)
+
+ command = 'sudo ip netns exec {} ifconfig {} {} netmask {}'.format(dhcp_namespace,
+ ns_veth,
+ ip_listen_address,
+ netmask)
+ self.run_command(command)
+ return True
+ except RunCommandException as e:
+ self.logger.error("create_dhcp_interfaces ssh Exception: {}".format(str(e)))
+ return False
+
+ def delete_qrouter_connection(self, vlan, link):
+ """
+ Delete qrouter Namesapce with all veth interfaces need it
+ :param vlan:
+ :param link:
+ :return:
+ """
+
+ if self.test:
+ return True
+ try:
+ ns_qouter = '{}-qrouter'.format(str(vlan))
+ qrouter_ovs_veth = '{}-vethOQ'.format(str(vlan))
+ qrouter_ns_veth = '{}-vethQO'.format(str(vlan))
+ qrouter_br_veth = '{}-vethBQ'.format(str(vlan))
+ qrouter_ns_router_veth = '{}-vethQB'.format(str(vlan))
+
+ command = 'sudo ovs-vsctl del-port br-int {}'.format(qrouter_ovs_veth)
+ self.run_command(command, ignore_exit_status=True)
+
+ # down ns veth
+ command = 'sudo ip netns exec {} ip link set dev {} down'.format(ns_qouter, qrouter_ns_veth)
+ self.run_command(command, ignore_exit_status=True)
+
+ command = 'sudo ip netns exec {} ip link delete {} '.format(ns_qouter, qrouter_ns_veth)
+ self.run_command(command, ignore_exit_status=True)
+
+ command = 'sudo ip netns del ' + ns_qouter
+ self.run_command(command)
+
+ # down ovs veth interface
+ command = 'sudo ip link set dev {} down'.format(qrouter_br_veth)
+ self.run_command(command, ignore_exit_status=True)
+
+ # down br veth interface
+ command = 'sudo ip link set dev {} down'.format(qrouter_ovs_veth)
+ self.run_command(command, ignore_exit_status=True)
+
+ # delete veth interface
+ command = 'sudo ip link delete {} '.format(link, qrouter_ovs_veth)
+ self.run_command(command, ignore_exit_status=True)
+
+ # down br veth interface
+ command = 'sudo ip link set dev {} down'.format(qrouter_ns_router_veth)
+ self.run_command(command, ignore_exit_status=True)
+
+ # delete veth interface
+ command = 'sudo ip link delete {} '.format(link, qrouter_ns_router_veth)
+ self.run_command(command, ignore_exit_status=True)
+
+ # down br veth interface
+ command = 'sudo brctl delif {} {}'.format(link, qrouter_br_veth)
+ self.run_command(command)
+
+ # delete NS
+ return True
+ except RunCommandException as e:
+ self.logger.error("delete_qrouter_connection ssh Exception: {}".format(str(e)))
+ return False
+
+ def create_qrouter_ovs_connection(self, vlan, gateway, dhcp_cidr):
+ """
+ Create qrouter Namesapce with all veth interfaces need it between NS and OVS
+ :param vlan:
+ :param gateway:
+ :return:
+ """
+
+ if self.test:
+ return True
+
+ try:
+ ns_qouter = '{}-qrouter'.format(str(vlan))
+ qrouter_ovs_veth ='{}-vethOQ'.format(str(vlan))
+ qrouter_ns_veth = '{}-vethQO'.format(str(vlan))
+
+ # Create NS
+ command = 'sudo ip netns add {}'.format(ns_qouter)
+ self.run_command(command)
+
+ # Create pait veth
+ command = 'sudo ip link add {} type veth peer name {}'.format(qrouter_ns_veth, qrouter_ovs_veth)
+ self.run_command(command, ignore_exit_status=True)
+
+ # up ovs veth interface
+ command = 'sudo ip link set dev {} up'.format(qrouter_ovs_veth)
+ self.run_command(command)
+
+ # add ovs veth to ovs br-int
+ command = 'sudo ovs-vsctl add-port br-int {} tag={}'.format(qrouter_ovs_veth, vlan)
+ self.run_command(command)
+
+ # add veth to ns
+ command = 'sudo ip link set {} netns {}'.format(qrouter_ns_veth, ns_qouter)
+ self.run_command(command)
+
+ # up ns loopback
+ command = 'sudo ip netns exec {} ip link set dev lo up'.format(ns_qouter)
+ self.run_command(command)
+
+ # up ns veth
+ command = 'sudo ip netns exec {} ip link set dev {} up'.format(ns_qouter, qrouter_ns_veth)
+ self.run_command(command)
+
+ from netaddr import IPNetwork
+ ip_tools = IPNetwork(dhcp_cidr)
+ cidr_len = ip_tools.prefixlen
+
+ # set gw to ns veth
+ command = 'sudo ip netns exec {} ip address add {}/{} dev {}'.format(ns_qouter, gateway, cidr_len, qrouter_ns_veth)
+ self.run_command(command)
+
+ return True
+
+ except RunCommandException as e:
+ self.logger.error("Create_dhcp_interfaces ssh Exception: {}".format(str(e)))
+ return False
+
+ def add_ns_routes(self, vlan, routes):
+ """
+
+ :param vlan:
+ :param routes:
+ :return:
+ """