blob: 529a0cd46cdd8295aff2c1a4b8b515b3cdbb085d [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;
Harold Huangb7d6b8e2017-04-03 17:13:33 +080076import java.util.Optional;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070077import java.util.Set;
Claudine Chiu7f872a72016-12-16 13:51:39 -050078import java.util.concurrent.atomic.AtomicBoolean;
Claudine Chiu945828d2016-11-21 12:47:07 -050079import java.util.function.BiFunction;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070080
Brian Stanke0e5c94e2016-03-08 11:20:04 -050081import static com.google.common.base.Preconditions.checkNotNull;
82import static com.google.common.base.Preconditions.checkState;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070083import static org.slf4j.LoggerFactory.getLogger;
84
85/**
Brian Stanke0e5c94e2016-03-08 11:20:04 -050086 * Implementation of the virtual network store.
Thomas Vachuska33979fd2015-07-31 11:41:14 -070087 */
88@Component(immediate = true)
89@Service
90public class DistributedVirtualNetworkStore
91 extends AbstractStore<VirtualNetworkEvent, VirtualNetworkStoreDelegate>
92 implements VirtualNetworkStore {
93
94 private final Logger log = getLogger(getClass());
95
Brian Stanke0e5c94e2016-03-08 11:20:04 -050096 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
97 protected StorageService storageService;
Thomas Vachuska33979fd2015-07-31 11:41:14 -070098
Brian Stanke0e5c94e2016-03-08 11:20:04 -050099 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
100 protected CoreService coreService;
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700101
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500102 private IdGenerator idGenerator;
103
104 // Track tenants by ID
105 private DistributedSet<TenantId> tenantIdSet;
106
107 // Listener for tenant events
108 private final SetEventListener<TenantId> setListener = new InternalSetListener();
109
110 // Track virtual networks by network Id
111 private ConsistentMap<NetworkId, VirtualNetwork> networkIdVirtualNetworkConsistentMap;
112 private Map<NetworkId, VirtualNetwork> networkIdVirtualNetworkMap;
113
114 // Listener for virtual network events
Claudine Chiu945828d2016-11-21 12:47:07 -0500115 private final MapEventListener<NetworkId, VirtualNetwork> virtualNetworkMapListener =
116 new InternalMapListener<>((mapEventType, virtualNetwork) -> {
117 VirtualNetworkEvent.Type eventType =
sangyun-han3c3e99e2017-02-08 15:30:53 +0900118 mapEventType.equals(MapEvent.Type.INSERT)
119 ? VirtualNetworkEvent.Type.NETWORK_ADDED :
120 mapEventType.equals(MapEvent.Type.UPDATE)
121 ? VirtualNetworkEvent.Type.NETWORK_UPDATED :
122 mapEventType.equals(MapEvent.Type.REMOVE)
123 ? VirtualNetworkEvent.Type.NETWORK_REMOVED : null;
Claudine Chiu945828d2016-11-21 12:47:07 -0500124 return eventType == null ? null : new VirtualNetworkEvent(eventType, virtualNetwork.id());
125 });
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500126
sangyun-han3c3e99e2017-02-08 15:30:53 +0900127 // Listener for virtual device events
128 private final MapEventListener<DeviceId, VirtualDevice> virtualDeviceMapListener =
129 new InternalMapListener<>((mapEventType, virtualDevice) -> {
130 VirtualNetworkEvent.Type eventType =
131 mapEventType.equals(MapEvent.Type.INSERT)
132 ? VirtualNetworkEvent.Type.VIRTUAL_DEVICE_ADDED :
133 mapEventType.equals(MapEvent.Type.UPDATE)
134 ? VirtualNetworkEvent.Type.VIRTUAL_DEVICE_UPDATED :
135 mapEventType.equals(MapEvent.Type.REMOVE)
136 ? VirtualNetworkEvent.Type.VIRTUAL_DEVICE_REMOVED : null;
137 return eventType == null ? null :
138 new VirtualNetworkEvent(eventType, virtualDevice.networkId(), virtualDevice);
139 });
140
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500141 // Track virtual network IDs by tenant Id
142 private ConsistentMap<TenantId, Set<NetworkId>> tenantIdNetworkIdSetConsistentMap;
143 private Map<TenantId, Set<NetworkId>> tenantIdNetworkIdSetMap;
144
145 // Track virtual devices by device Id
146 private ConsistentMap<DeviceId, VirtualDevice> deviceIdVirtualDeviceConsistentMap;
147 private Map<DeviceId, VirtualDevice> deviceIdVirtualDeviceMap;
148
149 // Track device IDs by network Id
150 private ConsistentMap<NetworkId, Set<DeviceId>> networkIdDeviceIdSetConsistentMap;
151 private Map<NetworkId, Set<DeviceId>> networkIdDeviceIdSetMap;
152
Brian Stanke7a81b532016-06-14 15:43:51 -0400153 // Track virtual hosts by host Id
154 private ConsistentMap<HostId, VirtualHost> hostIdVirtualHostConsistentMap;
155 private Map<HostId, VirtualHost> hostIdVirtualHostMap;
156
157 // Track host IDs by network Id
158 private ConsistentMap<NetworkId, Set<HostId>> networkIdHostIdSetConsistentMap;
159 private Map<NetworkId, Set<HostId>> networkIdHostIdSetMap;
160
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500161 // Track virtual links by network Id
162 private ConsistentMap<NetworkId, Set<VirtualLink>> networkIdVirtualLinkSetConsistentMap;
163 private Map<NetworkId, Set<VirtualLink>> networkIdVirtualLinkSetMap;
164
165 // Track virtual ports by network Id
166 private ConsistentMap<NetworkId, Set<VirtualPort>> networkIdVirtualPortSetConsistentMap;
167 private Map<NetworkId, Set<VirtualPort>> networkIdVirtualPortSetMap;
168
Brian Stanke11f6d532016-07-05 16:17:59 -0400169 // Track intent key to intent data
170 private ConsistentMap<Key, IntentData> intentKeyIntentDataConsistentMap;
171 private Map<Key, IntentData> intentKeyIntentDataMap;
172
173 // Track intent ID to TunnelIds
174 private ConsistentMap<Key, Set<TunnelId>> intentKeyTunnelIdSetConsistentMap;
175 private Map<Key, Set<TunnelId>> intentKeyTunnelIdSetMap;
176
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500177 private static final Serializer SERIALIZER = Serializer
178 .using(new KryoNamespace.Builder().register(KryoNamespaces.API)
179 .register(TenantId.class)
HIGUCHI Yutac0f50452016-05-13 19:26:05 -0700180 .register(NetworkId.class)
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500181 .register(VirtualNetwork.class)
Brian Stanke5df14472016-03-11 19:34:38 -0500182 .register(DefaultVirtualNetwork.class)
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500183 .register(VirtualDevice.class)
Brian Stanke5df14472016-03-11 19:34:38 -0500184 .register(DefaultVirtualDevice.class)
Brian Stanke7a81b532016-06-14 15:43:51 -0400185 .register(VirtualHost.class)
186 .register(DefaultVirtualHost.class)
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500187 .register(VirtualLink.class)
Brian Stanke5df14472016-03-11 19:34:38 -0500188 .register(DefaultVirtualLink.class)
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500189 .register(VirtualPort.class)
Brian Stanke5df14472016-03-11 19:34:38 -0500190 .register(DefaultVirtualPort.class)
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500191 .register(Device.class)
Brian Stanke9a108972016-04-11 15:25:17 -0400192 .register(TunnelId.class)
Brian Stanke11f6d532016-07-05 16:17:59 -0400193 .register(IntentData.class)
194 .register(VirtualNetworkIntent.class)
195 .register(WallClockTimestamp.class)
HIGUCHI Yuta03666a32016-05-18 11:49:09 -0700196 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
Brian Stanke11f6d532016-07-05 16:17:59 -0400197 .build());
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500198
199 /**
200 * Distributed network store service activate method.
201 */
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700202 @Activate
203 public void activate() {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500204 idGenerator = coreService.getIdGenerator(VirtualNetworkService.VIRTUAL_NETWORK_TOPIC);
205
206 tenantIdSet = storageService.<TenantId>setBuilder()
207 .withSerializer(SERIALIZER)
208 .withName("onos-tenantId")
209 .withRelaxedReadConsistency()
210 .build()
211 .asDistributedSet();
212 tenantIdSet.addListener(setListener);
213
214 networkIdVirtualNetworkConsistentMap = storageService.<NetworkId, VirtualNetwork>consistentMapBuilder()
215 .withSerializer(SERIALIZER)
216 .withName("onos-networkId-virtualnetwork")
217 .withRelaxedReadConsistency()
218 .build();
Claudine Chiu945828d2016-11-21 12:47:07 -0500219 networkIdVirtualNetworkConsistentMap.addListener(virtualNetworkMapListener);
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500220 networkIdVirtualNetworkMap = networkIdVirtualNetworkConsistentMap.asJavaMap();
221
222 tenantIdNetworkIdSetConsistentMap = storageService.<TenantId, Set<NetworkId>>consistentMapBuilder()
223 .withSerializer(SERIALIZER)
224 .withName("onos-tenantId-networkIds")
225 .withRelaxedReadConsistency()
226 .build();
227 tenantIdNetworkIdSetMap = tenantIdNetworkIdSetConsistentMap.asJavaMap();
228
229 deviceIdVirtualDeviceConsistentMap = storageService.<DeviceId, VirtualDevice>consistentMapBuilder()
230 .withSerializer(SERIALIZER)
231 .withName("onos-deviceId-virtualdevice")
232 .withRelaxedReadConsistency()
233 .build();
Claudine Chiu945828d2016-11-21 12:47:07 -0500234 deviceIdVirtualDeviceConsistentMap.addListener(virtualDeviceMapListener);
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500235 deviceIdVirtualDeviceMap = deviceIdVirtualDeviceConsistentMap.asJavaMap();
236
237 networkIdDeviceIdSetConsistentMap = storageService.<NetworkId, Set<DeviceId>>consistentMapBuilder()
238 .withSerializer(SERIALIZER)
239 .withName("onos-networkId-deviceIds")
240 .withRelaxedReadConsistency()
241 .build();
242 networkIdDeviceIdSetMap = networkIdDeviceIdSetConsistentMap.asJavaMap();
243
Brian Stanke7a81b532016-06-14 15:43:51 -0400244 hostIdVirtualHostConsistentMap = storageService.<HostId, VirtualHost>consistentMapBuilder()
245 .withSerializer(SERIALIZER)
246 .withName("onos-hostId-virtualhost")
247 .withRelaxedReadConsistency()
248 .build();
249 hostIdVirtualHostMap = hostIdVirtualHostConsistentMap.asJavaMap();
250
251 networkIdHostIdSetConsistentMap = storageService.<NetworkId, Set<HostId>>consistentMapBuilder()
252 .withSerializer(SERIALIZER)
253 .withName("onos-networkId-hostIds")
254 .withRelaxedReadConsistency()
255 .build();
256 networkIdHostIdSetMap = networkIdHostIdSetConsistentMap.asJavaMap();
257
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500258 networkIdVirtualLinkSetConsistentMap = storageService.<NetworkId, Set<VirtualLink>>consistentMapBuilder()
259 .withSerializer(SERIALIZER)
260 .withName("onos-networkId-virtuallinks")
261 .withRelaxedReadConsistency()
262 .build();
263 networkIdVirtualLinkSetMap = networkIdVirtualLinkSetConsistentMap.asJavaMap();
264
265 networkIdVirtualPortSetConsistentMap = storageService.<NetworkId, Set<VirtualPort>>consistentMapBuilder()
266 .withSerializer(SERIALIZER)
Brian Stanke11f6d532016-07-05 16:17:59 -0400267 .withName("onos-networkId-virtualports")
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500268 .withRelaxedReadConsistency()
269 .build();
270 networkIdVirtualPortSetMap = networkIdVirtualPortSetConsistentMap.asJavaMap();
271
Brian Stanke11f6d532016-07-05 16:17:59 -0400272 intentKeyTunnelIdSetConsistentMap = storageService.<Key, Set<TunnelId>>consistentMapBuilder()
273 .withSerializer(SERIALIZER)
274 .withName("onos-intentKey-tunnelIds")
275 .withRelaxedReadConsistency()
276 .build();
277 intentKeyTunnelIdSetMap = intentKeyTunnelIdSetConsistentMap.asJavaMap();
278
279 intentKeyIntentDataConsistentMap = storageService.<Key, IntentData>consistentMapBuilder()
280 .withSerializer(SERIALIZER)
281 .withName("onos-intentKey-intentData")
282 .withRelaxedReadConsistency()
283 .build();
284 intentKeyIntentDataMap = intentKeyIntentDataConsistentMap.asJavaMap();
285
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700286 log.info("Started");
287 }
288
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500289 /**
290 * Distributed network store service deactivate method.
291 */
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700292 @Deactivate
293 public void deactivate() {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500294 tenantIdSet.removeListener(setListener);
Claudine Chiu945828d2016-11-21 12:47:07 -0500295 networkIdVirtualNetworkConsistentMap.removeListener(virtualNetworkMapListener);
296 deviceIdVirtualDeviceConsistentMap.removeListener(virtualDeviceMapListener);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700297 log.info("Stopped");
298 }
299
300 @Override
301 public void addTenantId(TenantId tenantId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500302 tenantIdSet.add(tenantId);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700303 }
304
305 @Override
306 public void removeTenantId(TenantId tenantId) {
Harold Huangb7d6b8e2017-04-03 17:13:33 +0800307 //Remove all the virtual networks of this tenant
308 Set<VirtualNetwork> networkIdSet = getNetworks(tenantId);
309 if (networkIdSet != null) {
310 networkIdSet.forEach(virtualNetwork -> removeNetwork(virtualNetwork.id()));
311 }
312
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500313 tenantIdSet.remove(tenantId);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700314 }
315
316 @Override
317 public Set<TenantId> getTenantIds() {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500318 return ImmutableSet.copyOf(tenantIdSet);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700319 }
320
321 @Override
322 public VirtualNetwork addNetwork(TenantId tenantId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500323
324 checkState(tenantIdSet.contains(tenantId), "The tenant has not been registered. " + tenantId.id());
325 VirtualNetwork virtualNetwork = new DefaultVirtualNetwork(genNetworkId(), tenantId);
326 //TODO update both maps in one transaction.
327 networkIdVirtualNetworkMap.put(virtualNetwork.id(), virtualNetwork);
Brian Stanke5df14472016-03-11 19:34:38 -0500328
329 Set<NetworkId> networkIdSet = tenantIdNetworkIdSetMap.get(tenantId);
330 if (networkIdSet == null) {
331 networkIdSet = new HashSet<>();
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500332 }
Brian Stanke5df14472016-03-11 19:34:38 -0500333 networkIdSet.add(virtualNetwork.id());
334 tenantIdNetworkIdSetMap.put(tenantId, networkIdSet);
335
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500336 return virtualNetwork;
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700337 }
338
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500339 /**
340 * Returns a new network identifier from a virtual network block of identifiers.
341 *
342 * @return NetworkId network identifier
343 */
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700344 private NetworkId genNetworkId() {
Kenji HIKICHIcfdf91b2016-05-25 13:04:45 +0900345 NetworkId networkId;
346 do {
347 networkId = NetworkId.networkId(idGenerator.getNewId());
348 } while (!networkId.isVirtualNetworkId());
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700349
Kenji HIKICHIcfdf91b2016-05-25 13:04:45 +0900350 return networkId;
351 }
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700352
353 @Override
354 public void removeNetwork(NetworkId networkId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500355 // Make sure that the virtual network exists before attempting to remove it.
Claudine Chiu70e2db82017-05-04 09:53:11 -0400356 checkState(networkExists(networkId), "The network does not exist.");
Brian Stanke5df14472016-03-11 19:34:38 -0500357
Claudine Chiu70e2db82017-05-04 09:53:11 -0400358 //Remove all the devices of this network
359 Set<VirtualDevice> deviceSet = getDevices(networkId);
360 if (deviceSet != null) {
361 deviceSet.forEach(virtualDevice -> removeDevice(networkId, virtualDevice.id()));
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500362 }
Claudine Chiu70e2db82017-05-04 09:53:11 -0400363 //TODO update both maps in one transaction.
364
365 VirtualNetwork virtualNetwork = networkIdVirtualNetworkMap.remove(networkId);
366 if (virtualNetwork == null) {
367 return;
368 }
369 TenantId tenantId = virtualNetwork.tenantId();
370
371 Set<NetworkId> networkIdSet = new HashSet<>();
372 tenantIdNetworkIdSetMap.get(tenantId).forEach(networkId1 -> {
373 if (networkId1.id().equals(networkId.id())) {
374 networkIdSet.add(networkId1);
375 }
376 });
377
378 tenantIdNetworkIdSetMap.compute(virtualNetwork.tenantId(), (id, existingNetworkIds) -> {
379 if (existingNetworkIds == null || existingNetworkIds.isEmpty()) {
380 return new HashSet<>();
381 } else {
382 return new HashSet<>(Sets.difference(existingNetworkIds, networkIdSet));
383 }
384 });
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500385 }
386
387 /**
388 * Returns if the network identifier exists.
389 *
390 * @param networkId network identifier
391 * @return true if the network identifier exists, false otherwise.
392 */
393 private boolean networkExists(NetworkId networkId) {
Brian Stanke612cebf2016-05-02 10:21:33 -0400394 checkNotNull(networkId, "The network identifier cannot be null.");
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500395 return (networkIdVirtualNetworkMap.containsKey(networkId));
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700396 }
397
Claudine Chiu70e2db82017-05-04 09:53:11 -0400398
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700399 @Override
400 public VirtualDevice addDevice(NetworkId networkId, DeviceId deviceId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500401 checkState(networkExists(networkId), "The network has not been added.");
402 Set<DeviceId> deviceIdSet = networkIdDeviceIdSetMap.get(networkId);
403 if (deviceIdSet == null) {
404 deviceIdSet = new HashSet<>();
405 }
406 VirtualDevice virtualDevice = new DefaultVirtualDevice(networkId, deviceId);
407 //TODO update both maps in one transaction.
408 deviceIdVirtualDeviceMap.put(deviceId, virtualDevice);
409 deviceIdSet.add(deviceId);
410 networkIdDeviceIdSetMap.put(networkId, deviceIdSet);
411 return virtualDevice;
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700412 }
413
414 @Override
415 public void removeDevice(NetworkId networkId, DeviceId deviceId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500416 checkState(networkExists(networkId), "The network has not been added.");
Harold Huangb7d6b8e2017-04-03 17:13:33 +0800417 //Remove all the virtual ports of the this device
418 Set<VirtualPort> virtualPorts = getPorts(networkId, deviceId);
419 if (virtualPorts != null) {
420 virtualPorts.forEach(virtualPort -> removePort(networkId, deviceId, virtualPort.number()));
421 }
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500422 //TODO update both maps in one transaction.
Brian Stanke5df14472016-03-11 19:34:38 -0500423
424 Set<DeviceId> deviceIdSet = new HashSet<>();
425 networkIdDeviceIdSetMap.get(networkId).forEach(deviceId1 -> {
426 if (deviceId1.equals(deviceId)) {
427 deviceIdSet.add(deviceId1);
428 }
429 });
430
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500431 if (deviceIdSet != null) {
432 networkIdDeviceIdSetMap.compute(networkId, (id, existingDeviceIds) -> {
433 if (existingDeviceIds == null || existingDeviceIds.isEmpty()) {
HIGUCHI Yutac0f50452016-05-13 19:26:05 -0700434 return new HashSet<>();
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500435 } else {
HIGUCHI Yutac0f50452016-05-13 19:26:05 -0700436 return new HashSet<>(Sets.difference(existingDeviceIds, deviceIdSet));
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500437 }
438 });
439
Brian Stanke5df14472016-03-11 19:34:38 -0500440 deviceIdVirtualDeviceMap.remove(deviceId);
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500441 }
Brian Stanke7a81b532016-06-14 15:43:51 -0400442 }
443
444 @Override
445 public VirtualHost addHost(NetworkId networkId, HostId hostId, MacAddress mac,
446 VlanId vlan, HostLocation location, Set<IpAddress> ips) {
447 checkState(networkExists(networkId), "The network has not been added.");
Harold Huangb7d6b8e2017-04-03 17:13:33 +0800448 checkState(virtualPortExists(networkId, location.deviceId(), location.port()),
449 "The virtual port has not been created.");
Brian Stanke7a81b532016-06-14 15:43:51 -0400450 Set<HostId> hostIdSet = networkIdHostIdSetMap.get(networkId);
451 if (hostIdSet == null) {
452 hostIdSet = new HashSet<>();
453 }
454 VirtualHost virtualhost = new DefaultVirtualHost(networkId, hostId, mac, vlan, location, ips);
455 //TODO update both maps in one transaction.
456 hostIdVirtualHostMap.put(hostId, virtualhost);
457 hostIdSet.add(hostId);
458 networkIdHostIdSetMap.put(networkId, hostIdSet);
459 return virtualhost;
460 }
461
462 @Override
463 public void removeHost(NetworkId networkId, HostId hostId) {
464 checkState(networkExists(networkId), "The network has not been added.");
465 //TODO update both maps in one transaction.
466
467 Set<HostId> hostIdSet = new HashSet<>();
468 networkIdHostIdSetMap.get(networkId).forEach(hostId1 -> {
469 if (hostId1.equals(hostId)) {
470 hostIdSet.add(hostId1);
471 }
472 });
473
474 if (hostIdSet != null) {
475 networkIdHostIdSetMap.compute(networkId, (id, existingHostIds) -> {
476 if (existingHostIds == null || existingHostIds.isEmpty()) {
477 return new HashSet<>();
478 } else {
479 return new HashSet<>(Sets.difference(existingHostIds, hostIdSet));
480 }
481 });
482
483 hostIdVirtualHostMap.remove(hostId);
484 }
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700485 }
486
Harold Huangb7d6b8e2017-04-03 17:13:33 +0800487 /**
488 * Returns if the given virtual port exists.
489 *
490 * @param networkId network identifier
491 * @param deviceId virtual device Id
492 * @param portNumber virtual port number
493 * @return true if the virtual port exists, false otherwise.
494 */
495 private boolean virtualPortExists(NetworkId networkId, DeviceId deviceId, PortNumber portNumber) {
496 Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId);
497 if (virtualPortSet != null) {
498 return virtualPortSet.stream().anyMatch(
499 p -> p.element().id().equals(deviceId) &&
500 p.number().equals(portNumber));
501 } else {
502 return false;
503 }
504 }
505
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700506 @Override
Brian Stanke612cebf2016-05-02 10:21:33 -0400507 public VirtualLink addLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst,
508 Link.State state, TunnelId realizedBy) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500509 checkState(networkExists(networkId), "The network has not been added.");
Harold Huangb7d6b8e2017-04-03 17:13:33 +0800510 checkState(virtualPortExists(networkId, src.deviceId(), src.port()),
511 "The source virtual port has not been added.");
512 checkState(virtualPortExists(networkId, dst.deviceId(), dst.port()),
513 "The destination virtual port has not been added.");
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500514 Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId);
515 if (virtualLinkSet == null) {
516 virtualLinkSet = new HashSet<>();
517 }
Harold Huang7362e672017-04-19 10:00:11 +0800518
Brian Stanke9a108972016-04-11 15:25:17 -0400519 // validate that the link does not already exist in this network
Harold Huang7362e672017-04-19 10:00:11 +0800520 checkState(getLink(networkId, src, dst) == null,
521 "The virtual link already exists");
522 checkState(getLink(networkId, src, null) == null,
523 "The source connection point has been used by another link");
524 checkState(getLink(networkId, null, dst) == null,
525 "The destination connection point has been used by another link");
Brian Stanke9a108972016-04-11 15:25:17 -0400526
527 VirtualLink virtualLink = DefaultVirtualLink.builder()
528 .networkId(networkId)
529 .src(src)
530 .dst(dst)
Brian Stanke612cebf2016-05-02 10:21:33 -0400531 .state(state)
Brian Stanke9a108972016-04-11 15:25:17 -0400532 .tunnelId(realizedBy)
533 .build();
534
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500535 virtualLinkSet.add(virtualLink);
536 networkIdVirtualLinkSetMap.put(networkId, virtualLinkSet);
537 return virtualLink;
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700538 }
539
540 @Override
Brian Stanke612cebf2016-05-02 10:21:33 -0400541 public void updateLink(VirtualLink virtualLink, TunnelId tunnelId, Link.State state) {
Brian Stanke9a108972016-04-11 15:25:17 -0400542 checkState(networkExists(virtualLink.networkId()), "The network has not been added.");
543 Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(virtualLink.networkId());
544 if (virtualLinkSet == null) {
545 virtualLinkSet = new HashSet<>();
Harold Huang7362e672017-04-19 10:00:11 +0800546 networkIdVirtualLinkSetMap.put(virtualLink.networkId(), virtualLinkSet);
547 log.warn("The updated virtual link {} has not been added", virtualLink);
548 return;
Brian Stanke9a108972016-04-11 15:25:17 -0400549 }
Harold Huang7362e672017-04-19 10:00:11 +0800550 if (!virtualLinkSet.remove(virtualLink)) {
551 log.warn("The updated virtual link {} does not exist", virtualLink);
552 return;
553 }
Brian Stanke9a108972016-04-11 15:25:17 -0400554
555 VirtualLink newVirtualLink = DefaultVirtualLink.builder()
556 .networkId(virtualLink.networkId())
557 .src(virtualLink.src())
558 .dst(virtualLink.dst())
559 .tunnelId(tunnelId)
Brian Stanke612cebf2016-05-02 10:21:33 -0400560 .state(state)
Brian Stanke9a108972016-04-11 15:25:17 -0400561 .build();
562
563 virtualLinkSet.add(newVirtualLink);
564 networkIdVirtualLinkSetMap.put(newVirtualLink.networkId(), virtualLinkSet);
565 }
566
567 @Override
568 public VirtualLink removeLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500569 checkState(networkExists(networkId), "The network has not been added.");
Brian Stanke5df14472016-03-11 19:34:38 -0500570
Brian Stanke9a108972016-04-11 15:25:17 -0400571 final VirtualLink virtualLink = getLink(networkId, src, dst);
572 if (virtualLink == null) {
Harold Huang7362e672017-04-19 10:00:11 +0800573 log.warn("The removed virtual link between {} and {} does not exist", src, dst);
Brian Stanke9a108972016-04-11 15:25:17 -0400574 return null;
575 }
Brian Stanke5df14472016-03-11 19:34:38 -0500576 Set<VirtualLink> virtualLinkSet = new HashSet<>();
Brian Stanke9a108972016-04-11 15:25:17 -0400577 virtualLinkSet.add(virtualLink);
Brian Stanke5df14472016-03-11 19:34:38 -0500578
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500579 if (virtualLinkSet != null) {
580 networkIdVirtualLinkSetMap.compute(networkId, (id, existingVirtualLinks) -> {
581 if (existingVirtualLinks == null || existingVirtualLinks.isEmpty()) {
HIGUCHI Yutac0f50452016-05-13 19:26:05 -0700582 return new HashSet<>();
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500583 } else {
HIGUCHI Yutac0f50452016-05-13 19:26:05 -0700584 return new HashSet<>(Sets.difference(existingVirtualLinks, virtualLinkSet));
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500585 }
586 });
587 }
Brian Stanke9a108972016-04-11 15:25:17 -0400588 return virtualLink;
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700589 }
590
591 @Override
Yoonseon Han6c603892016-09-01 11:52:21 -0700592 public VirtualPort addPort(NetworkId networkId, DeviceId deviceId,
593 PortNumber portNumber, ConnectPoint realizedBy) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500594 checkState(networkExists(networkId), "The network has not been added.");
595 Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId);
Yoonseon Han6c603892016-09-01 11:52:21 -0700596
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500597 if (virtualPortSet == null) {
598 virtualPortSet = new HashSet<>();
599 }
Yoonseon Han6c603892016-09-01 11:52:21 -0700600
Claudine Chiu465a2602017-03-17 18:33:36 -0400601 VirtualDevice device = deviceIdVirtualDeviceMap.get(deviceId);
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500602 checkNotNull(device, "The device has not been created for deviceId: " + deviceId);
Yoonseon Han6c603892016-09-01 11:52:21 -0700603
Harold Huangb7d6b8e2017-04-03 17:13:33 +0800604 checkState(!virtualPortExists(networkId, deviceId, portNumber),
605 "The requested Port Number has been added.");
Yoonseon Han6c603892016-09-01 11:52:21 -0700606
607 VirtualPort virtualPort = new DefaultVirtualPort(networkId, device,
608 portNumber, realizedBy);
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500609 virtualPortSet.add(virtualPort);
610 networkIdVirtualPortSetMap.put(networkId, virtualPortSet);
Claudine Chiu7f872a72016-12-16 13:51:39 -0500611 notifyDelegate(new VirtualNetworkEvent(VirtualNetworkEvent.Type.VIRTUAL_PORT_ADDED,
Claudine Chiu465a2602017-03-17 18:33:36 -0400612 networkId, device, virtualPort));
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500613 return virtualPort;
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700614 }
615
616 @Override
Yoonseon Han6c603892016-09-01 11:52:21 -0700617 public void bindPort(NetworkId networkId, DeviceId deviceId,
618 PortNumber portNumber, ConnectPoint realizedBy) {
619
620 Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap
621 .get(networkId);
622
Harold Huangb7d6b8e2017-04-03 17:13:33 +0800623 Optional<VirtualPort> virtualPortOptional = virtualPortSet.stream().filter(
Yoonseon Han6c603892016-09-01 11:52:21 -0700624 p -> p.element().id().equals(deviceId) &&
Harold Huangb7d6b8e2017-04-03 17:13:33 +0800625 p.number().equals(portNumber)).findFirst();
626 checkState(virtualPortOptional.isPresent(), "The virtual port has not been added.");
Yoonseon Han6c603892016-09-01 11:52:21 -0700627
Claudine Chiu465a2602017-03-17 18:33:36 -0400628 VirtualDevice device = deviceIdVirtualDeviceMap.get(deviceId);
Yoonseon Han6c603892016-09-01 11:52:21 -0700629 checkNotNull(device, "The device has not been created for deviceId: "
630 + deviceId);
631
Harold Huangb7d6b8e2017-04-03 17:13:33 +0800632 VirtualPort vPort = virtualPortOptional.get();
Yoonseon Han6c603892016-09-01 11:52:21 -0700633 virtualPortSet.remove(vPort);
634 vPort = new DefaultVirtualPort(networkId, device, portNumber, realizedBy);
635 virtualPortSet.add(vPort);
636 networkIdVirtualPortSetMap.put(networkId, virtualPortSet);
Claudine Chiu7f872a72016-12-16 13:51:39 -0500637 notifyDelegate(new VirtualNetworkEvent(VirtualNetworkEvent.Type.VIRTUAL_PORT_UPDATED,
Claudine Chiu465a2602017-03-17 18:33:36 -0400638 networkId, device, vPort));
Yoonseon Han6c603892016-09-01 11:52:21 -0700639 }
640
641 @Override
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700642 public void removePort(NetworkId networkId, DeviceId deviceId, PortNumber portNumber) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500643 checkState(networkExists(networkId), "The network has not been added.");
Claudine Chiu465a2602017-03-17 18:33:36 -0400644 VirtualDevice device = deviceIdVirtualDeviceMap.get(deviceId);
645 checkNotNull(device, "The device has not been created for deviceId: "
646 + deviceId);
Brian Stanke5df14472016-03-11 19:34:38 -0500647
Harold Huangb7d6b8e2017-04-03 17:13:33 +0800648 if (networkIdVirtualPortSetMap.get(networkId) == null) {
649 log.warn("No port has been created for NetworkId: {}", networkId);
650 return;
651 }
652
Brian Stanke5df14472016-03-11 19:34:38 -0500653 Set<VirtualPort> virtualPortSet = new HashSet<>();
654 networkIdVirtualPortSetMap.get(networkId).forEach(port -> {
655 if (port.element().id().equals(deviceId) && port.number().equals(portNumber)) {
656 virtualPortSet.add(port);
657 }
658 });
659
Claudine Chiu7f872a72016-12-16 13:51:39 -0500660 if (!virtualPortSet.isEmpty()) {
661 AtomicBoolean portRemoved = new AtomicBoolean(false);
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500662 networkIdVirtualPortSetMap.compute(networkId, (id, existingVirtualPorts) -> {
663 if (existingVirtualPorts == null || existingVirtualPorts.isEmpty()) {
HIGUCHI Yutac0f50452016-05-13 19:26:05 -0700664 return new HashSet<>();
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500665 } else {
Claudine Chiu7f872a72016-12-16 13:51:39 -0500666 portRemoved.set(true);
HIGUCHI Yutac0f50452016-05-13 19:26:05 -0700667 return new HashSet<>(Sets.difference(existingVirtualPorts, virtualPortSet));
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500668 }
669 });
Claudine Chiu7f872a72016-12-16 13:51:39 -0500670 if (portRemoved.get()) {
671 virtualPortSet.forEach(virtualPort -> notifyDelegate(
672 new VirtualNetworkEvent(VirtualNetworkEvent.Type.VIRTUAL_PORT_REMOVED,
Claudine Chiu465a2602017-03-17 18:33:36 -0400673 networkId, device, virtualPort)
Claudine Chiu7f872a72016-12-16 13:51:39 -0500674 ));
Harold Huangb7d6b8e2017-04-03 17:13:33 +0800675
676 //Remove all the virtual links connected to this virtual port
677 Set<VirtualLink> existingVirtualLinks = networkIdVirtualLinkSetMap.get(networkId);
678 if (existingVirtualLinks != null && !existingVirtualLinks.isEmpty()) {
679 Set<VirtualLink> virtualLinkSet = new HashSet<>();
680 ConnectPoint cp = new ConnectPoint(deviceId, portNumber);
681 existingVirtualLinks.forEach(virtualLink -> {
682 if (virtualLink.src().equals(cp) || virtualLink.dst().equals(cp)) {
683 virtualLinkSet.add(virtualLink);
684 }
685 });
686 virtualLinkSet.forEach(virtualLink ->
687 removeLink(networkId, virtualLink.src(), virtualLink.dst()));
688 }
689
690 //Remove all the hosts connected to this virtual port
691 Set<HostId> hostIdSet = new HashSet<>();
692 hostIdVirtualHostMap.forEach((hostId, virtualHost) -> {
693 if (virtualHost.location().deviceId().equals(deviceId) &&
694 virtualHost.location().port().equals(portNumber)) {
695 hostIdSet.add(hostId);
696 }
697 });
698 hostIdSet.forEach(hostId -> removeHost(networkId, hostId));
Claudine Chiu7f872a72016-12-16 13:51:39 -0500699 }
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500700 }
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700701 }
702
703 @Override
704 public Set<VirtualNetwork> getNetworks(TenantId tenantId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500705 Set<NetworkId> networkIdSet = tenantIdNetworkIdSetMap.get(tenantId);
706 Set<VirtualNetwork> virtualNetworkSet = new HashSet<>();
707 if (networkIdSet != null) {
708 networkIdSet.forEach(networkId -> virtualNetworkSet.add(networkIdVirtualNetworkMap.get(networkId)));
709 }
710 return ImmutableSet.copyOf(virtualNetworkSet);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700711 }
712
713 @Override
Brian Stanke86914282016-05-25 15:36:50 -0400714 public VirtualNetwork getNetwork(NetworkId networkId) {
715 return networkIdVirtualNetworkMap.get(networkId);
716 }
717
718 @Override
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700719 public Set<VirtualDevice> getDevices(NetworkId networkId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500720 checkState(networkExists(networkId), "The network has not been added.");
721 Set<DeviceId> deviceIdSet = networkIdDeviceIdSetMap.get(networkId);
722 Set<VirtualDevice> virtualDeviceSet = new HashSet<>();
723 if (deviceIdSet != null) {
724 deviceIdSet.forEach(deviceId -> virtualDeviceSet.add(deviceIdVirtualDeviceMap.get(deviceId)));
725 }
726 return ImmutableSet.copyOf(virtualDeviceSet);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700727 }
728
729 @Override
Brian Stanke7a81b532016-06-14 15:43:51 -0400730 public Set<VirtualHost> getHosts(NetworkId networkId) {
731 checkState(networkExists(networkId), "The network has not been added.");
732 Set<HostId> hostIdSet = networkIdHostIdSetMap.get(networkId);
733 Set<VirtualHost> virtualHostSet = new HashSet<>();
734 if (hostIdSet != null) {
735 hostIdSet.forEach(hostId -> virtualHostSet.add(hostIdVirtualHostMap.get(hostId)));
736 }
737 return ImmutableSet.copyOf(virtualHostSet);
738 }
739
740 @Override
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700741 public Set<VirtualLink> getLinks(NetworkId networkId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500742 checkState(networkExists(networkId), "The network has not been added.");
Brian Stanke5df14472016-03-11 19:34:38 -0500743 Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId);
744 if (virtualLinkSet == null) {
745 virtualLinkSet = new HashSet<>();
746 }
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500747 return ImmutableSet.copyOf(virtualLinkSet);
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700748 }
749
Brian Stanke612cebf2016-05-02 10:21:33 -0400750 @Override
751 public VirtualLink getLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst) {
Brian Stanke9a108972016-04-11 15:25:17 -0400752 Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId);
753 if (virtualLinkSet == null) {
754 return null;
755 }
756
757 VirtualLink virtualLink = null;
758 for (VirtualLink link : virtualLinkSet) {
Harold Huang7362e672017-04-19 10:00:11 +0800759 if (src == null && link.dst().equals(dst)) {
760 virtualLink = link;
761 break;
762 } else if (dst == null && link.src().equals(src)) {
763 virtualLink = link;
764 break;
765 } else if (link.src().equals(src) && link.dst().equals(dst)) {
Brian Stanke9a108972016-04-11 15:25:17 -0400766 virtualLink = link;
767 break;
768 }
769 }
770 return virtualLink;
771 }
772
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700773 @Override
774 public Set<VirtualPort> getPorts(NetworkId networkId, DeviceId deviceId) {
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500775 checkState(networkExists(networkId), "The network has not been added.");
Brian Stanke5df14472016-03-11 19:34:38 -0500776 Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId);
777 if (virtualPortSet == null) {
778 virtualPortSet = new HashSet<>();
779 }
780
Brian Stanke612cebf2016-05-02 10:21:33 -0400781 if (deviceId == null) {
782 return ImmutableSet.copyOf(virtualPortSet);
783 }
784
Brian Stanke5df14472016-03-11 19:34:38 -0500785 Set<VirtualPort> portSet = new HashSet<>();
786 virtualPortSet.forEach(virtualPort -> {
787 if (virtualPort.element().id().equals(deviceId)) {
788 portSet.add(virtualPort);
789 }
790 });
791 return ImmutableSet.copyOf(portSet);
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500792 }
793
Brian Stanke11f6d532016-07-05 16:17:59 -0400794 @Override
795 public synchronized void addOrUpdateIntent(Intent intent, IntentState state) {
796 checkNotNull(intent, "Intent cannot be null");
797 IntentData intentData = removeIntent(intent.key());
798 if (intentData == null) {
799 intentData = new IntentData(intent, state, new WallClockTimestamp(System.currentTimeMillis()));
800 } else {
801 intentData = new IntentData(intent, state, intentData.version());
802 }
803 intentKeyIntentDataMap.put(intent.key(), intentData);
804 }
805
806 @Override
807 public IntentData removeIntent(Key intentKey) {
808 checkNotNull(intentKey, "Intent key cannot be null");
809 return intentKeyIntentDataMap.remove(intentKey);
810 }
811
812 @Override
813 public void addTunnelId(Intent intent, TunnelId tunnelId) {
814 // Add the tunnelId to the intent key set map
815 Set<TunnelId> tunnelIdSet = intentKeyTunnelIdSetMap.remove(intent.key());
816 if (tunnelIdSet == null) {
817 tunnelIdSet = new HashSet<>();
818 }
819 tunnelIdSet.add(tunnelId);
820 intentKeyTunnelIdSetMap.put(intent.key(), tunnelIdSet);
821 }
822
823 @Override
824 public Set<TunnelId> getTunnelIds(Intent intent) {
825 Set<TunnelId> tunnelIdSet = intentKeyTunnelIdSetMap.get(intent.key());
826 return tunnelIdSet == null ? new HashSet<TunnelId>() : ImmutableSet.copyOf(tunnelIdSet);
827 }
828
829 @Override
830 public void removeTunnelId(Intent intent, TunnelId tunnelId) {
831 Set<TunnelId> tunnelIdSet = new HashSet<>();
832 intentKeyTunnelIdSetMap.get(intent.key()).forEach(tunnelId1 -> {
833 if (tunnelId1.equals(tunnelId)) {
834 tunnelIdSet.add(tunnelId);
835 }
836 });
837
838 if (!tunnelIdSet.isEmpty()) {
839 intentKeyTunnelIdSetMap.compute(intent.key(), (key, existingTunnelIds) -> {
840 if (existingTunnelIds == null || existingTunnelIds.isEmpty()) {
841 return new HashSet<>();
842 } else {
843 return new HashSet<>(Sets.difference(existingTunnelIds, tunnelIdSet));
844 }
845 });
846 }
847 }
848
849 @Override
850 public Set<Intent> getIntents() {
851 Set<Intent> intents = new HashSet<>();
852 intentKeyIntentDataMap.values().forEach(intentData -> intents.add(intentData.intent()));
853 return ImmutableSet.copyOf(intents);
854 }
855
856 @Override
857 public Intent getIntent(Key key) {
858 IntentData intentData = intentKeyIntentDataMap.get(key);
859 return intentData == null ? null : intentData.intent();
860 }
861
862 @Override
863 public Set<IntentData> getIntentData() {
864 return ImmutableSet.copyOf(intentKeyIntentDataMap.values());
865 }
866
867 @Override
868 public IntentData getIntentData(Key key) {
869 IntentData intentData = intentKeyIntentDataMap.get(key);
870 return intentData == null ? null : new IntentData(intentData);
871 }
872
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500873 /**
874 * Listener class to map listener set events to the virtual network events.
875 */
876 private class InternalSetListener implements SetEventListener<TenantId> {
877 @Override
878 public void event(SetEvent<TenantId> event) {
879 VirtualNetworkEvent.Type type = null;
880 switch (event.type()) {
881 case ADD:
882 type = VirtualNetworkEvent.Type.TENANT_REGISTERED;
883 break;
884 case REMOVE:
885 type = VirtualNetworkEvent.Type.TENANT_UNREGISTERED;
886 break;
887 default:
888 log.error("Unsupported event type: " + event.type());
889 }
890 notifyDelegate(new VirtualNetworkEvent(type, null));
891 }
892 }
893
894 /**
895 * Listener class to map listener map events to the virtual network events.
896 */
Claudine Chiu945828d2016-11-21 12:47:07 -0500897 private class InternalMapListener<K, V> implements MapEventListener<K, V> {
898
899 private final BiFunction<MapEvent.Type, V, VirtualNetworkEvent> createEvent;
900
901 InternalMapListener(BiFunction<MapEvent.Type, V, VirtualNetworkEvent> createEvent) {
902 this.createEvent = createEvent;
903 }
904
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500905 @Override
Claudine Chiu945828d2016-11-21 12:47:07 -0500906 public void event(MapEvent<K, V> event) {
907 checkNotNull(event.key());
908 VirtualNetworkEvent vnetEvent = null;
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500909 switch (event.type()) {
910 case INSERT:
Claudine Chiu945828d2016-11-21 12:47:07 -0500911 vnetEvent = createEvent.apply(event.type(), event.newValue().value());
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500912 break;
913 case UPDATE:
914 if ((event.oldValue().value() != null) && (event.newValue().value() == null)) {
Claudine Chiu945828d2016-11-21 12:47:07 -0500915 vnetEvent = createEvent.apply(MapEvent.Type.REMOVE, event.oldValue().value());
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500916 } else {
Claudine Chiu945828d2016-11-21 12:47:07 -0500917 vnetEvent = createEvent.apply(event.type(), event.newValue().value());
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500918 }
919 break;
920 case REMOVE:
Claudine Chiu945828d2016-11-21 12:47:07 -0500921 if (event.oldValue() != null) {
922 vnetEvent = createEvent.apply(event.type(), event.oldValue().value());
923 }
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500924 break;
925 default:
926 log.error("Unsupported event type: " + event.type());
927 }
Claudine Chiu945828d2016-11-21 12:47:07 -0500928 if (vnetEvent != null) {
929 notifyDelegate(vnetEvent);
930 }
Brian Stanke0e5c94e2016-03-08 11:20:04 -0500931 }
Thomas Vachuska33979fd2015-07-31 11:41:14 -0700932 }
933}