blob: d585138fffe165a826613c5edd1c840d09858ceb [file] [log] [blame]
Claudine Chiu30a8a2a2016-07-14 17:09:04 -04001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
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 */
16
17package org.onosproject.incubator.net.virtual.impl;
18
19import com.google.common.collect.Iterators;
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.junit.TestUtils;
yoonseonc6a69272017-01-12 18:22:20 -080024import org.onlab.osgi.TestServiceDirectory;
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040025import org.onosproject.common.event.impl.TestEventDispatcher;
26import org.onosproject.core.CoreService;
27import org.onosproject.incubator.net.virtual.TenantId;
Harold Huangb7d6b8e2017-04-03 17:13:33 +080028import org.onosproject.incubator.net.virtual.VirtualDevice;
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040029import org.onosproject.incubator.net.virtual.VirtualHost;
30import org.onosproject.incubator.net.virtual.VirtualNetwork;
31import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore;
32import org.onosproject.net.ConnectPoint;
33import org.onosproject.net.DeviceId;
34import org.onosproject.net.Host;
35import org.onosproject.net.NetTestTools;
36import org.onosproject.net.TestDeviceParams;
37import org.onosproject.net.host.HostService;
38import org.onosproject.net.intent.FakeIntentManager;
39import org.onosproject.net.intent.TestableIntentService;
40import org.onosproject.store.service.TestStorageService;
41
42import java.util.Collection;
43import java.util.Iterator;
44
45import static org.junit.Assert.*;
46
47/**
48 * Junit tests for VirtualNetworkHostService.
49 */
yoonseon214963b2016-11-21 15:41:07 -080050public class VirtualNetworkHostManagerTest extends TestDeviceParams {
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040051 private final String tenantIdValue1 = "TENANT_ID1";
52
53 private VirtualNetworkManager manager;
54 private DistributedVirtualNetworkStore virtualNetworkManagerStore;
55 private TestableIntentService intentService = new FakeIntentManager();
yoonseonc6a69272017-01-12 18:22:20 -080056 private TestServiceDirectory testDirectory;
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040057
58 @Before
59 public void setUp() throws Exception {
60 virtualNetworkManagerStore = new DistributedVirtualNetworkStore();
61
62 CoreService coreService = new TestCoreService();
yoonseonc6a69272017-01-12 18:22:20 -080063 TestUtils.setField(virtualNetworkManagerStore, "coreService", coreService);
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040064 TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService());
65 virtualNetworkManagerStore.activate();
66
67 manager = new VirtualNetworkManager();
68 manager.store = virtualNetworkManagerStore;
69 manager.intentService = intentService;
yoonseon322c9c32016-12-07 16:47:02 -080070 manager.coreService = coreService;
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040071 NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher());
yoonseonc6a69272017-01-12 18:22:20 -080072
73 testDirectory = new TestServiceDirectory();
74 TestUtils.setField(manager, "serviceDirectory", testDirectory);
75
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040076 manager.activate();
77 }
78
79 @After
80 public void tearDown() {
81 virtualNetworkManagerStore.deactivate();
82 manager.deactivate();
83 NetTestTools.injectEventDispatcher(manager, null);
84 }
85
86 /**
87 * Sets up a virtual network with hosts.
88 *
89 * @return virtual network
90 */
91 private VirtualNetwork setupVnet() {
92 manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
93 VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
Harold Huangb7d6b8e2017-04-03 17:13:33 +080094
95 VirtualDevice virtualDevice1 =
96 manager.createVirtualDevice(virtualNetwork.id(), DID1);
97 VirtualDevice virtualDevice2 =
98 manager.createVirtualDevice(virtualNetwork.id(), DID2);
99
100 ConnectPoint hostCp1 = new ConnectPoint(DID1, P1);
101 ConnectPoint hostCp2 = new ConnectPoint(DID2, P2);
102 manager.createVirtualPort(virtualNetwork.id(), hostCp1.deviceId(), hostCp1.port(),
103 new ConnectPoint(virtualDevice1.id(), hostCp1.port()));
104 manager.createVirtualPort(virtualNetwork.id(), hostCp2.deviceId(), hostCp2.port(),
105 new ConnectPoint(virtualDevice2.id(), hostCp2.port()));
106
Claudine Chiu30a8a2a2016-07-14 17:09:04 -0400107 manager.createVirtualHost(virtualNetwork.id(), HID1, MAC1, VLAN1, LOC1, IPSET1);
108 manager.createVirtualHost(virtualNetwork.id(), HID2, MAC2, VLAN2, LOC2, IPSET2);
109 return virtualNetwork;
110 }
111
112 /**
113 * Sets up a virtual network with no hosts.
114 *
115 * @return virtual network
116 */
117 private VirtualNetwork setupEmptyVnet() {
118 manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
Harold Huangb7d6b8e2017-04-03 17:13:33 +0800119 VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
120
121 VirtualDevice virtualDevice1 =
122 manager.createVirtualDevice(virtualNetwork.id(), DID1);
123 VirtualDevice virtualDevice2 =
124 manager.createVirtualDevice(virtualNetwork.id(), DID2);
125
126 ConnectPoint hostCp1 = new ConnectPoint(DID1, P1);
127 ConnectPoint hostCp2 = new ConnectPoint(DID2, P2);
128 manager.createVirtualPort(virtualNetwork.id(), hostCp1.deviceId(), hostCp1.port(),
129 new ConnectPoint(virtualDevice1.id(), hostCp1.port()));
130 manager.createVirtualPort(virtualNetwork.id(), hostCp2.deviceId(), hostCp2.port(),
131 new ConnectPoint(virtualDevice2.id(), hostCp2.port()));
132
133 return virtualNetwork;
Claudine Chiu30a8a2a2016-07-14 17:09:04 -0400134 }
135
136 /**
137 * Tests the getHosts(), getHost(), getHostsByXX(), getConnectedHosts() methods
138 * on a non-empty virtual network.
139 */
140 @Test
141 public void testGetHostsOnNonEmptyVnet() {
142 VirtualNetwork virtualNetwork = setupEmptyVnet();
143 VirtualHost vhost1 = manager.createVirtualHost(virtualNetwork.id(), HID1, MAC1, VLAN1, LOC1, IPSET1);
144 VirtualHost vhost2 = manager.createVirtualHost(virtualNetwork.id(), HID2, MAC2, VLAN2, LOC2, IPSET2);
145 HostService hostService = manager.get(virtualNetwork.id(), HostService.class);
146
147 // test the getHosts() and getHostCount() methods
148 Iterator<Host> itHosts = hostService.getHosts().iterator();
149 assertEquals("The host set size did not match.", 2, Iterators.size(itHosts));
150 assertEquals("The host count did not match.", 2, hostService.getHostCount());
151
152 // test the getHost() method
153 Host testHost = hostService.getHost(HID2);
154 assertEquals("The expected host did not match.", vhost2, testHost);
155
156 // test the getHostsByVlan(...) method
157 Collection<Host> collHost = hostService.getHostsByVlan(VLAN1);
158 assertEquals("The host set size did not match.", 1, collHost.size());
159 assertTrue("The host did not match.", collHost.contains(vhost1));
160
161 // test the getHostsByMac(...) method
162 collHost = hostService.getHostsByMac(MAC2);
163 assertEquals("The host set size did not match.", 1, collHost.size());
164 assertTrue("The host did not match.", collHost.contains(vhost2));
165
166 // test the getHostsByIp(...) method
167 collHost = hostService.getHostsByIp(IP1);
168 assertEquals("The host set size did not match.", 2, collHost.size());
169 collHost = hostService.getHostsByIp(IP2);
170 assertEquals("The host set size did not match.", 1, collHost.size());
171 assertTrue("The host did not match.", collHost.contains(vhost1));
172
173 // test the getConnectedHosts(ConnectPoint) method
174 collHost = hostService.getConnectedHosts(LOC1);
175 assertEquals("The host set size did not match.", 1, collHost.size());
176 assertTrue("The host did not match.", collHost.contains(vhost1));
177
178 // test the getConnectedHosts(DeviceId) method
179 collHost = hostService.getConnectedHosts(DID2);
180 assertEquals("The host set size did not match.", 1, collHost.size());
181 assertTrue("The host did not match.", collHost.contains(vhost2));
182 }
183
184 /**
185 * Tests the getHosts(), getHost(), getHostsByXX(), getConnectedHosts() methods
186 * on an empty virtual network.
187 */
188 @Test
189 public void testGetHostsOnEmptyVnet() {
190 VirtualNetwork virtualNetwork = setupEmptyVnet();
191 HostService hostService = manager.get(virtualNetwork.id(), HostService.class);
192
193 // test the getHosts() and getHostCount() methods
194 Iterator<Host> itHosts = hostService.getHosts().iterator();
195 assertEquals("The host set size did not match.", 0, Iterators.size(itHosts));
196 assertEquals("The host count did not match.", 0, hostService.getHostCount());
197
198 // test the getHost() method
199 Host testHost = hostService.getHost(HID2);
200 assertNull("The host should be null.", testHost);
201
202 // test the getHostsByVlan(...) method
203 Collection<Host> collHost = hostService.getHostsByVlan(VLAN1);
204 assertEquals("The host set size did not match.", 0, collHost.size());
205
206 // test the getHostsByMac(...) method
207 collHost = hostService.getHostsByMac(MAC2);
208 assertEquals("The host set size did not match.", 0, collHost.size());
209
210 // test the getHostsByIp(...) method
211 collHost = hostService.getHostsByIp(IP1);
212 assertEquals("The host set size did not match.", 0, collHost.size());
213
214 // test the getConnectedHosts(ConnectPoint) method
215 collHost = hostService.getConnectedHosts(LOC1);
216 assertEquals("The host set size did not match.", 0, collHost.size());
217
218 // test the getConnectedHosts(DeviceId) method
219 collHost = hostService.getConnectedHosts(DID2);
220 assertEquals("The host set size did not match.", 0, collHost.size());
221 }
222
223 /**
224 * Tests querying for a host using a null host identifier.
225 */
226 @Test(expected = NullPointerException.class)
227 public void testGetHostByNullId() {
228 VirtualNetwork vnet = setupEmptyVnet();
229 HostService hostService = manager.get(vnet.id(), HostService.class);
230
231 hostService.getHost(null);
232 }
233
234 /**
235 * Tests querying for hosts with null mac.
236 */
237 @Test(expected = NullPointerException.class)
238 public void testGetHostsByNullMac() {
239 VirtualNetwork vnet = setupEmptyVnet();
240 HostService hostService = manager.get(vnet.id(), HostService.class);
241
242 hostService.getHostsByMac(null);
243 }
244
245 /**
246 * Tests querying for hosts with null vlan.
247 */
248 @Test(expected = NullPointerException.class)
249 public void testGetHostsByNullVlan() {
250 VirtualNetwork vnet = setupEmptyVnet();
251 HostService hostService = manager.get(vnet.id(), HostService.class);
252
253 hostService.getHostsByVlan(null);
254 }
255
256 /**
257 * Tests querying for hosts with null ip.
258 */
259 @Test(expected = NullPointerException.class)
260 public void testGetHostsByNullIp() {
261 VirtualNetwork vnet = setupVnet();
262 HostService hostService = manager.get(vnet.id(), HostService.class);
263
264 hostService.getHostsByIp(null);
265 }
266
267 /**
268 * Tests querying for connected hosts with null host location (connect point).
269 */
270 @Test(expected = NullPointerException.class)
271 public void testGetConnectedHostsByNullLoc() {
272 VirtualNetwork vnet = setupEmptyVnet();
273 HostService hostService = manager.get(vnet.id(), HostService.class);
274
275 hostService.getConnectedHosts((ConnectPoint) null);
276 }
277
278 /**
279 * Tests querying for connected hosts with null device id.
280 */
281 @Test(expected = NullPointerException.class)
282 public void testGetConnectedHostsByNullDeviceId() {
283 VirtualNetwork vnet = setupVnet();
284 HostService hostService = manager.get(vnet.id(), HostService.class);
285
286 hostService.getConnectedHosts((DeviceId) null);
287 }
288
289}