def unset_ruijie_vlan(vif_id, net_id):

    LOG.debug("unset_ruijie_vlan, net id is %s, vif id is %s",net_id, vif_id)
    vm_eth_binding = rgos_db.get_ruijie_vm_eth_binding(vif_id)
    if vm_eth_binding == []:
        return
    mac_address = vm_eth_binding[0].mac_address
    LOG.debug("vif id %s has been connected to mac %s",vif_id, mac_address)
    switch_binding = rgos_db.get_ruijie_switch_eth_binding(mac_address)
    if switch_binding == []:
        return;
    ip = switch_binding[0].ip_address
    ifx = switch_binding[0].port_id
    ovs_binding = vlan_mgr.get_network_binding(None, net_id)
    if ovs_binding == None:
        return
    vlan = ovs_binding.segmentation_id
    LOG.debug("the switch ip is %s, ifx is %s, vlan is %s",ip, ifx, vlan)
    uuid = 0
    rgos_db.remove_ruijie_vlan_binding(ip, ifx, vlan, uuid)
    ruijie_vlan_binding = rgos_db.get_ruijie_vlan_binding(ip, ifx, vlan)
    if ruijie_vlan_binding == []:
        LOG.debug("to unset the vlan of ruijie switch now")
        unset_switch_vlan(ip, ifx, vlan)
    return 
