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