blob: 11a79908684cf17f4fc3b481d2b8147b20433377 [file] [log] [blame]
Thomas Vachuska33979fd2015-07-31 11:41:14 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska33979fd2015-07-31 11:41:14 -07003 *
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 */
16package org.onosproject.incubator.net.virtual.impl;
17
Brian Stanke86914282016-05-25 15:36:50 -040018import com.google.common.collect.Maps;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
Brian Stanke11f6d532016-07-05 16:17:59 -040025import org.onlab.osgi.DefaultServiceDirectory;
yoonseonc6a69272017-01-12 18:22:20 -080026import org.onlab.osgi.ServiceDirectory;
Brian Stanke7a81b532016-06-14 15:43:51 -040027import org.onlab.packet.IpAddress;
28import org.onlab.packet.MacAddress;
29import org.onlab.packet.VlanId;
yoonseon322c9c32016-12-07 16:47:02 -080030import org.onosproject.core.ApplicationId;
31import org.onosproject.core.CoreService;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070032import org.onosproject.incubator.net.tunnel.TunnelId;
Brian Stankefb61df42016-07-25 11:47:51 -040033import org.onosproject.incubator.net.virtual.DefaultVirtualLink;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070034import org.onosproject.incubator.net.virtual.NetworkId;
35import org.onosproject.incubator.net.virtual.TenantId;
36import org.onosproject.incubator.net.virtual.VirtualDevice;
Brian Stanke7a81b532016-06-14 15:43:51 -040037import org.onosproject.incubator.net.virtual.VirtualHost;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070038import org.onosproject.incubator.net.virtual.VirtualLink;
39import org.onosproject.incubator.net.virtual.VirtualNetwork;
40import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService;
41import org.onosproject.incubator.net.virtual.VirtualNetworkEvent;
Brian Stanke11f6d532016-07-05 16:17:59 -040042import org.onosproject.incubator.net.virtual.VirtualNetworkIntent;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070043import org.onosproject.incubator.net.virtual.VirtualNetworkListener;
44import org.onosproject.incubator.net.virtual.VirtualNetworkService;
45import org.onosproject.incubator.net.virtual.VirtualNetworkStore;
46import org.onosproject.incubator.net.virtual.VirtualNetworkStoreDelegate;
47import org.onosproject.incubator.net.virtual.VirtualPort;
yoonseon214963b2016-11-21 15:41:07 -080048import org.onosproject.incubator.net.virtual.VnetService;
Claudine Chiu465a2602017-03-17 18:33:36 -040049import org.onosproject.incubator.net.virtual.event.VirtualEvent;
50import org.onosproject.incubator.net.virtual.event.VirtualListenerRegistryManager;
yoonseon6b972c32016-12-06 16:45:03 -080051import org.onosproject.incubator.net.virtual.provider.VirtualNetworkProvider;
52import org.onosproject.incubator.net.virtual.provider.VirtualNetworkProviderRegistry;
53import org.onosproject.incubator.net.virtual.provider.VirtualNetworkProviderService;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070054import org.onosproject.net.ConnectPoint;
55import org.onosproject.net.DeviceId;
Brian Stanke7a81b532016-06-14 15:43:51 -040056import org.onosproject.net.HostId;
57import org.onosproject.net.HostLocation;
Brian Stanke612cebf2016-05-02 10:21:33 -040058import org.onosproject.net.Link;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070059import org.onosproject.net.PortNumber;
Brian Stanke86914282016-05-25 15:36:50 -040060import org.onosproject.net.device.DeviceService;
Claudine Chiu70222ad2016-11-17 22:29:20 -050061import org.onosproject.net.flow.FlowRuleService;
yoonseon86bebed2017-02-03 15:23:57 -080062import org.onosproject.net.flowobjective.FlowObjectiveService;
Claudine Chiu465a2602017-03-17 18:33:36 -040063import org.onosproject.net.group.GroupService;
Claudine Chiu30a8a2a2016-07-14 17:09:04 -040064import org.onosproject.net.host.HostService;
Brian Stanke11f6d532016-07-05 16:17:59 -040065import org.onosproject.net.intent.IntentEvent;
66import org.onosproject.net.intent.IntentListener;
67import org.onosproject.net.intent.IntentService;
68import org.onosproject.net.intent.IntentState;
Brian Stanke86914282016-05-25 15:36:50 -040069import org.onosproject.net.link.LinkService;
yoonseon322c9c32016-12-07 16:47:02 -080070import org.onosproject.net.packet.PacketService;
Thomas Vachuska3d62fd72015-09-25 14:58:13 -070071import org.onosproject.net.provider.AbstractListenerProviderRegistry;
72import org.onosproject.net.provider.AbstractProviderService;
Claudine Chiu45920dd2016-07-28 19:19:46 +000073import org.onosproject.net.topology.PathService;
Brian Stanke8e9f8d12016-06-08 14:48:33 -040074import org.onosproject.net.topology.TopologyService;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070075import org.slf4j.Logger;
76import org.slf4j.LoggerFactory;
77
Brian Stanke86914282016-05-25 15:36:50 -040078import java.util.Map;
Claudine Chiue7f6b912017-02-24 14:13:30 -050079import java.util.Objects;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070080import java.util.Set;
81
82import static com.google.common.base.Preconditions.checkNotNull;
83
84/**
85 * Implementation of the virtual network service.
86 */
87@Component(immediate = true)
88@Service
89public class VirtualNetworkManager
yoonseon214963b2016-11-21 15:41:07 -080090 extends AbstractListenerProviderRegistry<VirtualNetworkEvent,
91 VirtualNetworkListener, VirtualNetworkProvider, VirtualNetworkProviderService>
92 implements VirtualNetworkService, VirtualNetworkAdminService,
93 VirtualNetworkProviderRegistry {
Thomas Vachuska33979fd2015-07-31 11:41:14 -070094
95 private final Logger log = LoggerFactory.getLogger(getClass());
96
97 private static final String TENANT_NULL = "Tenant ID cannot be null";
98 private static final String NETWORK_NULL = "Network ID cannot be null";
99 private static final String DEVICE_NULL = "Device ID cannot be null";
100 private static final String LINK_POINT_NULL = "Link end-point cannot be null";
101
yoonseon322c9c32016-12-07 16:47:02 -0800102 private static final String VIRTUAL_NETWORK_APP_ID_STRING =
103 "org.onosproject.virtual-network";
104
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700105 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
106 protected VirtualNetworkStore store;
107
Brian Stanke11f6d532016-07-05 16:17:59 -0400108 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
109 protected IntentService intentService;
110
yoonseon322c9c32016-12-07 16:47:02 -0800111 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
112 protected CoreService coreService;
113
yoonseon214963b2016-11-21 15:41:07 -0800114 private final InternalVirtualIntentListener intentListener =
115 new InternalVirtualIntentListener();
Brian Stanke11f6d532016-07-05 16:17:59 -0400116
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500117 private VirtualNetworkStoreDelegate delegate = this::post;
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700118
yoonseonc6a69272017-01-12 18:22:20 -0800119 private ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
yoonseon322c9c32016-12-07 16:47:02 -0800120 private ApplicationId appId;
yoonseonc6a69272017-01-12 18:22:20 -0800121
Thomas Vachuska3d62fd72015-09-25 14:58:13 -0700122 // TODO: figure out how to coordinate "implementation" of a virtual network in a cluster
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700123
Brian Stanke11f6d532016-07-05 16:17:59 -0400124 /**
125 * Only used for Junit test methods outside of this package.
126 *
127 * @param store virtual network store
128 */
129 public void setStore(VirtualNetworkStore store) {
130 this.store = store;
131 }
132
133 /**
134 * Only used for Junit test methods outside of this package.
135 *
136 * @param intentService intent service
137 */
138
139 public void setIntentService(IntentService intentService) {
140 this.intentService = intentService;
141 }
142
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700143 @Activate
Brian Stanke11f6d532016-07-05 16:17:59 -0400144 public void activate() {
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700145 store.setDelegate(delegate);
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500146 eventDispatcher.addSink(VirtualNetworkEvent.class, listenerRegistry);
Claudine Chiu465a2602017-03-17 18:33:36 -0400147 eventDispatcher.addSink(VirtualEvent.class,
148 VirtualListenerRegistryManager.getInstance());
Brian Stanke11f6d532016-07-05 16:17:59 -0400149 intentService.addListener(intentListener);
yoonseon322c9c32016-12-07 16:47:02 -0800150 appId = coreService.registerApplication(VIRTUAL_NETWORK_APP_ID_STRING);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700151 log.info("Started");
152 }
153
154 @Deactivate
Brian Stanke11f6d532016-07-05 16:17:59 -0400155 public void deactivate() {
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700156 store.unsetDelegate(delegate);
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500157 eventDispatcher.removeSink(VirtualNetworkEvent.class);
Claudine Chiu465a2602017-03-17 18:33:36 -0400158 eventDispatcher.removeSink(VirtualEvent.class);
Brian Stanke11f6d532016-07-05 16:17:59 -0400159 intentService.removeListener(intentListener);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700160 log.info("Stopped");
161 }
162
163 @Override
164 public void registerTenantId(TenantId tenantId) {
165 checkNotNull(tenantId, TENANT_NULL);
166 store.addTenantId(tenantId);
167 }
168
169 @Override
170 public void unregisterTenantId(TenantId tenantId) {
171 checkNotNull(tenantId, TENANT_NULL);
172 store.removeTenantId(tenantId);
173 }
174
175 @Override
176 public Set<TenantId> getTenantIds() {
177 return store.getTenantIds();
178 }
179
180 @Override
181 public VirtualNetwork createVirtualNetwork(TenantId tenantId) {
182 checkNotNull(tenantId, TENANT_NULL);
183 return store.addNetwork(tenantId);
184 }
185
186 @Override
187 public void removeVirtualNetwork(NetworkId networkId) {
188 checkNotNull(networkId, NETWORK_NULL);
189 store.removeNetwork(networkId);
190 }
191
192 @Override
193 public VirtualDevice createVirtualDevice(NetworkId networkId, DeviceId deviceId) {
194 checkNotNull(networkId, NETWORK_NULL);
195 checkNotNull(deviceId, DEVICE_NULL);
196 return store.addDevice(networkId, deviceId);
197 }
198
199 @Override
200 public void removeVirtualDevice(NetworkId networkId, DeviceId deviceId) {
201 checkNotNull(networkId, NETWORK_NULL);
202 checkNotNull(deviceId, DEVICE_NULL);
203 store.removeDevice(networkId, deviceId);
204 }
205
206 @Override
yoonseon214963b2016-11-21 15:41:07 -0800207 public VirtualHost createVirtualHost(NetworkId networkId, HostId hostId,
208 MacAddress mac, VlanId vlan,
209 HostLocation location, Set<IpAddress> ips) {
Brian Stanke7a81b532016-06-14 15:43:51 -0400210 checkNotNull(networkId, NETWORK_NULL);
211 checkNotNull(hostId, DEVICE_NULL);
212 return store.addHost(networkId, hostId, mac, vlan, location, ips);
213 }
214
215 @Override
216 public void removeVirtualHost(NetworkId networkId, HostId hostId) {
217 checkNotNull(networkId, NETWORK_NULL);
218 checkNotNull(hostId, DEVICE_NULL);
219 store.removeHost(networkId, hostId);
220 }
221
222 @Override
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700223 public VirtualLink createVirtualLink(NetworkId networkId,
Brian Stanke9a108972016-04-11 15:25:17 -0400224 ConnectPoint src, ConnectPoint dst) {
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700225 checkNotNull(networkId, NETWORK_NULL);
226 checkNotNull(src, LINK_POINT_NULL);
227 checkNotNull(dst, LINK_POINT_NULL);
Brian Stankefb61df42016-07-25 11:47:51 -0400228 ConnectPoint physicalSrc = mapVirtualToPhysicalPort(networkId, src);
229 checkNotNull(physicalSrc, LINK_POINT_NULL);
230 ConnectPoint physicalDst = mapVirtualToPhysicalPort(networkId, dst);
231 checkNotNull(physicalDst, LINK_POINT_NULL);
232
233 VirtualNetworkProvider provider = getProvider(DefaultVirtualLink.PID);
234 Link.State state = Link.State.INACTIVE;
235 if (provider != null) {
236 boolean traversable = provider.isTraversable(physicalSrc, physicalDst);
237 state = traversable ? Link.State.ACTIVE : Link.State.INACTIVE;
238 }
239 return store.addLink(networkId, src, dst, state, null);
240 }
241
242 /**
243 * Maps the virtual connect point to a physical connect point.
244 *
245 * @param networkId network identifier
246 * @param virtualCp virtual connect point
247 * @return physical connect point
248 */
249 private ConnectPoint mapVirtualToPhysicalPort(NetworkId networkId,
250 ConnectPoint virtualCp) {
251 Set<VirtualPort> ports = store.getPorts(networkId, virtualCp.deviceId());
252 for (VirtualPort port : ports) {
253 if (port.number().equals(virtualCp.port())) {
Yoonseon Han6c603892016-09-01 11:52:21 -0700254 return new ConnectPoint(port.realizedBy().deviceId(),
255 port.realizedBy().port());
Brian Stankefb61df42016-07-25 11:47:51 -0400256 }
257 }
258 return null;
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700259 }
260
Brian Stanke612cebf2016-05-02 10:21:33 -0400261 /**
262 * Maps the physical connect point to a virtual connect point.
263 *
Brian Stanke11f6d532016-07-05 16:17:59 -0400264 * @param networkId network identifier
Brian Stanke612cebf2016-05-02 10:21:33 -0400265 * @param physicalCp physical connect point
266 * @return virtual connect point
267 */
268 private ConnectPoint mapPhysicalToVirtualToPort(NetworkId networkId,
Brian Stanke11f6d532016-07-05 16:17:59 -0400269 ConnectPoint physicalCp) {
Brian Stanke612cebf2016-05-02 10:21:33 -0400270 Set<VirtualPort> ports = store.getPorts(networkId, null);
271 for (VirtualPort port : ports) {
Yoonseon Han6c603892016-09-01 11:52:21 -0700272 if (port.realizedBy().deviceId().equals(physicalCp.elementId()) &&
273 port.realizedBy().port().equals(physicalCp.port())) {
Brian Stanke612cebf2016-05-02 10:21:33 -0400274 return new ConnectPoint(port.element().id(), port.number());
275 }
276 }
277 return null;
278 }
279
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700280 @Override
yoonseon214963b2016-11-21 15:41:07 -0800281 public void removeVirtualLink(NetworkId networkId, ConnectPoint src,
282 ConnectPoint dst) {
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700283 checkNotNull(networkId, NETWORK_NULL);
284 checkNotNull(src, LINK_POINT_NULL);
285 checkNotNull(dst, LINK_POINT_NULL);
Brian Stanke11f6d532016-07-05 16:17:59 -0400286 store.removeLink(networkId, src, dst);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700287 }
288
289 @Override
290 public VirtualPort createVirtualPort(NetworkId networkId, DeviceId deviceId,
Yoonseon Han6c603892016-09-01 11:52:21 -0700291 PortNumber portNumber, ConnectPoint realizedBy) {
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700292 checkNotNull(networkId, NETWORK_NULL);
293 checkNotNull(deviceId, DEVICE_NULL);
294 checkNotNull(portNumber, "Port description cannot be null");
295 return store.addPort(networkId, deviceId, portNumber, realizedBy);
296 }
297
298 @Override
Yoonseon Han6c603892016-09-01 11:52:21 -0700299 public void bindVirtualPort(NetworkId networkId, DeviceId deviceId,
300 PortNumber portNumber, ConnectPoint realizedBy) {
301 checkNotNull(networkId, NETWORK_NULL);
302 checkNotNull(deviceId, DEVICE_NULL);
303 checkNotNull(portNumber, "Port description cannot be null");
304 checkNotNull(realizedBy, "Physical port description cannot be null");
305
306 store.bindPort(networkId, deviceId, portNumber, realizedBy);
307 }
308
309 @Override
yoonseon214963b2016-11-21 15:41:07 -0800310 public void removeVirtualPort(NetworkId networkId, DeviceId deviceId,
311 PortNumber portNumber) {
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700312 checkNotNull(networkId, NETWORK_NULL);
313 checkNotNull(deviceId, DEVICE_NULL);
314 checkNotNull(portNumber, "Port number cannot be null");
315 store.removePort(networkId, deviceId, portNumber);
316 }
317
318 @Override
yoonseonc6a69272017-01-12 18:22:20 -0800319 public ServiceDirectory getServiceDirectory() {
320 return serviceDirectory;
321 }
322
323 @Override
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700324 public Set<VirtualNetwork> getVirtualNetworks(TenantId tenantId) {
325 checkNotNull(tenantId, TENANT_NULL);
326 return store.getNetworks(tenantId);
327 }
328
Brian Stanke11f6d532016-07-05 16:17:59 -0400329 /**
330 * Returns the virtual network matching the network identifier.
331 *
332 * @param networkId network identifier
333 * @return virtual network
334 */
Brian Stanke86914282016-05-25 15:36:50 -0400335 private VirtualNetwork getVirtualNetwork(NetworkId networkId) {
336 checkNotNull(networkId, NETWORK_NULL);
337 return store.getNetwork(networkId);
338 }
339
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700340 @Override
341 public Set<VirtualDevice> getVirtualDevices(NetworkId networkId) {
342 checkNotNull(networkId, NETWORK_NULL);
343 return store.getDevices(networkId);
344 }
345
346 @Override
Brian Stanke7a81b532016-06-14 15:43:51 -0400347 public Set<VirtualHost> getVirtualHosts(NetworkId networkId) {
348 checkNotNull(networkId, NETWORK_NULL);
349 return store.getHosts(networkId);
350 }
351
352 @Override
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700353 public Set<VirtualLink> getVirtualLinks(NetworkId networkId) {
354 checkNotNull(networkId, NETWORK_NULL);
355 return store.getLinks(networkId);
356 }
357
358 @Override
359 public Set<VirtualPort> getVirtualPorts(NetworkId networkId, DeviceId deviceId) {
360 checkNotNull(networkId, NETWORK_NULL);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700361 return store.getPorts(networkId, deviceId);
362 }
363
Brian Stanke86914282016-05-25 15:36:50 -0400364 private final Map<ServiceKey, VnetService> networkServices = Maps.newConcurrentMap();
365
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700366 @Override
367 public <T> T get(NetworkId networkId, Class<T> serviceClass) {
368 checkNotNull(networkId, NETWORK_NULL);
Brian Stanke86914282016-05-25 15:36:50 -0400369 ServiceKey serviceKey = networkServiceKey(networkId, serviceClass);
370 VnetService service = lookup(serviceKey);
371 if (service == null) {
372 service = create(serviceKey);
373 }
374 return (T) service;
375 }
376
yoonseon322c9c32016-12-07 16:47:02 -0800377 @Override
378 public ApplicationId getVirtualNetworkApplicationId(NetworkId networkId) {
379 return appId;
380 }
381
Brian Stanke11f6d532016-07-05 16:17:59 -0400382 /**
383 * Returns the Vnet service matching the service key.
384 *
385 * @param serviceKey service key
386 * @return vnet service
387 */
Brian Stanke86914282016-05-25 15:36:50 -0400388 private VnetService lookup(ServiceKey serviceKey) {
389 return networkServices.get(serviceKey);
390 }
391
Brian Stanke11f6d532016-07-05 16:17:59 -0400392 /**
393 * Creates a new service key using the specified network identifier and service class.
394 *
395 * @param networkId network identifier
396 * @param serviceClass service class
397 * @param <T> type of service
398 * @return service key
399 */
Brian Stanke86914282016-05-25 15:36:50 -0400400 private <T> ServiceKey networkServiceKey(NetworkId networkId, Class<T> serviceClass) {
401 return new ServiceKey(networkId, serviceClass);
402 }
403
404
Brian Stanke11f6d532016-07-05 16:17:59 -0400405 /**
406 * Create a new vnet service instance.
407 *
408 * @param serviceKey service key
409 * @return vnet service
410 */
Brian Stanke86914282016-05-25 15:36:50 -0400411 private VnetService create(ServiceKey serviceKey) {
412 VirtualNetwork network = getVirtualNetwork(serviceKey.networkId());
Brian Stanke11f6d532016-07-05 16:17:59 -0400413 checkNotNull(network, NETWORK_NULL);
yoonseonc6a69272017-01-12 18:22:20 -0800414
Brian Stanke86914282016-05-25 15:36:50 -0400415 VnetService service;
416 if (serviceKey.serviceClass.equals(DeviceService.class)) {
yoonseonc6a69272017-01-12 18:22:20 -0800417 service = new VirtualNetworkDeviceManager(this, network.id());
Brian Stanke86914282016-05-25 15:36:50 -0400418 } else if (serviceKey.serviceClass.equals(LinkService.class)) {
yoonseonc6a69272017-01-12 18:22:20 -0800419 service = new VirtualNetworkLinkManager(this, network.id());
Brian Stanke8e9f8d12016-06-08 14:48:33 -0400420 } else if (serviceKey.serviceClass.equals(TopologyService.class)) {
yoonseonc6a69272017-01-12 18:22:20 -0800421 service = new VirtualNetworkTopologyManager(this, network.id());
Brian Stanke11f6d532016-07-05 16:17:59 -0400422 } else if (serviceKey.serviceClass.equals(IntentService.class)) {
yoonseonc6a69272017-01-12 18:22:20 -0800423 service = new VirtualNetworkIntentManager(this, network.id());
Claudine Chiu30a8a2a2016-07-14 17:09:04 -0400424 } else if (serviceKey.serviceClass.equals(HostService.class)) {
yoonseonc6a69272017-01-12 18:22:20 -0800425 service = new VirtualNetworkHostManager(this, network.id());
Claudine Chiu45920dd2016-07-28 19:19:46 +0000426 } else if (serviceKey.serviceClass.equals(PathService.class)) {
yoonseonc6a69272017-01-12 18:22:20 -0800427 service = new VirtualNetworkPathManager(this, network.id());
Claudine Chiu70222ad2016-11-17 22:29:20 -0500428 } else if (serviceKey.serviceClass.equals(FlowRuleService.class)) {
yoonseonc6a69272017-01-12 18:22:20 -0800429 service = new VirtualNetworkFlowRuleManager(this, network.id());
yoonseon322c9c32016-12-07 16:47:02 -0800430 } else if (serviceKey.serviceClass.equals(PacketService.class)) {
431 service = new VirtualNetworkPacketManager(this, network.id());
yoonseondc3210d2017-01-25 16:03:10 -0800432 } else if (serviceKey.serviceClass.equals(GroupService.class)) {
433 service = new VirtualNetworkGroupManager(this, network.id());
yoonseon86bebed2017-02-03 15:23:57 -0800434 } else if (serviceKey.serviceClass.equals(FlowObjectiveService.class)) {
435 service = new VirtualNetworkFlowObjectiveManager(this, network.id());
Brian Stanke86914282016-05-25 15:36:50 -0400436 } else {
437 return null;
438 }
439 networkServices.put(serviceKey, service);
440 return service;
441 }
442
Brian Stanke11f6d532016-07-05 16:17:59 -0400443 /**
444 * Service key class.
445 */
Claudine Chiu9a18f492017-02-27 15:57:06 -0500446 private static class ServiceKey {
Brian Stanke86914282016-05-25 15:36:50 -0400447 final NetworkId networkId;
448 final Class serviceClass;
449
Brian Stanke11f6d532016-07-05 16:17:59 -0400450 /**
451 * Constructor for service key.
452 *
453 * @param networkId network identifier
454 * @param serviceClass service class
455 */
Brian Stanke86914282016-05-25 15:36:50 -0400456 public ServiceKey(NetworkId networkId, Class serviceClass) {
yoonseon214963b2016-11-21 15:41:07 -0800457
Brian Stanke86914282016-05-25 15:36:50 -0400458 checkNotNull(networkId, NETWORK_NULL);
459 this.networkId = networkId;
460 this.serviceClass = serviceClass;
461 }
462
Brian Stanke11f6d532016-07-05 16:17:59 -0400463 /**
464 * Returns the network identifier.
465 *
466 * @return network identifier
467 */
Brian Stanke86914282016-05-25 15:36:50 -0400468 public NetworkId networkId() {
469 return networkId;
470 }
471
Brian Stanke11f6d532016-07-05 16:17:59 -0400472 /**
473 * Returns the service class.
474 *
475 * @return service class
476 */
Brian Stanke86914282016-05-25 15:36:50 -0400477 public Class serviceClass() {
478 return serviceClass;
479 }
Claudine Chiue7f6b912017-02-24 14:13:30 -0500480
481 @Override
482 public int hashCode() {
483 return Objects.hash(networkId, serviceClass);
484 }
485
486 @Override
487 public boolean equals(Object obj) {
488 if (this == obj) {
489 return true;
490 }
491 if (obj instanceof ServiceKey) {
492 ServiceKey that = (ServiceKey) obj;
493 return Objects.equals(this.networkId, that.networkId) &&
494 Objects.equals(this.serviceClass, that.serviceClass);
495 }
496 return false;
497 }
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700498 }
499
Brian Stanke11f6d532016-07-05 16:17:59 -0400500 /**
501 * Internal intent event listener.
502 */
503 private class InternalVirtualIntentListener implements IntentListener {
yoonseon214963b2016-11-21 15:41:07 -0800504
Brian Stanke11f6d532016-07-05 16:17:59 -0400505 @Override
506 public void event(IntentEvent event) {
507
508 // Ignore intent events that are not relevant.
509 if (!isRelevant(event)) {
510 return;
511 }
512
513 VirtualNetworkIntent intent = (VirtualNetworkIntent) event.subject();
514
515 switch (event.type()) {
516 case INSTALL_REQ:
517 store.addOrUpdateIntent(intent, IntentState.INSTALL_REQ);
518 break;
519 case INSTALLED:
520 store.addOrUpdateIntent(intent, IntentState.INSTALLED);
521 break;
522 case WITHDRAW_REQ:
523 store.addOrUpdateIntent(intent, IntentState.WITHDRAW_REQ);
524 break;
525 case WITHDRAWN:
526 store.addOrUpdateIntent(intent, IntentState.WITHDRAWN);
527 break;
528 case FAILED:
529 store.addOrUpdateIntent(intent, IntentState.FAILED);
530 break;
531 case CORRUPT:
532 store.addOrUpdateIntent(intent, IntentState.CORRUPT);
533 break;
534 case PURGED:
535 store.removeIntent(intent.key());
536 default:
537 break;
538 }
539 }
540
541 @Override
542 public boolean isRelevant(IntentEvent event) {
543 if (event.subject() instanceof VirtualNetworkIntent) {
544 return true;
545 }
546 return false;
547 }
548 }
549
550
Thomas Vachuska3d62fd72015-09-25 14:58:13 -0700551 @Override
yoonseon214963b2016-11-21 15:41:07 -0800552 protected VirtualNetworkProviderService
553 createProviderService(VirtualNetworkProvider provider) {
Thomas Vachuska3d62fd72015-09-25 14:58:13 -0700554 return new InternalVirtualNetworkProviderService(provider);
555 }
556
yoonseon6b972c32016-12-06 16:45:03 -0800557
Brian Stankefb61df42016-07-25 11:47:51 -0400558 /**
559 * Service issued to registered virtual network providers so that they
560 * can interact with the core.
561 */
Thomas Vachuska3d62fd72015-09-25 14:58:13 -0700562 private class InternalVirtualNetworkProviderService
563 extends AbstractProviderService<VirtualNetworkProvider>
564 implements VirtualNetworkProviderService {
Brian Stankefb61df42016-07-25 11:47:51 -0400565 /**
566 * Constructor.
567 * @param provider virtual network provider
568 */
Thomas Vachuska3d62fd72015-09-25 14:58:13 -0700569 InternalVirtualNetworkProviderService(VirtualNetworkProvider provider) {
570 super(provider);
571 }
Brian Stanke4d579882016-04-22 13:28:46 -0400572
573 @Override
Brian Stankefb61df42016-07-25 11:47:51 -0400574 public void topologyChanged(Set<Set<ConnectPoint>> clusters) {
Brian Stankefb61df42016-07-25 11:47:51 -0400575 Set<TenantId> tenantIds = getTenantIds();
576 tenantIds.forEach(tenantId -> {
577 Set<VirtualNetwork> virtualNetworks = getVirtualNetworks(tenantId);
578
579 virtualNetworks.forEach(virtualNetwork -> {
580 Set<VirtualLink> virtualLinks = getVirtualLinks(virtualNetwork.id());
581
582 virtualLinks.forEach(virtualLink -> {
yoonseon214963b2016-11-21 15:41:07 -0800583 if (isVirtualLinkInCluster(virtualNetwork.id(),
584 virtualLink, clusters)) {
585 store.updateLink(virtualLink, virtualLink.tunnelId(),
586 Link.State.ACTIVE);
Brian Stankefb61df42016-07-25 11:47:51 -0400587 } else {
yoonseon214963b2016-11-21 15:41:07 -0800588 store.updateLink(virtualLink, virtualLink.tunnelId(),
589 Link.State.INACTIVE);
Brian Stankefb61df42016-07-25 11:47:51 -0400590 }
591 });
592 });
593 });
594 }
595
596 /**
yoonseon214963b2016-11-21 15:41:07 -0800597 * Determines if the virtual link (both source and destination connect point)
598 * is in a cluster.
Brian Stankefb61df42016-07-25 11:47:51 -0400599 *
600 * @param networkId virtual network identifier
601 * @param virtualLink virtual link
602 * @param clusters topology clusters
603 * @return true if the virtual link is in a cluster.
604 */
605 private boolean isVirtualLinkInCluster(NetworkId networkId, VirtualLink virtualLink,
606 Set<Set<ConnectPoint>> clusters) {
yoonseon214963b2016-11-21 15:41:07 -0800607 ConnectPoint srcPhysicalCp =
608 mapVirtualToPhysicalPort(networkId, virtualLink.src());
609 ConnectPoint dstPhysicalCp =
610 mapVirtualToPhysicalPort(networkId, virtualLink.dst());
Brian Stankefb61df42016-07-25 11:47:51 -0400611
612 final boolean[] foundSrc = {false};
613 final boolean[] foundDst = {false};
614 clusters.forEach(connectPoints -> {
615 connectPoints.forEach(connectPoint -> {
616 if (connectPoint.equals(srcPhysicalCp)) {
617 foundSrc[0] = true;
618 } else if (connectPoint.equals(dstPhysicalCp)) {
619 foundDst[0] = true;
620 }
621 });
622 if (foundSrc[0] && foundDst[0]) {
623 return;
624 }
625 });
626 return foundSrc[0] && foundDst[0];
627 }
628
629 @Override
yoonseon214963b2016-11-21 15:41:07 -0800630 public void tunnelUp(NetworkId networkId, ConnectPoint src,
631 ConnectPoint dst, TunnelId tunnelId) {
Brian Stanke612cebf2016-05-02 10:21:33 -0400632 ConnectPoint srcVirtualCp = mapPhysicalToVirtualToPort(networkId, src);
633 ConnectPoint dstVirtualCp = mapPhysicalToVirtualToPort(networkId, dst);
634 if ((srcVirtualCp == null) || (dstVirtualCp == null)) {
635 log.error("Src or dst virtual connection point was not found.");
636 }
637
638 VirtualLink virtualLink = store.getLink(networkId, srcVirtualCp, dstVirtualCp);
639 if (virtualLink != null) {
640 store.updateLink(virtualLink, tunnelId, Link.State.ACTIVE);
641 }
Brian Stanke4d579882016-04-22 13:28:46 -0400642 }
643
644 @Override
yoonseon214963b2016-11-21 15:41:07 -0800645 public void tunnelDown(NetworkId networkId, ConnectPoint src,
646 ConnectPoint dst, TunnelId tunnelId) {
Brian Stanke612cebf2016-05-02 10:21:33 -0400647 ConnectPoint srcVirtualCp = mapPhysicalToVirtualToPort(networkId, src);
648 ConnectPoint dstVirtualCp = mapPhysicalToVirtualToPort(networkId, dst);
649 if ((srcVirtualCp == null) || (dstVirtualCp == null)) {
650 log.error("Src or dst virtual connection point was not found.");
651 }
Brian Stanke4d579882016-04-22 13:28:46 -0400652
Brian Stanke612cebf2016-05-02 10:21:33 -0400653 VirtualLink virtualLink = store.getLink(networkId, srcVirtualCp, dstVirtualCp);
654 if (virtualLink != null) {
655 store.updateLink(virtualLink, tunnelId, Link.State.INACTIVE);
656 }
Brian Stanke4d579882016-04-22 13:28:46 -0400657 }
Thomas Vachuska3d62fd72015-09-25 14:58:13 -0700658 }
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700659}