Make sure invokeOperation is set once
[controller.git] / opendaylight / adsal / switchmanager / implementation / src / test / java / org / opendaylight / controller / switchmanager / internal / SwitchManagerTest.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.switchmanager.internal;
10
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
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.ConstructionException;
20 import org.opendaylight.controller.sal.core.Latency;
21 import org.opendaylight.controller.sal.core.Node;
22 import org.opendaylight.controller.sal.core.NodeConnector;
23 import org.opendaylight.controller.sal.core.Property;
24 import org.opendaylight.controller.sal.core.State;
25 import org.opendaylight.controller.sal.core.UpdateType;
26 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
27 import org.opendaylight.controller.sal.utils.NodeCreator;
28 import org.opendaylight.controller.sal.utils.Status;
29 import org.opendaylight.controller.switchmanager.SubnetConfig;
30
31 public class SwitchManagerTest {
32
33     @Test
34     public void testSwitchManagerAddRemoveSubnet() throws ConstructionException {
35         SwitchManager switchmgr = new SwitchManager();
36         switchmgr.startUp();
37
38         // Create the node connector string list
39         Node node1 = new Node(Node.NodeIDType.OPENFLOW, 1L);
40         Node node2 = new Node(Node.NodeIDType.OPENFLOW, 2L);
41         NodeConnector nc1 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, (short)1, node1);
42         NodeConnector nc2 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, (short)2, node2);
43         NodeConnector nc3 = new NodeConnector(NodeConnector.NodeConnectorIDType.OPENFLOW, (short)3, node1);
44         List<String> portList = new ArrayList<String>();
45         portList.add(nc1.toString());
46         portList.add(nc2.toString());
47         portList.add(nc3.toString());
48
49
50         SubnetConfig subnet = new SubnetConfig("subnet", "10.0.0.254/16", portList);
51         Status addResult = (switchmgr.addSubnet(subnet));
52         Assert.assertTrue(addResult.isSuccess());
53
54         Status removeResult = (switchmgr.removeSubnet(subnet.getName()));
55         Assert.assertTrue(removeResult.isSuccess());
56
57         SubnetConfig subnetConfigResult = switchmgr.getSubnetConfig(subnet.getName());
58         Assert.assertNull(subnetConfigResult);
59
60         subnet = new SubnetConfig("hr", "0.0.0.0", portList);
61         Status status = switchmgr.addSubnet(subnet);
62         Assert.assertFalse(status.isSuccess());
63
64         subnet = new SubnetConfig("hr", "12.12.12.254/16", null);
65         status = switchmgr.addSubnet(subnet);
66         Assert.assertTrue(status.isSuccess());
67
68     }
69
70     @Test
71     public void testSwitchManagerAddRemovePortsToSubnet() {
72         SwitchManager switchmgr = new SwitchManager();
73         switchmgr.startUp();
74
75         List<String> portList = new ArrayList<String>();
76         portList.add("OF|1@OF|1");
77         portList.add("OF|2@OF|00:00:00:00:00:00:00:02");
78         portList.add("OF|3@OF|00:00:00:00:00:00:00:01");
79
80         SubnetConfig subnet = new SubnetConfig("eng", "11.1.1.254/16", portList);
81         Status status = (switchmgr.addSubnet(subnet));
82         Assert.assertTrue(status.isSuccess());
83
84
85         // Empty port set
86         List<String> badPortSet = new ArrayList<String>();
87         status = switchmgr.addPortsToSubnet("eng", badPortSet);
88         Assert.assertFalse(status.isSuccess());
89
90         // Non existant subnet
91         status = switchmgr.removePortsFromSubnet("hr", badPortSet);
92         Assert.assertFalse(status.isSuccess());
93
94         // Port set containing non conventional but parsable port
95         badPortSet.add("1/1");
96         status = switchmgr.addPortsToSubnet("eng", badPortSet);
97         Assert.assertTrue(status.isSuccess());
98
99         // Port set containing non parsable port
100         badPortSet.add("OF1/1");
101         status = switchmgr.addPortsToSubnet("eng", badPortSet);
102         Assert.assertTrue(status.isSuccess());
103     }
104
105     @Test
106     public void testSwitchManagerNodeConnectors() {
107         SwitchManager switchmgr = new SwitchManager();
108         switchmgr.startUp();
109
110         State state;
111         Bandwidth bw;
112         Latency l;
113
114         NodeConnector[] headnc = new NodeConnector[5];
115         NodeConnector[] tailnc = new NodeConnector[5];
116
117         Set<Property> props = new HashSet<Property>();
118         state = new State(State.EDGE_UP);
119         bw = new Bandwidth(Bandwidth.BW100Gbps);
120         l = new Latency(Latency.LATENCY100ns);
121         props.add(state);
122         props.add(bw);
123         props.add(l);
124
125         for (short i = 1; i < 6; i = (short) (i + 1)) {
126
127             headnc[i - 1] = NodeConnectorCreator.createOFNodeConnector(i,
128                     NodeCreator.createOFNode((long) i));
129             tailnc[i - 1] = NodeConnectorCreator
130                     .createOFNodeConnector((short) (i + 10),
131                             NodeCreator.createOFNode((long) (i + 10)));
132             switchmgr.updateNode(headnc[i - 1].getNode(), UpdateType.ADDED,
133                     props);
134             switchmgr.updateNode(tailnc[i - 1].getNode(), UpdateType.ADDED,
135                     props);
136
137             Assert.assertFalse(switchmgr.doesNodeConnectorExist(headnc[i - 1]));
138             switchmgr.updateNodeConnector(headnc[i - 1], UpdateType.ADDED,
139                     props);
140             Assert.assertTrue(switchmgr.doesNodeConnectorExist(headnc[i - 1]));
141
142             Assert.assertFalse(switchmgr.doesNodeConnectorExist(tailnc[i - 1]));
143             switchmgr.updateNodeConnector(tailnc[i - 1], UpdateType.ADDED,
144                     props);
145             Assert.assertTrue(switchmgr.doesNodeConnectorExist(tailnc[i - 1]));
146         }
147
148         for (int i = 0; i < 5; i++) {
149             Property bwProp = switchmgr.getNodeConnectorProp(headnc[i],
150                     Bandwidth.BandwidthPropName);
151             Assert.assertTrue(bwProp.equals(bw));
152             Property latencyProp = switchmgr.getNodeConnectorProp(tailnc[i],
153                     Latency.LatencyPropName);
154             Assert.assertEquals(latencyProp, l);
155         }
156
157         Set<Node> nodes = switchmgr.getNodes();
158         for (int i = 0; i < 5; i++) {
159             if (nodes.contains(headnc[i].getNode()) == true) {
160                 nodes.remove(headnc[i].getNode());
161             }
162
163             if (nodes.contains(tailnc[i].getNode()) == true) {
164                 nodes.remove(tailnc[i].getNode());
165             }
166
167         }
168         Assert.assertTrue(nodes.isEmpty());
169     }
170
171 }