blob: ff3c696e16ae8694dca0b4528881b07851befbe9 [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() {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500145 eventDispatcher.addSink(VirtualNetworkEvent.class, listenerRegistry);
Claudine Chiu465a2602017-03-17 18:33:36 -0400146 eventDispatcher.addSink(VirtualEvent.class,
147 VirtualListenerRegistryManager.getInstance());
Yoonseon Hanb14461b2017-03-07 14:08:01 +0900148 store.setDelegate(delegate);
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
Yoonseon Hanb14461b2017-03-07 14:08:01 +0900367 @SuppressWarnings("unchecked")
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700368 public <T> T get(NetworkId networkId, Class<T> serviceClass) {
369 checkNotNull(networkId, NETWORK_NULL);
Brian Stanke86914282016-05-25 15:36:50 -0400370 ServiceKey serviceKey = networkServiceKey(networkId, serviceClass);
371 VnetService service = lookup(serviceKey);
372 if (service == null) {
373 service = create(serviceKey);
374 }
375 return (T) service;
376 }
377
yoonseon322c9c32016-12-07 16:47:02 -0800378 @Override
379 public ApplicationId getVirtualNetworkApplicationId(NetworkId networkId) {
380 return appId;
381 }
382
Brian Stanke11f6d532016-07-05 16:17:59 -0400383 /**
384 * Returns the Vnet service matching the service key.
385 *
386 * @param serviceKey service key
387 * @return vnet service
388 */
Brian Stanke86914282016-05-25 15:36:50 -0400389 private VnetService lookup(ServiceKey serviceKey) {
390 return networkServices.get(serviceKey);
391 }
392
Brian Stanke11f6d532016-07-05 16:17:59 -0400393 /**
394 * Creates a new service key using the specified network identifier and service class.
395 *
396 * @param networkId network identifier
397 * @param serviceClass service class
398 * @param <T> type of service
399 * @return service key
400 */
Brian Stanke86914282016-05-25 15:36:50 -0400401 private <T> ServiceKey networkServiceKey(NetworkId networkId, Class<T> serviceClass) {
402 return new ServiceKey(networkId, serviceClass);
403 }
404
405
Brian Stanke11f6d532016-07-05 16:17:59 -0400406 /**
407 * Create a new vnet service instance.
408 *
409 * @param serviceKey service key
410 * @return vnet service
411 */
Brian Stanke86914282016-05-25 15:36:50 -0400412 private VnetService create(ServiceKey serviceKey) {
413 VirtualNetwork network = getVirtualNetwork(serviceKey.networkId());
Brian Stanke11f6d532016-07-05 16:17:59 -0400414 checkNotNull(network, NETWORK_NULL);
yoonseonc6a69272017-01-12 18:22:20 -0800415
Brian Stanke86914282016-05-25 15:36:50 -0400416 VnetService service;
417 if (serviceKey.serviceClass.equals(DeviceService.class)) {
yoonseonc6a69272017-01-12 18:22:20 -0800418 service = new VirtualNetworkDeviceManager(this, network.id());
Brian Stanke86914282016-05-25 15:36:50 -0400419 } else if (serviceKey.serviceClass.equals(LinkService.class)) {
yoonseonc6a69272017-01-12 18:22:20 -0800420 service = new VirtualNetworkLinkManager(this, network.id());
Brian Stanke8e9f8d12016-06-08 14:48:33 -0400421 } else if (serviceKey.serviceClass.equals(TopologyService.class)) {
yoonseonc6a69272017-01-12 18:22:20 -0800422 service = new VirtualNetworkTopologyManager(this, network.id());
Brian Stanke11f6d532016-07-05 16:17:59 -0400423 } else if (serviceKey.serviceClass.equals(IntentService.class)) {
yoonseonc6a69272017-01-12 18:22:20 -0800424 service = new VirtualNetworkIntentManager(this, network.id());
Claudine Chiu30a8a2a2016-07-14 17:09:04 -0400425 } else if (serviceKey.serviceClass.equals(HostService.class)) {
yoonseonc6a69272017-01-12 18:22:20 -0800426 service = new VirtualNetworkHostManager(this, network.id());
Claudine Chiu45920dd2016-07-28 19:19:46 +0000427 } else if (serviceKey.serviceClass.equals(PathService.class)) {
yoonseonc6a69272017-01-12 18:22:20 -0800428 service = new VirtualNetworkPathManager(this, network.id());
Claudine Chiu70222ad2016-11-17 22:29:20 -0500429 } else if (serviceKey.serviceClass.equals(FlowRuleService.class)) {
yoonseonc6a69272017-01-12 18:22:20 -0800430 service = new VirtualNetworkFlowRuleManager(this, network.id());
yoonseon322c9c32016-12-07 16:47:02 -0800431 } else if (serviceKey.serviceClass.equals(PacketService.class)) {
432 service = new VirtualNetworkPacketManager(this, network.id());
yoonseondc3210d2017-01-25 16:03:10 -0800433 } else if (serviceKey.serviceClass.equals(GroupService.class)) {
434 service = new VirtualNetworkGroupManager(this, network.id());
yoonseon86bebed2017-02-03 15:23:57 -0800435 } else if (serviceKey.serviceClass.equals(FlowObjectiveService.class)) {
436 service = new VirtualNetworkFlowObjectiveManager(this, network.id());
Brian Stanke86914282016-05-25 15:36:50 -0400437 } else {
438 return null;
439 }
440 networkServices.put(serviceKey, service);
441 return service;
442 }
443
Brian Stanke11f6d532016-07-05 16:17:59 -0400444 /**
445 * Service key class.
446 */
Claudine Chiu9a18f492017-02-27 15:57:06 -0500447 private static class ServiceKey {
Brian Stanke86914282016-05-25 15:36:50 -0400448 final NetworkId networkId;
449 final Class serviceClass;
450
Brian Stanke11f6d532016-07-05 16:17:59 -0400451 /**
452 * Constructor for service key.
453 *
454 * @param networkId network identifier
455 * @param serviceClass service class
456 */
Yoonseon Hanb14461b2017-03-07 14:08:01 +0900457 ServiceKey(NetworkId networkId, Class serviceClass) {
yoonseon214963b2016-11-21 15:41:07 -0800458
Brian Stanke86914282016-05-25 15:36:50 -0400459 checkNotNull(networkId, NETWORK_NULL);
460 this.networkId = networkId;
461 this.serviceClass = serviceClass;
462 }
463
Brian Stanke11f6d532016-07-05 16:17:59 -0400464 /**
465 * Returns the network identifier.
466 *
467 * @return network identifier
468 */
Brian Stanke86914282016-05-25 15:36:50 -0400469 public NetworkId networkId() {
470 return networkId;
471 }
472
Brian Stanke11f6d532016-07-05 16:17:59 -0400473 /**
474 * Returns the service class.
475 *
476 * @return service class
477 */
Brian Stanke86914282016-05-25 15:36:50 -0400478 public Class serviceClass() {
479 return serviceClass;
480 }
Claudine Chiue7f6b912017-02-24 14:13:30 -0500481
482 @Override
483 public int hashCode() {
484 return Objects.hash(networkId, serviceClass);
485 }
486
487 @Override
488 public boolean equals(Object obj) {
489 if (this == obj) {
490 return true;
491 }
492 if (obj instanceof ServiceKey) {
493 ServiceKey that = (ServiceKey) obj;
494 return Objects.equals(this.networkId, that.networkId) &&
495 Objects.equals(this.serviceClass, that.serviceClass);
496 }
497 return false;
498 }
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700499 }
500
Brian Stanke11f6d532016-07-05 16:17:59 -0400501 /**
502 * Internal intent event listener.
503 */
504 private class InternalVirtualIntentListener implements IntentListener {
yoonseon214963b2016-11-21 15:41:07 -0800505
Brian Stanke11f6d532016-07-05 16:17:59 -0400506 @Override
507 public void event(IntentEvent event) {
508
509 // Ignore intent events that are not relevant.
510 if (!isRelevant(event)) {
511 return;
512 }
513
514 VirtualNetworkIntent intent = (VirtualNetworkIntent) event.subject();
515
516 switch (event.type()) {
517 case INSTALL_REQ:
518 store.addOrUpdateIntent(intent, IntentState.INSTALL_REQ);
519 break;
520 case INSTALLED:
521 store.addOrUpdateIntent(intent, IntentState.INSTALLED);
522 break;
523 case WITHDRAW_REQ:
524 store.addOrUpdateIntent(intent, IntentState.WITHDRAW_REQ);
525 break;
526 case WITHDRAWN:
527 store.addOrUpdateIntent(intent, IntentState.WITHDRAWN);
528 break;
529 case FAILED:
530 store.addOrUpdateIntent(intent, IntentState.FAILED);
531 break;
532 case CORRUPT:
533 store.addOrUpdateIntent(intent, IntentState.CORRUPT);
534 break;
535 case PURGED:
536 store.removeIntent(intent.key());
537 default:
538 break;
539 }
540 }
541
542 @Override
543 public boolean isRelevant(IntentEvent event) {
544 if (event.subject() instanceof VirtualNetworkIntent) {
545 return true;
546 }
547 return false;
548 }
549 }
550
551
Thomas Vachuska3d62fd72015-09-25 14:58:13 -0700552 @Override
yoonseon214963b2016-11-21 15:41:07 -0800553 protected VirtualNetworkProviderService
554 createProviderService(VirtualNetworkProvider provider) {
Thomas Vachuska3d62fd72015-09-25 14:58:13 -0700555 return new InternalVirtualNetworkProviderService(provider);
556 }
557
yoonseon6b972c32016-12-06 16:45:03 -0800558
Brian Stankefb61df42016-07-25 11:47:51 -0400559 /**
560 * Service issued to registered virtual network providers so that they
561 * can interact with the core.
562 */
Thomas Vachuska3d62fd72015-09-25 14:58:13 -0700563 private class InternalVirtualNetworkProviderService
564 extends AbstractProviderService<VirtualNetworkProvider>
565 implements VirtualNetworkProviderService {
Brian Stankefb61df42016-07-25 11:47:51 -0400566 /**
567 * Constructor.
568 * @param provider virtual network provider
569 */
Thomas Vachuska3d62fd72015-09-25 14:58:13 -0700570 InternalVirtualNetworkProviderService(VirtualNetworkProvider provider) {
571 super(provider);
572 }
Brian Stanke4d579882016-04-22 13:28:46 -0400573
574 @Override
Brian Stankefb61df42016-07-25 11:47:51 -0400575 public void topologyChanged(Set<Set<ConnectPoint>> clusters) {
Brian Stankefb61df42016-07-25 11:47:51 -0400576 Set<TenantId> tenantIds = getTenantIds();
577 tenantIds.forEach(tenantId -> {
578 Set<VirtualNetwork> virtualNetworks = getVirtualNetworks(tenantId);
579
580 virtualNetworks.forEach(virtualNetwork -> {
581 Set<VirtualLink> virtualLinks = getVirtualLinks(virtualNetwork.id());
582
583 virtualLinks.forEach(virtualLink -> {
yoonseon214963b2016-11-21 15:41:07 -0800584 if (isVirtualLinkInCluster(virtualNetwork.id(),
585 virtualLink, clusters)) {
586 store.updateLink(virtualLink, virtualLink.tunnelId(),
587 Link.State.ACTIVE);
Brian Stankefb61df42016-07-25 11:47:51 -0400588 } else {
yoonseon214963b2016-11-21 15:41:07 -0800589 store.updateLink(virtualLink, virtualLink.tunnelId(),
590 Link.State.INACTIVE);
Brian Stankefb61df42016-07-25 11:47:51 -0400591 }
592 });
593 });
594 });
595 }
596
597 /**
yoonseon214963b2016-11-21 15:41:07 -0800598 * Determines if the virtual link (both source and destination connect point)
599 * is in a cluster.
Brian Stankefb61df42016-07-25 11:47:51 -0400600 *
601 * @param networkId virtual network identifier
602 * @param virtualLink virtual link
603 * @param clusters topology clusters
604 * @return true if the virtual link is in a cluster.
605 */
606 private boolean isVirtualLinkInCluster(NetworkId networkId, VirtualLink virtualLink,
607 Set<Set<ConnectPoint>> clusters) {
yoonseon214963b2016-11-21 15:41:07 -0800608 ConnectPoint srcPhysicalCp =
609 mapVirtualToPhysicalPort(networkId, virtualLink.src());
610 ConnectPoint dstPhysicalCp =
611 mapVirtualToPhysicalPort(networkId, virtualLink.dst());
Brian Stankefb61df42016-07-25 11:47:51 -0400612
613 final boolean[] foundSrc = {false};
614 final boolean[] foundDst = {false};
615 clusters.forEach(connectPoints -> {
616 connectPoints.forEach(connectPoint -> {
617 if (connectPoint.equals(srcPhysicalCp)) {
618 foundSrc[0] = true;
619 } else if (connectPoint.equals(dstPhysicalCp)) {
620 foundDst[0] = true;
621 }
622 });
623 if (foundSrc[0] && foundDst[0]) {
624 return;
625 }
626 });
627 return foundSrc[0] && foundDst[0];
628 }
629
630 @Override
yoonseon214963b2016-11-21 15:41:07 -0800631 public void tunnelUp(NetworkId networkId, ConnectPoint src,
632 ConnectPoint dst, TunnelId tunnelId) {
Brian Stanke612cebf2016-05-02 10:21:33 -0400633 ConnectPoint srcVirtualCp = mapPhysicalToVirtualToPort(networkId, src);
634 ConnectPoint dstVirtualCp = mapPhysicalToVirtualToPort(networkId, dst);
635 if ((srcVirtualCp == null) || (dstVirtualCp == null)) {
636 log.error("Src or dst virtual connection point was not found.");
637 }
638
639 VirtualLink virtualLink = store.getLink(networkId, srcVirtualCp, dstVirtualCp);
640 if (virtualLink != null) {
641 store.updateLink(virtualLink, tunnelId, Link.State.ACTIVE);
642 }
Brian Stanke4d579882016-04-22 13:28:46 -0400643 }
644
645 @Override
yoonseon214963b2016-11-21 15:41:07 -0800646 public void tunnelDown(NetworkId networkId, ConnectPoint src,
647 ConnectPoint dst, TunnelId tunnelId) {
Brian Stanke612cebf2016-05-02 10:21:33 -0400648 ConnectPoint srcVirtualCp = mapPhysicalToVirtualToPort(networkId, src);
649 ConnectPoint dstVirtualCp = mapPhysicalToVirtualToPort(networkId, dst);
650 if ((srcVirtualCp == null) || (dstVirtualCp == null)) {
651 log.error("Src or dst virtual connection point was not found.");
652 }
Brian Stanke4d579882016-04-22 13:28:46 -0400653
Brian Stanke612cebf2016-05-02 10:21:33 -0400654 VirtualLink virtualLink = store.getLink(networkId, srcVirtualCp, dstVirtualCp);
655 if (virtualLink != null) {
656 store.updateLink(virtualLink, tunnelId, Link.State.INACTIVE);
657 }
Brian Stanke4d579882016-04-22 13:28:46 -0400658 }
Thomas Vachuska3d62fd72015-09-25 14:58:13 -0700659 }
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700660}