blob: b4c1feecbf5c1db959bba5beeb0d0222e5d465f1 [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.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 Stanke7a81b532016-06-14 15:43:51 -040026import org.onlab.packet.IpAddress;
27import org.onlab.packet.MacAddress;
28import org.onlab.packet.VlanId;
Brian Stanke0e5c94e2016-03-08 11:20:04 -050029import org.onlab.util.KryoNamespace;
30import org.onosproject.core.CoreService;
31import org.onosproject.core.IdGenerator;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070032import org.onosproject.incubator.net.tunnel.TunnelId;
33import org.onosproject.incubator.net.virtual.DefaultVirtualDevice;
Brian Stanke7a81b532016-06-14 15:43:51 -040034import org.onosproject.incubator.net.virtual.DefaultVirtualHost;
Brian Stanke0e5c94e2016-03-08 11:20:04 -050035import org.onosproject.incubator.net.virtual.DefaultVirtualLink;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070036import org.onosproject.incubator.net.virtual.DefaultVirtualNetwork;
Brian Stanke0e5c94e2016-03-08 11:20:04 -050037import org.onosproject.incubator.net.virtual.DefaultVirtualPort;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070038import org.onosproject.incubator.net.virtual.NetworkId;
39import org.onosproject.incubator.net.virtual.TenantId;
40import org.onosproject.incubator.net.virtual.VirtualDevice;
Brian Stanke7a81b532016-06-14 15:43:51 -040041import org.onosproject.incubator.net.virtual.VirtualHost;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070042import org.onosproject.incubator.net.virtual.VirtualLink;
43import org.onosproject.incubator.net.virtual.VirtualNetwork;
44import org.onosproject.incubator.net.virtual.VirtualNetworkEvent;
Brian Stanke11f6d532016-07-05 16:17:59 -040045import org.onosproject.incubator.net.virtual.VirtualNetworkIntent;
Brian Stanke0e5c94e2016-03-08 11:20:04 -050046import org.onosproject.incubator.net.virtual.VirtualNetworkService;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070047import org.onosproject.incubator.net.virtual.VirtualNetworkStore;
48import org.onosproject.incubator.net.virtual.VirtualNetworkStoreDelegate;
49import org.onosproject.incubator.net.virtual.VirtualPort;
50import org.onosproject.net.ConnectPoint;
Brian Stanke0e5c94e2016-03-08 11:20:04 -050051import org.onosproject.net.Device;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070052import org.onosproject.net.DeviceId;
Brian Stanke7a81b532016-06-14 15:43:51 -040053import org.onosproject.net.HostId;
54import org.onosproject.net.HostLocation;
Brian Stanke612cebf2016-05-02 10:21:33 -040055import org.onosproject.net.Link;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070056import org.onosproject.net.PortNumber;
Brian Stanke11f6d532016-07-05 16:17:59 -040057import org.onosproject.net.intent.Intent;
58import org.onosproject.net.intent.IntentData;
59import org.onosproject.net.intent.IntentState;
60import org.onosproject.net.intent.Key;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070061import org.onosproject.store.AbstractStore;
Brian Stanke0e5c94e2016-03-08 11:20:04 -050062import org.onosproject.store.serializers.KryoNamespaces;
63import org.onosproject.store.service.ConsistentMap;
64import org.onosproject.store.service.DistributedSet;
65import org.onosproject.store.service.MapEvent;
66import org.onosproject.store.service.MapEventListener;
67import org.onosproject.store.service.Serializer;
68import org.onosproject.store.service.SetEvent;
69import org.onosproject.store.service.SetEventListener;
70import org.onosproject.store.service.StorageService;
Brian Stanke11f6d532016-07-05 16:17:59 -040071import org.onosproject.store.service.WallClockTimestamp;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070072import org.slf4j.Logger;
73
Brian Stanke0e5c94e2016-03-08 11:20:04 -050074import java.util.HashSet;
75import java.util.Map;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070076import java.util.Set;
Claudine Chiu7f872a72016-12-16 13:51:39 -050077import java.util.concurrent.atomic.AtomicBoolean;
Claudine Chiu945828d2016-11-21 12:47:07 -050078import java.util.function.BiFunction;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070079
Brian Stanke0e5c94e2016-03-08 11:20:04 -050080import static com.google.common.base.Preconditions.checkNotNull;
81import static com.google.common.base.Preconditions.checkState;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070082import static org.slf4j.LoggerFactory.getLogger;
83
84/**
Brian Stanke0e5c94e2016-03-08 11:20:04 -050085 * Implementation of the virtual network store.
Thomas Vachuska33979fd2015-07-31 11:41:14 -070086 */
87@Component(immediate = true)
88@Service
89public class DistributedVirtualNetworkStore
90 extends AbstractStore<VirtualNetworkEvent, VirtualNetworkStoreDelegate>
91 implements VirtualNetworkStore {
92
93 private final Logger log = getLogger(getClass());
94
Brian Stanke0e5c94e2016-03-08 11:20:04 -050095 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected StorageService storageService;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070097
Brian Stanke0e5c94e2016-03-08 11:20:04 -050098 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
99 protected CoreService coreService;
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700100
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500101 private IdGenerator idGenerator;
102
103 // Track tenants by ID
104 private DistributedSet<TenantId> tenantIdSet;
105
106 // Listener for tenant events
107 private final SetEventListener<TenantId> setListener = new InternalSetListener();
108
109 // Track virtual networks by network Id
110 private ConsistentMap<NetworkId, VirtualNetwork> networkIdVirtualNetworkConsistentMap;
111 private Map<NetworkId, VirtualNetwork> networkIdVirtualNetworkMap;
112
113 // Listener for virtual network events
Claudine Chiu945828d2016-11-21 12:47:07 -0500114 private final MapEventListener<NetworkId, VirtualNetwork> virtualNetworkMapListener =
115 new InternalMapListener<>((mapEventType, virtualNetwork) -> {
116 VirtualNetworkEvent.Type eventType =
117 mapEventType.equals(MapEvent.Type.INSERT) ? VirtualNetworkEvent.Type.NETWORK_ADDED :
118 mapEventType.equals(MapEvent.Type.UPDATE) ? VirtualNetworkEvent.Type.NETWORK_UPDATED :
119 mapEventType.equals(MapEvent.Type.REMOVE) ? VirtualNetworkEvent.Type.NETWORK_REMOVED : null;
120 return eventType == null ? null : new VirtualNetworkEvent(eventType, virtualNetwork.id());
121 });
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500122
123 // Track virtual network IDs by tenant Id
124 private ConsistentMap<TenantId, Set<NetworkId>> tenantIdNetworkIdSetConsistentMap;
125 private Map<TenantId, Set<NetworkId>> tenantIdNetworkIdSetMap;
126
127 // Track virtual devices by device Id
128 private ConsistentMap<DeviceId, VirtualDevice> deviceIdVirtualDeviceConsistentMap;
129 private Map<DeviceId, VirtualDevice> deviceIdVirtualDeviceMap;
130
Claudine Chiu945828d2016-11-21 12:47:07 -0500131 // Listener for virtual device events
132 private final MapEventListener<DeviceId, VirtualDevice> virtualDeviceMapListener =
133 new InternalMapListener<>((mapEventType, virtualDevice) -> {
134 VirtualNetworkEvent.Type eventType =
135 mapEventType.equals(MapEvent.Type.INSERT) ? VirtualNetworkEvent.Type.VIRTUAL_DEVICE_ADDED :
136 mapEventType.equals(MapEvent.Type.UPDATE) ? VirtualNetworkEvent.Type.VIRTUAL_DEVICE_UPDATED :
137 mapEventType.equals(MapEvent.Type.REMOVE) ? VirtualNetworkEvent.Type.VIRTUAL_DEVICE_REMOVED : null;
138 return eventType == null ? null :
139 new VirtualNetworkEvent(eventType, virtualDevice.networkId(), virtualDevice);
140 });
141
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500142 // Track device IDs by network Id
143 private ConsistentMap<NetworkId, Set<DeviceId>> networkIdDeviceIdSetConsistentMap;
144 private Map<NetworkId, Set<DeviceId>> networkIdDeviceIdSetMap;
145
Brian Stanke7a81b532016-06-14 15:43:51 -0400146 // Track virtual hosts by host Id
147 private ConsistentMap<HostId, VirtualHost> hostIdVirtualHostConsistentMap;
148 private Map<HostId, VirtualHost> hostIdVirtualHostMap;
149
150 // Track host IDs by network Id
151 private ConsistentMap<NetworkId, Set<HostId>> networkIdHostIdSetConsistentMap;
152 private Map<NetworkId, Set<HostId>> networkIdHostIdSetMap;
153
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500154 // Track virtual links by network Id
155 private ConsistentMap<NetworkId, Set<VirtualLink>> networkIdVirtualLinkSetConsistentMap;
156 private Map<NetworkId, Set<VirtualLink>> networkIdVirtualLinkSetMap;
157
158 // Track virtual ports by network Id
159 private ConsistentMap<NetworkId, Set<VirtualPort>> networkIdVirtualPortSetConsistentMap;
160 private Map<NetworkId, Set<VirtualPort>> networkIdVirtualPortSetMap;
161
Brian Stanke11f6d532016-07-05 16:17:59 -0400162 // Track intent key to intent data
163 private ConsistentMap<Key, IntentData> intentKeyIntentDataConsistentMap;
164 private Map<Key, IntentData> intentKeyIntentDataMap;
165
166 // Track intent ID to TunnelIds
167 private ConsistentMap<Key, Set<TunnelId>> intentKeyTunnelIdSetConsistentMap;
168 private Map<Key, Set<TunnelId>> intentKeyTunnelIdSetMap;
169
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500170 private static final Serializer SERIALIZER = Serializer
171 .using(new KryoNamespace.Builder().register(KryoNamespaces.API)
172 .register(TenantId.class)
HIGUCHI Yutac0f50452016-05-13 19:26:05 -0700173 .register(NetworkId.class)
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500174 .register(VirtualNetwork.class)
Brian Stanke5df14472016-03-11 19:34:38 -0500175 .register(DefaultVirtualNetwork.class)
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500176 .register(VirtualDevice.class)
Brian Stanke5df14472016-03-11 19:34:38 -0500177 .register(DefaultVirtualDevice.class)
Brian Stanke7a81b532016-06-14 15:43:51 -0400178 .register(VirtualHost.class)
179 .register(DefaultVirtualHost.class)
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500180 .register(VirtualLink.class)
Brian Stanke5df14472016-03-11 19:34:38 -0500181 .register(DefaultVirtualLink.class)
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500182 .register(VirtualPort.class)
Brian Stanke5df14472016-03-11 19:34:38 -0500183 .register(DefaultVirtualPort.class)
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500184 .register(Device.class)
Brian Stanke9a108972016-04-11 15:25:17 -0400185 .register(TunnelId.class)
Brian Stanke11f6d532016-07-05 16:17:59 -0400186 .register(IntentData.class)
187 .register(VirtualNetworkIntent.class)
188 .register(WallClockTimestamp.class)
HIGUCHI Yuta03666a32016-05-18 11:49:09 -0700189 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
Brian Stanke11f6d532016-07-05 16:17:59 -0400190 .build());
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500191
192 /**
193 * Distributed network store service activate method.
194 */
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700195 @Activate
196 public void activate() {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500197 idGenerator = coreService.getIdGenerator(VirtualNetworkService.VIRTUAL_NETWORK_TOPIC);
198
199 tenantIdSet = storageService.<TenantId>setBuilder()
200 .withSerializer(SERIALIZER)
201 .withName("onos-tenantId")
202 .withRelaxedReadConsistency()
203 .build()
204 .asDistributedSet();
205 tenantIdSet.addListener(setListener);
206
207 networkIdVirtualNetworkConsistentMap = storageService.<NetworkId, VirtualNetwork>consistentMapBuilder()
208 .withSerializer(SERIALIZER)
209 .withName("onos-networkId-virtualnetwork")
210 .withRelaxedReadConsistency()
211 .build();
Claudine Chiu945828d2016-11-21 12:47:07 -0500212 networkIdVirtualNetworkConsistentMap.addListener(virtualNetworkMapListener);
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500213 networkIdVirtualNetworkMap = networkIdVirtualNetworkConsistentMap.asJavaMap();
214
215 tenantIdNetworkIdSetConsistentMap = storageService.<TenantId, Set<NetworkId>>consistentMapBuilder()
216 .withSerializer(SERIALIZER)
217 .withName("onos-tenantId-networkIds")
218 .withRelaxedReadConsistency()
219 .build();
220 tenantIdNetworkIdSetMap = tenantIdNetworkIdSetConsistentMap.asJavaMap();
221
222 deviceIdVirtualDeviceConsistentMap = storageService.<DeviceId, VirtualDevice>consistentMapBuilder()
223 .withSerializer(SERIALIZER)
224 .withName("onos-deviceId-virtualdevice")
225 .withRelaxedReadConsistency()
226 .build();
Claudine Chiu945828d2016-11-21 12:47:07 -0500227 deviceIdVirtualDeviceConsistentMap.addListener(virtualDeviceMapListener);
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500228 deviceIdVirtualDeviceMap = deviceIdVirtualDeviceConsistentMap.asJavaMap();
229
230 networkIdDeviceIdSetConsistentMap = storageService.<NetworkId, Set<DeviceId>>consistentMapBuilder()
231 .withSerializer(SERIALIZER)
232 .withName("onos-networkId-deviceIds")
233 .withRelaxedReadConsistency()
234 .build();
235 networkIdDeviceIdSetMap = networkIdDeviceIdSetConsistentMap.asJavaMap();
236
Brian Stanke7a81b532016-06-14 15:43:51 -0400237 hostIdVirtualHostConsistentMap = storageService.<HostId, VirtualHost>consistentMapBuilder()
238 .withSerializer(SERIALIZER)
239 .withName("onos-hostId-virtualhost")
240 .withRelaxedReadConsistency()
241 .build();
242 hostIdVirtualHostMap = hostIdVirtualHostConsistentMap.asJavaMap();
243
244 networkIdHostIdSetConsistentMap = storageService.<NetworkId, Set<HostId>>consistentMapBuilder()
245 .withSerializer(SERIALIZER)
246 .withName("onos-networkId-hostIds")
247 .withRelaxedReadConsistency()
248 .build();
249 networkIdHostIdSetMap = networkIdHostIdSetConsistentMap.asJavaMap();
250
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500251 networkIdVirtualLinkSetConsistentMap = storageService.<NetworkId, Set<VirtualLink>>consistentMapBuilder()
252 .withSerializer(SERIALIZER)
253 .withName("onos-networkId-virtuallinks")
254 .withRelaxedReadConsistency()
255 .build();
256 networkIdVirtualLinkSetMap = networkIdVirtualLinkSetConsistentMap.asJavaMap();
257
258 networkIdVirtualPortSetConsistentMap = storageService.<NetworkId, Set<VirtualPort>>consistentMapBuilder()
259 .withSerializer(SERIALIZER)
Brian Stanke11f6d532016-07-05 16:17:59 -0400260 .withName("onos-networkId-virtualports")
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500261 .withRelaxedReadConsistency()
262 .build();
263 networkIdVirtualPortSetMap = networkIdVirtualPortSetConsistentMap.asJavaMap();
264
Brian Stanke11f6d532016-07-05 16:17:59 -0400265 intentKeyTunnelIdSetConsistentMap = storageService.<Key, Set<TunnelId>>consistentMapBuilder()
266 .withSerializer(SERIALIZER)
267 .withName("onos-intentKey-tunnelIds")
268 .withRelaxedReadConsistency()
269 .build();
270 intentKeyTunnelIdSetMap = intentKeyTunnelIdSetConsistentMap.asJavaMap();
271
272 intentKeyIntentDataConsistentMap = storageService.<Key, IntentData>consistentMapBuilder()
273 .withSerializer(SERIALIZER)
274 .withName("onos-intentKey-intentData")
275 .withRelaxedReadConsistency()
276 .build();
277 intentKeyIntentDataMap = intentKeyIntentDataConsistentMap.asJavaMap();
278
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700279 log.info("Started");
280 }
281
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500282 /**
283 * Distributed network store service deactivate method.
284 */
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700285 @Deactivate
286 public void deactivate() {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500287 tenantIdSet.removeListener(setListener);
Claudine Chiu945828d2016-11-21 12:47:07 -0500288 networkIdVirtualNetworkConsistentMap.removeListener(virtualNetworkMapListener);
289 deviceIdVirtualDeviceConsistentMap.removeListener(virtualDeviceMapListener);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700290 log.info("Stopped");
291 }
292
293 @Override
294 public void addTenantId(TenantId tenantId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500295 tenantIdSet.add(tenantId);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700296 }
297
298 @Override
299 public void removeTenantId(TenantId tenantId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500300 tenantIdSet.remove(tenantId);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700301 }
302
303 @Override
304 public Set<TenantId> getTenantIds() {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500305 return ImmutableSet.copyOf(tenantIdSet);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700306 }
307
308 @Override
309 public VirtualNetwork addNetwork(TenantId tenantId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500310
311 checkState(tenantIdSet.contains(tenantId), "The tenant has not been registered. " + tenantId.id());
312 VirtualNetwork virtualNetwork = new DefaultVirtualNetwork(genNetworkId(), tenantId);
313 //TODO update both maps in one transaction.
314 networkIdVirtualNetworkMap.put(virtualNetwork.id(), virtualNetwork);
Brian Stanke5df14472016-03-11 19:34:38 -0500315
316 Set<NetworkId> networkIdSet = tenantIdNetworkIdSetMap.get(tenantId);
317 if (networkIdSet == null) {
318 networkIdSet = new HashSet<>();
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500319 }
Brian Stanke5df14472016-03-11 19:34:38 -0500320 networkIdSet.add(virtualNetwork.id());
321 tenantIdNetworkIdSetMap.put(tenantId, networkIdSet);
322
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500323 return virtualNetwork;
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700324 }
325
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500326 /**
327 * Returns a new network identifier from a virtual network block of identifiers.
328 *
329 * @return NetworkId network identifier
330 */
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700331 private NetworkId genNetworkId() {
Kenji HIKICHIcfdf91b2016-05-25 13:04:45 +0900332 NetworkId networkId;
333 do {
334 networkId = NetworkId.networkId(idGenerator.getNewId());
335 } while (!networkId.isVirtualNetworkId());
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700336
Kenji HIKICHIcfdf91b2016-05-25 13:04:45 +0900337 return networkId;
338 }
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700339
340 @Override
341 public void removeNetwork(NetworkId networkId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500342 // Make sure that the virtual network exists before attempting to remove it.
343 if (networkExists(networkId)) {
Brian Stanke5df14472016-03-11 19:34:38 -0500344 //TODO update both maps in one transaction.
345
346 VirtualNetwork virtualNetwork = networkIdVirtualNetworkMap.remove(networkId);
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500347 if (virtualNetwork == null) {
348 return;
349 }
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500350 TenantId tenantId = virtualNetwork.tenantId();
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500351
Brian Stanke5df14472016-03-11 19:34:38 -0500352 Set<NetworkId> networkIdSet = new HashSet<>();
353 tenantIdNetworkIdSetMap.get(tenantId).forEach(networkId1 -> {
354 if (networkId1.id().equals(networkId.id())) {
355 networkIdSet.add(networkId1);
356 }
357 });
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500358
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500359 tenantIdNetworkIdSetMap.compute(virtualNetwork.tenantId(), (id, existingNetworkIds) -> {
360 if (existingNetworkIds == null || existingNetworkIds.isEmpty()) {
HIGUCHI Yutac0f50452016-05-13 19:26:05 -0700361 return new HashSet<>();
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500362 } else {
HIGUCHI Yutac0f50452016-05-13 19:26:05 -0700363 return new HashSet<>(Sets.difference(existingNetworkIds, networkIdSet));
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500364 }
365 });
366 }
367 }
368
369 /**
370 * Returns if the network identifier exists.
371 *
372 * @param networkId network identifier
373 * @return true if the network identifier exists, false otherwise.
374 */
375 private boolean networkExists(NetworkId networkId) {
Brian Stanke612cebf2016-05-02 10:21:33 -0400376 checkNotNull(networkId, "The network identifier cannot be null.");
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500377 return (networkIdVirtualNetworkMap.containsKey(networkId));
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700378 }
379
380 @Override
381 public VirtualDevice addDevice(NetworkId networkId, DeviceId deviceId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500382 checkState(networkExists(networkId), "The network has not been added.");
383 Set<DeviceId> deviceIdSet = networkIdDeviceIdSetMap.get(networkId);
384 if (deviceIdSet == null) {
385 deviceIdSet = new HashSet<>();
386 }
387 VirtualDevice virtualDevice = new DefaultVirtualDevice(networkId, deviceId);
388 //TODO update both maps in one transaction.
389 deviceIdVirtualDeviceMap.put(deviceId, virtualDevice);
390 deviceIdSet.add(deviceId);
391 networkIdDeviceIdSetMap.put(networkId, deviceIdSet);
392 return virtualDevice;
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700393 }
394
395 @Override
396 public void removeDevice(NetworkId networkId, DeviceId deviceId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500397 checkState(networkExists(networkId), "The network has not been added.");
398 //TODO update both maps in one transaction.
Brian Stanke5df14472016-03-11 19:34:38 -0500399
400 Set<DeviceId> deviceIdSet = new HashSet<>();
401 networkIdDeviceIdSetMap.get(networkId).forEach(deviceId1 -> {
402 if (deviceId1.equals(deviceId)) {
403 deviceIdSet.add(deviceId1);
404 }
405 });
406
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500407 if (deviceIdSet != null) {
408 networkIdDeviceIdSetMap.compute(networkId, (id, existingDeviceIds) -> {
409 if (existingDeviceIds == null || existingDeviceIds.isEmpty()) {
HIGUCHI Yutac0f50452016-05-13 19:26:05 -0700410 return new HashSet<>();
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500411 } else {
HIGUCHI Yutac0f50452016-05-13 19:26:05 -0700412 return new HashSet<>(Sets.difference(existingDeviceIds, deviceIdSet));
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500413 }
414 });
415
Brian Stanke5df14472016-03-11 19:34:38 -0500416 deviceIdVirtualDeviceMap.remove(deviceId);
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500417 }
Brian Stanke7a81b532016-06-14 15:43:51 -0400418 //TODO remove virtual links and ports when removing the virtual device
419 }
420
421 @Override
422 public VirtualHost addHost(NetworkId networkId, HostId hostId, MacAddress mac,
423 VlanId vlan, HostLocation location, Set<IpAddress> ips) {
424 checkState(networkExists(networkId), "The network has not been added.");
425 Set<HostId> hostIdSet = networkIdHostIdSetMap.get(networkId);
426 if (hostIdSet == null) {
427 hostIdSet = new HashSet<>();
428 }
429 VirtualHost virtualhost = new DefaultVirtualHost(networkId, hostId, mac, vlan, location, ips);
430 //TODO update both maps in one transaction.
431 hostIdVirtualHostMap.put(hostId, virtualhost);
432 hostIdSet.add(hostId);
433 networkIdHostIdSetMap.put(networkId, hostIdSet);
434 return virtualhost;
435 }
436
437 @Override
438 public void removeHost(NetworkId networkId, HostId hostId) {
439 checkState(networkExists(networkId), "The network has not been added.");
440 //TODO update both maps in one transaction.
441
442 Set<HostId> hostIdSet = new HashSet<>();
443 networkIdHostIdSetMap.get(networkId).forEach(hostId1 -> {
444 if (hostId1.equals(hostId)) {
445 hostIdSet.add(hostId1);
446 }
447 });
448
449 if (hostIdSet != null) {
450 networkIdHostIdSetMap.compute(networkId, (id, existingHostIds) -> {
451 if (existingHostIds == null || existingHostIds.isEmpty()) {
452 return new HashSet<>();
453 } else {
454 return new HashSet<>(Sets.difference(existingHostIds, hostIdSet));
455 }
456 });
457
458 hostIdVirtualHostMap.remove(hostId);
459 }
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700460 }
461
462 @Override
Brian Stanke612cebf2016-05-02 10:21:33 -0400463 public VirtualLink addLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst,
464 Link.State state, TunnelId realizedBy) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500465 checkState(networkExists(networkId), "The network has not been added.");
466 Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId);
467 if (virtualLinkSet == null) {
468 virtualLinkSet = new HashSet<>();
469 }
Brian Stanke9a108972016-04-11 15:25:17 -0400470 // validate that the link does not already exist in this network
471 checkState(getLink(networkId, src, dst) == null, "The virtual link already exists");
472
473 VirtualLink virtualLink = DefaultVirtualLink.builder()
474 .networkId(networkId)
475 .src(src)
476 .dst(dst)
Brian Stanke612cebf2016-05-02 10:21:33 -0400477 .state(state)
Brian Stanke9a108972016-04-11 15:25:17 -0400478 .tunnelId(realizedBy)
479 .build();
480
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500481 virtualLinkSet.add(virtualLink);
482 networkIdVirtualLinkSetMap.put(networkId, virtualLinkSet);
483 return virtualLink;
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700484 }
485
486 @Override
Brian Stanke612cebf2016-05-02 10:21:33 -0400487 public void updateLink(VirtualLink virtualLink, TunnelId tunnelId, Link.State state) {
Brian Stanke9a108972016-04-11 15:25:17 -0400488 checkState(networkExists(virtualLink.networkId()), "The network has not been added.");
489 Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(virtualLink.networkId());
490 if (virtualLinkSet == null) {
491 virtualLinkSet = new HashSet<>();
492 }
493 virtualLinkSet.remove(virtualLink);
494
495 VirtualLink newVirtualLink = DefaultVirtualLink.builder()
496 .networkId(virtualLink.networkId())
497 .src(virtualLink.src())
498 .dst(virtualLink.dst())
499 .tunnelId(tunnelId)
Brian Stanke612cebf2016-05-02 10:21:33 -0400500 .state(state)
Brian Stanke9a108972016-04-11 15:25:17 -0400501 .build();
502
503 virtualLinkSet.add(newVirtualLink);
504 networkIdVirtualLinkSetMap.put(newVirtualLink.networkId(), virtualLinkSet);
505 }
506
507 @Override
508 public VirtualLink removeLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500509 checkState(networkExists(networkId), "The network has not been added.");
Brian Stanke5df14472016-03-11 19:34:38 -0500510
Brian Stanke9a108972016-04-11 15:25:17 -0400511 final VirtualLink virtualLink = getLink(networkId, src, dst);
512 if (virtualLink == null) {
513 return null;
514 }
Brian Stanke5df14472016-03-11 19:34:38 -0500515 Set<VirtualLink> virtualLinkSet = new HashSet<>();
Brian Stanke9a108972016-04-11 15:25:17 -0400516 virtualLinkSet.add(virtualLink);
Brian Stanke5df14472016-03-11 19:34:38 -0500517
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500518 if (virtualLinkSet != null) {
519 networkIdVirtualLinkSetMap.compute(networkId, (id, existingVirtualLinks) -> {
520 if (existingVirtualLinks == null || existingVirtualLinks.isEmpty()) {
HIGUCHI Yutac0f50452016-05-13 19:26:05 -0700521 return new HashSet<>();
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500522 } else {
HIGUCHI Yutac0f50452016-05-13 19:26:05 -0700523 return new HashSet<>(Sets.difference(existingVirtualLinks, virtualLinkSet));
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500524 }
525 });
526 }
Brian Stanke9a108972016-04-11 15:25:17 -0400527 return virtualLink;
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700528 }
529
530 @Override
Yoonseon Han6c603892016-09-01 11:52:21 -0700531 public VirtualPort addPort(NetworkId networkId, DeviceId deviceId,
532 PortNumber portNumber, ConnectPoint realizedBy) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500533 checkState(networkExists(networkId), "The network has not been added.");
534 Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId);
Yoonseon Han6c603892016-09-01 11:52:21 -0700535
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500536 if (virtualPortSet == null) {
537 virtualPortSet = new HashSet<>();
538 }
Yoonseon Han6c603892016-09-01 11:52:21 -0700539
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500540 Device device = deviceIdVirtualDeviceMap.get(deviceId);
541 checkNotNull(device, "The device has not been created for deviceId: " + deviceId);
Yoonseon Han6c603892016-09-01 11:52:21 -0700542
543 boolean exist = virtualPortSet.stream().anyMatch(
544 p -> p.element().id().equals(deviceId) &&
545 p.number().equals(portNumber));
546 checkState(!exist, "The requested Port Number is already in use");
547
548 VirtualPort virtualPort = new DefaultVirtualPort(networkId, device,
549 portNumber, realizedBy);
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500550 virtualPortSet.add(virtualPort);
551 networkIdVirtualPortSetMap.put(networkId, virtualPortSet);
Claudine Chiu7f872a72016-12-16 13:51:39 -0500552 notifyDelegate(new VirtualNetworkEvent(VirtualNetworkEvent.Type.VIRTUAL_PORT_ADDED,
553 networkId, virtualPort));
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500554 return virtualPort;
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700555 }
556
557 @Override
Yoonseon Han6c603892016-09-01 11:52:21 -0700558 public void bindPort(NetworkId networkId, DeviceId deviceId,
559 PortNumber portNumber, ConnectPoint realizedBy) {
560
561 Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap
562 .get(networkId);
563
564 VirtualPort vPort = virtualPortSet.stream().filter(
565 p -> p.element().id().equals(deviceId) &&
566 p.number().equals(portNumber)).findFirst().get();
567 checkNotNull(vPort, "The virtual port has not been added.");
568
569 Device device = deviceIdVirtualDeviceMap.get(deviceId);
570 checkNotNull(device, "The device has not been created for deviceId: "
571 + deviceId);
572
573 virtualPortSet.remove(vPort);
574 vPort = new DefaultVirtualPort(networkId, device, portNumber, realizedBy);
575 virtualPortSet.add(vPort);
576 networkIdVirtualPortSetMap.put(networkId, virtualPortSet);
Claudine Chiu7f872a72016-12-16 13:51:39 -0500577 notifyDelegate(new VirtualNetworkEvent(VirtualNetworkEvent.Type.VIRTUAL_PORT_UPDATED,
578 networkId, vPort));
Yoonseon Han6c603892016-09-01 11:52:21 -0700579 }
580
581 @Override
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700582 public void removePort(NetworkId networkId, DeviceId deviceId, PortNumber portNumber) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500583 checkState(networkExists(networkId), "The network has not been added.");
Brian Stanke5df14472016-03-11 19:34:38 -0500584
585 Set<VirtualPort> virtualPortSet = new HashSet<>();
586 networkIdVirtualPortSetMap.get(networkId).forEach(port -> {
587 if (port.element().id().equals(deviceId) && port.number().equals(portNumber)) {
588 virtualPortSet.add(port);
589 }
590 });
591
Claudine Chiu7f872a72016-12-16 13:51:39 -0500592 if (!virtualPortSet.isEmpty()) {
593 AtomicBoolean portRemoved = new AtomicBoolean(false);
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500594 networkIdVirtualPortSetMap.compute(networkId, (id, existingVirtualPorts) -> {
595 if (existingVirtualPorts == null || existingVirtualPorts.isEmpty()) {
HIGUCHI Yutac0f50452016-05-13 19:26:05 -0700596 return new HashSet<>();
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500597 } else {
Claudine Chiu7f872a72016-12-16 13:51:39 -0500598 portRemoved.set(true);
HIGUCHI Yutac0f50452016-05-13 19:26:05 -0700599 return new HashSet<>(Sets.difference(existingVirtualPorts, virtualPortSet));
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500600 }
601 });
Claudine Chiu7f872a72016-12-16 13:51:39 -0500602 if (portRemoved.get()) {
603 virtualPortSet.forEach(virtualPort -> notifyDelegate(
604 new VirtualNetworkEvent(VirtualNetworkEvent.Type.VIRTUAL_PORT_REMOVED,
605 networkId, virtualPort)
606 ));
607 }
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500608 }
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700609 }
610
611 @Override
612 public Set<VirtualNetwork> getNetworks(TenantId tenantId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500613 Set<NetworkId> networkIdSet = tenantIdNetworkIdSetMap.get(tenantId);
614 Set<VirtualNetwork> virtualNetworkSet = new HashSet<>();
615 if (networkIdSet != null) {
616 networkIdSet.forEach(networkId -> virtualNetworkSet.add(networkIdVirtualNetworkMap.get(networkId)));
617 }
618 return ImmutableSet.copyOf(virtualNetworkSet);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700619 }
620
621 @Override
Brian Stanke86914282016-05-25 15:36:50 -0400622 public VirtualNetwork getNetwork(NetworkId networkId) {
623 return networkIdVirtualNetworkMap.get(networkId);
624 }
625
626 @Override
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700627 public Set<VirtualDevice> getDevices(NetworkId networkId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500628 checkState(networkExists(networkId), "The network has not been added.");
629 Set<DeviceId> deviceIdSet = networkIdDeviceIdSetMap.get(networkId);
630 Set<VirtualDevice> virtualDeviceSet = new HashSet<>();
631 if (deviceIdSet != null) {
632 deviceIdSet.forEach(deviceId -> virtualDeviceSet.add(deviceIdVirtualDeviceMap.get(deviceId)));
633 }
634 return ImmutableSet.copyOf(virtualDeviceSet);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700635 }
636
637 @Override
Brian Stanke7a81b532016-06-14 15:43:51 -0400638 public Set<VirtualHost> getHosts(NetworkId networkId) {
639 checkState(networkExists(networkId), "The network has not been added.");
640 Set<HostId> hostIdSet = networkIdHostIdSetMap.get(networkId);
641 Set<VirtualHost> virtualHostSet = new HashSet<>();
642 if (hostIdSet != null) {
643 hostIdSet.forEach(hostId -> virtualHostSet.add(hostIdVirtualHostMap.get(hostId)));
644 }
645 return ImmutableSet.copyOf(virtualHostSet);
646 }
647
648 @Override
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700649 public Set<VirtualLink> getLinks(NetworkId networkId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500650 checkState(networkExists(networkId), "The network has not been added.");
Brian Stanke5df14472016-03-11 19:34:38 -0500651 Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId);
652 if (virtualLinkSet == null) {
653 virtualLinkSet = new HashSet<>();
654 }
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500655 return ImmutableSet.copyOf(virtualLinkSet);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700656 }
657
Brian Stanke612cebf2016-05-02 10:21:33 -0400658 @Override
659 public VirtualLink getLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst) {
Brian Stanke9a108972016-04-11 15:25:17 -0400660 Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId);
661 if (virtualLinkSet == null) {
662 return null;
663 }
664
665 VirtualLink virtualLink = null;
666 for (VirtualLink link : virtualLinkSet) {
667 if (link.src().equals(src) && link.dst().equals(dst)) {
668 virtualLink = link;
669 break;
670 }
671 }
672 return virtualLink;
673 }
674
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700675 @Override
676 public Set<VirtualPort> getPorts(NetworkId networkId, DeviceId deviceId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500677 checkState(networkExists(networkId), "The network has not been added.");
Brian Stanke5df14472016-03-11 19:34:38 -0500678 Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId);
679 if (virtualPortSet == null) {
680 virtualPortSet = new HashSet<>();
681 }
682
Brian Stanke612cebf2016-05-02 10:21:33 -0400683 if (deviceId == null) {
684 return ImmutableSet.copyOf(virtualPortSet);
685 }
686
Brian Stanke5df14472016-03-11 19:34:38 -0500687 Set<VirtualPort> portSet = new HashSet<>();
688 virtualPortSet.forEach(virtualPort -> {
689 if (virtualPort.element().id().equals(deviceId)) {
690 portSet.add(virtualPort);
691 }
692 });
693 return ImmutableSet.copyOf(portSet);
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500694 }
695
Brian Stanke11f6d532016-07-05 16:17:59 -0400696 @Override
697 public synchronized void addOrUpdateIntent(Intent intent, IntentState state) {
698 checkNotNull(intent, "Intent cannot be null");
699 IntentData intentData = removeIntent(intent.key());
700 if (intentData == null) {
701 intentData = new IntentData(intent, state, new WallClockTimestamp(System.currentTimeMillis()));
702 } else {
703 intentData = new IntentData(intent, state, intentData.version());
704 }
705 intentKeyIntentDataMap.put(intent.key(), intentData);
706 }
707
708 @Override
709 public IntentData removeIntent(Key intentKey) {
710 checkNotNull(intentKey, "Intent key cannot be null");
711 return intentKeyIntentDataMap.remove(intentKey);
712 }
713
714 @Override
715 public void addTunnelId(Intent intent, TunnelId tunnelId) {
716 // Add the tunnelId to the intent key set map
717 Set<TunnelId> tunnelIdSet = intentKeyTunnelIdSetMap.remove(intent.key());
718 if (tunnelIdSet == null) {
719 tunnelIdSet = new HashSet<>();
720 }
721 tunnelIdSet.add(tunnelId);
722 intentKeyTunnelIdSetMap.put(intent.key(), tunnelIdSet);
723 }
724
725 @Override
726 public Set<TunnelId> getTunnelIds(Intent intent) {
727 Set<TunnelId> tunnelIdSet = intentKeyTunnelIdSetMap.get(intent.key());
728 return tunnelIdSet == null ? new HashSet<TunnelId>() : ImmutableSet.copyOf(tunnelIdSet);
729 }
730
731 @Override
732 public void removeTunnelId(Intent intent, TunnelId tunnelId) {
733 Set<TunnelId> tunnelIdSet = new HashSet<>();
734 intentKeyTunnelIdSetMap.get(intent.key()).forEach(tunnelId1 -> {
735 if (tunnelId1.equals(tunnelId)) {
736 tunnelIdSet.add(tunnelId);
737 }
738 });
739
740 if (!tunnelIdSet.isEmpty()) {
741 intentKeyTunnelIdSetMap.compute(intent.key(), (key, existingTunnelIds) -> {
742 if (existingTunnelIds == null || existingTunnelIds.isEmpty()) {
743 return new HashSet<>();
744 } else {
745 return new HashSet<>(Sets.difference(existingTunnelIds, tunnelIdSet));
746 }
747 });
748 }
749 }
750
751 @Override
752 public Set<Intent> getIntents() {
753 Set<Intent> intents = new HashSet<>();
754 intentKeyIntentDataMap.values().forEach(intentData -> intents.add(intentData.intent()));
755 return ImmutableSet.copyOf(intents);
756 }
757
758 @Override
759 public Intent getIntent(Key key) {
760 IntentData intentData = intentKeyIntentDataMap.get(key);
761 return intentData == null ? null : intentData.intent();
762 }
763
764 @Override
765 public Set<IntentData> getIntentData() {
766 return ImmutableSet.copyOf(intentKeyIntentDataMap.values());
767 }
768
769 @Override
770 public IntentData getIntentData(Key key) {
771 IntentData intentData = intentKeyIntentDataMap.get(key);
772 return intentData == null ? null : new IntentData(intentData);
773 }
774
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500775 /**
776 * Listener class to map listener set events to the virtual network events.
777 */
778 private class InternalSetListener implements SetEventListener<TenantId> {
779 @Override
780 public void event(SetEvent<TenantId> event) {
781 VirtualNetworkEvent.Type type = null;
782 switch (event.type()) {
783 case ADD:
784 type = VirtualNetworkEvent.Type.TENANT_REGISTERED;
785 break;
786 case REMOVE:
787 type = VirtualNetworkEvent.Type.TENANT_UNREGISTERED;
788 break;
789 default:
790 log.error("Unsupported event type: " + event.type());
791 }
792 notifyDelegate(new VirtualNetworkEvent(type, null));
793 }
794 }
795
796 /**
797 * Listener class to map listener map events to the virtual network events.
798 */
Claudine Chiu945828d2016-11-21 12:47:07 -0500799 private class InternalMapListener<K, V> implements MapEventListener<K, V> {
800
801 private final BiFunction<MapEvent.Type, V, VirtualNetworkEvent> createEvent;
802
803 InternalMapListener(BiFunction<MapEvent.Type, V, VirtualNetworkEvent> createEvent) {
804 this.createEvent = createEvent;
805 }
806
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500807 @Override
Claudine Chiu945828d2016-11-21 12:47:07 -0500808 public void event(MapEvent<K, V> event) {
809 checkNotNull(event.key());
810 VirtualNetworkEvent vnetEvent = null;
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500811 switch (event.type()) {
812 case INSERT:
Claudine Chiu945828d2016-11-21 12:47:07 -0500813 vnetEvent = createEvent.apply(event.type(), event.newValue().value());
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500814 break;
815 case UPDATE:
816 if ((event.oldValue().value() != null) && (event.newValue().value() == null)) {
Claudine Chiu945828d2016-11-21 12:47:07 -0500817 vnetEvent = createEvent.apply(MapEvent.Type.REMOVE, event.oldValue().value());
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500818 } else {
Claudine Chiu945828d2016-11-21 12:47:07 -0500819 vnetEvent = createEvent.apply(event.type(), event.newValue().value());
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500820 }
821 break;
822 case REMOVE:
Claudine Chiu945828d2016-11-21 12:47:07 -0500823 if (event.oldValue() != null) {
824 vnetEvent = createEvent.apply(event.type(), event.oldValue().value());
825 }
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500826 break;
827 default:
828 log.error("Unsupported event type: " + event.type());
829 }
Claudine Chiu945828d2016-11-21 12:47:07 -0500830 if (vnetEvent != null) {
831 notifyDelegate(vnetEvent);
832 }
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500833 }
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700834 }
835}