2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.switchmanager.internal;
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
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;
31 public class SwitchManagerTest {
34 public void testSwitchManagerAddRemoveSubnet() throws ConstructionException {
35 SwitchManager switchmgr = new SwitchManager();
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());
50 SubnetConfig subnet = new SubnetConfig("subnet", "10.0.0.254/16", portList);
51 Status addResult = (switchmgr.addSubnet(subnet));
52 Assert.assertTrue(addResult.isSuccess());
54 Status removeResult = (switchmgr.removeSubnet(subnet.getName()));
55 Assert.assertTrue(removeResult.isSuccess());
57 SubnetConfig subnetConfigResult = switchmgr.getSubnetConfig(subnet.getName());
58 Assert.assertNull(subnetConfigResult);
60 subnet = new SubnetConfig("hr", "0.0.0.0", portList);
61 Status status = switchmgr.addSubnet(subnet);
62 Assert.assertFalse(status.isSuccess());
64 subnet = new SubnetConfig("hr", "12.12.12.254/16", null);
65 status = switchmgr.addSubnet(subnet);
66 Assert.assertTrue(status.isSuccess());
71 public void testSwitchManagerAddRemovePortsToSubnet() {
72 SwitchManager switchmgr = new SwitchManager();
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");
80 SubnetConfig subnet = new SubnetConfig("eng", "11.1.1.254/16", portList);
81 Status status = (switchmgr.addSubnet(subnet));
82 Assert.assertTrue(status.isSuccess());
86 List<String> badPortSet = new ArrayList<String>();
87 status = switchmgr.addPortsToSubnet("eng", badPortSet);
88 Assert.assertFalse(status.isSuccess());
90 // Non existant subnet
91 status = switchmgr.removePortsFromSubnet("hr", badPortSet);
92 Assert.assertFalse(status.isSuccess());
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());
99 // Port set containing non parsable port
100 badPortSet.add("OF1/1");
101 status = switchmgr.addPortsToSubnet("eng", badPortSet);
102 Assert.assertTrue(status.isSuccess());
106 public void testSwitchManagerNodeConnectors() {
107 SwitchManager switchmgr = new SwitchManager();
114 NodeConnector[] headnc = new NodeConnector[5];
115 NodeConnector[] tailnc = new NodeConnector[5];
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);
125 for (short i = 1; i < 6; i = (short) (i + 1)) {
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,
134 switchmgr.updateNode(tailnc[i - 1].getNode(), UpdateType.ADDED,
137 Assert.assertFalse(switchmgr.doesNodeConnectorExist(headnc[i - 1]));
138 switchmgr.updateNodeConnector(headnc[i - 1], UpdateType.ADDED,
140 Assert.assertTrue(switchmgr.doesNodeConnectorExist(headnc[i - 1]));
142 Assert.assertFalse(switchmgr.doesNodeConnectorExist(tailnc[i - 1]));
143 switchmgr.updateNodeConnector(tailnc[i - 1], UpdateType.ADDED,
145 Assert.assertTrue(switchmgr.doesNodeConnectorExist(tailnc[i - 1]));
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);
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());
163 if (nodes.contains(tailnc[i].getNode()) == true) {
164 nodes.remove(tailnc[i].getNode());
168 Assert.assertTrue(nodes.isEmpty());