blob: 01611f345bfc56dd7f509f04e53d748c3209161b [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;
yoonseon322c9c32016-12-07 16:47:02 -080069 manager.coreService = coreService;
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040070 NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher());
yoonseonc6a69272017-01-12 18:22:20 -080071
72 testDirectory = new TestServiceDirectory();
73 TestUtils.setField(manager, "serviceDirectory", testDirectory);
74
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040075 manager.activate();
76 }
77
78 @After
79 public void tearDown() {
80 virtualNetworkManagerStore.deactivate();
81 manager.deactivate();
82 NetTestTools.injectEventDispatcher(manager, null);
83 }
84
85 /**
86 * Sets up a virtual network with hosts.
87 *
88 * @return virtual network
89 */
90 private VirtualNetwork setupVnet() {
91 manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
92 VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
93 manager.createVirtualHost(virtualNetwork.id(), HID1, MAC1, VLAN1, LOC1, IPSET1);
94 manager.createVirtualHost(virtualNetwork.id(), HID2, MAC2, VLAN2, LOC2, IPSET2);
95 return virtualNetwork;
96 }
97
98 /**
99 * Sets up a virtual network with no hosts.
100 *
101 * @return virtual network
102 */
103 private VirtualNetwork setupEmptyVnet() {
104 manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
105 return manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
106 }
107
108 /**
109 * Tests the getHosts(), getHost(), getHostsByXX(), getConnectedHosts() methods
110 * on a non-empty virtual network.
111 */
112 @Test
113 public void testGetHostsOnNonEmptyVnet() {
114 VirtualNetwork virtualNetwork = setupEmptyVnet();
115 VirtualHost vhost1 = manager.createVirtualHost(virtualNetwork.id(), HID1, MAC1, VLAN1, LOC1, IPSET1);
116 VirtualHost vhost2 = manager.createVirtualHost(virtualNetwork.id(), HID2, MAC2, VLAN2, LOC2, IPSET2);
117 HostService hostService = manager.get(virtualNetwork.id(), HostService.class);
118
119 // test the getHosts() and getHostCount() methods
120 Iterator<Host> itHosts = hostService.getHosts().iterator();
121 assertEquals("The host set size did not match.", 2, Iterators.size(itHosts));
122 assertEquals("The host count did not match.", 2, hostService.getHostCount());
123
124 // test the getHost() method
125 Host testHost = hostService.getHost(HID2);
126 assertEquals("The expected host did not match.", vhost2, testHost);
127
128 // test the getHostsByVlan(...) method
129 Collection<Host> collHost = hostService.getHostsByVlan(VLAN1);
130 assertEquals("The host set size did not match.", 1, collHost.size());
131 assertTrue("The host did not match.", collHost.contains(vhost1));
132
133 // test the getHostsByMac(...) method
134 collHost = hostService.getHostsByMac(MAC2);
135 assertEquals("The host set size did not match.", 1, collHost.size());
136 assertTrue("The host did not match.", collHost.contains(vhost2));
137
138 // test the getHostsByIp(...) method
139 collHost = hostService.getHostsByIp(IP1);
140 assertEquals("The host set size did not match.", 2, collHost.size());
141 collHost = hostService.getHostsByIp(IP2);
142 assertEquals("The host set size did not match.", 1, collHost.size());
143 assertTrue("The host did not match.", collHost.contains(vhost1));
144
145 // test the getConnectedHosts(ConnectPoint) method
146 collHost = hostService.getConnectedHosts(LOC1);
147 assertEquals("The host set size did not match.", 1, collHost.size());
148 assertTrue("The host did not match.", collHost.contains(vhost1));
149
150 // test the getConnectedHosts(DeviceId) method
151 collHost = hostService.getConnectedHosts(DID2);
152 assertEquals("The host set size did not match.", 1, collHost.size());
153 assertTrue("The host did not match.", collHost.contains(vhost2));
154 }
155
156 /**
157 * Tests the getHosts(), getHost(), getHostsByXX(), getConnectedHosts() methods
158 * on an empty virtual network.
159 */
160 @Test
161 public void testGetHostsOnEmptyVnet() {
162 VirtualNetwork virtualNetwork = setupEmptyVnet();
163 HostService hostService = manager.get(virtualNetwork.id(), HostService.class);
164
165 // test the getHosts() and getHostCount() methods
166 Iterator<Host> itHosts = hostService.getHosts().iterator();
167 assertEquals("The host set size did not match.", 0, Iterators.size(itHosts));
168 assertEquals("The host count did not match.", 0, hostService.getHostCount());
169
170 // test the getHost() method
171 Host testHost = hostService.getHost(HID2);
172 assertNull("The host should be null.", testHost);
173
174 // test the getHostsByVlan(...) method
175 Collection<Host> collHost = hostService.getHostsByVlan(VLAN1);
176 assertEquals("The host set size did not match.", 0, collHost.size());
177
178 // test the getHostsByMac(...) method
179 collHost = hostService.getHostsByMac(MAC2);
180 assertEquals("The host set size did not match.", 0, collHost.size());
181
182 // test the getHostsByIp(...) method
183 collHost = hostService.getHostsByIp(IP1);
184 assertEquals("The host set size did not match.", 0, collHost.size());
185
186 // test the getConnectedHosts(ConnectPoint) method
187 collHost = hostService.getConnectedHosts(LOC1);
188 assertEquals("The host set size did not match.", 0, collHost.size());
189
190 // test the getConnectedHosts(DeviceId) method
191 collHost = hostService.getConnectedHosts(DID2);
192 assertEquals("The host set size did not match.", 0, collHost.size());
193 }
194
195 /**
196 * Tests querying for a host using a null host identifier.
197 */
198 @Test(expected = NullPointerException.class)
199 public void testGetHostByNullId() {
200 VirtualNetwork vnet = setupEmptyVnet();
201 HostService hostService = manager.get(vnet.id(), HostService.class);
202
203 hostService.getHost(null);
204 }
205
206 /**
207 * Tests querying for hosts with null mac.
208 */
209 @Test(expected = NullPointerException.class)
210 public void testGetHostsByNullMac() {
211 VirtualNetwork vnet = setupEmptyVnet();
212 HostService hostService = manager.get(vnet.id(), HostService.class);
213
214 hostService.getHostsByMac(null);
215 }
216
217 /**
218 * Tests querying for hosts with null vlan.
219 */
220 @Test(expected = NullPointerException.class)
221 public void testGetHostsByNullVlan() {
222 VirtualNetwork vnet = setupEmptyVnet();
223 HostService hostService = manager.get(vnet.id(), HostService.class);
224
225 hostService.getHostsByVlan(null);
226 }
227
228 /**
229 * Tests querying for hosts with null ip.
230 */
231 @Test(expected = NullPointerException.class)
232 public void testGetHostsByNullIp() {
233 VirtualNetwork vnet = setupVnet();
234 HostService hostService = manager.get(vnet.id(), HostService.class);
235
236 hostService.getHostsByIp(null);
237 }
238
239 /**
240 * Tests querying for connected hosts with null host location (connect point).
241 */
242 @Test(expected = NullPointerException.class)
243 public void testGetConnectedHostsByNullLoc() {
244 VirtualNetwork vnet = setupEmptyVnet();
245 HostService hostService = manager.get(vnet.id(), HostService.class);
246
247 hostService.getConnectedHosts((ConnectPoint) null);
248 }
249
250 /**
251 * Tests querying for connected hosts with null device id.
252 */
253 @Test(expected = NullPointerException.class)
254 public void testGetConnectedHostsByNullDeviceId() {
255 VirtualNetwork vnet = setupVnet();
256 HostService hostService = manager.get(vnet.id(), HostService.class);
257
258 hostService.getConnectedHosts((DeviceId) null);
259 }
260
261}