blob: 7e98c7b2744d7a3bdbe796fc167a4661d84be6cd [file] [log] [blame]
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +09001/*
Brian O'Connor80dff972017-08-03 22:46:30 -07002 * Copyright 2017-present Open Networking Foundation
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +09003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.opencord.cordvtn.impl;
17
18import com.google.common.collect.Lists;
Hyunsun Moon41eef922017-04-26 14:00:01 +090019import com.google.common.util.concurrent.MoreExecutors;
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090020import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090023import org.onlab.junit.TestUtils;
24import org.onosproject.cluster.ClusterServiceAdapter;
25import org.onosproject.cluster.LeadershipServiceAdapter;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreServiceAdapter;
28import org.onosproject.core.DefaultApplicationId;
29import org.onosproject.event.Event;
30import org.onosproject.net.Device;
31import org.onosproject.net.config.NetworkConfigServiceAdapter;
32import org.onosproject.store.service.TestStorageService;
33import org.opencord.cordvtn.api.node.CordVtnNode;
34import org.opencord.cordvtn.api.node.CordVtnNodeEvent;
35import org.opencord.cordvtn.api.node.CordVtnNodeListener;
36
37import java.util.List;
38
39import static org.junit.Assert.assertEquals;
40import static org.junit.Assert.assertTrue;
41import static org.opencord.cordvtn.api.node.CordVtnNodeEvent.Type.*;
42import static org.opencord.cordvtn.api.node.CordVtnNodeState.COMPLETE;
43import static org.opencord.cordvtn.api.node.CordVtnNodeState.INIT;
44import static org.opencord.cordvtn.impl.DefaultCordVtnNode.updatedState;
45
46/**
47 * Unit tests for {@link CordVtnNodeManager}.
48 */
49public class CordVtnNodeManagerTest extends CordVtnNodeTest {
50
51 private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
52 private static final String ERR_SIZE = "Number of nodes did not match";
53 private static final String ERR_NODE = "Node did not match";
54 private static final String ERR_NOT_FOUND = "Node did not exist";
55 private static final String ERR_STATE = "Node state did not match";
56
57 private static final String HOSTNAME_1 = "node-01";
58 private static final String HOSTNAME_2 = "node-02";
59 private static final String HOSTNAME_3 = "node-03";
60
61 private static final Device OF_DEVICE_1 = createDevice(1);
62 private static final Device OF_DEVICE_2 = createDevice(2);
63 private static final Device OF_DEVICE_3 = createDevice(3);
64
65 private static final CordVtnNode NODE_1 = createNode(HOSTNAME_1, OF_DEVICE_1, INIT);
66 private static final CordVtnNode NODE_2 = createNode(HOSTNAME_2, OF_DEVICE_2, INIT);
67 private static final CordVtnNode NODE_3 = createNode(HOSTNAME_3, OF_DEVICE_3, COMPLETE);
68
69 private final TestCordVtnNodeListener testListener = new TestCordVtnNodeListener();
70
71 private CordVtnNodeManager target;
72 private DistributedCordVtnNodeStore nodeStore;
73
74 @Before
75 public void setUp() throws Exception {
76 nodeStore = new DistributedCordVtnNodeStore();
77 TestUtils.setField(nodeStore, "coreService", new TestCoreService());
78 TestUtils.setField(nodeStore, "storageService", new TestStorageService());
Hyunsun Moon41eef922017-04-26 14:00:01 +090079 TestUtils.setField(nodeStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090080 nodeStore.activate();
81
82 nodeStore.createNode(NODE_2);
83 nodeStore.createNode(NODE_3);
84
85 target = new CordVtnNodeManager();
86 target.coreService = new TestCoreService();
87 target.leadershipService = new TestLeadershipService();
88 target.configService = new TestConfigService();
89 target.clusterService = new TestClusterService();
90 target.nodeStore = nodeStore;
91
92 target.activate();
93 target.addListener(testListener);
94 clearEvents();
95 }
96
97 @After
98 public void tearDown() {
99 target.removeListener(testListener);
100 nodeStore.deactivate();
101 target.deactivate();
102 nodeStore = null;
103 target = null;
104 }
105
106 /**
107 * Checks getting nodes returns correct set of the existing nodes.
108 */
109 @Test
110 public void testGetNodes() {
111 assertEquals(ERR_SIZE, 2, target.nodes().size());
112 assertTrue(ERR_NOT_FOUND, target.nodes().contains(NODE_2));
113 assertTrue(ERR_NOT_FOUND, target.nodes().contains(NODE_3));
114 }
115
116 /**
117 * Checks getting complete nodes method returns correct set of the
118 * existing complete nodes.
119 */
120 @Test
121 public void testGetCompleteNodes() {
122 assertEquals(ERR_SIZE, 1, target.completeNodes().size());
123 assertTrue(ERR_NOT_FOUND, target.completeNodes().contains(NODE_3));
124 }
125
126 /**
127 * Checks if getting node by hostname returns correct node.
128 */
129 @Test
130 public void testGetNodeByHostname() {
131 CordVtnNode node = target.node(HOSTNAME_2);
132 assertEquals(ERR_NODE, NODE_2, node);
133 }
134
135 /**
136 * Checks if getting node by device ID returns correct node.
137 */
138 @Test
139 public void testGetNodeByDeviceId() {
140 CordVtnNode node = target.node(OF_DEVICE_2.id());
141 assertEquals(ERR_NODE, NODE_2, node);
142 }
143
144 /**
145 * Checks if node creation with null fails with exception.
146 */
147 @Test(expected = NullPointerException.class)
148 public void testCreateNullNode() {
149 target.createNode(null);
150 }
151
152 /**
153 * Checks if node removal with null hostname fails with exception.
154 */
155 @Test(expected = IllegalArgumentException.class)
156 public void testRemoveNullHostname() {
157 target.removeNode(null);
158 }
159
160 /**
161 * Checks if node update with null fails with exception.
162 */
163 @Test(expected = NullPointerException.class)
164 public void testUpdateNullNode() {
165 target.updateNode(null);
166 }
167
168 /**
169 * Checks if duplicate node creation fails with exception.
170 */
171 @Test(expected = IllegalArgumentException.class)
172 public void testCreateDuplicateNode() {
173 target.createNode(NODE_1);
174 target.createNode(NODE_1);
175 }
176
177 /**
178 * Checks if unregistered node update fails with exception.
179 */
180 @Test(expected = IllegalArgumentException.class)
181 public void testUpdateUnregisteredNode() {
182 target.updateNode(NODE_1);
183 }
184
185 /**
186 * Checks the basic node creation and removal.
187 */
188 @Test
189 public void testCreateAndRemoveNode() {
190 target.createNode(NODE_1);
191 assertEquals(ERR_SIZE, 3, target.nodes().size());
192 assertTrue(target.node(OF_DEVICE_1.id()) != null);
193
194 target.removeNode(HOSTNAME_1);
195 assertEquals(ERR_SIZE, 2, target.nodes().size());
196 assertTrue(target.node(OF_DEVICE_1.id()) == null);
197
198 validateEvents(NODE_CREATED, NODE_REMOVED);
199 }
200
201 /**
202 * Checks if node complete event is triggered when the node state changes
203 * to COMPLETE.
204 */
205 @Test
206 public void testUpdateNodeStateComplete() {
207 target.updateNode(updatedState(NODE_2, COMPLETE));
208 CordVtnNode node = target.node(HOSTNAME_2);
209 assertEquals(ERR_STATE, COMPLETE, node.state());
210
211 validateEvents(NODE_UPDATED, NODE_COMPLETE);
212 }
213
214 /**
215 * Checks if the node incomplete event is triggered when the node state
216 * falls back from COMPLETE to INIT.
217 */
218 @Test
219 public void testUpdateNodeStateIncomplete() {
220 target.updateNode(updatedState(NODE_3, INIT));
221 CordVtnNode node = target.node(HOSTNAME_3);
222 assertEquals(ERR_STATE, INIT, node.state());
223
224 validateEvents(NODE_UPDATED, NODE_INCOMPLETE);
225 }
226
227 private void clearEvents() {
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +0900228 testListener.events.clear();
229 }
230
231 private void validateEvents(Enum... types) {
Hyunsun Moon41eef922017-04-26 14:00:01 +0900232 int i = 0;
233 assertEquals("Number of events did not match", types.length, testListener.events.size());
234 for (Event event : testListener.events) {
235 assertEquals("Incorrect event received", types[i], event.type());
236 i++;
237 }
238 testListener.events.clear();
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +0900239 }
240
241 private static class TestCordVtnNodeListener implements CordVtnNodeListener {
242 private List<CordVtnNodeEvent> events = Lists.newArrayList();
243
244 @Override
245 public void event(CordVtnNodeEvent event) {
246 events.add(event);
247 }
248 }
249
250 private static class TestCoreService extends CoreServiceAdapter {
251
252 @Override
253 public ApplicationId registerApplication(String name) {
254 return TEST_APP_ID;
255 }
256 }
257
258 private class TestConfigService extends NetworkConfigServiceAdapter {
259
260 }
261
262 private class TestClusterService extends ClusterServiceAdapter {
263
264 }
265
266 private static class TestLeadershipService extends LeadershipServiceAdapter {
267
268 }
269}