def update_ruijie_vlan(vif_id, net_id, old_seg_id):
    
    LOG.debug("update_ruijie_vlan, net id is %s, vif id is %s, old vid %s",net_id, vif_id, old_seg_id)
    vm_eth_binding = rgos_db.get_ruijie_vm_eth_binding(vif_id)
    if vm_eth_binding == []:
        return
    mac_address = vm_eth_binding[0].mac_address
    LOG.debug("interface id %s has been connected to mac %s",vif_id, mac_address)
    switch_binding = rgos_db.get_ruijie_switch_eth_binding(mac_address)
    if switch_binding == []:
        return;
    ip = switch_binding[0].ip_address
    ifx = switch_binding[0].port_id
    ovs_binding = vlan_mgr.get_network_binding(None, net_id)
    if ovs_binding == None:
        return
    vlan = ovs_binding.segmentation_id
    
    # del old ruijie switch vlan
    rgos_db.remove_ruijie_vlan_binding(ip, ifx, old_seg_id, vif_id)
    ruijie_vlan_binding = rgos_db.get_ruijie_vlan_binding(ip, ifx, old_seg_id)
    if ruijie_vlan_binding == []:
        LOG.debug("to unset the vlan of ruijie switch now")
        unset_switch_vlan(ip, ifx, old_seg_id)
    
    ruijie_vlan_binding = rgos_db.get_ruijie_vlan_binding(ip, ifx, vlan)
    if ruijie_vlan_binding == []:
        LOG.debug("to set the vlan of ruijie switch now")
        set_switch_vlan(ip, ifx, vlan)
    rgos_db.add_ruijie_vlan_binding(ip, ifx, vlan, vif_id)
    
    return 
    def update_network(self, context, id, network):
        self._check_provider_update(context, network["network"])
        segmentation_id = network["network"]["segmentation_id"]
        LOG.debug("update_network: segmentation id = %s", segmentation_id)
        session = context.session
        with session.begin(subtransactions=True):
            binding = rgos_vlanmgr.get_network_binding(session, id)
            if binding.network_type == constants.TYPE_VLAN:
                rgos_vlanmgr.release_vlan(
                    session, binding.physical_network, binding.segmentation_id, self.network_vlan_ranges
                )
                rgos_vlanmgr.reserve_specific_vlan(session, binding.physical_network, segmentation_id)
                session.delete(binding)
                session.flush()
                rgos_vlanmgr.add_network_binding(
                    session, id, binding.network_type, binding.physical_network, segmentation_id
                )

        session = context.session
        with session.begin(subtransactions=True):
            net = super(RgosQuantumPlugin, self).update_network(context, id, network)
            self._process_l3_update(context, network["network"], id)
            self._extend_network_dict_provider(context, net)
            self._extend_network_dict_l3(context, net)
        return net
    def _extend_network_dict_provider(self, context, network):
        if self._check_provider_view_auth(context, network):
            binding = rgos_vlanmgr.get_network_binding(context.session, network["id"])
            network[provider.NETWORK_TYPE] = binding.network_type

            if binding.network_type == constants.TYPE_VLAN:
                network[provider.PHYSICAL_NETWORK] = binding.physical_network
                network[provider.SEGMENTATION_ID] = binding.segmentation_id
 def update_port(self, context, id, port):
     if self.agent_rpc:
         original_port = super(RgosQuantumPlugin, self).get_port(context, id)
     port = super(RgosQuantumPlugin, self).update_port(context, id, port)
     if self.agent_rpc:
         if original_port["admin_state_up"] != port["admin_state_up"]:
             binding = rgos_vlanmgr.get_network_binding(None, port["network_id"])
             self.notifier.port_update(
                 self.rpc_context, port, binding.network_type, binding.segmentation_id, binding.physical_network
             )
     return port
 def delete_network(self, context, id):
     session = context.session
     with session.begin(subtransactions=True):
         binding = rgos_vlanmgr.get_network_binding(session, id)
         super(RgosQuantumPlugin, self).delete_network(context, id)
         if binding.network_type == constants.TYPE_VLAN:
             rgos_vlanmgr.release_vlan(
                 session, binding.physical_network, binding.segmentation_id, self.network_vlan_ranges
             )
         # the network_binding record is deleted via cascade from
         # the network record, so explicit removal is not necessary
     if self.agent_rpc:
         self.notifier.network_delete(self.rpc_context, id)
    def vm_eth_unbind(self, vif, eth_mac):
        LOG.debug('del vm eth binding, vif: %s eth_mac: %s', vif, eth_mac)
        net_uuid = self.get_net_uuid(vif)
        rgos_db.remove_ruijie_vm_eth_binding(vif, eth_mac)

        # update Ruijie switch
        switch_binding = rgos_db.get_ruijie_switch_eth_binding(eth_mac)
        if switch_binding == []:
            return;
        ip = switch_binding[0].ip_address
        ifx = switch_binding[0].port_id
        ovs_binding = rgos_vlanmgr.get_network_binding(None, net_uuid)
        if ovs_binding == None:
            return
        vlan = ovs_binding.segmentation_id
        LOG.info("the switch ip is %s, ifx is %s, vlan is %s" 
                 % (ip, ifx, vlan))
        rgos_db.remove_ruijie_vlan_binding(ip, ifx, vlan, vif)
        ruijie_vlan_binding = rgos_db.get_ruijie_vlan_binding(ip, ifx, vlan)
        if ruijie_vlan_binding == []:
            LOG.info("to set the vlan of ruijie switch now")
            switch_driver.unset_switch_vlan(ip, 22, ifx, vlan)
 def get_device_details(self, rpc_context, **kwargs):
     """Agent requests device details"""
     agent_id = kwargs.get("agent_id")
     device = kwargs.get("device")
     LOG.debug("Device %s details requested from %s", device, agent_id)
     port = rgos_db.get_port(device)
     if port:
         binding = rgos_vlanmgr.get_network_binding(None, port["network_id"])
         entry = {
             "device": device,
             "network_id": port["network_id"],
             "port_id": port["id"],
             "admin_state_up": port["admin_state_up"],
             "network_type": binding.network_type,
             "segmentation_id": binding.segmentation_id,
             "physical_network": binding.physical_network,
         }
         # Set the port status to UP
         rgos_db.set_port_status(port["id"], q_const.PORT_STATUS_ACTIVE)
     else:
         entry = {"device": device}
         LOG.debug("%s can not be found in database", device)
     return entry