def setUp(self): port1 = Port('Port', '1', 'PortId1', 'NodeId', 'LinkId1', 'LinkId2', {}) port2 = Port('Port', '1', 'PortId2', 'NodeId', 'LinkId2', 'LinkId1', {}) self.target = PortChanged('NodeId', 'ID', PortChanged.Action.UPDATE, '1', port1, port2)
def _delete_port_conversion(self, network_id, port): logging.debug(">> %s", stack()[0][3]) resp_list = {} if network_id is None or port is None: return resp_list dst_ports = self._conversion_table.get_port(network_id, port.node_id, port.port_id) for dst_port in dst_ports: port_id = dst_port.split("::") if port_id[0] not in self._network_interfaces: continue network_if = self._network_interfaces[port_id[0]] resp = network_if.del_port(port_id[1], port_id[2]) resp_port = None if resp.body is not None: try: resp_port = Port.create_from_packed(resp.body) except KeyError, err: logging.error("DELETE Port Invalid Response Message" + " KeyError: " + str(err)) return None resp_list[dst_port] = resp_port
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 = Port.create_from_packed(packed[cls.CURR]) elif action == cls.Action.DELETE: prev = Port.create_from_packed(packed[cls.PREV]) elif action == cls.Action.UPDATE: version = packed[cls.VERSION] prev = Port.create_from_packed(packed[cls.PREV]) curr = Port.create_from_packed(packed[cls.CURR]) return cls(packed[cls.NODE_ID], packed[cls.ID], action, version, prev, curr)
def test_create_from_packed(self): packed = self.target.packed_object() result = Port.create_from_packed(packed) self.assertEqual(result.type, 'Port') self.assertEqual(result.version, '1') self.assertEqual(result.port_id, 'PortId') self.assertEqual(result.node_id, 'NodeId') self.assertEqual(result.out_link, 'OutLink') self.assertEqual(result.in_link, 'InLink') self.assertEqual(len(result.attributes), 1) self.assertEqual(result.attributes['Key'], 'Val')
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 create_from_packed(cls, packed): version = None if cls.VERSION in packed: version = packed[cls.VERSION] ports = {} for port_id, port in packed[cls.PORTS].items(): ports[port_id] = Port.create_from_packed(port) return cls(packed[cls.TYPE], version, packed[cls.NODE_ID], ports, packed[cls.ATTRIBUTES])
def _update_port_conversion(self, network_id, port_prev, port_curr, attributes): logging.debug(">> %s", stack()[0][3]) resp_list = {} if network_id is None or port_curr is None: return resp_list attributes_list = [] if attributes is None: attributes_list = [] else: attributes_list = attributes # get ignore list port_ignore_attributes = self.__get_ignore_keys(self.attributes_port, attributes_list) for dst_port in self._conversion_table.get_port(network_id, port_curr.node_id, port_curr.port_id): port_id = dst_port.split("::") if port_id[0] not in self._network_interfaces: continue network_if = self._network_interfaces[port_id[0]] # get port port = network_if.get_port(port_id[1], port_id[2]) if port is None: continue # attr copy (curr -> body) updated = False curr_attr = port_curr.attributes for attr_key in curr_attr: if (attr_key in port_ignore_attributes or (attr_key in port.attributes and port.attributes[attr_key] == curr_attr[attr_key])): continue updated = True port.attributes[attr_key] = \ port_curr.attributes[attr_key] # put node if updated: resp = network_if.put_port(port) try: resp_port = Port.create_from_packed(resp.body) resp_list[dst_port] = resp_port except KeyError, err: logging.error("PUT Port Invalid Response Message" + " KeyError: " + str(err))
def test_create_from_packed_without_version(self): packed = {'type': 'Port', 'port_id': 'PortId', 'node_id': 'NodeId', 'out_link': 'OutLink', 'in_link': 'InLink', 'attributes': {'Key': 'Val'}} result = Port.create_from_packed(packed) self.assertEqual(result.type, 'Port') self.assertEqual(result.version, None) self.assertEqual(result.port_id, 'PortId') self.assertEqual(result.node_id, 'NodeId') self.assertEqual(result.out_link, 'OutLink') self.assertEqual(result.in_link, 'InLink') self.assertEqual(len(result.attributes), 1) self.assertEqual(result.attributes['Key'], 'Val')
def get_physical_port(self, physical_id): logging.debug("GET PhysicalPort NetworkID:" + self.network_id + " PhysicalID:" + physical_id) path = self.PHYSICAL_PORTS_PATH % physical_id resp = self._get_object_to_remote_object(path) if resp.is_error(Request.Method.GET): return None port = None try: port = Port.create_from_packed(resp.body) except KeyError, err: logging.error("GET PhysicalPort Invalid Response Message" + " KeyError: " + str(err)) return None
def get_port(self, node_id, port_id): logging.debug("GET Port NetworkID:" + self.network_id + " NodeID:" + node_id + " PortID:" + port_id) path = self.PORT_PATH % (node_id, port_id) resp = self._get_object_to_remote_object(path) if resp.is_error(Request.Method.GET): return None port = None try: port = Port.create_from_packed(resp.body) except KeyError, err: logging.error("GET Port Invalid Response Message" + " KeyError: " + str(err)) return None
def get_ports(self, node_id): logging.debug("GET Ports NetworkID:" + self.network_id + " NodeID:" + node_id) path = self.PORTS_PATH % node_id resp = self._get_object_to_remote_object(path) if resp.is_error(Request.Method.GET): return None ports = {} try: for port_id, port in resp.body.items(): ports[port_id] = Port.create_from_packed(port) except KeyError, err: logging.error("GET Ports Invalid Response Message" + " KeyError: " + str(err)) return None
def test_create_from_packed_without_version(self): packed = { 'type': 'Port', 'port_id': 'PortId', 'node_id': 'NodeId', 'out_link': 'OutLink', 'in_link': 'InLink', 'attributes': { 'Key': 'Val' } } result = Port.create_from_packed(packed) self.assertEqual(result.type, 'Port') self.assertEqual(result.version, None) self.assertEqual(result.port_id, 'PortId') self.assertEqual(result.node_id, 'NodeId') self.assertEqual(result.out_link, 'OutLink') self.assertEqual(result.in_link, 'InLink') self.assertEqual(len(result.attributes), 1) self.assertEqual(result.attributes['Key'], 'Val')
def _add_port_conversion(self, network_id, port): 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_port(port) resp_list[nw_id] = resp try: resp_port = Port.create_from_packed(resp.body) self._conversion_table.add_entry_port(network_id, port.node_id, port.port_id, nw_id, resp_port.node_id, resp_port.port_id) except KeyError, err: logging.error("PUT Port Invalid Response Message" + " KeyError: " + str(err))
def setUp(self): port = Port('Port', '1', 'PortId', 'NodeId', 'OutLink', 'InLink', {'PortKey': 'PortVal'}) self.target = Node('Node', '1', 'NodeId', {'PortId': port}, {'NodeKey': 'NodeVal'})
class PortTest(unittest.TestCase): def setUp(self): self.target = Port('Port', '1', 'PortId', 'NodeId', 'OutLink', 'InLink', {'Key': 'Val'}) def tearDown(self): pass def test_constructor(self): self.assertEqual(self.target._body[Port.TYPE], 'Port') self.assertEqual(self.target._body[Port.VERSION], '1') self.assertEqual(self.target._body[Port.PORT_ID], 'PortId') self.assertEqual(self.target._body[Port.NODE_ID], 'NodeId') self.assertEqual(self.target._body[Port.OUT_LINK], 'OutLink') self.assertEqual(self.target._body[Port.IN_LINK], 'InLink') self.assertEqual(self.target._body[Port.ATTRIBUTES]['Key'], 'Val') def test_type(self): self.assertEqual(self.target.type, 'Port') def test_version(self): self.assertEqual(self.target.version, '1') def test_port_id(self): self.assertEqual(self.target.port_id, 'PortId') def test_node_id(self): self.assertEqual(self.target.node_id, 'NodeId') def test_out_link(self): self.assertEqual(self.target.out_link, 'OutLink') def test_in_link(self): self.assertEqual(self.target.in_link, 'InLink') def test_attributes(self): result = self.target.attributes self.assertEqual(len(result), 1) self.assertEqual(result['Key'], 'Val') def test_create_from_packed(self): packed = self.target.packed_object() result = Port.create_from_packed(packed) self.assertEqual(result.type, 'Port') self.assertEqual(result.version, '1') self.assertEqual(result.port_id, 'PortId') self.assertEqual(result.node_id, 'NodeId') self.assertEqual(result.out_link, 'OutLink') self.assertEqual(result.in_link, 'InLink') self.assertEqual(len(result.attributes), 1) self.assertEqual(result.attributes['Key'], 'Val') def test_create_from_packed_without_version(self): packed = { 'type': 'Port', 'port_id': 'PortId', 'node_id': 'NodeId', 'out_link': 'OutLink', 'in_link': 'InLink', 'attributes': { 'Key': 'Val' } } result = Port.create_from_packed(packed) self.assertEqual(result.type, 'Port') self.assertEqual(result.version, None) self.assertEqual(result.port_id, 'PortId') self.assertEqual(result.node_id, 'NodeId') self.assertEqual(result.out_link, 'OutLink') self.assertEqual(result.in_link, 'InLink') self.assertEqual(len(result.attributes), 1) self.assertEqual(result.attributes['Key'], 'Val') def test_packed_object(self): result = self.target.packed_object() self.assertEqual( result, { 'type': 'Port', 'version': '1', 'port_id': 'PortId', 'node_id': 'NodeId', 'out_link': 'OutLink', 'in_link': 'InLink', 'attributes': { 'Key': 'Val' } })
def setUp(self): self.target = Port('Port', '1', 'PortId', 'NodeId', 'OutLink', 'InLink', {'Key': 'Val'})
class PortTest(unittest.TestCase): def setUp(self): self.target = Port('Port', '1', 'PortId', 'NodeId', 'OutLink', 'InLink', {'Key': 'Val'}) def tearDown(self): pass def test_constructor(self): self.assertEqual(self.target._body[Port.TYPE], 'Port') self.assertEqual(self.target._body[Port.VERSION], '1') self.assertEqual(self.target._body[Port.PORT_ID], 'PortId') self.assertEqual(self.target._body[Port.NODE_ID], 'NodeId') self.assertEqual(self.target._body[Port.OUT_LINK], 'OutLink') self.assertEqual(self.target._body[Port.IN_LINK], 'InLink') self.assertEqual(self.target._body[Port.ATTRIBUTES]['Key'], 'Val') def test_type(self): self.assertEqual(self.target.type, 'Port') def test_version(self): self.assertEqual(self.target.version, '1') def test_port_id(self): self.assertEqual(self.target.port_id, 'PortId') def test_node_id(self): self.assertEqual(self.target.node_id, 'NodeId') def test_out_link(self): self.assertEqual(self.target.out_link, 'OutLink') def test_in_link(self): self.assertEqual(self.target.in_link, 'InLink') def test_attributes(self): result = self.target.attributes self.assertEqual(len(result), 1) self.assertEqual(result['Key'], 'Val') def test_create_from_packed(self): packed = self.target.packed_object() result = Port.create_from_packed(packed) self.assertEqual(result.type, 'Port') self.assertEqual(result.version, '1') self.assertEqual(result.port_id, 'PortId') self.assertEqual(result.node_id, 'NodeId') self.assertEqual(result.out_link, 'OutLink') self.assertEqual(result.in_link, 'InLink') self.assertEqual(len(result.attributes), 1) self.assertEqual(result.attributes['Key'], 'Val') def test_create_from_packed_without_version(self): packed = {'type': 'Port', 'port_id': 'PortId', 'node_id': 'NodeId', 'out_link': 'OutLink', 'in_link': 'InLink', 'attributes': {'Key': 'Val'}} result = Port.create_from_packed(packed) self.assertEqual(result.type, 'Port') self.assertEqual(result.version, None) self.assertEqual(result.port_id, 'PortId') self.assertEqual(result.node_id, 'NodeId') self.assertEqual(result.out_link, 'OutLink') self.assertEqual(result.in_link, 'InLink') self.assertEqual(len(result.attributes), 1) self.assertEqual(result.attributes['Key'], 'Val') def test_packed_object(self): result = self.target.packed_object() self.assertEqual(result, {'type': 'Port', 'version': '1', 'port_id': 'PortId', 'node_id': 'NodeId', 'out_link': 'OutLink', 'in_link': 'InLink', 'attributes': {'Key': 'Val'}})