Resubmitted with source code synchronized. Added integration test for hosttracker...
[controller.git] / opendaylight / switchmanager / implementation / src / test / java / org / opendaylight / controller / switchmanager / internal / SwitchManagerImplTest.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.switchmanager.internal;
11
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.Set;
15
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.opendaylight.controller.sal.core.Bandwidth;
19 import org.opendaylight.controller.sal.core.Latency;
20 import org.opendaylight.controller.sal.core.Node;
21 import org.opendaylight.controller.sal.core.NodeConnector;
22 import org.opendaylight.controller.sal.core.Property;
23 import org.opendaylight.controller.sal.core.State;
24 import org.opendaylight.controller.sal.core.UpdateType;
25 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
26 import org.opendaylight.controller.sal.utils.NodeCreator;
27 import org.opendaylight.controller.sal.utils.Status;
28 import org.opendaylight.controller.switchmanager.SubnetConfig;
29
30 public class SwitchManagerImplTest {
31
32         @Test
33         public void testSwitchManagerAddRemoveSubnet() {
34                 SwitchManagerImpl switchmgr = new SwitchManagerImpl();
35                 switchmgr.nonClusterObjectCreate();
36                 
37                 ArrayList<String>portList = new ArrayList<String>();
38                 portList.add("1/1");
39                 portList.add("1/2");
40                 portList.add("1/3");
41
42                 
43                 SubnetConfig subnet = new SubnetConfig("subnet", "10.0.0.254/16", portList);
44                 //System.out.println("*" + switchmgr.addSubnet(subnet) + "*");
45                 Status addResult = (switchmgr.addSubnet(subnet));
46                 Assert.assertTrue(addResult.isSuccess());
47                 
48                 Status removeResult = (switchmgr.removeSubnet(subnet.getName()));
49                 Assert.assertTrue(removeResult.isSuccess());
50
51                 SubnetConfig subnetConfigResult = switchmgr.getSubnetConfig(subnet.getName());
52                 Assert.assertTrue(subnetConfigResult == null);
53                 
54         }
55         
56         @Test
57         public void testSwitchManagerNodeConnectors() {
58                 SwitchManagerImpl switchmgr = new SwitchManagerImpl();
59                 switchmgr.nonClusterObjectCreate();
60                 
61                 State state;
62                 Bandwidth bw;
63                 Latency l;
64                 
65                 NodeConnector[] headnc = new NodeConnector[5];
66                 NodeConnector[] tailnc = new NodeConnector[5];
67
68                 Set<Property> props = new HashSet<Property>();
69                 state = new State(State.EDGE_UP);
70                 bw = new Bandwidth(Bandwidth.BW100Gbps);
71                 l = new Latency(Latency.LATENCY100ns);
72                 props.add(state);
73                 props.add(bw);
74                 props.add(l);
75                 
76                 for (short i = 1; i < 6; i = (short)(i + 1)) {
77
78                                 headnc[i - 1] = NodeConnectorCreator.createOFNodeConnector(i, NodeCreator.createOFNode((long)i));
79                                 tailnc[i - 1] = NodeConnectorCreator.createOFNodeConnector((short)(i+10), NodeCreator.createOFNode((long)(i+10)));
80                                 switchmgr.updateNode(headnc[i - 1].getNode(), UpdateType.ADDED, props);
81                                 switchmgr.updateNode(tailnc[i - 1].getNode(), UpdateType.ADDED, props);
82
83                                 switchmgr.updateNodeConnector(headnc[i - 1], UpdateType.ADDED, props);
84                                 switchmgr.updateNodeConnector(tailnc[i - 1], UpdateType.ADDED, props);
85                 }
86                 
87                 for (int i = 0; i < 5; i++) {
88                         Property bwProp = switchmgr.getNodeConnectorProp(headnc[i], Bandwidth.BandwidthPropName);
89                         Assert.assertTrue(bwProp.equals(bw));
90                         Property latencyProp = switchmgr.getNodeConnectorProp(tailnc[i], Latency.LatencyPropName);
91                         Assert.assertEquals(latencyProp, l);
92                         
93                         byte[] headNodeMac = switchmgr.getNodeMAC(headnc[i].getNode());
94                         Assert.assertTrue(headNodeMac[headNodeMac.length - 1] == (byte)(i + 1));
95                 }
96                 
97                 Set<Node> nodes = switchmgr.getNodes();
98                 for (int i = 0; i < 5; i++) {
99                         if (nodes.contains(headnc[i].getNode()) == true) 
100                                         nodes.remove(headnc[i].getNode());
101                         
102                         if (nodes.contains(tailnc[i].getNode()) == true) 
103                                         nodes.remove(tailnc[i].getNode());
104                         
105                 }
106                 Assert.assertTrue(nodes.isEmpty());
107         }
108         
109 }