850563fe95b07774b13b2e645bb7de52bec8daee
[controller.git] / opendaylight / topologymanager / src / test / java / org / opendaylight / controller / topologymanager / internal / TopologyManagerImplTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.topologymanager.internal;
10
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.concurrent.ConcurrentMap;
20
21 import org.junit.Assert;
22 import org.junit.Test;
23 import org.opendaylight.controller.sal.core.Bandwidth;
24 import org.opendaylight.controller.sal.core.ConstructionException;
25 import org.opendaylight.controller.sal.core.Edge;
26 import org.opendaylight.controller.sal.core.Host;
27 import org.opendaylight.controller.sal.core.Latency;
28 import org.opendaylight.controller.sal.core.Node;
29 import org.opendaylight.controller.sal.core.Node.NodeIDType;
30 import org.opendaylight.controller.sal.core.NodeConnector;
31 import org.opendaylight.controller.sal.core.Property;
32 import org.opendaylight.controller.sal.core.State;
33 import org.opendaylight.controller.sal.core.UpdateType;
34 import org.opendaylight.controller.sal.packet.address.EthernetAddress;
35 import org.opendaylight.controller.sal.topology.TopoEdgeUpdate;
36 import org.opendaylight.controller.sal.utils.StatusCode;
37 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
38 import org.opendaylight.controller.sal.utils.NodeCreator;
39 import org.opendaylight.controller.topologymanager.TopologyUserLinkConfig;
40
41 public class TopologyManagerImplTest {
42
43     /*
44      * Sets the node, edges and properties for edges here: Edge <SwitchId :
45      * NodeConnectorId> : <1:1>--><11:11>; <1:2>--><11:12>; <3:3>--><13:13>;
46      * <3:4>--><13:14>; <5:5>--><15:15>; <5:6>--><15:16>; Method used by two
47      * tests: testGetNodeEdges and testGetEdges
48      *
49      * @param topoManagerImpl
50      *
51      * @throws ConstructionException
52      */
53     public void setNodeEdges(TopologyManagerImpl topoManagerImpl)
54             throws ConstructionException {
55         topoManagerImpl.nonClusterObjectCreate();
56
57         State state;
58         Bandwidth bw;
59         Latency l;
60
61         Set<Property> props = new HashSet<Property>();
62         state = new State(State.EDGE_UP);
63         bw = new Bandwidth(Bandwidth.BW100Gbps);
64         l = new Latency(Latency.LATENCY100ns);
65         props.add(state);
66         props.add(bw);
67         props.add(l);
68
69         for (short i = 1; i < 6; i = (short) (i + 2)) {
70             List<TopoEdgeUpdate> topoedgeupdateList = new ArrayList<TopoEdgeUpdate>();
71             NodeConnector headnc1 = NodeConnectorCreator.createOFNodeConnector(
72                     i, NodeCreator.createOFNode((long) i));
73             NodeConnector tailnc1 = NodeConnectorCreator
74                     .createOFNodeConnector((short) (i + 10),
75                             NodeCreator.createOFNode((long) (i + 10)));
76             Edge e1 = new Edge(headnc1, tailnc1);
77             TopoEdgeUpdate teu1 = new TopoEdgeUpdate(e1, props,
78                     UpdateType.ADDED);
79             topoedgeupdateList.add(teu1);
80
81             NodeConnector headnc2 = NodeConnectorCreator.createOFNodeConnector(
82                     (short) (i + 1), headnc1.getNode());
83             NodeConnector tailnc2 = NodeConnectorCreator.createOFNodeConnector(
84                     (short) (i + 11), tailnc1.getNode());
85             Edge e2 = new Edge(headnc2, tailnc2);
86             TopoEdgeUpdate teu2 = new TopoEdgeUpdate(e2, props,
87                     UpdateType.ADDED);
88             topoedgeupdateList.add(teu2);
89             topoManagerImpl.edgeUpdate(topoedgeupdateList);
90
91         }
92
93     }
94
95     @Test
96     public void testGetNodeEdges() throws ConstructionException {
97         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
98         setNodeEdges(topoManagerImpl);
99
100         Map<Node, Set<Edge>> nodeEdgeMap = topoManagerImpl.getNodeEdges();
101         for (Iterator<Map.Entry<Node, Set<Edge>>> i = nodeEdgeMap.entrySet()
102                 .iterator(); i.hasNext();) {
103             Map.Entry<Node, Set<Edge>> entry = i.next();
104             Node node = entry.getKey();
105             Long nodeId = ((Long) node.getID()).longValue();
106             Assert.assertTrue((node.getType().equals(NodeIDType.OPENFLOW)));
107
108             Set<Edge> edges = entry.getValue();
109             for (Edge edge : edges) {
110                 Long headNcId = ((Short) edge.getHeadNodeConnector().getID())
111                         .longValue();
112                 Long tailNcId = ((Short) edge.getTailNodeConnector().getID())
113                         .longValue();
114                 if (nodeId == 1 || nodeId == 3 || nodeId == 5) {
115                     Assert.assertTrue((headNcId.equals(nodeId) && tailNcId
116                             .equals(nodeId + 10))
117                             || (headNcId.equals(nodeId + 10) && tailNcId
118                                     .equals(nodeId))
119                                     || (headNcId.equals(nodeId + 1) && tailNcId
120                                             .equals(nodeId + 11))
121                                             || (headNcId.equals(nodeId + 11) && tailNcId
122                                                     .equals(nodeId + 1)));
123                 } else if (nodeId == 11 || nodeId == 13 || nodeId == 15) {
124                     Assert.assertTrue((headNcId.equals(nodeId) && tailNcId
125                             .equals(nodeId - 10))
126                             || (headNcId.equals(nodeId) && tailNcId
127                                     .equals(nodeId - 10))
128                                     || (headNcId.equals(nodeId - 9) && tailNcId
129                                             .equals(nodeId + 1))
130                                             || (headNcId.equals(nodeId + 1) && tailNcId
131                                                     .equals(nodeId - 9)));
132                 }
133             }
134             i.remove();
135         }
136         Assert.assertTrue(nodeEdgeMap.isEmpty());
137     }
138
139     @Test
140     public void testGetEdges() throws ConstructionException {
141         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
142         setNodeEdges(topoManagerImpl);
143
144         Map<Edge, Set<Property>> edgeProperty = topoManagerImpl.getEdges();
145
146         for (Iterator<Map.Entry<Edge, Set<Property>>> i = edgeProperty
147                 .entrySet().iterator(); i.hasNext();) {
148             Map.Entry<Edge, Set<Property>> entry = i.next();
149             Edge e = entry.getKey();
150             NodeConnector headnc = e.getHeadNodeConnector();
151             NodeConnector tailnc = e.getTailNodeConnector();
152
153             Long headNodeId = (Long) headnc.getNode().getID();
154
155             Long headNcId = ((Short) headnc.getID()).longValue();
156             Long tailNcId = ((Short) tailnc.getID()).longValue();
157
158             if (headNodeId == 1 || headNodeId == 3 || headNodeId == 5) {
159                 Assert.assertTrue((headNcId.equals(headNodeId) && tailNcId
160                         .equals(headNodeId + 10))
161                         || (headNcId.equals(headNodeId + 10) && tailNcId
162                                 .equals(headNodeId))
163                                 || (headNcId.equals(headNodeId + 1) && tailNcId
164                                         .equals(headNodeId + 11))
165                                         || (headNcId.equals(headNodeId + 11) && tailNcId
166                                                 .equals(headNodeId + 1)));
167             } else if (headNodeId == 11 || headNodeId == 13 || headNodeId == 15) {
168                 Assert.assertTrue((headNcId.equals(headNodeId) && tailNcId
169                         .equals(headNodeId - 10))
170                         || (headNcId.equals(headNodeId) && tailNcId
171                                 .equals(headNodeId - 10))
172                                 || (headNcId.equals(headNodeId - 9) && tailNcId
173                                         .equals(headNodeId + 1))
174                                         || (headNcId.equals(headNodeId + 1) && tailNcId
175                                                 .equals(headNodeId - 9)));
176             }
177
178             Set<Property> prop = entry.getValue();
179             for (Property p : prop) {
180                 String pName;
181                 long pValue;
182                 if (p instanceof Bandwidth) {
183                     Bandwidth b = (Bandwidth) p;
184                     pName = Bandwidth.BandwidthPropName;
185                     pValue = b.getValue();
186                     Assert.assertTrue(pName.equals(p.getName())
187                             && pValue == Bandwidth.BW100Gbps);
188                     continue;
189                 }
190                 if (p instanceof Latency) {
191                     Latency l = (Latency) p;
192                     pName = Latency.LatencyPropName;
193                     pValue = l.getValue();
194                     Assert.assertTrue(pName.equals(p.getName())
195                             && pValue == Latency.LATENCY100ns);
196                     continue;
197                 }
198                 if (p instanceof State) {
199                     State state = (State) p;
200                     pName = State.StatePropName;
201                     pValue = state.getValue();
202                     Assert.assertTrue(pName.equals(p.getName())
203                             && pValue == State.EDGE_UP);
204                     continue;
205                 }
206             }
207             i.remove();
208         }
209         Assert.assertTrue(edgeProperty.isEmpty());
210     }
211
212     @Test
213     public void testAddDeleteUserLink() {
214         TopologyUserLinkConfig link1 = new TopologyUserLinkConfig("default1",
215                 "OF|1@OF|2", "OF|1@OF|2");
216         TopologyUserLinkConfig link2 = new TopologyUserLinkConfig("default1",
217                 "OF|10@OF|20", "OF|10@OF|20");
218         TopologyUserLinkConfig link3 = new TopologyUserLinkConfig("default2",
219                 "OF|1@OF|2", "OF|1@OF|2");
220         TopologyUserLinkConfig link4 = new TopologyUserLinkConfig("default20",
221                 "OF|10@OF|20", "OF|10@OF|20");
222
223         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
224         topoManagerImpl.nonClusterObjectCreate();
225
226         Assert.assertTrue(topoManagerImpl.addUserLink(link1).isSuccess());
227         Assert.assertTrue(topoManagerImpl.addUserLink(link2).getCode() == StatusCode.CONFLICT);
228         Assert.assertTrue(topoManagerImpl.addUserLink(link3).getCode() == StatusCode.CONFLICT);
229         Assert.assertTrue(topoManagerImpl.addUserLink(link4).isSuccess());
230
231         Assert.assertTrue(topoManagerImpl.deleteUserLink(null).getCode() == StatusCode.BADREQUEST);
232         Assert.assertTrue(topoManagerImpl.deleteUserLink(link1.getName())
233                 .isSuccess());
234         Assert.assertTrue(topoManagerImpl.deleteUserLink(link4.getName())
235                 .isSuccess());
236         Assert.assertTrue(topoManagerImpl.getUserLinks().isEmpty());
237
238     }
239
240     @Test
241     public void testGetUserLink() {
242         TopologyUserLinkConfig[] link = new TopologyUserLinkConfig[5];
243         TopologyUserLinkConfig[] reverseLink = new TopologyUserLinkConfig[5];
244         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
245         topoManagerImpl.nonClusterObjectCreate();
246
247         String name = "Test";
248         String srcSwitchId = null;
249         String srcNodeConnectorIDType = null;
250         String srcPort = null;
251         String srcNodeIDType = null;
252         String dstNodeIDType = null;
253         String dstSwitchId = null;
254         String dstNodeConnectorIDType = null;
255         String dstPort = null;
256         String srcNodeConnector = null;
257         String dstNodeConnector = null;
258
259         /* Creating userlinks and checking for their validity */
260         link[0] = new TopologyUserLinkConfig(name, srcNodeConnector, dstNodeConnector);
261         Assert.assertTrue(link[0].isValid() == false);
262
263         srcNodeConnector = "OF|1@OF|1";
264         link[0] = new TopologyUserLinkConfig(name, srcNodeConnector, dstNodeConnector);
265         Assert.assertTrue(link[0].isValid() == false);
266
267         dstNodeConnector = "OF|1@OF|2";
268         link[0] = new TopologyUserLinkConfig(name, srcNodeConnector, dstNodeConnector);
269         Assert.assertTrue(link[0].isValid() == true);
270
271         Integer i;
272
273         for (i = 0; i < 5; i++) {
274             link[i] = new TopologyUserLinkConfig();
275
276             name = Integer.toString(i + 1);
277             srcSwitchId = Integer.toString(i + 1);
278             srcPort = Integer.toString(i + 1);
279             dstSwitchId = Integer.toString((i + 1) * 10);
280             dstPort = Integer.toString((i + 1) * 10);
281
282             link[i].setName(name);
283             srcNodeConnectorIDType = dstNodeConnectorIDType = "INCORRECT";
284             srcNodeConnector = srcNodeConnectorIDType+"|"+srcSwitchId+"@"+srcNodeConnectorIDType+"|"+srcPort;
285             dstNodeConnector = dstNodeConnectorIDType+"|"+dstSwitchId+"@"+dstNodeConnectorIDType+"|"+dstPort;
286
287             link[i].setSrcNodeConnector(srcNodeConnector);
288             Assert.assertTrue(link[i].isValid() == false);
289
290             srcNodeConnectorIDType = "OF";
291             srcNodeConnector = srcNodeConnectorIDType+"|"+srcSwitchId+"@"+srcNodeConnectorIDType+"|"+srcPort;
292             link[i].setSrcNodeConnector(srcNodeConnector);
293             Assert.assertTrue(link[i].isValid() == false);
294
295             dstNodeConnectorIDType = "OF";
296             dstNodeConnector = dstNodeConnectorIDType+"|"+dstSwitchId+"@"+dstNodeConnectorIDType+"|"+dstPort;
297             link[i].setDstNodeConnector(dstNodeConnector);
298             Assert.assertTrue(link[i].isValid() == true);
299
300             reverseLink[i] = new TopologyUserLinkConfig(name, dstNodeConnector, srcNodeConnector);
301             topoManagerImpl.addUserLink(link[i]);
302         }
303
304         ConcurrentMap<String, TopologyUserLinkConfig> userLinks = topoManagerImpl
305                 .getUserLinks();
306         TopologyUserLinkConfig resultLink;
307
308         for (i = 0; i < 5; i++) {
309             resultLink = userLinks.get(((Integer) (i + 1)).toString());
310
311             Assert.assertTrue(resultLink.getName().equals(
312                     reverseLink[i].getName()));
313             Assert.assertTrue(resultLink.getDstNodeConnector().equals(
314                     reverseLink[i].getSrcNodeConnector()));
315             Assert.assertTrue(resultLink.getSrcNodeConnector().equals(
316                     reverseLink[i].getDstNodeConnector()));
317         }
318     }
319
320     @Test
321     public void testHostLinkMethods() throws ConstructionException,
322     UnknownHostException {
323         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
324         topoManagerImpl.nonClusterObjectCreate();
325         int hostCounter = 0;
326
327         State state;
328         Bandwidth bw;
329         Latency l;
330         Set<Property> props = new HashSet<Property>();
331         state = new State(State.EDGE_UP);
332         bw = new Bandwidth(Bandwidth.BW100Gbps);
333         l = new Latency(Latency.LATENCY100ns);
334         props.add(state);
335         props.add(bw);
336         props.add(l);
337
338         EthernetAddress ea;
339         InetAddress ip;
340         Host[] h = new Host[5];
341         NodeConnector[] nc = new NodeConnector[5];
342
343         /*
344          * Adding host, nodeConnector to hostsDB for the i = 0,1,2,3. No host
345          * added for i = 4
346          */
347         for (int i = 0; i < 5; i++) {
348             if (hostCounter < 4) {
349                 ea = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
350                         (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) i });
351                 String stringIP = new StringBuilder().append(i + 1).append(".")
352                         .append(i + 10).append(".").append(i + 20).append(".")
353                         .append(i + 30).toString();
354                 ip = InetAddress.getByName(stringIP);
355                 h[hostCounter] = new Host(ea, ip);
356             } else {
357                 h[hostCounter] = null;
358             }
359             hostCounter++;
360             nc[i] = NodeConnectorCreator.createOFNodeConnector((short) (i + 1),
361                     NodeCreator.createOFNode((long) (i + 1)));
362             topoManagerImpl
363             .updateHostLink(nc[i], h[i], UpdateType.ADDED, props);
364         }
365
366         for (int i = 0; i < 5; i++) {
367             Host host = topoManagerImpl.getHostAttachedToNodeConnector(nc[i]);
368             if (i == 4)
369                 Assert.assertTrue(host == null);
370             else
371                 Assert.assertTrue(host.equals(h[i]));
372         }
373
374         Set<NodeConnector> ncSet = topoManagerImpl.getNodeConnectorWithHost();
375         for (int i = 0; i < 5; i++) {
376             Assert.assertTrue(ncSet.remove(nc[i]));
377         }
378         Assert.assertTrue(ncSet.isEmpty());
379     }
380
381     @Test
382     public void testGetNodesWithNodeConnectorHost()
383             throws ConstructionException, UnknownHostException {
384         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
385         topoManagerImpl.nonClusterObjectCreate();
386         int hostCounter = 0;
387
388         State state;
389         Bandwidth bw;
390         Latency l;
391         Set<Property> props = new HashSet<Property>();
392         state = new State(State.EDGE_UP);
393         bw = new Bandwidth(Bandwidth.BW100Gbps);
394         l = new Latency(Latency.LATENCY100ns);
395         props.add(state);
396         props.add(bw);
397         props.add(l);
398
399         EthernetAddress ea;
400         InetAddress ip;
401         Host[] h = new Host[5];
402         NodeConnector[] nc = new NodeConnector[5];
403
404         /*
405          * Adding host, nodeconnector, properties of edge to hostsDB for the
406          * first three nodes only
407          */
408         for (int i = 1; i < 6; i++) {
409             if (i < 4) {
410                 ea = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
411                         (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) i });
412                 String stringIP = new StringBuilder().append(i).append(".")
413                         .append(i + 10).append(".").append(i + 20).append(".")
414                         .append(i + 30).toString();
415                 ip = InetAddress.getByName(stringIP);
416                 h[hostCounter] = new Host(ea, ip);
417             } else {
418                 h[hostCounter] = null;
419             }
420             hostCounter++;
421             nc[i - 1] = NodeConnectorCreator.createOFNodeConnector((short) i,
422                     NodeCreator.createOFNode((long) i));
423             topoManagerImpl.updateHostLink(nc[i - 1], h[i - 1],
424                     UpdateType.ADDED, props);
425         }
426
427         /* Get the nodes which have host connected to its nodeConnector */
428         Map<Node, Set<NodeConnector>> nodeNCmap = topoManagerImpl
429                 .getNodesWithNodeConnectorHost();
430         for (int i = 1; i < 6; i++) {
431             Node node = nc[i - 1].getNode();
432             Set<NodeConnector> ncSet = nodeNCmap.get(nc[i - 1].getNode());
433
434             Assert.assertTrue(ncSet == nodeNCmap.remove(node));
435         }
436
437         Assert.assertTrue(nodeNCmap.isEmpty());
438     }
439 }