X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Ftopologymanager%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Ftopologymanager%2Finternal%2FTopologyManagerImplTest.java;h=850563fe95b07774b13b2e645bb7de52bec8daee;hp=f2cf2367ea6792628de73bd7e40b646b97e6bb61;hb=eb9e1983a0fce7e4a381eff33e40cae957cddf53;hpb=29f7cfb54b580928c7feac63abce028a7014b0d5 diff --git a/opendaylight/topologymanager/src/test/java/org/opendaylight/controller/topologymanager/internal/TopologyManagerImplTest.java b/opendaylight/topologymanager/src/test/java/org/opendaylight/controller/topologymanager/internal/TopologyManagerImplTest.java index f2cf2367ea..850563fe95 100644 --- a/opendaylight/topologymanager/src/test/java/org/opendaylight/controller/topologymanager/internal/TopologyManagerImplTest.java +++ b/opendaylight/topologymanager/src/test/java/org/opendaylight/controller/topologymanager/internal/TopologyManagerImplTest.java @@ -1,4 +1,3 @@ - /* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * @@ -11,8 +10,10 @@ package org.opendaylight.controller.topologymanager.internal; import java.net.InetAddress; import java.net.UnknownHostException; +import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentMap; @@ -31,337 +32,408 @@ import org.opendaylight.controller.sal.core.Property; import org.opendaylight.controller.sal.core.State; import org.opendaylight.controller.sal.core.UpdateType; import org.opendaylight.controller.sal.packet.address.EthernetAddress; +import org.opendaylight.controller.sal.topology.TopoEdgeUpdate; import org.opendaylight.controller.sal.utils.StatusCode; import org.opendaylight.controller.sal.utils.NodeConnectorCreator; import org.opendaylight.controller.sal.utils.NodeCreator; import org.opendaylight.controller.topologymanager.TopologyUserLinkConfig; -public class TopologyManagerImplTest { - - /* - * Sets the node, edges and properties for edges here: - * Edge : - * <1:1>--><11:11>; <1:2>--><11:12>; - * <3:3>--><13:13>; <3:4>--><13:14>; - * <5:5>--><15:15>; <5:6>--><15:16>; - * Method used by two tests: testGetNodeEdges and testGetEdges - * @param topoManagerImpl - * @throws ConstructionException - */ - public void setNodeEdges(TopologyManagerImpl topoManagerImpl) throws ConstructionException { - topoManagerImpl.nonClusterObjectCreate(); - - State state; - Bandwidth bw; - Latency l; - - Set props = new HashSet(); - state = new State(State.EDGE_UP); - bw = new Bandwidth(Bandwidth.BW100Gbps); - l = new Latency(Latency.LATENCY100ns); - props.add(state); - props.add(bw); - props.add(l); - - for (short i = 1; i < 6; i=(short) (i+2)) { - NodeConnector headnc1 = NodeConnectorCreator.createOFNodeConnector(i, NodeCreator.createOFNode((long)i)); - NodeConnector tailnc1 = NodeConnectorCreator.createOFNodeConnector((short)(i+10), NodeCreator.createOFNode((long)(i+10))); - Edge e1 = new Edge(headnc1, tailnc1); - topoManagerImpl.edgeUpdate(e1, UpdateType.ADDED, props); - - NodeConnector headnc2 = NodeConnectorCreator.createOFNodeConnector((short) (i+1), headnc1.getNode()); - NodeConnector tailnc2 = NodeConnectorCreator.createOFNodeConnector((short)(i+11), tailnc1.getNode()); - Edge e2 = new Edge(headnc2, tailnc2); - topoManagerImpl.edgeUpdate(e2, UpdateType.ADDED, props); - } - } - - @Test - public void testGetNodeEdges() throws ConstructionException { - TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl(); - setNodeEdges(topoManagerImpl); - - Map> nodeEdgeMap = topoManagerImpl.getNodeEdges(); - for (Iterator>> i = nodeEdgeMap.entrySet().iterator(); i.hasNext();) { - Map.Entry> entry = i.next(); - Node node = entry.getKey(); - Long nodeId = ((Long) node.getID()).longValue(); - Assert.assertTrue((node.getType().equals(NodeIDType.OPENFLOW))); - - Set edges = entry.getValue(); - for (Edge edge : edges) { - Long headNcId = ((Short)edge.getHeadNodeConnector().getID()).longValue(); - Long tailNcId = ((Short) edge.getTailNodeConnector().getID()).longValue(); - if (nodeId == 1 || nodeId == 3 || nodeId == 5) { - Assert.assertTrue((headNcId.equals(nodeId) && tailNcId.equals(nodeId + 10)) || - (headNcId.equals(nodeId + 10) && tailNcId.equals(nodeId)) || - (headNcId.equals(nodeId + 1) && tailNcId.equals(nodeId + 11)) || - (headNcId.equals(nodeId + 11) && tailNcId.equals(nodeId + 1))); - } else if (nodeId == 11 || nodeId == 13 || nodeId == 15) { - Assert.assertTrue((headNcId.equals(nodeId) && tailNcId.equals(nodeId - 10)) || - (headNcId.equals(nodeId) && tailNcId.equals(nodeId - 10)) || - (headNcId.equals(nodeId - 9) && tailNcId.equals(nodeId + 1)) || - (headNcId.equals(nodeId + 1) && tailNcId.equals(nodeId - 9))); - } - } - i.remove(); - } - Assert.assertTrue(nodeEdgeMap.isEmpty()); - } - - @Test - public void testGetEdges() throws ConstructionException { - TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl(); - setNodeEdges(topoManagerImpl); - - Map> edgeProperty = topoManagerImpl.getEdges(); - - for (Iterator >> i = edgeProperty.entrySet().iterator() ; i.hasNext();) { - Map.Entry> entry = i.next(); - Edge e = entry.getKey(); - NodeConnector headnc = e.getHeadNodeConnector(); - NodeConnector tailnc = e.getTailNodeConnector(); - - Long headNodeId = (Long) headnc.getNode().getID(); - - Long headNcId = ((Short)headnc.getID()).longValue(); - Long tailNcId = ((Short)tailnc.getID()).longValue(); - - if (headNodeId == 1 || headNodeId == 3 || headNodeId == 5) { - Assert.assertTrue((headNcId.equals(headNodeId) && tailNcId.equals(headNodeId + 10)) || - (headNcId.equals(headNodeId + 10) && tailNcId.equals(headNodeId)) || - (headNcId.equals(headNodeId + 1) && tailNcId.equals(headNodeId + 11)) || - (headNcId.equals(headNodeId + 11) && tailNcId.equals(headNodeId + 1))); - } else if (headNodeId == 11 || headNodeId == 13 || headNodeId == 15) { - Assert.assertTrue((headNcId.equals(headNodeId) && tailNcId.equals(headNodeId - 10)) || - (headNcId.equals(headNodeId) && tailNcId.equals(headNodeId - 10)) || - (headNcId.equals(headNodeId - 9) && tailNcId.equals(headNodeId + 1)) || - (headNcId.equals(headNodeId + 1) && tailNcId.equals(headNodeId - 9))); - } - - Set prop = entry.getValue(); - for (Property p : prop) { - String pName; - long pValue; - if (p instanceof Bandwidth) { - Bandwidth b = (Bandwidth)p; - pName = Bandwidth.BandwidthPropName; - pValue = b.getValue(); - Assert.assertTrue(pName.equals(p.getName()) && pValue == Bandwidth.BW100Gbps ); - continue; - } - if (p instanceof Latency) { - Latency l = (Latency)p; - pName = Latency.LatencyPropName; - pValue = l.getValue(); - Assert.assertTrue(pName.equals(p.getName()) && pValue == Latency.LATENCY100ns); - continue; - } - if (p instanceof State) { - State state = (State)p; - pName = State.StatePropName; - pValue = state.getValue(); - Assert.assertTrue(pName.equals(p.getName()) && pValue == State.EDGE_UP); - continue; - } - } - i.remove(); - } - Assert.assertTrue(edgeProperty.isEmpty()); - } - - - @Test - public void testAddDeleteUserLink () { - TopologyUserLinkConfig link1 = new TopologyUserLinkConfig("default1", "1", "2", "1", "2"); - TopologyUserLinkConfig link2 = new TopologyUserLinkConfig("default1", "10", "20", "10", "20"); - TopologyUserLinkConfig link3 = new TopologyUserLinkConfig("default2", "1", "2", "1", "2"); - TopologyUserLinkConfig link4 = new TopologyUserLinkConfig("default20", "10", "20", "10", "20"); - - TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl(); - topoManagerImpl.nonClusterObjectCreate(); - - Assert.assertTrue (topoManagerImpl.addUserLink(link1).isSuccess()); - Assert.assertTrue (topoManagerImpl.addUserLink(link2).getCode() == StatusCode.CONFLICT); - Assert.assertTrue (topoManagerImpl.addUserLink(link3).getCode() == StatusCode.CONFLICT); - Assert.assertTrue (topoManagerImpl.addUserLink(link4).isSuccess()); - - Assert.assertTrue (topoManagerImpl.deleteUserLink(null).getCode() == StatusCode.BADREQUEST); - Assert.assertTrue (topoManagerImpl.deleteUserLink(link1.getName()).isSuccess()); - Assert.assertTrue (topoManagerImpl.deleteUserLink(link4.getName()).isSuccess()); - Assert.assertTrue (topoManagerImpl.getUserLinks().isEmpty()); - - } - - @Test - public void testGetUserLink () { - TopologyUserLinkConfig[] link = new TopologyUserLinkConfig[5]; - TopologyUserLinkConfig[] reverseLink = new TopologyUserLinkConfig[5]; - TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl(); - topoManagerImpl.nonClusterObjectCreate(); - - String name = null; - String srcSwitchId = null; - String srcPort = null; - String dstSwitchId = null; - String dstPort = null; - - /*Creating userlinks and checking for their validity*/ - link[0] = new TopologyUserLinkConfig(name, srcSwitchId, srcPort, dstSwitchId, dstPort); - Assert.assertTrue(link[0].isValid() == false); - - srcSwitchId = "Z"; - link[0] = new TopologyUserLinkConfig(name, srcSwitchId, srcPort, dstSwitchId, dstPort); - Assert.assertTrue(link[0].isValid() == false); - - dstSwitchId = null; - link[0] = new TopologyUserLinkConfig(name, srcSwitchId, srcPort, dstSwitchId, dstPort); - Assert.assertTrue(link[0].isValid() == false); - - - Integer i; - - for (i = 0; i < 5; i++) { - link[i] = new TopologyUserLinkConfig(name, srcSwitchId, srcPort, dstSwitchId, dstPort); - - name = Integer.toString(i + 1); - srcSwitchId = Integer.toString(i + 1); - srcPort = Integer.toString(i + 1); - dstSwitchId = Integer.toString((i + 1)*10); - dstPort = Integer.toString((i + 1)*10); - - link[i].setName(name); - link[i].setSrcSwitchId(srcSwitchId); - link[i].setSrcPort(srcPort); - link[i].setDstSwitchId(dstSwitchId); - link[i].setDstPort(dstPort); - - Assert.assertTrue(link[i].isValid() == true); - - reverseLink[i] = new TopologyUserLinkConfig(name, dstSwitchId, dstPort, srcSwitchId, srcPort); - - topoManagerImpl.addUserLink(link[i]); - } - ConcurrentMap userLinks = topoManagerImpl.getUserLinks(); - TopologyUserLinkConfig resultLink; - - for (i = 0; i < 5; i++) { - resultLink = userLinks.get(((Integer)(i + 1)).toString()); - - Assert.assertTrue(resultLink.getName().equals(reverseLink[i].getName())); - Assert.assertTrue(resultLink.getDstSwitchId().equals(reverseLink[i].getSrcSwitchId())); - Assert.assertTrue(resultLink.getDstPort().equals(reverseLink[i].getSrcPort())); - Assert.assertTrue(resultLink.getSrcSwitchId().equals(reverseLink[i].getDstSwitchId())); - Assert.assertTrue(resultLink.getSrcPort().equals(reverseLink[i].getDstPort())); - } - } - - @Test - public void testHostLinkMethods() throws ConstructionException, UnknownHostException { - TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl(); - topoManagerImpl.nonClusterObjectCreate(); - int hostCounter = 0; - - State state; - Bandwidth bw; - Latency l; - Set props = new HashSet(); - state = new State(State.EDGE_UP); - bw = new Bandwidth(Bandwidth.BW100Gbps); - l = new Latency(Latency.LATENCY100ns); - props.add(state); - props.add(bw); - props.add(l); - - EthernetAddress ea; - InetAddress ip; - Host[] h = new Host[5]; - NodeConnector[] nc = new NodeConnector[5]; - - /* Adding host, nodeConnector to hostsDB for the i = 0,1,2,3. No host - * added for i = 4 - */ - for (int i = 0; i < 5; i++) { - if (hostCounter < 4) { - ea = new EthernetAddress(new byte[]{(byte)0x0, (byte)0x0, - (byte)0x0, (byte)0x0, - (byte)0x0, (byte)i}); - String stringIP = new StringBuilder().append(i + 1).append(".").append(i+10).append(".").append(i+20).append(".").append(i+30).toString(); - ip = InetAddress.getByName(stringIP); - h[hostCounter] = new Host(ea, ip); - } else { - h[hostCounter] = null; - } - hostCounter++; - nc[i] = NodeConnectorCreator.createOFNodeConnector((short)(i + 1), NodeCreator.createOFNode((long)(i + 1))); - topoManagerImpl.updateHostLink(nc[i], h[i], UpdateType.ADDED, props); - } - - for (int i = 0; i < 5; i++) { - Host host = topoManagerImpl.getHostAttachedToNodeConnector(nc[i]); - if (i == 4) - Assert.assertTrue(host == null); - else - Assert.assertTrue(host.equals(h[i])); - } - - Set ncSet = topoManagerImpl.getNodeConnectorWithHost(); - for (int i = 0; i < 5; i++) { - Assert.assertTrue(ncSet.remove(nc[i])); - } - Assert.assertTrue(ncSet.isEmpty()); - } - - @Test - public void testGetNodesWithNodeConnectorHost() throws ConstructionException, UnknownHostException { - TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl(); - topoManagerImpl.nonClusterObjectCreate(); - int hostCounter = 0; - - State state; - Bandwidth bw; - Latency l; - Set props = new HashSet(); - state = new State(State.EDGE_UP); - bw = new Bandwidth(Bandwidth.BW100Gbps); - l = new Latency(Latency.LATENCY100ns); - props.add(state); - props.add(bw); - props.add(l); - - EthernetAddress ea; - InetAddress ip; - Host[] h = new Host[5]; - NodeConnector[] nc = new NodeConnector[5]; - - /*Adding host, nodeconnector, properties of edge to hostsDB for the first three nodes only*/ - for (int i = 1; i < 6; i++) { - if (i < 4) { - ea = new EthernetAddress(new byte[]{(byte)0x0, (byte)0x0, - (byte)0x0, (byte)0x0, - (byte)0x0, (byte)i}); - String stringIP = new StringBuilder().append(i).append(".").append(i+10).append(".").append(i+20).append(".").append(i+30).toString(); - ip = InetAddress.getByName(stringIP); - h[hostCounter] = new Host(ea, ip); - } - else { - h[hostCounter] = null; - } - hostCounter++; - nc[i - 1] = NodeConnectorCreator.createOFNodeConnector((short)i, NodeCreator.createOFNode((long)i)); - topoManagerImpl.updateHostLink(nc[i - 1], h[i - 1], UpdateType.ADDED, props); - } - - /*Get the nodes which have host connected to its nodeConnector*/ - Map> nodeNCmap = topoManagerImpl.getNodesWithNodeConnectorHost(); - for (int i = 1; i < 6; i++) { - Node node = nc[i - 1].getNode(); - Set ncSet = nodeNCmap.get(nc[i - 1].getNode()); - - Assert.assertTrue(ncSet == nodeNCmap.remove(node)); - } - - Assert.assertTrue(nodeNCmap.isEmpty()); - } -} +public class TopologyManagerImplTest { + + /* + * Sets the node, edges and properties for edges here: Edge : <1:1>--><11:11>; <1:2>--><11:12>; <3:3>--><13:13>; + * <3:4>--><13:14>; <5:5>--><15:15>; <5:6>--><15:16>; Method used by two + * tests: testGetNodeEdges and testGetEdges + * + * @param topoManagerImpl + * + * @throws ConstructionException + */ + public void setNodeEdges(TopologyManagerImpl topoManagerImpl) + throws ConstructionException { + topoManagerImpl.nonClusterObjectCreate(); + + State state; + Bandwidth bw; + Latency l; + + Set props = new HashSet(); + state = new State(State.EDGE_UP); + bw = new Bandwidth(Bandwidth.BW100Gbps); + l = new Latency(Latency.LATENCY100ns); + props.add(state); + props.add(bw); + props.add(l); + + for (short i = 1; i < 6; i = (short) (i + 2)) { + List topoedgeupdateList = new ArrayList(); + NodeConnector headnc1 = NodeConnectorCreator.createOFNodeConnector( + i, NodeCreator.createOFNode((long) i)); + NodeConnector tailnc1 = NodeConnectorCreator + .createOFNodeConnector((short) (i + 10), + NodeCreator.createOFNode((long) (i + 10))); + Edge e1 = new Edge(headnc1, tailnc1); + TopoEdgeUpdate teu1 = new TopoEdgeUpdate(e1, props, + UpdateType.ADDED); + topoedgeupdateList.add(teu1); + + NodeConnector headnc2 = NodeConnectorCreator.createOFNodeConnector( + (short) (i + 1), headnc1.getNode()); + NodeConnector tailnc2 = NodeConnectorCreator.createOFNodeConnector( + (short) (i + 11), tailnc1.getNode()); + Edge e2 = new Edge(headnc2, tailnc2); + TopoEdgeUpdate teu2 = new TopoEdgeUpdate(e2, props, + UpdateType.ADDED); + topoedgeupdateList.add(teu2); + topoManagerImpl.edgeUpdate(topoedgeupdateList); + + } + + } + + @Test + public void testGetNodeEdges() throws ConstructionException { + TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl(); + setNodeEdges(topoManagerImpl); + + Map> nodeEdgeMap = topoManagerImpl.getNodeEdges(); + for (Iterator>> i = nodeEdgeMap.entrySet() + .iterator(); i.hasNext();) { + Map.Entry> entry = i.next(); + Node node = entry.getKey(); + Long nodeId = ((Long) node.getID()).longValue(); + Assert.assertTrue((node.getType().equals(NodeIDType.OPENFLOW))); + + Set edges = entry.getValue(); + for (Edge edge : edges) { + Long headNcId = ((Short) edge.getHeadNodeConnector().getID()) + .longValue(); + Long tailNcId = ((Short) edge.getTailNodeConnector().getID()) + .longValue(); + if (nodeId == 1 || nodeId == 3 || nodeId == 5) { + Assert.assertTrue((headNcId.equals(nodeId) && tailNcId + .equals(nodeId + 10)) + || (headNcId.equals(nodeId + 10) && tailNcId + .equals(nodeId)) + || (headNcId.equals(nodeId + 1) && tailNcId + .equals(nodeId + 11)) + || (headNcId.equals(nodeId + 11) && tailNcId + .equals(nodeId + 1))); + } else if (nodeId == 11 || nodeId == 13 || nodeId == 15) { + Assert.assertTrue((headNcId.equals(nodeId) && tailNcId + .equals(nodeId - 10)) + || (headNcId.equals(nodeId) && tailNcId + .equals(nodeId - 10)) + || (headNcId.equals(nodeId - 9) && tailNcId + .equals(nodeId + 1)) + || (headNcId.equals(nodeId + 1) && tailNcId + .equals(nodeId - 9))); + } + } + i.remove(); + } + Assert.assertTrue(nodeEdgeMap.isEmpty()); + } + + @Test + public void testGetEdges() throws ConstructionException { + TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl(); + setNodeEdges(topoManagerImpl); + + Map> edgeProperty = topoManagerImpl.getEdges(); + + for (Iterator>> i = edgeProperty + .entrySet().iterator(); i.hasNext();) { + Map.Entry> entry = i.next(); + Edge e = entry.getKey(); + NodeConnector headnc = e.getHeadNodeConnector(); + NodeConnector tailnc = e.getTailNodeConnector(); + + Long headNodeId = (Long) headnc.getNode().getID(); + + Long headNcId = ((Short) headnc.getID()).longValue(); + Long tailNcId = ((Short) tailnc.getID()).longValue(); + + if (headNodeId == 1 || headNodeId == 3 || headNodeId == 5) { + Assert.assertTrue((headNcId.equals(headNodeId) && tailNcId + .equals(headNodeId + 10)) + || (headNcId.equals(headNodeId + 10) && tailNcId + .equals(headNodeId)) + || (headNcId.equals(headNodeId + 1) && tailNcId + .equals(headNodeId + 11)) + || (headNcId.equals(headNodeId + 11) && tailNcId + .equals(headNodeId + 1))); + } else if (headNodeId == 11 || headNodeId == 13 || headNodeId == 15) { + Assert.assertTrue((headNcId.equals(headNodeId) && tailNcId + .equals(headNodeId - 10)) + || (headNcId.equals(headNodeId) && tailNcId + .equals(headNodeId - 10)) + || (headNcId.equals(headNodeId - 9) && tailNcId + .equals(headNodeId + 1)) + || (headNcId.equals(headNodeId + 1) && tailNcId + .equals(headNodeId - 9))); + } + + Set prop = entry.getValue(); + for (Property p : prop) { + String pName; + long pValue; + if (p instanceof Bandwidth) { + Bandwidth b = (Bandwidth) p; + pName = Bandwidth.BandwidthPropName; + pValue = b.getValue(); + Assert.assertTrue(pName.equals(p.getName()) + && pValue == Bandwidth.BW100Gbps); + continue; + } + if (p instanceof Latency) { + Latency l = (Latency) p; + pName = Latency.LatencyPropName; + pValue = l.getValue(); + Assert.assertTrue(pName.equals(p.getName()) + && pValue == Latency.LATENCY100ns); + continue; + } + if (p instanceof State) { + State state = (State) p; + pName = State.StatePropName; + pValue = state.getValue(); + Assert.assertTrue(pName.equals(p.getName()) + && pValue == State.EDGE_UP); + continue; + } + } + i.remove(); + } + Assert.assertTrue(edgeProperty.isEmpty()); + } + + @Test + public void testAddDeleteUserLink() { + TopologyUserLinkConfig link1 = new TopologyUserLinkConfig("default1", + "OF|1@OF|2", "OF|1@OF|2"); + TopologyUserLinkConfig link2 = new TopologyUserLinkConfig("default1", + "OF|10@OF|20", "OF|10@OF|20"); + TopologyUserLinkConfig link3 = new TopologyUserLinkConfig("default2", + "OF|1@OF|2", "OF|1@OF|2"); + TopologyUserLinkConfig link4 = new TopologyUserLinkConfig("default20", + "OF|10@OF|20", "OF|10@OF|20"); + + TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl(); + topoManagerImpl.nonClusterObjectCreate(); + + Assert.assertTrue(topoManagerImpl.addUserLink(link1).isSuccess()); + Assert.assertTrue(topoManagerImpl.addUserLink(link2).getCode() == StatusCode.CONFLICT); + Assert.assertTrue(topoManagerImpl.addUserLink(link3).getCode() == StatusCode.CONFLICT); + Assert.assertTrue(topoManagerImpl.addUserLink(link4).isSuccess()); + + Assert.assertTrue(topoManagerImpl.deleteUserLink(null).getCode() == StatusCode.BADREQUEST); + Assert.assertTrue(topoManagerImpl.deleteUserLink(link1.getName()) + .isSuccess()); + Assert.assertTrue(topoManagerImpl.deleteUserLink(link4.getName()) + .isSuccess()); + Assert.assertTrue(topoManagerImpl.getUserLinks().isEmpty()); + + } + + @Test + public void testGetUserLink() { + TopologyUserLinkConfig[] link = new TopologyUserLinkConfig[5]; + TopologyUserLinkConfig[] reverseLink = new TopologyUserLinkConfig[5]; + TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl(); + topoManagerImpl.nonClusterObjectCreate(); + + String name = "Test"; + String srcSwitchId = null; + String srcNodeConnectorIDType = null; + String srcPort = null; + String srcNodeIDType = null; + String dstNodeIDType = null; + String dstSwitchId = null; + String dstNodeConnectorIDType = null; + String dstPort = null; + String srcNodeConnector = null; + String dstNodeConnector = null; + + /* Creating userlinks and checking for their validity */ + link[0] = new TopologyUserLinkConfig(name, srcNodeConnector, dstNodeConnector); + Assert.assertTrue(link[0].isValid() == false); + srcNodeConnector = "OF|1@OF|1"; + link[0] = new TopologyUserLinkConfig(name, srcNodeConnector, dstNodeConnector); + Assert.assertTrue(link[0].isValid() == false); + + dstNodeConnector = "OF|1@OF|2"; + link[0] = new TopologyUserLinkConfig(name, srcNodeConnector, dstNodeConnector); + Assert.assertTrue(link[0].isValid() == true); + + Integer i; + + for (i = 0; i < 5; i++) { + link[i] = new TopologyUserLinkConfig(); + + name = Integer.toString(i + 1); + srcSwitchId = Integer.toString(i + 1); + srcPort = Integer.toString(i + 1); + dstSwitchId = Integer.toString((i + 1) * 10); + dstPort = Integer.toString((i + 1) * 10); + + link[i].setName(name); + srcNodeConnectorIDType = dstNodeConnectorIDType = "INCORRECT"; + srcNodeConnector = srcNodeConnectorIDType+"|"+srcSwitchId+"@"+srcNodeConnectorIDType+"|"+srcPort; + dstNodeConnector = dstNodeConnectorIDType+"|"+dstSwitchId+"@"+dstNodeConnectorIDType+"|"+dstPort; + + link[i].setSrcNodeConnector(srcNodeConnector); + Assert.assertTrue(link[i].isValid() == false); + + srcNodeConnectorIDType = "OF"; + srcNodeConnector = srcNodeConnectorIDType+"|"+srcSwitchId+"@"+srcNodeConnectorIDType+"|"+srcPort; + link[i].setSrcNodeConnector(srcNodeConnector); + Assert.assertTrue(link[i].isValid() == false); + + dstNodeConnectorIDType = "OF"; + dstNodeConnector = dstNodeConnectorIDType+"|"+dstSwitchId+"@"+dstNodeConnectorIDType+"|"+dstPort; + link[i].setDstNodeConnector(dstNodeConnector); + Assert.assertTrue(link[i].isValid() == true); + + reverseLink[i] = new TopologyUserLinkConfig(name, dstNodeConnector, srcNodeConnector); + topoManagerImpl.addUserLink(link[i]); + } + + ConcurrentMap userLinks = topoManagerImpl + .getUserLinks(); + TopologyUserLinkConfig resultLink; + + for (i = 0; i < 5; i++) { + resultLink = userLinks.get(((Integer) (i + 1)).toString()); + + Assert.assertTrue(resultLink.getName().equals( + reverseLink[i].getName())); + Assert.assertTrue(resultLink.getDstNodeConnector().equals( + reverseLink[i].getSrcNodeConnector())); + Assert.assertTrue(resultLink.getSrcNodeConnector().equals( + reverseLink[i].getDstNodeConnector())); + } + } + + @Test + public void testHostLinkMethods() throws ConstructionException, + UnknownHostException { + TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl(); + topoManagerImpl.nonClusterObjectCreate(); + int hostCounter = 0; + + State state; + Bandwidth bw; + Latency l; + Set props = new HashSet(); + state = new State(State.EDGE_UP); + bw = new Bandwidth(Bandwidth.BW100Gbps); + l = new Latency(Latency.LATENCY100ns); + props.add(state); + props.add(bw); + props.add(l); + + EthernetAddress ea; + InetAddress ip; + Host[] h = new Host[5]; + NodeConnector[] nc = new NodeConnector[5]; + + /* + * Adding host, nodeConnector to hostsDB for the i = 0,1,2,3. No host + * added for i = 4 + */ + for (int i = 0; i < 5; i++) { + if (hostCounter < 4) { + ea = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0, + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) i }); + String stringIP = new StringBuilder().append(i + 1).append(".") + .append(i + 10).append(".").append(i + 20).append(".") + .append(i + 30).toString(); + ip = InetAddress.getByName(stringIP); + h[hostCounter] = new Host(ea, ip); + } else { + h[hostCounter] = null; + } + hostCounter++; + nc[i] = NodeConnectorCreator.createOFNodeConnector((short) (i + 1), + NodeCreator.createOFNode((long) (i + 1))); + topoManagerImpl + .updateHostLink(nc[i], h[i], UpdateType.ADDED, props); + } + + for (int i = 0; i < 5; i++) { + Host host = topoManagerImpl.getHostAttachedToNodeConnector(nc[i]); + if (i == 4) + Assert.assertTrue(host == null); + else + Assert.assertTrue(host.equals(h[i])); + } + + Set ncSet = topoManagerImpl.getNodeConnectorWithHost(); + for (int i = 0; i < 5; i++) { + Assert.assertTrue(ncSet.remove(nc[i])); + } + Assert.assertTrue(ncSet.isEmpty()); + } + + @Test + public void testGetNodesWithNodeConnectorHost() + throws ConstructionException, UnknownHostException { + TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl(); + topoManagerImpl.nonClusterObjectCreate(); + int hostCounter = 0; + + State state; + Bandwidth bw; + Latency l; + Set props = new HashSet(); + state = new State(State.EDGE_UP); + bw = new Bandwidth(Bandwidth.BW100Gbps); + l = new Latency(Latency.LATENCY100ns); + props.add(state); + props.add(bw); + props.add(l); + + EthernetAddress ea; + InetAddress ip; + Host[] h = new Host[5]; + NodeConnector[] nc = new NodeConnector[5]; + + /* + * Adding host, nodeconnector, properties of edge to hostsDB for the + * first three nodes only + */ + for (int i = 1; i < 6; i++) { + if (i < 4) { + ea = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0, + (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) i }); + String stringIP = new StringBuilder().append(i).append(".") + .append(i + 10).append(".").append(i + 20).append(".") + .append(i + 30).toString(); + ip = InetAddress.getByName(stringIP); + h[hostCounter] = new Host(ea, ip); + } else { + h[hostCounter] = null; + } + hostCounter++; + nc[i - 1] = NodeConnectorCreator.createOFNodeConnector((short) i, + NodeCreator.createOFNode((long) i)); + topoManagerImpl.updateHostLink(nc[i - 1], h[i - 1], + UpdateType.ADDED, props); + } + + /* Get the nodes which have host connected to its nodeConnector */ + Map> nodeNCmap = topoManagerImpl + .getNodesWithNodeConnectorHost(); + for (int i = 1; i < 6; i++) { + Node node = nc[i - 1].getNode(); + Set ncSet = nodeNCmap.get(nc[i - 1].getNode()); + + Assert.assertTrue(ncSet == nodeNCmap.remove(node)); + } + + Assert.assertTrue(nodeNCmap.isEmpty()); + } +}