def _set_allowed_address_pairs(self, intf):
     aap_list = []
     for aap_entry in intf.get(self.ALLOWED_ADDRESS_PAIRS, None) or []:
         aap = vnc_api.AllowedAddressPair()
         if aap_entry.get(self.PREFIX, None):
             cidr = aap_entry.get(self.PREFIX).split('/')
             if len(cidr) == 1:
                 if (vnc_api.IPAddress(cidr[0]).version == 4):
                     subnet = vnc_api.SubnetType(cidr[0], 32)
                 elif (vnc_api.IPAddress(cidr[0]).version == 6):
                     subnet = vnc_api.SubnetType(cidr[0], 128)
             elif len(cidr) == 2:
                 subnet = vnc_api.SubnetType(cidr[0], int(cidr[1]))
             else:
                 LOG.warn(
                     _("'Invalid allowed address ip %s.") %
                     aap_entry.get(self.PREFIX))
                 raise
         aap.set_ip(subnet)
         aap.set_mac(aap_entry.get(self.MAC_ADDRESS, None))
         if not aap_entry.get(self.MAC_ADDRESS, None):
             aap.set_mac("")
         else:
             aap.set_mac(aap_entry.get(self.MAC_ADDRESS))
         if not aap_entry.get(self.ADDRESS_MODE, None):
             aap.set_address_mode("active-active")
         else:
             aap.set_address_mode(aap_entry.get(self.ADDRESS_MODE))
         aap_list.append(aap)
     if aap_list:
         aaps = vnc_api.AllowedAddressPairs()
         aaps.set_allowed_address_pair(aap_list)
         return aaps
     return None
Esempio n. 2
0
    def _set_vmi_allowed_addr_pairs(self, vmi_obj, allowed_addr_pairs):
        aap_array = []
        for address_pair in allowed_addr_pairs or []:
            mode = u'active-standby'
            if 'mac_address' not in address_pair:
                address_pair['mac_address'] = ""

            cidr = address_pair['ip_address'].split('/')
            if len(cidr) == 1:
                subnet = vnc_api.SubnetType(cidr[0], 32)
            elif len(cidr) == 2:
                subnet = vnc_api.SubnetType(cidr[0], int(cidr[1]))
            else:
                self._raise_contrail_exception(
                    'BadRequest', resource='port',
                    msg='Invalid address pair argument')

            aap_array.append(vnc_api.AllowedAddressPair(
                subnet,
                address_pair['mac_address'], mode))

        aaps = vnc_api.AllowedAddressPairs()
        if aap_array:
            aaps.set_allowed_address_pair(aap_array)
        vmi_obj.set_virtual_machine_interface_allowed_address_pairs(aaps)
Esempio n. 3
0
def actionLif(data):
    args = data.split(' ')
    oper = args[0]
    mac = args[1]
    ip = args[2]
    terminal = args[3]
    if oper == 'add':
        already_exists = False
        lif_has_vmi = False
        allowed_address_pair_exists = False
        allowed_address_pairs_exists = False
        vmi_created = False
        svc_lif = args[4]
        print svc_lif
        vnName = svc_lif.split('__')[0]
        vr = svc_lif.split('__')[1]
        tenant = svc_lif.split('__')[2]
        svcId = svc_lif.split('__')[3]
        vn = getVirtualNetwork(tenant, vnName)
        physicalInterface = getPhysicalInterface(vr)
        logicalInterface = getLogicalInterface(physicalInterface, vnName + '_' + svcId)
        if logicalInterface.get_virtual_machine_interface_refs():
            lif_has_vmi = True
            for vmInt in logicalInterface.get_virtual_machine_interface_refs():
                vmIntObj = vnc_client.virtual_machine_interface_read(id = vmInt['uuid'])
                if vmIntObj.virtual_machine_interface_mac_addresses.mac_address[0] == mac:
                    for instIp in vmIntObj.get_instance_ip_back_refs():
                        instIpObj = vnc_client.instance_ip_read(id = instIp['uuid'])
                        if instIpObj.get_instance_ip_address() == ip:
                            already_exists = True
        if not already_exists and not lif_has_vmi:
            vmInterface = createVirtualMachineInterface(tenant, vnName, mac)
            instanceIp = createInstanceIp(ip, vmInterface, vn)
            logicalInterface.add_virtual_machine_interface(vmInterface)
            vnc_client.logical_interface_update(logicalInterface)
            vmi_created = True
        else:
            print 'interface already exists'
        if not already_exists and lif_has_vmi:
            print 'blabla'
            for vmInt in logicalInterface.get_virtual_machine_interface_refs():
                vmIntObj = vnc_client.virtual_machine_interface_read(id = vmInt['uuid'])
                if vmIntObj.get_virtual_machine_interface_allowed_address_pairs():
                    allowed_address_pairs_exists = True
                    allowedAddressPairs = vmIntObj.get_virtual_machine_interface_allowed_address_pairs()
                    if allowedAddressPairs.get_allowed_address_pair():
                        allowedAddressPair = allowedAddressPairs.get_allowed_address_pair()
                        for ap in allowedAddressPair:
                            if ap.get_mac() == mac and ap.get_ip().ip_prefix == ip:
                                allowed_address_pair_exists = True
        if not allowed_address_pair_exists and not vmi_created:
            if not allowed_address_pairs_exists:
                allowedAddressPairs = vnc_api.AllowedAddressPairs()
            ip = {'ip_prefix':ip,'ip_prefix_len':32}
            addrPair = vnc_api.AllowedAddressPair(ip=ip, mac=mac, address_mode='active-standby')
            allowedAddressPairs.add_allowed_address_pair(addrPair)
            vmIntObj.set_virtual_machine_interface_allowed_address_pairs(allowedAddressPairs)
            vnc_client.virtual_machine_interface_update(vmIntObj)
                 
    if oper == 'del':
       vmInt = getVirtualMachineInterface(mac)
       if vmInt:
           if vmInt.get_instance_ip_back_refs():
               for instIp in vmInt.get_instance_ip_back_refs():
                   vnc_client.instance_ip_delete(id=instIp['uuid'])
           if vmInt.get_logical_interface_back_refs():
               for logicalInterface in vmInt.get_logical_interface_back_refs():
                   logInt = vnc_client.logical_interface_read(id = logicalInterface['uuid'])
               logInt.del_virtual_machine_interface(vmInt)
               vnc_client.logical_interface_update(logInt)
           vnc_client.virtual_machine_interface_delete(id = vmInt.get_uuid())
       else:
           allowedApList = getAllowedAddressPair(mac, ip) 
           if allowedApList:
               vmIntObj = allowedApList[0]
               allowedAddressPairs = allowedApList[1]
               ap = allowedApList[2]
               allowedAddressPairs.delete_allowed_address_pair(ap)
               vmIntObj.set_virtual_machine_interface_allowed_address_pairs(allowedAddressPairs)
               vnc_client.virtual_machine_interface_update(vmIntObj)