Claudine Chiu
Committed by Gerrit Code Review

ONOS-2184 - add virtual network host service.

Change-Id: I06922cb1fbd5d351d62215ae2d84b85e15853585
1 +/*
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 +
17 +package org.onosproject.incubator.net.virtual.impl;
18 +
19 +import org.onlab.packet.IpAddress;
20 +import org.onlab.packet.MacAddress;
21 +import org.onlab.packet.VlanId;
22 +import org.onosproject.event.AbstractListenerManager;
23 +import org.onosproject.incubator.net.virtual.VirtualHost;
24 +import org.onosproject.incubator.net.virtual.VirtualNetwork;
25 +import org.onosproject.incubator.net.virtual.VirtualNetworkService;
26 +import org.onosproject.net.ConnectPoint;
27 +import org.onosproject.net.DeviceId;
28 +import org.onosproject.net.Host;
29 +import org.onosproject.net.HostId;
30 +import org.onosproject.net.host.HostEvent;
31 +import org.onosproject.net.host.HostListener;
32 +import org.onosproject.net.host.HostService;
33 +
34 +import java.util.Collection;
35 +import java.util.Objects;
36 +import java.util.Optional;
37 +import java.util.Set;
38 +import java.util.function.Predicate;
39 +import java.util.stream.Collectors;
40 +
41 +import static com.google.common.base.Preconditions.checkNotNull;
42 +
43 +/**
44 + * Host service implementation built on the virtual network service.
45 + */
46 +public class VirtualNetworkHostService extends AbstractListenerManager<HostEvent, HostListener>
47 + implements HostService, VnetService {
48 +
49 + private static final String NETWORK_NULL = "Network ID cannot be null";
50 + private static final String HOST_NULL = "Host ID cannot be null";
51 +
52 + private final VirtualNetwork network;
53 + private final VirtualNetworkService manager;
54 +
55 + /**
56 + * Creates a new virtual network host service object.
57 + *
58 + * @param virtualNetworkManager virtual network manager service
59 + * @param network virtual network
60 + */
61 + public VirtualNetworkHostService(VirtualNetworkService virtualNetworkManager, VirtualNetwork network) {
62 + checkNotNull(network, NETWORK_NULL);
63 + this.network = network;
64 + this.manager = virtualNetworkManager;
65 + }
66 +
67 +
68 + @Override
69 + public int getHostCount() {
70 + return manager.getVirtualHosts(this.network.id()).size();
71 + }
72 +
73 + @Override
74 + public Iterable<Host> getHosts() {
75 + return getHostsColl();
76 + }
77 +
78 + @Override
79 + public Host getHost(HostId hostId) {
80 + checkNotNull(hostId, HOST_NULL);
81 + Optional<VirtualHost> foundHost = manager.getVirtualHosts(this.network.id())
82 + .stream()
83 + .filter(host -> hostId.equals(host.id()))
84 + .findFirst();
85 + if (foundHost.isPresent()) {
86 + return foundHost.get();
87 + }
88 + return null;
89 + }
90 +
91 + /**
92 + * Gets a collection of virtual hosts.
93 + *
94 + * @return collection of virtual hosts.
95 + */
96 + private Collection<Host> getHostsColl() {
97 + return manager.getVirtualHosts(this.network.id()).stream().collect(Collectors.toSet());
98 + }
99 +
100 + /**
101 + * Filters specified collection.
102 + *
103 + * @param collection collection of hosts to filter
104 + * @param predicate condition to filter on
105 + * @return collection of virtual hosts that satisfy the filter condition
106 + */
107 + private Set<Host> filter(Collection<Host> collection, Predicate<Host> predicate) {
108 + return collection.stream().filter(predicate).collect(Collectors.toSet());
109 + }
110 +
111 + @Override
112 + public Set<Host> getHostsByVlan(VlanId vlanId) {
113 + checkNotNull(vlanId, "VLAN identifier cannot be null");
114 + return filter(getHostsColl(), host -> Objects.equals(host.vlan(), vlanId));
115 + }
116 +
117 + @Override
118 + public Set<Host> getHostsByMac(MacAddress mac) {
119 + checkNotNull(mac, "MAC address cannot be null");
120 + return filter(getHostsColl(), host -> Objects.equals(host.mac(), mac));
121 + }
122 +
123 + @Override
124 + public Set<Host> getHostsByIp(IpAddress ip) {
125 + checkNotNull(ip, "IP address cannot be null");
126 + return filter(getHostsColl(), host -> host.ipAddresses().contains(ip));
127 + }
128 +
129 + @Override
130 + public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
131 + checkNotNull(connectPoint, "Connect point cannot be null");
132 + return filter(getHostsColl(), host -> host.location().equals(connectPoint));
133 + }
134 +
135 + @Override
136 + public Set<Host> getConnectedHosts(DeviceId deviceId) {
137 + checkNotNull(deviceId, "Device identifier cannot be null");
138 + return filter(getHostsColl(), host -> host.location().deviceId().equals(deviceId));
139 + }
140 +
141 + @Override
142 + public void startMonitoringIp(IpAddress ip) {
143 + //TODO check what needs to be done here
144 + }
145 +
146 + @Override
147 + public void stopMonitoringIp(IpAddress ip) {
148 + //TODO check what needs to be done here
149 + }
150 +
151 + @Override
152 + public void requestMac(IpAddress ip) {
153 + //TODO check what needs to be done here
154 + }
155 +
156 + @Override
157 + public VirtualNetwork network() {
158 + return network;
159 + }
160 +}
...@@ -52,6 +52,7 @@ import org.onosproject.net.Link; ...@@ -52,6 +52,7 @@ import org.onosproject.net.Link;
52 import org.onosproject.net.Port; 52 import org.onosproject.net.Port;
53 import org.onosproject.net.PortNumber; 53 import org.onosproject.net.PortNumber;
54 import org.onosproject.net.device.DeviceService; 54 import org.onosproject.net.device.DeviceService;
55 +import org.onosproject.net.host.HostService;
55 import org.onosproject.net.intent.IntentEvent; 56 import org.onosproject.net.intent.IntentEvent;
56 import org.onosproject.net.intent.IntentListener; 57 import org.onosproject.net.intent.IntentListener;
57 import org.onosproject.net.intent.IntentService; 58 import org.onosproject.net.intent.IntentService;
...@@ -338,6 +339,8 @@ public class VirtualNetworkManager ...@@ -338,6 +339,8 @@ public class VirtualNetworkManager
338 service = new VirtualNetworkTopologyService(this, network); 339 service = new VirtualNetworkTopologyService(this, network);
339 } else if (serviceKey.serviceClass.equals(IntentService.class)) { 340 } else if (serviceKey.serviceClass.equals(IntentService.class)) {
340 service = new VirtualNetworkIntentService(this, network, new DefaultServiceDirectory()); 341 service = new VirtualNetworkIntentService(this, network, new DefaultServiceDirectory());
342 + } else if (serviceKey.serviceClass.equals(HostService.class)) {
343 + service = new VirtualNetworkHostService(this, network);
341 } else { 344 } else {
342 return null; 345 return null;
343 } 346 }
......
1 +/*
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 +
17 +package org.onosproject.incubator.net.virtual.impl;
18 +
19 +import org.onosproject.core.CoreServiceAdapter;
20 +import org.onosproject.core.IdGenerator;
21 +import java.util.concurrent.atomic.AtomicLong;
22 +
23 +/**
24 + * Core service test class.
25 + */
26 +class TestCoreService extends CoreServiceAdapter {
27 +
28 + @Override
29 + public IdGenerator getIdGenerator(String topic) {
30 + return new IdGenerator() {
31 + private AtomicLong counter = new AtomicLong(0);
32 +
33 + @Override
34 + public long getNewId() {
35 + return counter.getAndIncrement();
36 + }
37 + };
38 + }
39 +}
1 +/*
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 +
17 +package org.onosproject.incubator.net.virtual.impl;
18 +
19 +import com.google.common.collect.Iterators;
20 +import org.junit.After;
21 +import org.junit.Before;
22 +import org.junit.Test;
23 +import org.onlab.junit.TestUtils;
24 +import org.onosproject.common.event.impl.TestEventDispatcher;
25 +import org.onosproject.core.CoreService;
26 +import org.onosproject.incubator.net.virtual.TenantId;
27 +import org.onosproject.incubator.net.virtual.VirtualHost;
28 +import org.onosproject.incubator.net.virtual.VirtualNetwork;
29 +import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore;
30 +import org.onosproject.net.ConnectPoint;
31 +import org.onosproject.net.DeviceId;
32 +import org.onosproject.net.Host;
33 +import org.onosproject.net.NetTestTools;
34 +import org.onosproject.net.TestDeviceParams;
35 +import org.onosproject.net.host.HostService;
36 +import org.onosproject.net.intent.FakeIntentManager;
37 +import org.onosproject.net.intent.TestableIntentService;
38 +import org.onosproject.store.service.TestStorageService;
39 +
40 +import java.util.Collection;
41 +import java.util.Iterator;
42 +
43 +import static org.junit.Assert.*;
44 +
45 +/**
46 + * Junit tests for VirtualNetworkHostService.
47 + */
48 +public class VirtualNetworkHostServiceTest extends TestDeviceParams {
49 + private final String tenantIdValue1 = "TENANT_ID1";
50 +
51 + private VirtualNetworkManager manager;
52 + private DistributedVirtualNetworkStore virtualNetworkManagerStore;
53 + private TestableIntentService intentService = new FakeIntentManager();
54 +
55 + @Before
56 + public void setUp() throws Exception {
57 + virtualNetworkManagerStore = new DistributedVirtualNetworkStore();
58 +
59 + CoreService coreService = new TestCoreService();
60 + virtualNetworkManagerStore.setCoreService(coreService);
61 + TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService());
62 + virtualNetworkManagerStore.activate();
63 +
64 + manager = new VirtualNetworkManager();
65 + manager.store = virtualNetworkManagerStore;
66 + manager.intentService = intentService;
67 + NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher());
68 + manager.activate();
69 + }
70 +
71 + @After
72 + public void tearDown() {
73 + virtualNetworkManagerStore.deactivate();
74 + manager.deactivate();
75 + NetTestTools.injectEventDispatcher(manager, null);
76 + }
77 +
78 + /**
79 + * Sets up a virtual network with hosts.
80 + *
81 + * @return virtual network
82 + */
83 + private VirtualNetwork setupVnet() {
84 + manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
85 + VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
86 + manager.createVirtualHost(virtualNetwork.id(), HID1, MAC1, VLAN1, LOC1, IPSET1);
87 + manager.createVirtualHost(virtualNetwork.id(), HID2, MAC2, VLAN2, LOC2, IPSET2);
88 + return virtualNetwork;
89 + }
90 +
91 + /**
92 + * Sets up a virtual network with no hosts.
93 + *
94 + * @return virtual network
95 + */
96 + private VirtualNetwork setupEmptyVnet() {
97 + manager.registerTenantId(TenantId.tenantId(tenantIdValue1));
98 + return manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1));
99 + }
100 +
101 + /**
102 + * Tests the getHosts(), getHost(), getHostsByXX(), getConnectedHosts() methods
103 + * on a non-empty virtual network.
104 + */
105 + @Test
106 + public void testGetHostsOnNonEmptyVnet() {
107 + VirtualNetwork virtualNetwork = setupEmptyVnet();
108 + VirtualHost vhost1 = manager.createVirtualHost(virtualNetwork.id(), HID1, MAC1, VLAN1, LOC1, IPSET1);
109 + VirtualHost vhost2 = manager.createVirtualHost(virtualNetwork.id(), HID2, MAC2, VLAN2, LOC2, IPSET2);
110 + HostService hostService = manager.get(virtualNetwork.id(), HostService.class);
111 +
112 + // test the getHosts() and getHostCount() methods
113 + Iterator<Host> itHosts = hostService.getHosts().iterator();
114 + assertEquals("The host set size did not match.", 2, Iterators.size(itHosts));
115 + assertEquals("The host count did not match.", 2, hostService.getHostCount());
116 +
117 + // test the getHost() method
118 + Host testHost = hostService.getHost(HID2);
119 + assertEquals("The expected host did not match.", vhost2, testHost);
120 +
121 + // test the getHostsByVlan(...) method
122 + Collection<Host> collHost = hostService.getHostsByVlan(VLAN1);
123 + assertEquals("The host set size did not match.", 1, collHost.size());
124 + assertTrue("The host did not match.", collHost.contains(vhost1));
125 +
126 + // test the getHostsByMac(...) method
127 + collHost = hostService.getHostsByMac(MAC2);
128 + assertEquals("The host set size did not match.", 1, collHost.size());
129 + assertTrue("The host did not match.", collHost.contains(vhost2));
130 +
131 + // test the getHostsByIp(...) method
132 + collHost = hostService.getHostsByIp(IP1);
133 + assertEquals("The host set size did not match.", 2, collHost.size());
134 + collHost = hostService.getHostsByIp(IP2);
135 + assertEquals("The host set size did not match.", 1, collHost.size());
136 + assertTrue("The host did not match.", collHost.contains(vhost1));
137 +
138 + // test the getConnectedHosts(ConnectPoint) method
139 + collHost = hostService.getConnectedHosts(LOC1);
140 + assertEquals("The host set size did not match.", 1, collHost.size());
141 + assertTrue("The host did not match.", collHost.contains(vhost1));
142 +
143 + // test the getConnectedHosts(DeviceId) method
144 + collHost = hostService.getConnectedHosts(DID2);
145 + assertEquals("The host set size did not match.", 1, collHost.size());
146 + assertTrue("The host did not match.", collHost.contains(vhost2));
147 + }
148 +
149 + /**
150 + * Tests the getHosts(), getHost(), getHostsByXX(), getConnectedHosts() methods
151 + * on an empty virtual network.
152 + */
153 + @Test
154 + public void testGetHostsOnEmptyVnet() {
155 + VirtualNetwork virtualNetwork = setupEmptyVnet();
156 + HostService hostService = manager.get(virtualNetwork.id(), HostService.class);
157 +
158 + // test the getHosts() and getHostCount() methods
159 + Iterator<Host> itHosts = hostService.getHosts().iterator();
160 + assertEquals("The host set size did not match.", 0, Iterators.size(itHosts));
161 + assertEquals("The host count did not match.", 0, hostService.getHostCount());
162 +
163 + // test the getHost() method
164 + Host testHost = hostService.getHost(HID2);
165 + assertNull("The host should be null.", testHost);
166 +
167 + // test the getHostsByVlan(...) method
168 + Collection<Host> collHost = hostService.getHostsByVlan(VLAN1);
169 + assertEquals("The host set size did not match.", 0, collHost.size());
170 +
171 + // test the getHostsByMac(...) method
172 + collHost = hostService.getHostsByMac(MAC2);
173 + assertEquals("The host set size did not match.", 0, collHost.size());
174 +
175 + // test the getHostsByIp(...) method
176 + collHost = hostService.getHostsByIp(IP1);
177 + assertEquals("The host set size did not match.", 0, collHost.size());
178 +
179 + // test the getConnectedHosts(ConnectPoint) method
180 + collHost = hostService.getConnectedHosts(LOC1);
181 + assertEquals("The host set size did not match.", 0, collHost.size());
182 +
183 + // test the getConnectedHosts(DeviceId) method
184 + collHost = hostService.getConnectedHosts(DID2);
185 + assertEquals("The host set size did not match.", 0, collHost.size());
186 + }
187 +
188 + /**
189 + * Tests querying for a host using a null host identifier.
190 + */
191 + @Test(expected = NullPointerException.class)
192 + public void testGetHostByNullId() {
193 + VirtualNetwork vnet = setupEmptyVnet();
194 + HostService hostService = manager.get(vnet.id(), HostService.class);
195 +
196 + hostService.getHost(null);
197 + }
198 +
199 + /**
200 + * Tests querying for hosts with null mac.
201 + */
202 + @Test(expected = NullPointerException.class)
203 + public void testGetHostsByNullMac() {
204 + VirtualNetwork vnet = setupEmptyVnet();
205 + HostService hostService = manager.get(vnet.id(), HostService.class);
206 +
207 + hostService.getHostsByMac(null);
208 + }
209 +
210 + /**
211 + * Tests querying for hosts with null vlan.
212 + */
213 + @Test(expected = NullPointerException.class)
214 + public void testGetHostsByNullVlan() {
215 + VirtualNetwork vnet = setupEmptyVnet();
216 + HostService hostService = manager.get(vnet.id(), HostService.class);
217 +
218 + hostService.getHostsByVlan(null);
219 + }
220 +
221 + /**
222 + * Tests querying for hosts with null ip.
223 + */
224 + @Test(expected = NullPointerException.class)
225 + public void testGetHostsByNullIp() {
226 + VirtualNetwork vnet = setupVnet();
227 + HostService hostService = manager.get(vnet.id(), HostService.class);
228 +
229 + hostService.getHostsByIp(null);
230 + }
231 +
232 + /**
233 + * Tests querying for connected hosts with null host location (connect point).
234 + */
235 + @Test(expected = NullPointerException.class)
236 + public void testGetConnectedHostsByNullLoc() {
237 + VirtualNetwork vnet = setupEmptyVnet();
238 + HostService hostService = manager.get(vnet.id(), HostService.class);
239 +
240 + hostService.getConnectedHosts((ConnectPoint) null);
241 + }
242 +
243 + /**
244 + * Tests querying for connected hosts with null device id.
245 + */
246 + @Test(expected = NullPointerException.class)
247 + public void testGetConnectedHostsByNullDeviceId() {
248 + VirtualNetwork vnet = setupVnet();
249 + HostService hostService = manager.get(vnet.id(), HostService.class);
250 +
251 + hostService.getConnectedHosts((DeviceId) null);
252 + }
253 +
254 +}