def _init_interfaces(self, nbunch=None): """Initialises interfaces""" if not nbunch: nbunch = [n for n in self._graph.nodes()] try: nbunch = list(unwrap_nodes(nbunch)) except AttributeError: pass # don't need to unwrap phy_graph = self._anm.overlay_nx_graphs["phy"] for node in nbunch: try: phy_interfaces = phy_graph.node[node]["_interfaces"] interface_data = { 'description': None, 'type': 'physical', } # need to do dict() to copy, otherwise all point to same memory location -> clobber data = dict( (key, dict(interface_data)) for key in phy_interfaces) self._graph.node[node]['_interfaces'] = data except KeyError: # no counterpart in physical graph, initialise log.debug("Initialise interfaces for %s in %s" % ( node, self._overlay_id)) self._graph.node[node]['_interfaces'] = {0: {'description': 'loopback', 'type': 'loopback'}}
def remove_nodes_from(self, nbunch): """Removes set of nodes from nbunch""" try: nbunch = unwrap_nodes(nbunch) except AttributeError: pass # don't need to unwrap self._graph.remove_nodes_from(nbunch)
def _init_interfaces(self, nbunch=None): """Initialises interfaces""" # TODO: this needs a major refactor! # store the original bunch to check if going input->phy if nbunch is not None: nbunch = list(nbunch) # listify generators original_nbunch = {} if nbunch is None: nbunch = [n for n in self._graph.nodes()] try: previous = list(nbunch) nbunch = list(unwrap_nodes(nbunch)) except AttributeError: pass # don't need to unwrap else: # record a dict of the new nbunch to the original for index, element in enumerate(nbunch): previous_element = previous[index] if previous_element is not None: original_nbunch[element] = previous[index] phy_graph = self._anm.overlay_nx_graphs['phy'] initialised_nodes = [] for node in nbunch: try: phy_interfaces = phy_graph.node[node]['_interfaces'] interface_data = {'description': None, 'type': 'physical'} # need to do dict() to copy, otherwise all point to same memory # location -> clobber data = dict((key, dict(interface_data)) for key in phy_interfaces) self._graph.node[node]['_interfaces'] = data except KeyError: # TODO: split this off into seperate function # test if adding from input graph # Note: this has to be done on a node-by-node basis # as ANK allows adding nodes from multiple graphs at once # TODO: warn if adding from multiple overlays at onc if self._overlay_id == "phy" and len(original_nbunch): # see if adding from input->phy, # overlay nodes were provided as input original_node = original_nbunch[node] if original_node.overlay_id == "input": # are doing input->phy # copy the original_interfaces = original_node.get( "_interfaces") if original_interfaces is not None: # Initialise with the keys int_data = {k: {"description": v.get("description"), "type": v.get("type")} for k, v in original_interfaces.items()} self._graph.node[node][ '_interfaces'] = int_data else: # no counterpart in physical graph, initialise # Can't do node log becaue node doesn't exist yet self._graph.node[node]['_interfaces'] = \ {0: {'description': 'loopback', 'type': 'loopback'}} initialised_nodes.append(node) if len(initialised_nodes): initialised_nodes = [NmNode(self.anm, self._overlay_id, n) for n in initialised_nodes] initialised_nodes = sorted([str(n) for n in initialised_nodes]) self.log.debug("Initialised interfaces for %s" % ", ".join(initialised_nodes))