def test_links_deletion(self): for user in User.objects.all(): user.delete() settings.DEFAULT_LAYER = 5 management.call_command('import_old_nodeshot', noinput=True) link = Link.objects.first() self.assertIn('imported', link.data) # --- delete oldlink --- # OldLink.objects.first().delete() # --- ensure link is deleted --- # management.call_command('import_old_nodeshot', noinput=True) self.assertEqual(Link.objects.count(), 0) # --- add not imported link --- # i_a = Interface.objects.filter(type=INTERFACE_TYPES['wireless']).first() i_b = Interface.objects.filter(type=INTERFACE_TYPES['wireless']).last() added_link = Link(interface_a=i_a, interface_b=i_b, status=LINK_STATUS['active']) added_link.full_clean() added_link.save() # --- ensure added_link is not deleted --- # management.call_command('import_old_nodeshot', noinput=True) self.assertEqual(Link.objects.count(), 1) check_link = Link.objects.first() self.assertEqual(check_link.pk, added_link.pk)
def import_links(self): self.verbose('retrieving links from old mysql DB...') self.old_links = list(OldLink.objects.all()) self.message('retrieved %d links' % len(self.old_links)) saved_links = [] for old_link in self.old_links: skip = False try: interface_a = Interface.objects.get(pk=old_link.from_interface_id) if interface_a.type != INTERFACE_TYPES.get('wireless'): interface_a.type = INTERFACE_TYPES.get('wireless') interface_a.save() except Interface.DoesNotExist: self.message('Interface #%s does not exist, probably link #%s is orphan!' % (old_link.from_interface_id, old_link.id)) skip = True try: interface_b = Interface.objects.get(pk=old_link.to_interface_id) if interface_b.type != INTERFACE_TYPES.get('wireless'): interface_b.type = INTERFACE_TYPES.get('wireless') interface_b.save() except Interface.DoesNotExist: self.message('Interface #%s does not exist, probably link #%s is orphan!' % (old_link.to_interface_id, old_link.id)) skip = True if skip: self.verbose('Skipping to next cycle') continue old_bandwidth = [old_link.sync_tx, old_link.sync_rx] link = Link(**{ "id": old_link.id, "interface_a": interface_a, "interface_b": interface_b, "status": LINK_STATUS.get('active'), "type": LINK_TYPES.get('radio'), "metric_type": 'etx', "metric_value": old_link.etx, "dbm": old_link.dbm, "min_rate": min(old_bandwidth), "max_rate": max(old_bandwidth), }) if old_link.hide: link.access_level = 3 try: link.full_clean() link.save() saved_links.append(link) self.verbose('Saved link %s' % link) except Exception as e: self.message('Could not save link %s, got exception: %s' % (old_link.id, e)) self.message('saved %d links into local DB' % len(saved_links)) self.saved_links = saved_links
def save_links(self): added_links = [] deleted_links = [] cnml_links = self.cnml.getLinks() current_links = Link.objects.filter(data__contains=['cnml_id'], layer=self.layer) # keep a list of link cnml_id # TODO: hstore seems to not work properly, something is wrong cnml_id_list = [] cnml_id_mapping = {} for current_link in current_links: cnml_id_list.append(current_link.data['cnml_id']) cnml_id_mapping[current_link.data['cnml_id']] = current_link.id total_n = len(cnml_links) for n, cnml_link in enumerate(cnml_links, 1): if str(cnml_link.id) in cnml_id_list: try: link_id = cnml_id_mapping[str(cnml_link.id)] # probably a link between a node of another zone except KeyError: continue link = Link.objects.get(id=link_id) added = False else: link = Link() added = True # link between a node which is not of this CNML zone if isinstance(cnml_link.nodeA, int) or isinstance(cnml_link.nodeB, int): continue node_a = Node.objects.get(data={'cnml_id': cnml_link.nodeA.id}) node_b = Node.objects.get(data={'cnml_id': cnml_link.nodeB.id}) link.node_a = node_a link.node_b = node_b link.type = LINK_TYPES.get(self.LINK_TYPE_MAPPING[cnml_link.type]) link.status = LINK_STATUS.get(self.LINK_STATUS_MAPPING[cnml_link.status]) self.verbose('[%d/%d] parsing link "%s" (%s<-->%s)' % (n, total_n, cnml_link.id, node_a, node_b)) # set interface_a and interface_b only if not planned # because there might be inconsistencies in the CNML from guifi.net if link.get_status_display() != 'planned': if cnml_link.interfaceA: interface_a = Interface.objects.get(data={'cnml_id': cnml_link.interfaceA.id}) link.interface_a = interface_a if cnml_link.interfaceB: interface_b = Interface.objects.get(data={'cnml_id': cnml_link.interfaceB.id}) link.interface_b = interface_b else: link.interface_a = None link.interface_b = None link.data['cnml_id'] = cnml_link.id try: link.full_clean() except Exception as e: print(e) continue link.save() if added: added_links.append(link) # delete links that are not in CNML anymore total_n = len(current_links) for n, current_link in enumerate(current_links, 1): self.verbose('[%d/%d] deleting old links' % (n, total_n)) try: self.cnml.getLink(int(current_link.data['cnml_id'])) except KeyError: deleted_links.append(current_link) current_link.delete() self.message += """ %s links added %s links deleted """ % ( len(added_links), len(deleted_links) )
def import_links(self): if not IMPORT_LINKS: self.message('skpped import_links because NODESHOT_OLDIMPORTER_IMPORT_LINKS is set to False') return self.verbose('retrieving links from old mysql DB...') self.old_links = list(OldLink.objects.all()) self.message('retrieved %d links' % len(self.old_links)) saved_links = [] for old_link in self.old_links: skip = False try: interface_a = Interface.objects.get(pk=old_link.from_interface_id) if interface_a.type != INTERFACE_TYPES.get('wireless'): interface_a.type = INTERFACE_TYPES.get('wireless') interface_a.save() except Interface.DoesNotExist: self.message('Interface #%s does not exist, probably link #%s is orphan!' % (old_link.from_interface_id, old_link.id)) skip = True try: interface_b = Interface.objects.get(pk=old_link.to_interface_id) if interface_b.type != INTERFACE_TYPES.get('wireless'): interface_b.type = INTERFACE_TYPES.get('wireless') interface_b.save() except Interface.DoesNotExist: self.message('Interface #%s does not exist, probably link #%s is orphan!' % (old_link.to_interface_id, old_link.id)) skip = True if skip: self.verbose('Skipping to next cycle') continue old_bandwidth = [old_link.sync_tx, old_link.sync_rx] link = Link(**{ "id": old_link.id, "interface_a": interface_a, "interface_b": interface_b, "status": LINK_STATUS.get('active'), "type": LINK_TYPES.get('radio'), "metric_type": 'etx', "metric_value": old_link.etx, "dbm": old_link.dbm, "min_rate": min(old_bandwidth), "max_rate": max(old_bandwidth), "data": { "imported": "true" } }) # if link already exists flag it for UPDATE instead of INSERT try: Link.objects.get(pk=old_link.id) link._state.adding = False except Link.DoesNotExist: pass if old_link.hide: link.access_level = 3 try: link.full_clean() link.save() saved_links.append(link) self.verbose('Saved link %s' % link) except Exception: tb = traceback.format_exc() self.message('Could not save link %s, got exception:\n\n%s' % (old_link.id, tb)) self.message('saved %d links into local DB' % len(saved_links)) self.saved_links = saved_links
def save_links(self): added_links = [] deleted_links = [] cnml_links = self.cnml.getLinks() current_links = Link.objects.filter(data__contains=['cnml_id'], layer=self.layer) # keep a list of link cnml_id # TODO: hstore seems to not work properly, something is wrong cnml_id_list = [] cnml_id_mapping = {} for current_link in current_links: cnml_id_list.append(current_link.data['cnml_id']) cnml_id_mapping[current_link.data['cnml_id']] = current_link.id total_n = len(cnml_links) for n, cnml_link in enumerate(cnml_links, 1): if str(cnml_link.id) in cnml_id_list: try: link_id = cnml_id_mapping[str(cnml_link.id)] # probably a link between a node of another zone except KeyError: continue link = Link.objects.get(id=link_id) added = False else: link = Link() added = True # link between a node which is not of this CNML zone if isinstance(cnml_link.nodeA, int) or isinstance( cnml_link.nodeB, int): continue node_a = Node.objects.get(data={'cnml_id': cnml_link.nodeA.id}) node_b = Node.objects.get(data={'cnml_id': cnml_link.nodeB.id}) link.node_a = node_a link.node_b = node_b link.type = LINK_TYPES.get(self.LINK_TYPE_MAPPING[cnml_link.type]) link.status = LINK_STATUS.get( self.LINK_STATUS_MAPPING[cnml_link.status]) self.verbose('[%d/%d] parsing link "%s" (%s<-->%s)' % (n, total_n, cnml_link.id, node_a, node_b)) # set interface_a and interface_b only if not planned # because there might be inconsistencies in the CNML from guifi.net if link.get_status_display() != 'planned': if cnml_link.interfaceA: interface_a = Interface.objects.get( data={'cnml_id': cnml_link.interfaceA.id}) link.interface_a = interface_a if cnml_link.interfaceB: interface_b = Interface.objects.get( data={'cnml_id': cnml_link.interfaceB.id}) link.interface_b = interface_b else: link.interface_a = None link.interface_b = None link.data['cnml_id'] = cnml_link.id try: link.full_clean() except Exception as e: print(e) continue link.save() if added: added_links.append(link) # delete links that are not in CNML anymore total_n = len(current_links) for n, current_link in enumerate(current_links, 1): self.verbose('[%d/%d] deleting old links' % (n, total_n)) try: self.cnml.getLink(int(current_link.data['cnml_id'])) except KeyError: deleted_links.append(current_link) current_link.delete() self.message += """ %s links added %s links deleted """ % (len(added_links), len(deleted_links))