Merge "Add IfNewHostNotify to DeviceManager"
[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(tailnc1, headnc1);
77             TopoEdgeUpdate teu1 = new TopoEdgeUpdate(e1, props,
78                     UpdateType.ADDED);
79             topoedgeupdateList.add(teu1);
80
81             NodeConnector tailnc2 = NodeConnectorCreator.createOFNodeConnector(
82                     (short) (i + 1), headnc1.getNode());
83             NodeConnector headnc2 = NodeConnectorCreator.createOFNodeConnector(
84                     (short) (i + 11), tailnc1.getNode());
85             Edge e2 = new Edge(tailnc2, headnc2);
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                 Assert.assertTrue(
115                         (headNcId.equals(nodeId) && tailNcId.equals(nodeId + 10))
116                         || (headNcId.equals(nodeId + 11) && tailNcId.equals(nodeId + 1))
117                         || (headNcId.equals(nodeId + 1) && tailNcId.equals(nodeId - 9))
118                         || (headNcId.equals(nodeId - 10) && tailNcId.equals(nodeId))
119                         );
120             }
121             i.remove();
122         }
123         Assert.assertTrue(nodeEdgeMap.isEmpty());
124     }
125
126     @Test
127     public void testGetEdges() throws ConstructionException {
128         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
129         setNodeEdges(topoManagerImpl);
130
131         Map<Edge, Set<Property>> edgeProperty = topoManagerImpl.getEdges();
132
133         for (Iterator<Map.Entry<Edge, Set<Property>>> i = edgeProperty
134                 .entrySet().iterator(); i.hasNext();) {
135             Map.Entry<Edge, Set<Property>> entry = i.next();
136             Edge e = entry.getKey();
137             NodeConnector headnc = e.getHeadNodeConnector();
138             NodeConnector tailnc = e.getTailNodeConnector();
139
140             Long headNodeId = (Long) headnc.getNode().getID();
141
142             Long headNcId = ((Short) headnc.getID()).longValue();
143             Long tailNcId = ((Short) tailnc.getID()).longValue();
144
145             if (headNodeId == 1 || headNodeId == 3 || headNodeId == 5) {
146                 Assert.assertTrue((headNcId.equals(headNodeId) && tailNcId
147                         .equals(headNodeId + 10))
148                         || (headNcId.equals(headNodeId + 10) && tailNcId
149                                 .equals(headNodeId))
150                                 || (headNcId.equals(headNodeId + 1) && tailNcId
151                                         .equals(headNodeId + 11))
152                                         || (headNcId.equals(headNodeId + 11) && tailNcId
153                                                 .equals(headNodeId + 1)));
154             } else if (headNodeId == 11 || headNodeId == 13 || headNodeId == 15) {
155                 Assert.assertTrue((headNcId.equals(headNodeId) && tailNcId
156                         .equals(headNodeId - 10))
157                         || (headNcId.equals(headNodeId) && tailNcId
158                                 .equals(headNodeId - 10))
159                                 || (headNcId.equals(headNodeId - 9) && tailNcId
160                                         .equals(headNodeId + 1))
161                                         || (headNcId.equals(headNodeId + 1) && tailNcId
162                                                 .equals(headNodeId - 9)));
163             }
164
165             Set<Property> prop = entry.getValue();
166             for (Property p : prop) {
167                 String pName;
168                 long pValue;
169                 if (p instanceof Bandwidth) {
170                     Bandwidth b = (Bandwidth) p;
171                     pName = Bandwidth.BandwidthPropName;
172                     pValue = b.getValue();
173                     Assert.assertTrue(pName.equals(p.getName())
174                             && pValue == Bandwidth.BW100Gbps);
175                     continue;
176                 }
177                 if (p instanceof Latency) {
178                     Latency l = (Latency) p;
179                     pName = Latency.LatencyPropName;
180                     pValue = l.getValue();
181                     Assert.assertTrue(pName.equals(p.getName())
182                             && pValue == Latency.LATENCY100ns);
183                     continue;
184                 }
185                 if (p instanceof State) {
186                     State state = (State) p;
187                     pName = State.StatePropName;
188                     pValue = state.getValue();
189                     Assert.assertTrue(pName.equals(p.getName())
190                             && pValue == State.EDGE_UP);
191                     continue;
192                 }
193             }
194             i.remove();
195         }
196         Assert.assertTrue(edgeProperty.isEmpty());
197     }
198
199     @Test
200     public void testAddDeleteUserLink() {
201         TopologyUserLinkConfig link1 = new TopologyUserLinkConfig("default1",
202                 "OF|1@OF|2", "OF|1@OF|2");
203         TopologyUserLinkConfig link2 = new TopologyUserLinkConfig("default1",
204                 "OF|10@OF|20", "OF|10@OF|20");
205         TopologyUserLinkConfig link3 = new TopologyUserLinkConfig("default2",
206                 "OF|1@OF|2", "OF|1@OF|2");
207         TopologyUserLinkConfig link4 = new TopologyUserLinkConfig("default20",
208                 "OF|10@OF|20", "OF|10@OF|20");
209
210         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
211         topoManagerImpl.nonClusterObjectCreate();
212
213         Assert.assertTrue(topoManagerImpl.addUserLink(link1).isSuccess());
214         Assert.assertTrue(topoManagerImpl.addUserLink(link2).getCode() == StatusCode.CONFLICT);
215         Assert.assertTrue(topoManagerImpl.addUserLink(link3).getCode() == StatusCode.CONFLICT);
216         Assert.assertTrue(topoManagerImpl.addUserLink(link4).isSuccess());
217
218         Assert.assertTrue(topoManagerImpl.deleteUserLink(null).getCode() == StatusCode.BADREQUEST);
219         Assert.assertTrue(topoManagerImpl.deleteUserLink(link1.getName())
220                 .isSuccess());
221         Assert.assertTrue(topoManagerImpl.deleteUserLink(link4.getName())
222                 .isSuccess());
223         Assert.assertTrue(topoManagerImpl.getUserLinks().isEmpty());
224
225     }
226
227     @Test
228     public void testGetUserLink() {
229         TopologyUserLinkConfig[] link = new TopologyUserLinkConfig[5];
230         TopologyUserLinkConfig[] reverseLink = new TopologyUserLinkConfig[5];
231         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
232         topoManagerImpl.nonClusterObjectCreate();
233
234         String name = "Test";
235         String srcSwitchId = null;
236         String srcNodeConnectorIDType = null;
237         String srcPort = null;
238         String srcNodeIDType = null;
239         String dstNodeIDType = null;
240         String dstSwitchId = null;
241         String dstNodeConnectorIDType = null;
242         String dstPort = null;
243         String srcNodeConnector = null;
244         String dstNodeConnector = null;
245
246         /* Creating userlinks and checking for their validity */
247         link[0] = new TopologyUserLinkConfig(name, srcNodeConnector, dstNodeConnector);
248         Assert.assertTrue(link[0].isValid() == false);
249
250         srcNodeConnector = "OF|1@OF|1";
251         link[0] = new TopologyUserLinkConfig(name, srcNodeConnector, dstNodeConnector);
252         Assert.assertTrue(link[0].isValid() == false);
253
254         dstNodeConnector = "OF|1@OF|2";
255         link[0] = new TopologyUserLinkConfig(name, srcNodeConnector, dstNodeConnector);
256         Assert.assertTrue(link[0].isValid() == true);
257
258         Integer i;
259
260         for (i = 0; i < 5; i++) {
261             link[i] = new TopologyUserLinkConfig();
262
263             name = Integer.toString(i + 1);
264             srcSwitchId = Integer.toString(i + 1);
265             srcPort = Integer.toString(i + 1);
266             dstSwitchId = Integer.toString((i + 1) * 10);
267             dstPort = Integer.toString((i + 1) * 10);
268
269             link[i].setName(name);
270             srcNodeConnectorIDType = dstNodeConnectorIDType = "INCORRECT";
271             srcNodeConnector = srcNodeConnectorIDType+"|"+srcSwitchId+"@"+srcNodeConnectorIDType+"|"+srcPort;
272             dstNodeConnector = dstNodeConnectorIDType+"|"+dstSwitchId+"@"+dstNodeConnectorIDType+"|"+dstPort;
273
274             link[i].setSrcNodeConnector(srcNodeConnector);
275             Assert.assertTrue(link[i].isValid() == false);
276
277             srcNodeConnectorIDType = "OF";
278             srcNodeConnector = srcNodeConnectorIDType+"|"+srcSwitchId+"@"+srcNodeConnectorIDType+"|"+srcPort;
279             link[i].setSrcNodeConnector(srcNodeConnector);
280             Assert.assertTrue(link[i].isValid() == false);
281
282             dstNodeConnectorIDType = "OF";
283             dstNodeConnector = dstNodeConnectorIDType+"|"+dstSwitchId+"@"+dstNodeConnectorIDType+"|"+dstPort;
284             link[i].setDstNodeConnector(dstNodeConnector);
285             Assert.assertTrue(link[i].isValid() == true);
286
287             reverseLink[i] = new TopologyUserLinkConfig(name, dstNodeConnector, srcNodeConnector);
288             topoManagerImpl.addUserLink(link[i]);
289         }
290
291         ConcurrentMap<String, TopologyUserLinkConfig> userLinks = topoManagerImpl
292                 .getUserLinks();
293         TopologyUserLinkConfig resultLink;
294
295         for (i = 0; i < 5; i++) {
296             resultLink = userLinks.get(((Integer) (i + 1)).toString());
297
298             Assert.assertTrue(resultLink.getName().equals(
299                     reverseLink[i].getName()));
300             Assert.assertTrue(resultLink.getDstNodeConnector().equals(
301                     reverseLink[i].getSrcNodeConnector()));
302             Assert.assertTrue(resultLink.getSrcNodeConnector().equals(
303                     reverseLink[i].getDstNodeConnector()));
304         }
305     }
306
307     @Test
308     public void testHostLinkMethods() throws ConstructionException,
309     UnknownHostException {
310         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
311         topoManagerImpl.nonClusterObjectCreate();
312         int hostCounter = 0;
313
314         State state;
315         Bandwidth bw;
316         Latency l;
317         Set<Property> props = new HashSet<Property>();
318         state = new State(State.EDGE_UP);
319         bw = new Bandwidth(Bandwidth.BW100Gbps);
320         l = new Latency(Latency.LATENCY100ns);
321         props.add(state);
322         props.add(bw);
323         props.add(l);
324
325         EthernetAddress ea;
326         InetAddress ip;
327         Host[] h = new Host[5];
328         NodeConnector[] nc = new NodeConnector[5];
329
330         /*
331          * Adding host, nodeConnector to hostsDB for the i = 0,1,2,3. No host
332          * added for i = 4
333          */
334         for (int i = 0; i < 5; i++) {
335             if (hostCounter < 4) {
336                 ea = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
337                         (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) i });
338                 String stringIP = new StringBuilder().append(i + 1).append(".")
339                         .append(i + 10).append(".").append(i + 20).append(".")
340                         .append(i + 30).toString();
341                 ip = InetAddress.getByName(stringIP);
342                 h[hostCounter] = new Host(ea, ip);
343             } else {
344                 h[hostCounter] = null;
345             }
346             hostCounter++;
347             nc[i] = NodeConnectorCreator.createOFNodeConnector((short) (i + 1),
348                     NodeCreator.createOFNode((long) (i + 1)));
349             topoManagerImpl
350             .updateHostLink(nc[i], h[i], UpdateType.ADDED, props);
351         }
352
353         for (int i = 0; i < 5; i++) {
354             Host host = topoManagerImpl.getHostAttachedToNodeConnector(nc[i]);
355             if (i == 4)
356                 Assert.assertTrue(host == null);
357             else
358                 Assert.assertTrue(host.equals(h[i]));
359         }
360
361         Set<NodeConnector> ncSet = topoManagerImpl.getNodeConnectorWithHost();
362         for (int i = 0; i < 5; i++) {
363             Assert.assertTrue(ncSet.remove(nc[i]));
364         }
365         Assert.assertTrue(ncSet.isEmpty());
366     }
367
368     @Test
369     public void testGetNodesWithNodeConnectorHost()
370             throws ConstructionException, UnknownHostException {
371         TopologyManagerImpl topoManagerImpl = new TopologyManagerImpl();
372         topoManagerImpl.nonClusterObjectCreate();
373         int hostCounter = 0;
374
375         State state;
376         Bandwidth bw;
377         Latency l;
378         Set<Property> props = new HashSet<Property>();
379         state = new State(State.EDGE_UP);
380         bw = new Bandwidth(Bandwidth.BW100Gbps);
381         l = new Latency(Latency.LATENCY100ns);
382         props.add(state);
383         props.add(bw);
384         props.add(l);
385
386         EthernetAddress ea;
387         InetAddress ip;
388         Host[] h = new Host[5];
389         NodeConnector[] nc = new NodeConnector[5];
390
391         /*
392          * Adding host, nodeconnector, properties of edge to hostsDB for the
393          * first three nodes only
394          */
395         for (int i = 1; i < 6; i++) {
396             if (i < 4) {
397                 ea = new EthernetAddress(new byte[] { (byte) 0x0, (byte) 0x0,
398                         (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) i });
399                 String stringIP = new StringBuilder().append(i).append(".")
400                         .append(i + 10).append(".").append(i + 20).append(".")
401                         .append(i + 30).toString();
402                 ip = InetAddress.getByName(stringIP);
403                 h[hostCounter] = new Host(ea, ip);
404             } else {
405                 h[hostCounter] = null;
406             }
407             hostCounter++;
408             nc[i - 1] = NodeConnectorCreator.createOFNodeConnector((short) i,
409                     NodeCreator.createOFNode((long) i));
410             topoManagerImpl.updateHostLink(nc[i - 1], h[i - 1],
411                     UpdateType.ADDED, props);
412         }
413
414         /* Get the nodes which have host connected to its nodeConnector */
415         Map<Node, Set<NodeConnector>> nodeNCmap = topoManagerImpl
416                 .getNodesWithNodeConnectorHost();
417         for (int i = 1; i < 6; i++) {
418             Node node = nc[i - 1].getNode();
419             Set<NodeConnector> ncSet = nodeNCmap.get(nc[i - 1].getNode());
420
421             Assert.assertTrue(ncSet == nodeNCmap.remove(node));
422         }
423
424         Assert.assertTrue(nodeNCmap.isEmpty());
425     }
426 }