blob: 65cee11a2becb51780674fc7a9601e7ff2011584 [file] [log] [blame]
Thomas Vachuska33979fd2015-07-31 11:41:14 -07001/*
Brian Stanke0e5c94e2016-03-08 11:20:04 -05002 * Copyright 2016 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.store.virtual.impl;
17
Brian Stanke0e5c94e2016-03-08 11:20:04 -050018import com.google.common.collect.ImmutableSet;
19import com.google.common.collect.Sets;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
Brian Stanke0e5c94e2016-03-08 11:20:04 -050023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070025import org.apache.felix.scr.annotations.Service;
Brian Stanke0e5c94e2016-03-08 11:20:04 -050026import org.onlab.util.KryoNamespace;
27import org.onosproject.core.CoreService;
28import org.onosproject.core.IdGenerator;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070029import org.onosproject.incubator.net.tunnel.TunnelId;
30import org.onosproject.incubator.net.virtual.DefaultVirtualDevice;
Brian Stanke0e5c94e2016-03-08 11:20:04 -050031import org.onosproject.incubator.net.virtual.DefaultVirtualLink;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070032import org.onosproject.incubator.net.virtual.DefaultVirtualNetwork;
Brian Stanke0e5c94e2016-03-08 11:20:04 -050033import org.onosproject.incubator.net.virtual.DefaultVirtualPort;
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;
37import org.onosproject.incubator.net.virtual.VirtualLink;
38import org.onosproject.incubator.net.virtual.VirtualNetwork;
39import org.onosproject.incubator.net.virtual.VirtualNetworkEvent;
Brian Stanke0e5c94e2016-03-08 11:20:04 -050040import org.onosproject.incubator.net.virtual.VirtualNetworkService;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070041import org.onosproject.incubator.net.virtual.VirtualNetworkStore;
42import org.onosproject.incubator.net.virtual.VirtualNetworkStoreDelegate;
43import org.onosproject.incubator.net.virtual.VirtualPort;
44import org.onosproject.net.ConnectPoint;
Brian Stanke0e5c94e2016-03-08 11:20:04 -050045import org.onosproject.net.Device;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070046import org.onosproject.net.DeviceId;
47import org.onosproject.net.Port;
48import org.onosproject.net.PortNumber;
49import org.onosproject.store.AbstractStore;
Brian Stanke0e5c94e2016-03-08 11:20:04 -050050import org.onosproject.store.serializers.KryoNamespaces;
51import org.onosproject.store.service.ConsistentMap;
52import org.onosproject.store.service.DistributedSet;
53import org.onosproject.store.service.MapEvent;
54import org.onosproject.store.service.MapEventListener;
55import org.onosproject.store.service.Serializer;
56import org.onosproject.store.service.SetEvent;
57import org.onosproject.store.service.SetEventListener;
58import org.onosproject.store.service.StorageService;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070059import org.slf4j.Logger;
60
Brian Stanke0e5c94e2016-03-08 11:20:04 -050061import java.util.HashSet;
62import java.util.Map;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070063import java.util.Set;
64
Brian Stanke0e5c94e2016-03-08 11:20:04 -050065import static com.google.common.base.Preconditions.checkNotNull;
66import static com.google.common.base.Preconditions.checkState;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070067import static org.slf4j.LoggerFactory.getLogger;
68
69/**
Brian Stanke0e5c94e2016-03-08 11:20:04 -050070 * Implementation of the virtual network store.
Thomas Vachuska33979fd2015-07-31 11:41:14 -070071 */
72@Component(immediate = true)
73@Service
74public class DistributedVirtualNetworkStore
75 extends AbstractStore<VirtualNetworkEvent, VirtualNetworkStoreDelegate>
76 implements VirtualNetworkStore {
77
78 private final Logger log = getLogger(getClass());
79
Brian Stanke0e5c94e2016-03-08 11:20:04 -050080 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 protected StorageService storageService;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070082
Brian Stanke0e5c94e2016-03-08 11:20:04 -050083 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected CoreService coreService;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070085
Brian Stanke0e5c94e2016-03-08 11:20:04 -050086 private IdGenerator idGenerator;
87
88 // Track tenants by ID
89 private DistributedSet<TenantId> tenantIdSet;
90
91 // Listener for tenant events
92 private final SetEventListener<TenantId> setListener = new InternalSetListener();
93
94 // Track virtual networks by network Id
95 private ConsistentMap<NetworkId, VirtualNetwork> networkIdVirtualNetworkConsistentMap;
96 private Map<NetworkId, VirtualNetwork> networkIdVirtualNetworkMap;
97
98 // Listener for virtual network events
99 private final MapEventListener<NetworkId, VirtualNetwork> virtualMapListener = new InternalMapListener();
100
101 // Track virtual network IDs by tenant Id
102 private ConsistentMap<TenantId, Set<NetworkId>> tenantIdNetworkIdSetConsistentMap;
103 private Map<TenantId, Set<NetworkId>> tenantIdNetworkIdSetMap;
104
105 // Track virtual devices by device Id
106 private ConsistentMap<DeviceId, VirtualDevice> deviceIdVirtualDeviceConsistentMap;
107 private Map<DeviceId, VirtualDevice> deviceIdVirtualDeviceMap;
108
109 // Track device IDs by network Id
110 private ConsistentMap<NetworkId, Set<DeviceId>> networkIdDeviceIdSetConsistentMap;
111 private Map<NetworkId, Set<DeviceId>> networkIdDeviceIdSetMap;
112
113 // Track virtual links by network Id
114 private ConsistentMap<NetworkId, Set<VirtualLink>> networkIdVirtualLinkSetConsistentMap;
115 private Map<NetworkId, Set<VirtualLink>> networkIdVirtualLinkSetMap;
116
117 // Track virtual ports by network Id
118 private ConsistentMap<NetworkId, Set<VirtualPort>> networkIdVirtualPortSetConsistentMap;
119 private Map<NetworkId, Set<VirtualPort>> networkIdVirtualPortSetMap;
120
121 private static final Serializer SERIALIZER = Serializer
122 .using(new KryoNamespace.Builder().register(KryoNamespaces.API)
123 .register(TenantId.class)
124 .register(NetworkId.class).register(DeviceId.class)
125 .register(VirtualNetwork.class)
126 .register(VirtualDevice.class)
127 .register(VirtualLink.class)
128 .register(VirtualPort.class)
129 .register(DeviceId.class)
130 .register(Device.class)
131 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID).build());
132
133 /**
134 * Distributed network store service activate method.
135 */
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700136 @Activate
137 public void activate() {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500138 idGenerator = coreService.getIdGenerator(VirtualNetworkService.VIRTUAL_NETWORK_TOPIC);
139
140 tenantIdSet = storageService.<TenantId>setBuilder()
141 .withSerializer(SERIALIZER)
142 .withName("onos-tenantId")
143 .withRelaxedReadConsistency()
144 .build()
145 .asDistributedSet();
146 tenantIdSet.addListener(setListener);
147
148 networkIdVirtualNetworkConsistentMap = storageService.<NetworkId, VirtualNetwork>consistentMapBuilder()
149 .withSerializer(SERIALIZER)
150 .withName("onos-networkId-virtualnetwork")
151 .withRelaxedReadConsistency()
152 .build();
153 networkIdVirtualNetworkConsistentMap.addListener(virtualMapListener);
154 networkIdVirtualNetworkMap = networkIdVirtualNetworkConsistentMap.asJavaMap();
155
156 tenantIdNetworkIdSetConsistentMap = storageService.<TenantId, Set<NetworkId>>consistentMapBuilder()
157 .withSerializer(SERIALIZER)
158 .withName("onos-tenantId-networkIds")
159 .withRelaxedReadConsistency()
160 .build();
161 tenantIdNetworkIdSetMap = tenantIdNetworkIdSetConsistentMap.asJavaMap();
162
163 deviceIdVirtualDeviceConsistentMap = storageService.<DeviceId, VirtualDevice>consistentMapBuilder()
164 .withSerializer(SERIALIZER)
165 .withName("onos-deviceId-virtualdevice")
166 .withRelaxedReadConsistency()
167 .build();
168 deviceIdVirtualDeviceMap = deviceIdVirtualDeviceConsistentMap.asJavaMap();
169
170 networkIdDeviceIdSetConsistentMap = storageService.<NetworkId, Set<DeviceId>>consistentMapBuilder()
171 .withSerializer(SERIALIZER)
172 .withName("onos-networkId-deviceIds")
173 .withRelaxedReadConsistency()
174 .build();
175 networkIdDeviceIdSetMap = networkIdDeviceIdSetConsistentMap.asJavaMap();
176
177 networkIdVirtualLinkSetConsistentMap = storageService.<NetworkId, Set<VirtualLink>>consistentMapBuilder()
178 .withSerializer(SERIALIZER)
179 .withName("onos-networkId-virtuallinks")
180 .withRelaxedReadConsistency()
181 .build();
182 networkIdVirtualLinkSetMap = networkIdVirtualLinkSetConsistentMap.asJavaMap();
183
184 networkIdVirtualPortSetConsistentMap = storageService.<NetworkId, Set<VirtualPort>>consistentMapBuilder()
185 .withSerializer(SERIALIZER)
186 .withName("onos-networkId-virtualportss")
187 .withRelaxedReadConsistency()
188 .build();
189 networkIdVirtualPortSetMap = networkIdVirtualPortSetConsistentMap.asJavaMap();
190
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700191 log.info("Started");
192 }
193
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500194 /**
195 * Distributed network store service deactivate method.
196 */
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700197 @Deactivate
198 public void deactivate() {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500199 tenantIdSet.removeListener(setListener);
200 networkIdVirtualNetworkConsistentMap.removeListener(virtualMapListener);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700201 log.info("Stopped");
202 }
203
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500204 /**
205 * This method is used for Junit tests to set the CoreService instance, which
206 * is required to set the IdGenerator instance.
207 *
208 * @param coreService core service instance
209 */
210 public void setCoreService(CoreService coreService) {
211 this.coreService = coreService;
212 }
213
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700214 @Override
215 public void addTenantId(TenantId tenantId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500216 tenantIdSet.add(tenantId);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700217 }
218
219 @Override
220 public void removeTenantId(TenantId tenantId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500221 tenantIdSet.remove(tenantId);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700222 }
223
224 @Override
225 public Set<TenantId> getTenantIds() {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500226 return ImmutableSet.copyOf(tenantIdSet);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700227 }
228
229 @Override
230 public VirtualNetwork addNetwork(TenantId tenantId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500231
232 checkState(tenantIdSet.contains(tenantId), "The tenant has not been registered. " + tenantId.id());
233 VirtualNetwork virtualNetwork = new DefaultVirtualNetwork(genNetworkId(), tenantId);
234 //TODO update both maps in one transaction.
235 networkIdVirtualNetworkMap.put(virtualNetwork.id(), virtualNetwork);
236 Set<NetworkId> virtualNetworkSet = tenantIdNetworkIdSetMap.get(tenantId);
237 if (virtualNetworkSet == null) {
238 virtualNetworkSet = new HashSet<>();
239 }
240 virtualNetworkSet.add(virtualNetwork.id());
241 tenantIdNetworkIdSetMap.put(tenantId, virtualNetworkSet);
242 return virtualNetwork;
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700243 }
244
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500245 /**
246 * Returns a new network identifier from a virtual network block of identifiers.
247 *
248 * @return NetworkId network identifier
249 */
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700250 private NetworkId genNetworkId() {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500251 return NetworkId.networkId(idGenerator.getNewId());
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700252 }
253
254
255 @Override
256 public void removeNetwork(NetworkId networkId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500257 // Make sure that the virtual network exists before attempting to remove it.
258 if (networkExists(networkId)) {
259 VirtualNetwork virtualNetwork = networkIdVirtualNetworkMap.get(networkId);
260 if (virtualNetwork == null) {
261 return;
262 }
263 //TODO update both maps in one transaction.
264 TenantId tenantId = virtualNetwork.tenantId();
265 networkIdVirtualNetworkMap.compute(networkId, (id, existingVirtualNetwork) -> null);
266
267
268 Set<NetworkId> virtualNetworkSet = tenantIdNetworkIdSetMap.get(tenantId);
269 tenantIdNetworkIdSetMap.compute(virtualNetwork.tenantId(), (id, existingNetworkIds) -> {
270 if (existingNetworkIds == null || existingNetworkIds.isEmpty()) {
271 return ImmutableSet.of();
272 } else {
273 return ImmutableSet.<NetworkId>builder()
274 .addAll(Sets.difference(existingNetworkIds,
275 ImmutableSet.copyOf(virtualNetworkSet)))
276 .build();
277 }
278 });
279 }
280 }
281
282 /**
283 * Returns if the network identifier exists.
284 *
285 * @param networkId network identifier
286 * @return true if the network identifier exists, false otherwise.
287 */
288 private boolean networkExists(NetworkId networkId) {
289 return (networkIdVirtualNetworkMap.containsKey(networkId));
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700290 }
291
292 @Override
293 public VirtualDevice addDevice(NetworkId networkId, DeviceId deviceId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500294 checkState(networkExists(networkId), "The network has not been added.");
295 Set<DeviceId> deviceIdSet = networkIdDeviceIdSetMap.get(networkId);
296 if (deviceIdSet == null) {
297 deviceIdSet = new HashSet<>();
298 }
299 VirtualDevice virtualDevice = new DefaultVirtualDevice(networkId, deviceId);
300 //TODO update both maps in one transaction.
301 deviceIdVirtualDeviceMap.put(deviceId, virtualDevice);
302 deviceIdSet.add(deviceId);
303 networkIdDeviceIdSetMap.put(networkId, deviceIdSet);
304 return virtualDevice;
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700305 }
306
307 @Override
308 public void removeDevice(NetworkId networkId, DeviceId deviceId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500309 checkState(networkExists(networkId), "The network has not been added.");
310 //TODO update both maps in one transaction.
311 Set<DeviceId> deviceIdSet = networkIdDeviceIdSetMap.get(networkId);
312 if (deviceIdSet != null) {
313 networkIdDeviceIdSetMap.compute(networkId, (id, existingDeviceIds) -> {
314 if (existingDeviceIds == null || existingDeviceIds.isEmpty()) {
315 return ImmutableSet.of();
316 } else {
317 return ImmutableSet.<DeviceId>builder()
318 .addAll(Sets.difference(existingDeviceIds,
319 ImmutableSet.copyOf(deviceIdSet)))
320 .build();
321 }
322 });
323
324 deviceIdVirtualDeviceMap.compute(deviceId, (id, existingVirtualDevice) -> null);
325
326 log.info("The deviceIdVirtualDeviceMap size is: " + getDevices(networkId));
327 }
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700328 }
329
330 @Override
331 public VirtualLink addLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst, TunnelId realizedBy) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500332 checkState(networkExists(networkId), "The network has not been added.");
333 Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId);
334 if (virtualLinkSet == null) {
335 virtualLinkSet = new HashSet<>();
336 }
337 VirtualLink virtualLink = new DefaultVirtualLink(networkId, src, dst, realizedBy);
338 virtualLinkSet.add(virtualLink);
339 networkIdVirtualLinkSetMap.put(networkId, virtualLinkSet);
340 return virtualLink;
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700341 }
342
343 @Override
344 public void removeLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500345 checkState(networkExists(networkId), "The network has not been added.");
346 Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId);
347 if (virtualLinkSet != null) {
348 networkIdVirtualLinkSetMap.compute(networkId, (id, existingVirtualLinks) -> {
349 if (existingVirtualLinks == null || existingVirtualLinks.isEmpty()) {
350 return ImmutableSet.of();
351 } else {
352 return ImmutableSet.<VirtualLink>builder()
353 .addAll(Sets.difference(existingVirtualLinks,
354 ImmutableSet.copyOf(virtualLinkSet)))
355 .build();
356 }
357 });
358 }
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700359 }
360
361 @Override
362 public VirtualPort addPort(NetworkId networkId, DeviceId deviceId, PortNumber portNumber, Port realizedBy) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500363 checkState(networkExists(networkId), "The network has not been added.");
364 Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId);
365 if (virtualPortSet == null) {
366 virtualPortSet = new HashSet<>();
367 }
368 Device device = deviceIdVirtualDeviceMap.get(deviceId);
369 checkNotNull(device, "The device has not been created for deviceId: " + deviceId);
370 VirtualPort virtualPort = new DefaultVirtualPort(networkId, device, portNumber, realizedBy);
371 virtualPortSet.add(virtualPort);
372 networkIdVirtualPortSetMap.put(networkId, virtualPortSet);
373 return virtualPort;
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700374 }
375
376 @Override
377 public void removePort(NetworkId networkId, DeviceId deviceId, PortNumber portNumber) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500378 checkState(networkExists(networkId), "The network has not been added.");
379 Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId);
380 if (virtualPortSet != null) {
381 networkIdVirtualPortSetMap.compute(networkId, (id, existingVirtualPorts) -> {
382 if (existingVirtualPorts == null || existingVirtualPorts.isEmpty()) {
383 return ImmutableSet.of();
384 } else {
385 return ImmutableSet.<VirtualPort>builder()
386 .addAll(Sets.difference(existingVirtualPorts,
387 ImmutableSet.copyOf(virtualPortSet)))
388 .build();
389 }
390 });
391 }
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700392 }
393
394 @Override
395 public Set<VirtualNetwork> getNetworks(TenantId tenantId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500396 Set<NetworkId> networkIdSet = tenantIdNetworkIdSetMap.get(tenantId);
397 Set<VirtualNetwork> virtualNetworkSet = new HashSet<>();
398 if (networkIdSet != null) {
399 networkIdSet.forEach(networkId -> virtualNetworkSet.add(networkIdVirtualNetworkMap.get(networkId)));
400 }
401 return ImmutableSet.copyOf(virtualNetworkSet);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700402 }
403
404 @Override
405 public Set<VirtualDevice> getDevices(NetworkId networkId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500406 checkState(networkExists(networkId), "The network has not been added.");
407 Set<DeviceId> deviceIdSet = networkIdDeviceIdSetMap.get(networkId);
408 Set<VirtualDevice> virtualDeviceSet = new HashSet<>();
409 if (deviceIdSet != null) {
410 deviceIdSet.forEach(deviceId -> virtualDeviceSet.add(deviceIdVirtualDeviceMap.get(deviceId)));
411 }
412 return ImmutableSet.copyOf(virtualDeviceSet);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700413 }
414
415 @Override
416 public Set<VirtualLink> getLinks(NetworkId networkId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500417 checkState(networkExists(networkId), "The network has not been added.");
418 Set<VirtualLink> virtualLinkSet = new HashSet<>();
419 virtualLinkSet.addAll(networkIdVirtualLinkSetMap.get(networkId));
420 return ImmutableSet.copyOf(virtualLinkSet);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700421 }
422
423 @Override
424 public Set<VirtualPort> getPorts(NetworkId networkId, DeviceId deviceId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500425 checkState(networkExists(networkId), "The network has not been added.");
426 Set<VirtualPort> virtualPortSet = new HashSet<>();
427 virtualPortSet.addAll(networkIdVirtualPortSetMap.get(networkId));
428 return ImmutableSet.copyOf(virtualPortSet);
429 }
430
431 /**
432 * Listener class to map listener set events to the virtual network events.
433 */
434 private class InternalSetListener implements SetEventListener<TenantId> {
435 @Override
436 public void event(SetEvent<TenantId> event) {
437 VirtualNetworkEvent.Type type = null;
438 switch (event.type()) {
439 case ADD:
440 type = VirtualNetworkEvent.Type.TENANT_REGISTERED;
441 break;
442 case REMOVE:
443 type = VirtualNetworkEvent.Type.TENANT_UNREGISTERED;
444 break;
445 default:
446 log.error("Unsupported event type: " + event.type());
447 }
448 notifyDelegate(new VirtualNetworkEvent(type, null));
449 }
450 }
451
452 /**
453 * Listener class to map listener map events to the virtual network events.
454 */
455 private class InternalMapListener implements MapEventListener<NetworkId, VirtualNetwork> {
456 @Override
457 public void event(MapEvent<NetworkId, VirtualNetwork> event) {
458 NetworkId networkId = checkNotNull(event.key());
459 VirtualNetworkEvent.Type type = null;
460 switch (event.type()) {
461 case INSERT:
462 type = VirtualNetworkEvent.Type.NETWORK_ADDED;
463 break;
464 case UPDATE:
465 if ((event.oldValue().value() != null) && (event.newValue().value() == null)) {
466 type = VirtualNetworkEvent.Type.NETWORK_REMOVED;
467 } else {
468 type = VirtualNetworkEvent.Type.NETWORK_UPDATED;
469 }
470 break;
471 case REMOVE:
472 type = VirtualNetworkEvent.Type.NETWORK_REMOVED;
473 break;
474 default:
475 log.error("Unsupported event type: " + event.type());
476 }
477 notifyDelegate(new VirtualNetworkEvent(type, networkId));
478 }
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700479 }
480}