blob: ed0f3c23c727e9d9f7d009b36afb15c540a24bd7 [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;
28import org.onosproject.incubator.net.virtual.VirtualHost;
29import org.onosproject.incubator.net.virtual.VirtualNetwork;
30import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore;
31import org.onosproject.net.ConnectPoint;
32import org.onosproject.net.DeviceId;
33import org.onosproject.net.Host;
34import org.onosproject.net.NetTestTools;
35import org.onosproject.net.TestDeviceParams;
36import org.onosproject.net.host.HostService;
37import org.onosproject.net.intent.FakeIntentManager;
38import org.onosproject.net.intent.TestableIntentService;
39import org.onosproject.store.service.TestStorageService;
40
41import java.util.Collection;
42import java.util.Iterator;
43
44import static org.junit.Assert.*;
45
46/**
47 * Junit tests for VirtualNetworkHostService.
48 */
yoonseon214963b2016-11-21 15:41:07 -080049public class VirtualNetworkHostManagerTest extends TestDeviceParams {
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040050 private final String tenantIdValue1 = "TENANT_ID1";
51
52 private VirtualNetworkManager manager;
53 private DistributedVirtualNetworkStore virtualNetworkManagerStore;
54 private TestableIntentService intentService = new FakeIntentManager();
yoonseonc6a69272017-01-12 18:22:20 -080055 private TestServiceDirectory testDirectory;
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040056
57 @Before
58 public void setUp() throws Exception {
59 virtualNetworkManagerStore = new DistributedVirtualNetworkStore();
60
61 CoreService coreService = new TestCoreService();
yoonseonc6a69272017-01-12 18:22:20 -080062 TestUtils.setField(virtualNetworkManagerStore, "coreService", coreService);
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040063 TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService());
64 virtualNetworkManagerStore.activate();
65
66 manager = new VirtualNetworkManager();
67 manager.store = virtualNetworkManagerStore;
68 manager.intentService = intentService;
69 NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher());
yoonseonc6a69272017-01-12 18:22:20 -080070
71 testDirectory = new TestServiceDirectory();
72 TestUtils.setField(manager, "serviceDirectory", testDirectory);
73
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040074 manager.activate();
75 }
76
77 @After
78 public void tearDown() {
79 virtualNetworkManagerStore.deactivate();
80 manager.deactivate();
81 NetTestTools.injectEventDispatcher(manager, null);
82 }
83
84 /**
85 * Sets up a virtual network with hosts.
86 *
87 * @return virtual network
88 */
89 private VirtualNetwork setupVnet() {
90 manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
91 VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
92 manager.createVirtualHost(virtualNetwork.id(), HID1, MAC1, VLAN1, LOC1, IPSET1);
93 manager.createVirtualHost(virtualNetwork.id(), HID2, MAC2, VLAN2, LOC2, IPSET2);
94 return virtualNetwork;
95 }
96
97 /**
98 * Sets up a virtual network with no hosts.
99 *
100 * @return virtual network
101 */
102 private VirtualNetwork setupEmptyVnet() {
103 manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
104 return manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
105 }
106
107 /**
108 * Tests the getHosts(), getHost(), getHostsByXX(), getConnectedHosts() methods
109 * on a non-empty virtual network.
110 */
111 @Test
112 public void testGetHostsOnNonEmptyVnet() {
113 VirtualNetwork virtualNetwork = setupEmptyVnet();
114 VirtualHost vhost1 = manager.createVirtualHost(virtualNetwork.id(), HID1, MAC1, VLAN1, LOC1, IPSET1);
115 VirtualHost vhost2 = manager.createVirtualHost(virtualNetwork.id(), HID2, MAC2, VLAN2, LOC2, IPSET2);
116 HostService hostService = manager.get(virtualNetwork.id(), HostService.class);
117
118 // test the getHosts() and getHostCount() methods
119 Iterator<Host> itHosts = hostService.getHosts().iterator();
120 assertEquals("The host set size did not match.", 2, Iterators.size(itHosts));
121 assertEquals("The host count did not match.", 2, hostService.getHostCount());
122
123 // test the getHost() method
124 Host testHost = hostService.getHost(HID2);
125 assertEquals("The expected host did not match.", vhost2, testHost);
126
127 // test the getHostsByVlan(...) method
128 Collection<Host> collHost = hostService.getHostsByVlan(VLAN1);
129 assertEquals("The host set size did not match.", 1, collHost.size());
130 assertTrue("The host did not match.", collHost.contains(vhost1));
131
132 // test the getHostsByMac(...) method
133 collHost = hostService.getHostsByMac(MAC2);
134 assertEquals("The host set size did not match.", 1, collHost.size());
135 assertTrue("The host did not match.", collHost.contains(vhost2));
136
137 // test the getHostsByIp(...) method
138 collHost = hostService.getHostsByIp(IP1);
139 assertEquals("The host set size did not match.", 2, collHost.size());
140 collHost = hostService.getHostsByIp(IP2);
141 assertEquals("The host set size did not match.", 1, collHost.size());
142 assertTrue("The host did not match.", collHost.contains(vhost1));
143
144 // test the getConnectedHosts(ConnectPoint) method
145 collHost = hostService.getConnectedHosts(LOC1);
146 assertEquals("The host set size did not match.", 1, collHost.size());
147 assertTrue("The host did not match.", collHost.contains(vhost1));
148
149 // test the getConnectedHosts(DeviceId) method
150 collHost = hostService.getConnectedHosts(DID2);
151 assertEquals("The host set size did not match.", 1, collHost.size());
152 assertTrue("The host did not match.", collHost.contains(vhost2));
153 }
154
155 /**
156 * Tests the getHosts(), getHost(), getHostsByXX(), getConnectedHosts() methods
157 * on an empty virtual network.
158 */
159 @Test
160 public void testGetHostsOnEmptyVnet() {
161 VirtualNetwork virtualNetwork = setupEmptyVnet();
162 HostService hostService = manager.get(virtualNetwork.id(), HostService.class);
163
164 // test the getHosts() and getHostCount() methods
165 Iterator<Host> itHosts = hostService.getHosts().iterator();
166 assertEquals("The host set size did not match.", 0, Iterators.size(itHosts));
167 assertEquals("The host count did not match.", 0, hostService.getHostCount());
168
169 // test the getHost() method
170 Host testHost = hostService.getHost(HID2);
171 assertNull("The host should be null.", testHost);
172
173 // test the getHostsByVlan(...) method
174 Collection<Host> collHost = hostService.getHostsByVlan(VLAN1);
175 assertEquals("The host set size did not match.", 0, collHost.size());
176
177 // test the getHostsByMac(...) method
178 collHost = hostService.getHostsByMac(MAC2);
179 assertEquals("The host set size did not match.", 0, collHost.size());
180
181 // test the getHostsByIp(...) method
182 collHost = hostService.getHostsByIp(IP1);
183 assertEquals("The host set size did not match.", 0, collHost.size());
184
185 // test the getConnectedHosts(ConnectPoint) method
186 collHost = hostService.getConnectedHosts(LOC1);
187 assertEquals("The host set size did not match.", 0, collHost.size());
188
189 // test the getConnectedHosts(DeviceId) method
190 collHost = hostService.getConnectedHosts(DID2);
191 assertEquals("The host set size did not match.", 0, collHost.size());
192 }
193
194 /**
195 * Tests querying for a host using a null host identifier.
196 */
197 @Test(expected = NullPointerException.class)
198 public void testGetHostByNullId() {
199 VirtualNetwork vnet = setupEmptyVnet();
200 HostService hostService = manager.get(vnet.id(), HostService.class);
201
202 hostService.getHost(null);
203 }
204
205 /**
206 * Tests querying for hosts with null mac.
207 */
208 @Test(expected = NullPointerException.class)
209 public void testGetHostsByNullMac() {
210 VirtualNetwork vnet = setupEmptyVnet();
211 HostService hostService = manager.get(vnet.id(), HostService.class);
212
213 hostService.getHostsByMac(null);
214 }
215
216 /**
217 * Tests querying for hosts with null vlan.
218 */
219 @Test(expected = NullPointerException.class)
220 public void testGetHostsByNullVlan() {
221 VirtualNetwork vnet = setupEmptyVnet();
222 HostService hostService = manager.get(vnet.id(), HostService.class);
223
224 hostService.getHostsByVlan(null);
225 }
226
227 /**
228 * Tests querying for hosts with null ip.
229 */
230 @Test(expected = NullPointerException.class)
231 public void testGetHostsByNullIp() {
232 VirtualNetwork vnet = setupVnet();
233 HostService hostService = manager.get(vnet.id(), HostService.class);
234
235 hostService.getHostsByIp(null);
236 }
237
238 /**
239 * Tests querying for connected hosts with null host location (connect point).
240 */
241 @Test(expected = NullPointerException.class)
242 public void testGetConnectedHostsByNullLoc() {
243 VirtualNetwork vnet = setupEmptyVnet();
244 HostService hostService = manager.get(vnet.id(), HostService.class);
245
246 hostService.getConnectedHosts((ConnectPoint) null);
247 }
248
249 /**
250 * Tests querying for connected hosts with null device id.
251 */
252 @Test(expected = NullPointerException.class)
253 public void testGetConnectedHostsByNullDeviceId() {
254 VirtualNetwork vnet = setupVnet();
255 HostService hostService = manager.get(vnet.id(), HostService.class);
256
257 hostService.getConnectedHosts((DeviceId) null);
258 }
259
260}