/* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ 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; import org.junit.Assert; import org.junit.Test; import org.opendaylight.controller.sal.core.Bandwidth; import org.opendaylight.controller.sal.core.ConstructionException; import org.opendaylight.controller.sal.core.Edge; import org.opendaylight.controller.sal.core.Host; import org.opendaylight.controller.sal.core.Latency; import org.opendaylight.controller.sal.core.Node; import org.opendaylight.controller.sal.core.Node.NodeIDType; import org.opendaylight.controller.sal.core.NodeConnector; 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)) { 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(tailnc1, headnc1); TopoEdgeUpdate teu1 = new TopoEdgeUpdate(e1, props, UpdateType.ADDED); topoedgeupdateList.add(teu1); NodeConnector tailnc2 = NodeConnectorCreator.createOFNodeConnector( (short) (i + 1), headnc1.getNode()); NodeConnector headnc2 = NodeConnectorCreator.createOFNodeConnector( (short) (i + 11), tailnc1.getNode()); Edge e2 = new Edge(tailnc2, headnc2); 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(); Assert.assertTrue( (headNcId.equals(nodeId) && tailNcId.equals(nodeId + 10)) || (headNcId.equals(nodeId + 11) && tailNcId.equals(nodeId + 1)) || (headNcId.equals(nodeId + 1) && tailNcId.equals(nodeId - 9)) || (headNcId.equals(nodeId - 10) && tailNcId.equals(nodeId)) ); } 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()); } }