def setUp(self): link1 = Link('Link', '1', 'LinkId1', 'NodeId1', 'PortId1', 'NodeId2', 'PortId3', {}) link2 = Link('Link', '1', 'LinkId2', 'NodeId2', 'PortId4', 'NodeId1', 'PortId2', {}) node1 = Node('Node', '1', 'NodeId1', {}, {}) node2 = Node('Node', '1', 'NodeId2', {}, {}) topology1 = Topology('Topology', 'version', {'NodeId1': node1}, {'LinkId1': link1}) topology2 = Topology('Topology', 'version', {'NodeId2': node2}, {'LinkId2': link2}) self.target = TopologyChanged('1', topology1, topology2)
def _delete_node_conversion(self, network_id, node): logging.debug(">> %s", stack()[0][3]) resp_list = {} if network_id is None or node is None: return resp_list dst_nodes = self._conversion_table.get_node(network_id, node.node_id) for dst_node in dst_nodes: node_id = dst_node.split("::") if node_id[0] not in self._network_interfaces: continue network_if = self._network_interfaces[node_id[0]] resp = network_if.del_node(node_id[1]) resp_node = None if resp.body is not None: try: resp_node = Node.create_from_packed(resp.body) except KeyError, err: logging.error("DELETE Node Invalid Response Message" + " KeyError: " + str(err)) return None resp_list[dst_node] = resp_node
def create_from_packed(cls, packed): action = packed[cls.ACTION] version = "" prev = None curr = None if action == cls.Action.ADD: version = packed[cls.VERSION] curr = Node.create_from_packed(packed[cls.CURR]) elif action == cls.Action.DELETE: prev = Node.create_from_packed(packed[cls.PREV]) elif action == cls.Action.UPDATE: version = packed[cls.VERSION] prev = Node.create_from_packed(packed[cls.PREV]) curr = Node.create_from_packed(packed[cls.CURR]) return cls(packed[cls.ID], action, version, prev, curr)
def test_create_from_packed(self): packed = self.target.packed_object() result = Node.create_from_packed(packed) self.assertEqual(result.type, 'Node') self.assertEqual(result.version, '1') self.assertEqual(result.node_id, 'NodeId') self.assertEqual(len(result.ports), 1) self.assertEqual(result.ports['PortId'].port_id, 'PortId') self.assertEqual(result.attributes['NodeKey'], 'NodeVal')
def setUp(self): port1 = Port('Port', '1', 'PortId1', 'NodeId1', 'LinkId1', None, {}) port2 = Port('Port', '1', 'PortId2', 'NodeId1', None, 'LinkId2', {}) port3 = Port('Port', '1', 'PortId3', 'NodeId2', None, 'LinkId1', {}) port4 = Port('Port', '1', 'PortId4', 'NodeId2', 'LinkId2', None, {}) node1 = Node('Node', '1', 'NodeId1', { 'PortId1': port1, 'PortId2': port2 }, {}) node2 = Node('Node', '1', 'NodeId2', { 'PortId3': port3, 'PortId4': port4 }, {}) link1 = Link('Link', '1', 'LinkId1', 'NodeId1', 'PortId1', 'NodeId2', 'PortId3', {}) link2 = Link('Link', '1', 'LinkId2', 'NodeId2', 'PortId4', 'NodeId1', 'PortId2', {}) self.nodes = {'NodeId1': node1, 'NodeId2': node2} self.links = {'LinkId1': link1, 'LinkId2': link2} self.target = Topology('Topology', '1', self.nodes, self.links)
def _update_node_conversion(self, network_id, node_prev, node_curr, attributes): logging.debug(">> %s", stack()[0][3]) resp_list = {} if network_id is None or node_curr is None: return resp_list attributes_list = [] if attributes is None: attributes_list = [] else: attributes_list = attributes # get ignore list node_ignore_attributes = self.__get_ignore_keys(self.attributes_node, attributes_list) for dst_node in self._conversion_table.get_node(network_id, node_curr.node_id): node_id = dst_node.split("::") if node_id[0] not in self._network_interfaces: continue network_if = self._network_interfaces[node_id[0]] # get node node = network_if.get_node(node_id[1]) if node is None: continue # attr copy (curr -> body) updated = False curr_attr = node_curr.attributes for attr_key in curr_attr: if (attr_key in node_ignore_attributes or (attr_key in node.attributes and node.attributes[attr_key] == curr_attr[attr_key])): continue updated = True node.attributes[attr_key] = \ node_curr.attributes[attr_key] # put node if updated: resp = network_if.put_node(node) try: resp_node = Node.create_from_packed(resp.body) resp_list[dst_node] = resp_node except KeyError, err: logging.error("PUT Node Invalid Response Message" + " KeyError: " + str(err))
def get_nodes(self): logging.debug("GET Nodes NetworkID:" + self.network_id) resp = self._get_object_to_remote_object(self.NODES_PATH) if resp.is_error(Request.Method.GET): return None nodes = {} try: for node_id, node in resp.body.items(): nodes[node_id] = Node.create_from_packed(node) except KeyError, err: logging.error("GET Nodes Invalid Response Message" + " KeyError: " + str(err)) return None
def create_from_packed(cls, packed): version = None if cls.VERSION in packed: version = packed[cls.VERSION] nodes = {} for node_id, node in packed[cls.NODES].items(): nodes[node_id] = Node.create_from_packed(node) links = {} for link_id, link in packed[cls.LINKS].items(): links[link_id] = Link.create_from_packed(link) return cls(packed[cls.TYPE], version, nodes, links)
def get_physical_node(self, physical_id): logging.debug("GET PhysicalNode NetworkID:" + self.network_id + " PhysicalID:" + physical_id) path = self.PHYSICAL_NODES_PATH % physical_id resp = self._get_object_to_remote_object(path) if resp.is_error(Request.Method.GET): return None node = None try: node = Node.create_from_packed(resp.body) except KeyError, err: logging.error("GET PhysicalNode Invalid Response Message" + " KeyError: " + str(err)) return None
def test_create_from_packed_without_version(self): packed = {'type': 'Node', 'node_id': 'NodeId', 'attributes': {'NodeKey': 'NodeVal'}, 'ports': {'PortId': {'type': 'Port', 'version': '1', 'port_id': 'PortId', 'node_id': 'NodeId', 'out_link': 'OutLink', 'in_link': 'InLink', 'attributes': {'PortKey': 'PortVal'}}}} result = Node.create_from_packed(packed) self.assertEqual(result.type, 'Node') self.assertEqual(result.version, None) self.assertEqual(result.node_id, 'NodeId') self.assertEqual(len(result.ports), 1) self.assertEqual(result.ports['PortId'].port_id, 'PortId') self.assertEqual(result.attributes['NodeKey'], 'NodeVal')
def _add_node_conversion(self, network_id, node): logging.debug(">> %s", stack()[0][3]) resp_list = {} for nw_id in self._conversion_table.get_network(network_id): if nw_id not in self._network_interfaces: continue network_if = self._network_interfaces[nw_id] resp = network_if.put_node(node) resp_list[nw_id] = resp try: resp_node = Node.create_from_packed(resp.body) self._conversion_table.add_entry_node(network_id, node.node_id, nw_id, resp_node.node_id) except KeyError, err: logging.error("PUT Node Invalid Response Message" + " KeyError: " + str(err))
def setUp(self): node1 = Node('Node', '1', 'NodeId1', {}, {}) node2 = Node('Node', '1', 'NodeId2', {}, {}) self.target = NodeChanged('ID', NodeChanged.Action.ADD, '1', node1, node2)
def setUp(self): port = Port('Port', '1', 'PortId', 'NodeId', 'OutLink', 'InLink', {'PortKey': 'PortVal'}) self.target = Node('Node', '1', 'NodeId', {'PortId': port}, {'NodeKey': 'NodeVal'})
class NodeTest(unittest.TestCase): def setUp(self): port = Port('Port', '1', 'PortId', 'NodeId', 'OutLink', 'InLink', {'PortKey': 'PortVal'}) self.target = Node('Node', '1', 'NodeId', {'PortId': port}, {'NodeKey': 'NodeVal'}) def tearDown(self): pass def test_constructor(self): self.assertEqual(self.target._body[Node.TYPE], 'Node') self.assertEqual(self.target._body[Node.VERSION], '1') self.assertEqual(self.target._body[Node.NODE_ID], 'NodeId') ports = self.target._body[Node.PORTS] self.assertEqual(len(ports), 1) self.assertEqual(ports['PortId'].port_id, 'PortId') self.assertEqual( self.target._body[Node.ATTRIBUTES]['NodeKey'], 'NodeVal') def test_type(self): self.assertEqual(self.target.type, 'Node') def test_version(self): self.assertEqual(self.target.version, '1') def test_node_id(self): self.assertEqual(self.target.node_id, 'NodeId') def test_ports(self): result = self.target.ports self.assertEqual(len(result), 1) self.assertEqual(result['PortId'].port_id, 'PortId') def test_attributes(self): result = self.target.attributes self.assertEqual(len(result), 1) self.assertEqual(result['NodeKey'], 'NodeVal') def test_create_from_packed(self): packed = self.target.packed_object() result = Node.create_from_packed(packed) self.assertEqual(result.type, 'Node') self.assertEqual(result.version, '1') self.assertEqual(result.node_id, 'NodeId') self.assertEqual(len(result.ports), 1) self.assertEqual(result.ports['PortId'].port_id, 'PortId') self.assertEqual(result.attributes['NodeKey'], 'NodeVal') def test_create_from_packed_without_version(self): packed = {'type': 'Node', 'node_id': 'NodeId', 'attributes': {'NodeKey': 'NodeVal'}, 'ports': {'PortId': {'type': 'Port', 'version': '1', 'port_id': 'PortId', 'node_id': 'NodeId', 'out_link': 'OutLink', 'in_link': 'InLink', 'attributes': {'PortKey': 'PortVal'}}}} result = Node.create_from_packed(packed) self.assertEqual(result.type, 'Node') self.assertEqual(result.version, None) self.assertEqual(result.node_id, 'NodeId') self.assertEqual(len(result.ports), 1) self.assertEqual(result.ports['PortId'].port_id, 'PortId') self.assertEqual(result.attributes['NodeKey'], 'NodeVal') def test_packed_object(self): result = self.target.packed_object() self.assertEqual(result, {'type': 'Node', 'version': '1', 'node_id': 'NodeId', 'attributes': {'NodeKey': 'NodeVal'}, 'ports': {'PortId': { 'type': 'Port', 'version': '1', 'port_id': 'PortId', 'node_id': 'NodeId', 'out_link': 'OutLink', 'in_link': 'InLink', 'attributes': { 'PortKey': 'PortVal'} }}})