+ def delete_qrouter_connection(self, vlan, link):
+ """
+ Delete qrouter Namesapce with all veth interfaces need it
+ :param vlan:
+ :param link:
+ :return:
+ """
+
+ ns_qouter = str(vlan) + '-qrouter'
+ qrouter_ovs_veth = str(vlan) + '-vethOQ'
+ qrouter_ns_veth = str(vlan) + '-vethQO'
+
+ qrouter_br_veth = str(vlan) + '-vethBQ'
+ qrouter_ns_router_veth = str(vlan) + '-vethQB'
+
+ # delete ovs veth to ovs br-int
+ command = 'sudo ovs-vsctl del-port br-int {}'.format(qrouter_ovs_veth)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ # down ns veth
+ command = 'sudo ip netns exec {} ip link set dev {} down'.format(ns_qouter, qrouter_ns_veth)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ # down ovs veth interface
+ command = 'sudo ip link set dev {} down'.format(qrouter_br_veth)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ # down br veth interface
+ command = 'sudo ip link set dev {} down'.format(qrouter_ovs_veth)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ # down br veth interface
+ command = 'sudo ip link set dev {} down'.format(qrouter_ns_router_veth)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ # down br veth interface
+ command = 'sudo brctl delif {} {}'.format(link, qrouter_br_veth)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+
+ # delete NS
+ command = 'sudo ip netns del ' + ns_qouter
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ 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:
+ """
+
+ ns_qouter = str(vlan) + '-qrouter'
+ qrouter_ovs_veth = str(vlan) + '-vethOQ'
+ qrouter_ns_veth = str(vlan) + '-vethQO'
+
+ # Create NS
+ command = 'sudo ip netns add ' + ns_qouter
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ # Create pait veth
+ command = 'sudo ip link add {} type veth peer name {}'.format(qrouter_ns_veth, qrouter_ovs_veth)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ # up ovs veth interface
+ command = 'sudo ip link set dev {} up'.format(qrouter_ovs_veth)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ # add ovs veth to ovs br-int
+ command = 'sudo ovs-vsctl add-port br-int {} tag={}'.format(qrouter_ovs_veth, vlan)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ # add veth to ns
+ command = 'sudo ip link set {} netns {}'.format(qrouter_ns_veth, ns_qouter)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ # up ns loopback
+ command = 'sudo ip netns exec {} ip link set dev lo up'.format(ns_qouter)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ # up ns veth
+ command = 'sudo ip netns exec {} ip link set dev {} up'.format(ns_qouter, qrouter_ns_veth)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ 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.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ def add_ns_routes(self, vlan, routes):
+
+ for key, value in routes.iteritems():
+ ns_qouter = str(vlan) + '-qrouter'
+ qrouter_ns_router_veth = str(vlan) + '-vethQB'
+ # up ns veth
+ if key == 'default':
+ command = 'sudo ip netns exec {} ip route add {} via {} '.format(ns_qouter, key, value)
+ else:
+ command = 'sudo ip netns exec {} ip route add {} via {} dev {}'.format(ns_qouter, key, value,
+ qrouter_ns_router_veth)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ def create_qrouter_br_connection(self, vlan, cidr, link):
+ """
+ Create veth interfaces between user bridge (link) and OVS
+ :param vlan:
+ :param link:
+ :return:
+ """
+
+ ns_qouter = str(vlan) + '-qrouter'
+ qrouter_ns_router_veth = str(vlan) + '-vethQB'
+ qrouter_br_veth = str(vlan) + '-vethBQ'
+
+ # Create pait veth
+ command = 'sudo ip link add {} type veth peer name {}'.format(qrouter_br_veth, qrouter_ns_router_veth)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ # up ovs veth interface
+ command = 'sudo ip link set dev {} up'.format(qrouter_br_veth)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ # add veth to ns
+ command = 'sudo ip link set {} netns {}'.format(qrouter_ns_router_veth, ns_qouter)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ # up ns veth
+ command = 'sudo ip netns exec {} ip link set dev {} up'.format(ns_qouter, qrouter_ns_router_veth)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ command = 'sudo ip netns exec {} ip address add {} dev {}'.format(ns_qouter, link['nat'], qrouter_ns_router_veth)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ command = 'sudo brctl show | grep {}'.format(link['iface'])
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ if content > '':
+ # up ns veth
+ command = 'sudo brctl addif {} {}'.format(link['iface'], qrouter_br_veth)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ # up ns veth
+ command = 'sudo ip netns exec {} iptables -t nat -A POSTROUTING -o {} -s {} -d {} -j MASQUERADE' \
+ .format(ns_qouter, qrouter_ns_router_veth, link['nat'], cidr)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+
+ else:
+ self.logger.error('Bridge {} given by user not exist'.format(qrouter_br_veth))
+
+
+
+ def create_link_bridge_to_ovs(self, vlan, link):
+ """
+ Create interfaces to connect a linux bridge with tenant net
+ :param vlan: segmentation id
+ :return: True if success
+ """
+ if self.test:
+ return True
+ try:
+
+ br_tap_name = str(vlan) + '-vethBO'
+ br_ovs_name = str(vlan) + '-vethOB'
+
+ # is a bridge or a interface
+ command = 'sudo brctl show | grep {}'.format(link)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ if content > '':
+ command = 'sudo ip link add {} type veth peer name {}'.format(br_tap_name, br_ovs_name)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ command = 'sudo ip link set dev {} up'.format(br_tap_name)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ command = 'sudo ip link set dev {} up'.format(br_ovs_name)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ command = 'sudo ovs-vsctl add-port br-int {} tag={}'.format(br_ovs_name, str(vlan))
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ command = 'sudo brctl addif ' + link + ' {}'.format(br_tap_name)
+ self.logger.debug("command: " + command)
+ (_, stdout, _) = self.ssh_conn.exec_command(command)
+ content = stdout.read()
+
+ if len(content) == 0:
+ return True
+ else:
+ return False
+ else:
+ self.logger.error('Link is not present, please check {}'.format(link))
+ return False
+ except paramiko.ssh_exception.SSHException as e:
+ self.logger.error("create_dhcp_interfaces ssh Exception: " + str(e))
+ if "SSH session not active" in str(e):
+ self.ssh_connect()
+ return False