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