Esempio n. 1
0
    def add_fdb_entries(self,
                      tunnel_name=None,
                      fdb_entries=None):
        for tunnel_name in fdb_entries:
            folder = fdb_entries[tunnel_name]['folder']
            request_url = self.bigip.icr_url + '/net/fdb/tunnel/'
            request_url += '~' + folder + '~' + tunnel_name
            existing_records = self.get_fdb_entry(tunnel_name=tunnel_name,
                                         mac=None,
                                         folder=folder)
            new_records = []
            new_mac_addresses = []
            new_arp_addresses = {}

            for mac in fdb_entries[tunnel_name]['records']:
                fdb_entry = dict()
                fdb_entry['name'] = mac
                fdb_entry['endpoint'] = mac['endpoint']
                new_records.append(fdb_entry)
                new_mac_addresses.append(mac)
                new_arp_addresses[mac] = mac['ip_address']

            for record in existing_records:
                if not record['name'] in new_mac_addresses:
                    new_records.append(record)
                else:
                    if record['name'] in new_arp_addresses:
                        del(new_arp_addresses[record['name']])

            payload = dict()
            payload['records'] = new_records
            response = self.bigip.icr_session.put(request_url,
                                        data=json.dumps(payload),
                                        timeout=const.CONNECTION_TIMEOUT)
            if response.status_code < 400:
                if const.FDB_POPULATE_STATIC_ARP:
                    for mac in new_arp_addresses:
                        try:
                            self.bigip.arp.create(
                                ip_address=new_arp_addresses[mac],
                                mac_address=mac,
                                folder=folder)
                        except Exception as e:
                            Log.error('L2GRE',
                                      'could not create static arp: %s'
                                      % e.message)
            return True
        else:
            Log.error('L2GRE', response.text)
            raise exceptions.L2GRETunnelUpdateException(response.text)
        return False
Esempio n. 2
0
    def add_fdb_entry(self,
                      tunnel_name=None,
                      mac_address=None,
                      vtep_ip_address=None,
                      arp_ip_address=None,
                      folder=None):
        folder = str(folder).replace('/', '')
        request_url = self.bigip.icr_url + '/net/fdb/tunnel/'
        request_url += '~' + folder + '~' + tunnel_name + '?ver=11.5.0'
        records = self.get_fdb_entry(tunnel_name=tunnel_name,
                                     mac=None,
                                     folder=folder)
        fdb_entry = dict()
        fdb_entry['name'] = mac_address
        fdb_entry['endpoint'] = vtep_ip_address

        for i in range(len(records)):
            if records[i]['name'] == mac_address:
                records[i] = fdb_entry
                break
        else:
            records.append(fdb_entry)

        payload = dict()
        payload['records'] = records
        response = self.bigip.icr_session.patch(
            request_url,
            data=json.dumps(payload),
            timeout=const.CONNECTION_TIMEOUT)
        if response.status_code < 400:
            if const.FDB_POPULATE_STATIC_ARP:
                if arp_ip_address:
                    try:
                        if self.bigip.arp.create(ip_address=arp_ip_address,
                                                 mac_address=mac_address,
                                                 folder=folder):
                            return True
                        else:
                            return False
                    except Exception as e:
                        Log.error(
                            'L2GRE',
                            'could not create static arp: %s' % e.message)
                        return False
            return True
        else:
            Log.error('L2GRE', response.text)
            raise exceptions.L2GRETunnelUpdateException(response.text)
        return False
Esempio n. 3
0
 def delete_all_fdb_entries(self, tunnel_name=None, folder='Common'):
     """ Delete all fdb entries for a tunnel """
     folder = str(folder).replace('/', '')
     request_url = self.bigip.icr_url + '/net/fdb/tunnel/'
     request_url += '~' + folder + '~' + tunnel_name + '?ver=11.5.0'
     response = self.bigip.icr_session.patch(
         request_url,
         data=json.dumps({'records': None}),
         timeout=const.CONNECTION_TIMEOUT)
     if response.status_code < 400 or response.status_code == 404:
         return True
     else:
         Log.error('L2GRE', response.text)
         raise exceptions.L2GRETunnelUpdateException(response.text)
     return False
Esempio n. 4
0
    def delete_tunnel(self, name=None, folder='Common'):
        """ Delete tunnel """
        folder = str(folder).replace('/', '')
        # delete arp and fdb records for this tunnel first
        request_url = self.bigip.icr_url + '/net/fdb/tunnel/'
        request_url += '~' + folder + '~' + name
        response = self.bigip.icr_session.get(request_url,
                                              timeout=const.CONNECTION_TIMEOUT)
        if response.status_code < 400:
            response_obj = json.loads(response.text)
            if const.FDB_POPULATE_STATIC_ARP:
                if 'records' in response_obj:
                    for record in response_obj['records']:
                        self.bigip.arp.delete_by_mac(
                            mac_address=record['name'], folder=folder)
            payload = dict()
            payload['records'] = []
            tunnel_link = self.bigip.icr_link(response_obj['selfLink'])
            response = self.bigip.icr_session.put(
                tunnel_link,
                data=json.dumps(payload),
                timeout=const.CONNECTION_TIMEOUT)
            response = self.bigip.icr_session.delete(tunnel_link)
            if response.status_code > 399:
                Log.error('fdb', response.text)
                raise exceptions.L2GRETunnelUpdateException(response.text)
        elif response.status_code != 404:
            Log.error('fdb', response.text)
            raise exceptions.L2GRETunnelQueryException(response.text)

        request_url = self.bigip.icr_url + '/net/tunnels/tunnel/'
        request_url += '~' + folder + '~' + name
        response = self.bigip.icr_session.delete(
            request_url, timeout=const.CONNECTION_TIMEOUT)
        if response.status_code < 400:
            return True
        elif response.status_code == 404:
            return True
        else:
            Log.error('L2GRE', response.text)
            raise exceptions.L2GRETunnelDeleteException(response.text)
        return False
Esempio n. 5
0
 def delete_fdb_entry(self,
                      tunnel_name=None,
                      mac_address=None,
                      arp_ip_address=None,
                      folder='Common'):
     """ Delete fdb entry for a tunnel """
     folder = str(folder).replace('/', '')
     if const.FDB_POPULATE_STATIC_ARP:
         if arp_ip_address:
             self.bigip.arp.delete(ip_address=arp_ip_address, folder=folder)
     request_url = self.bigip.icr_url + '/net/fdb/tunnel/'
     request_url += '~' + folder + '~' + tunnel_name + '?ver=11.5.0'
     records = self.get_fdb_entry(tunnel_name=tunnel_name,
                                  mac=None,
                                  folder=folder)
     if not records:
         return False
     original_len = len(records)
     records = [
         record for record in records if record.get('name') != mac_address
     ]
     if original_len != len(records):
         if len(records) == 0:
             records = None
         payload = dict()
         payload['records'] = records
         response = self.bigip.icr_session.patch(
             request_url,
             data=json.dumps(payload),
             timeout=const.CONNECTION_TIMEOUT)
         if response.status_code < 400:
             return True
         elif response.status_code == 404:
             return True
         else:
             Log.error('L2GRE', response.text)
             raise exceptions.L2GRETunnelUpdateException(response.text)
         return False
     return False
Esempio n. 6
0
    def delete_fdb_entries(self,
                           tunnel_name=None,
                           fdb_entries=None):
        for tunnel_name in fdb_entries:
            folder = fdb_entries[tunnel_name]['folder']
            request_url = self.bigip.icr_url + '/net/fdb/tunnel/'
            request_url += '~' + folder + '~' + tunnel_name
            existing_records = self.get_fdb_entry(tunnel_name=tunnel_name,
                                         mac=None,
                                         folder=folder)
            arps_to_delete = {}
            new_records = []

            for record in existing_records:
                for mac in fdb_entries[tunnel_name]['records']:
                    if record['name'] == mac:
                        arps_to_delete[mac] = mac['ip_address']
                        break
                else:
                    new_records.append(record)

            if len(new_records) == 0:
                new_records = None
            payload = dict()
            payload['records'] = new_records
            response = self.bigip.icr_session.put(request_url,
                                        data=json.dumps(payload),
                                        timeout=const.CONNECTION_TIMEOUT)
            if response.status_code < 400:
                if const.FDB_POPULATE_STATIC_ARP:
                    for mac in arps_to_delete:
                        self.bigip.arp.delete(
                                      ip_address=arps_to_delete[mac],
                                      folder='Common')
            return True
        else:
            Log.error('L2GRE', response.text)
            raise exceptions.L2GRETunnelUpdateException(response.text)
        return False