Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.incubator.store.virtual.impl; |
| 17 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Sets; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Service; |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 26 | import org.onlab.packet.IpAddress; |
| 27 | import org.onlab.packet.MacAddress; |
| 28 | import org.onlab.packet.VlanId; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 29 | import org.onlab.util.KryoNamespace; |
| 30 | import org.onosproject.core.CoreService; |
| 31 | import org.onosproject.core.IdGenerator; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 32 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 33 | import org.onosproject.incubator.net.virtual.DefaultVirtualDevice; |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 34 | import org.onosproject.incubator.net.virtual.DefaultVirtualHost; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 35 | import org.onosproject.incubator.net.virtual.DefaultVirtualLink; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 36 | import org.onosproject.incubator.net.virtual.DefaultVirtualNetwork; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 37 | import org.onosproject.incubator.net.virtual.DefaultVirtualPort; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 38 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 39 | import org.onosproject.incubator.net.virtual.TenantId; |
| 40 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 41 | import org.onosproject.incubator.net.virtual.VirtualHost; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 42 | import org.onosproject.incubator.net.virtual.VirtualLink; |
| 43 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 44 | import org.onosproject.incubator.net.virtual.VirtualNetworkEvent; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 45 | import org.onosproject.incubator.net.virtual.VirtualNetworkIntent; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 46 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 47 | import org.onosproject.incubator.net.virtual.VirtualNetworkStore; |
| 48 | import org.onosproject.incubator.net.virtual.VirtualNetworkStoreDelegate; |
| 49 | import org.onosproject.incubator.net.virtual.VirtualPort; |
| 50 | import org.onosproject.net.ConnectPoint; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 51 | import org.onosproject.net.Device; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 52 | import org.onosproject.net.DeviceId; |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 53 | import org.onosproject.net.HostId; |
| 54 | import org.onosproject.net.HostLocation; |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 55 | import org.onosproject.net.Link; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 56 | import org.onosproject.net.PortNumber; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 57 | import org.onosproject.net.intent.Intent; |
| 58 | import org.onosproject.net.intent.IntentData; |
| 59 | import org.onosproject.net.intent.IntentState; |
| 60 | import org.onosproject.net.intent.Key; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 61 | import org.onosproject.store.AbstractStore; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 62 | import org.onosproject.store.serializers.KryoNamespaces; |
| 63 | import org.onosproject.store.service.ConsistentMap; |
| 64 | import org.onosproject.store.service.DistributedSet; |
| 65 | import org.onosproject.store.service.MapEvent; |
| 66 | import org.onosproject.store.service.MapEventListener; |
| 67 | import org.onosproject.store.service.Serializer; |
| 68 | import org.onosproject.store.service.SetEvent; |
| 69 | import org.onosproject.store.service.SetEventListener; |
| 70 | import org.onosproject.store.service.StorageService; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 71 | import org.onosproject.store.service.WallClockTimestamp; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 72 | import org.slf4j.Logger; |
| 73 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 74 | import java.util.HashSet; |
| 75 | import java.util.Map; |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 76 | import java.util.Optional; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 77 | import java.util.Set; |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 78 | import java.util.concurrent.atomic.AtomicBoolean; |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 79 | import java.util.function.BiFunction; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 80 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 81 | import static com.google.common.base.Preconditions.checkNotNull; |
| 82 | import static com.google.common.base.Preconditions.checkState; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 83 | import static org.slf4j.LoggerFactory.getLogger; |
| 84 | |
| 85 | /** |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 86 | * Implementation of the virtual network store. |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 87 | */ |
| 88 | @Component(immediate = true) |
| 89 | @Service |
| 90 | public class DistributedVirtualNetworkStore |
| 91 | extends AbstractStore<VirtualNetworkEvent, VirtualNetworkStoreDelegate> |
| 92 | implements VirtualNetworkStore { |
| 93 | |
| 94 | private final Logger log = getLogger(getClass()); |
| 95 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected StorageService storageService; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 98 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 100 | protected CoreService coreService; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 101 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 102 | 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 Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 115 | private final MapEventListener<NetworkId, VirtualNetwork> virtualNetworkMapListener = |
| 116 | new InternalMapListener<>((mapEventType, virtualNetwork) -> { |
| 117 | VirtualNetworkEvent.Type eventType = |
sangyun-han | 3c3e99e | 2017-02-08 15:30:53 +0900 | [diff] [blame] | 118 | 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 Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 124 | return eventType == null ? null : new VirtualNetworkEvent(eventType, virtualNetwork.id()); |
| 125 | }); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 126 | |
sangyun-han | 3c3e99e | 2017-02-08 15:30:53 +0900 | [diff] [blame] | 127 | // 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 Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 141 | // 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 Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 153 | // 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 Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 161 | // 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 Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 169 | // 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 Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 177 | private static final Serializer SERIALIZER = Serializer |
| 178 | .using(new KryoNamespace.Builder().register(KryoNamespaces.API) |
| 179 | .register(TenantId.class) |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 180 | .register(NetworkId.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 181 | .register(VirtualNetwork.class) |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 182 | .register(DefaultVirtualNetwork.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 183 | .register(VirtualDevice.class) |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 184 | .register(DefaultVirtualDevice.class) |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 185 | .register(VirtualHost.class) |
| 186 | .register(DefaultVirtualHost.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 187 | .register(VirtualLink.class) |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 188 | .register(DefaultVirtualLink.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 189 | .register(VirtualPort.class) |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 190 | .register(DefaultVirtualPort.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 191 | .register(Device.class) |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 192 | .register(TunnelId.class) |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 193 | .register(IntentData.class) |
| 194 | .register(VirtualNetworkIntent.class) |
| 195 | .register(WallClockTimestamp.class) |
HIGUCHI Yuta | 03666a3 | 2016-05-18 11:49:09 -0700 | [diff] [blame] | 196 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 197 | .build()); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 198 | |
| 199 | /** |
| 200 | * Distributed network store service activate method. |
| 201 | */ |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 202 | @Activate |
| 203 | public void activate() { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 204 | 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 Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 219 | networkIdVirtualNetworkConsistentMap.addListener(virtualNetworkMapListener); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 220 | 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 Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 234 | deviceIdVirtualDeviceConsistentMap.addListener(virtualDeviceMapListener); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 235 | 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 Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 244 | 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 Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 258 | 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 Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 267 | .withName("onos-networkId-virtualports") |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 268 | .withRelaxedReadConsistency() |
| 269 | .build(); |
| 270 | networkIdVirtualPortSetMap = networkIdVirtualPortSetConsistentMap.asJavaMap(); |
| 271 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 272 | 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 Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 286 | log.info("Started"); |
| 287 | } |
| 288 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 289 | /** |
| 290 | * Distributed network store service deactivate method. |
| 291 | */ |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 292 | @Deactivate |
| 293 | public void deactivate() { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 294 | tenantIdSet.removeListener(setListener); |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 295 | networkIdVirtualNetworkConsistentMap.removeListener(virtualNetworkMapListener); |
| 296 | deviceIdVirtualDeviceConsistentMap.removeListener(virtualDeviceMapListener); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 297 | log.info("Stopped"); |
| 298 | } |
| 299 | |
| 300 | @Override |
| 301 | public void addTenantId(TenantId tenantId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 302 | tenantIdSet.add(tenantId); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | @Override |
| 306 | public void removeTenantId(TenantId tenantId) { |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 307 | //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 Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 313 | tenantIdSet.remove(tenantId); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public Set<TenantId> getTenantIds() { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 318 | return ImmutableSet.copyOf(tenantIdSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public VirtualNetwork addNetwork(TenantId tenantId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 323 | |
| 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 Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 328 | |
| 329 | Set<NetworkId> networkIdSet = tenantIdNetworkIdSetMap.get(tenantId); |
| 330 | if (networkIdSet == null) { |
| 331 | networkIdSet = new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 332 | } |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 333 | networkIdSet.add(virtualNetwork.id()); |
| 334 | tenantIdNetworkIdSetMap.put(tenantId, networkIdSet); |
| 335 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 336 | return virtualNetwork; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 339 | /** |
| 340 | * Returns a new network identifier from a virtual network block of identifiers. |
| 341 | * |
| 342 | * @return NetworkId network identifier |
| 343 | */ |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 344 | private NetworkId genNetworkId() { |
Kenji HIKICHI | cfdf91b | 2016-05-25 13:04:45 +0900 | [diff] [blame] | 345 | NetworkId networkId; |
| 346 | do { |
| 347 | networkId = NetworkId.networkId(idGenerator.getNewId()); |
| 348 | } while (!networkId.isVirtualNetworkId()); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 349 | |
Kenji HIKICHI | cfdf91b | 2016-05-25 13:04:45 +0900 | [diff] [blame] | 350 | return networkId; |
| 351 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 352 | |
| 353 | @Override |
| 354 | public void removeNetwork(NetworkId networkId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 355 | // Make sure that the virtual network exists before attempting to remove it. |
Claudine Chiu | 70e2db8 | 2017-05-04 09:53:11 -0400 | [diff] [blame^] | 356 | checkState(networkExists(networkId), "The network does not exist."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 357 | |
Claudine Chiu | 70e2db8 | 2017-05-04 09:53:11 -0400 | [diff] [blame^] | 358 | //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 Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 362 | } |
Claudine Chiu | 70e2db8 | 2017-05-04 09:53:11 -0400 | [diff] [blame^] | 363 | //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 Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 385 | } |
| 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 Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 394 | checkNotNull(networkId, "The network identifier cannot be null."); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 395 | return (networkIdVirtualNetworkMap.containsKey(networkId)); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Claudine Chiu | 70e2db8 | 2017-05-04 09:53:11 -0400 | [diff] [blame^] | 398 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 399 | @Override |
| 400 | public VirtualDevice addDevice(NetworkId networkId, DeviceId deviceId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 401 | 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 Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | @Override |
| 415 | public void removeDevice(NetworkId networkId, DeviceId deviceId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 416 | checkState(networkExists(networkId), "The network has not been added."); |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 417 | //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 Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 422 | //TODO update both maps in one transaction. |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 423 | |
| 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 Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 431 | if (deviceIdSet != null) { |
| 432 | networkIdDeviceIdSetMap.compute(networkId, (id, existingDeviceIds) -> { |
| 433 | if (existingDeviceIds == null || existingDeviceIds.isEmpty()) { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 434 | return new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 435 | } else { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 436 | return new HashSet<>(Sets.difference(existingDeviceIds, deviceIdSet)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 437 | } |
| 438 | }); |
| 439 | |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 440 | deviceIdVirtualDeviceMap.remove(deviceId); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 441 | } |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 442 | } |
| 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 Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 448 | checkState(virtualPortExists(networkId, location.deviceId(), location.port()), |
| 449 | "The virtual port has not been created."); |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 450 | 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 Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 487 | /** |
| 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 Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 506 | @Override |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 507 | public VirtualLink addLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst, |
| 508 | Link.State state, TunnelId realizedBy) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 509 | checkState(networkExists(networkId), "The network has not been added."); |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 510 | 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 Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 514 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId); |
| 515 | if (virtualLinkSet == null) { |
| 516 | virtualLinkSet = new HashSet<>(); |
| 517 | } |
Harold Huang | 7362e67 | 2017-04-19 10:00:11 +0800 | [diff] [blame] | 518 | |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 519 | // validate that the link does not already exist in this network |
Harold Huang | 7362e67 | 2017-04-19 10:00:11 +0800 | [diff] [blame] | 520 | 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 Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 526 | |
| 527 | VirtualLink virtualLink = DefaultVirtualLink.builder() |
| 528 | .networkId(networkId) |
| 529 | .src(src) |
| 530 | .dst(dst) |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 531 | .state(state) |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 532 | .tunnelId(realizedBy) |
| 533 | .build(); |
| 534 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 535 | virtualLinkSet.add(virtualLink); |
| 536 | networkIdVirtualLinkSetMap.put(networkId, virtualLinkSet); |
| 537 | return virtualLink; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | @Override |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 541 | public void updateLink(VirtualLink virtualLink, TunnelId tunnelId, Link.State state) { |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 542 | 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 Huang | 7362e67 | 2017-04-19 10:00:11 +0800 | [diff] [blame] | 546 | networkIdVirtualLinkSetMap.put(virtualLink.networkId(), virtualLinkSet); |
| 547 | log.warn("The updated virtual link {} has not been added", virtualLink); |
| 548 | return; |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 549 | } |
Harold Huang | 7362e67 | 2017-04-19 10:00:11 +0800 | [diff] [blame] | 550 | if (!virtualLinkSet.remove(virtualLink)) { |
| 551 | log.warn("The updated virtual link {} does not exist", virtualLink); |
| 552 | return; |
| 553 | } |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 554 | |
| 555 | VirtualLink newVirtualLink = DefaultVirtualLink.builder() |
| 556 | .networkId(virtualLink.networkId()) |
| 557 | .src(virtualLink.src()) |
| 558 | .dst(virtualLink.dst()) |
| 559 | .tunnelId(tunnelId) |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 560 | .state(state) |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 561 | .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 Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 569 | checkState(networkExists(networkId), "The network has not been added."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 570 | |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 571 | final VirtualLink virtualLink = getLink(networkId, src, dst); |
| 572 | if (virtualLink == null) { |
Harold Huang | 7362e67 | 2017-04-19 10:00:11 +0800 | [diff] [blame] | 573 | log.warn("The removed virtual link between {} and {} does not exist", src, dst); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 574 | return null; |
| 575 | } |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 576 | Set<VirtualLink> virtualLinkSet = new HashSet<>(); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 577 | virtualLinkSet.add(virtualLink); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 578 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 579 | if (virtualLinkSet != null) { |
| 580 | networkIdVirtualLinkSetMap.compute(networkId, (id, existingVirtualLinks) -> { |
| 581 | if (existingVirtualLinks == null || existingVirtualLinks.isEmpty()) { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 582 | return new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 583 | } else { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 584 | return new HashSet<>(Sets.difference(existingVirtualLinks, virtualLinkSet)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 585 | } |
| 586 | }); |
| 587 | } |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 588 | return virtualLink; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | @Override |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 592 | public VirtualPort addPort(NetworkId networkId, DeviceId deviceId, |
| 593 | PortNumber portNumber, ConnectPoint realizedBy) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 594 | checkState(networkExists(networkId), "The network has not been added."); |
| 595 | Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 596 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 597 | if (virtualPortSet == null) { |
| 598 | virtualPortSet = new HashSet<>(); |
| 599 | } |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 600 | |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 601 | VirtualDevice device = deviceIdVirtualDeviceMap.get(deviceId); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 602 | checkNotNull(device, "The device has not been created for deviceId: " + deviceId); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 603 | |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 604 | checkState(!virtualPortExists(networkId, deviceId, portNumber), |
| 605 | "The requested Port Number has been added."); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 606 | |
| 607 | VirtualPort virtualPort = new DefaultVirtualPort(networkId, device, |
| 608 | portNumber, realizedBy); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 609 | virtualPortSet.add(virtualPort); |
| 610 | networkIdVirtualPortSetMap.put(networkId, virtualPortSet); |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 611 | notifyDelegate(new VirtualNetworkEvent(VirtualNetworkEvent.Type.VIRTUAL_PORT_ADDED, |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 612 | networkId, device, virtualPort)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 613 | return virtualPort; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | @Override |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 617 | public void bindPort(NetworkId networkId, DeviceId deviceId, |
| 618 | PortNumber portNumber, ConnectPoint realizedBy) { |
| 619 | |
| 620 | Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap |
| 621 | .get(networkId); |
| 622 | |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 623 | Optional<VirtualPort> virtualPortOptional = virtualPortSet.stream().filter( |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 624 | p -> p.element().id().equals(deviceId) && |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 625 | p.number().equals(portNumber)).findFirst(); |
| 626 | checkState(virtualPortOptional.isPresent(), "The virtual port has not been added."); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 627 | |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 628 | VirtualDevice device = deviceIdVirtualDeviceMap.get(deviceId); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 629 | checkNotNull(device, "The device has not been created for deviceId: " |
| 630 | + deviceId); |
| 631 | |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 632 | VirtualPort vPort = virtualPortOptional.get(); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 633 | virtualPortSet.remove(vPort); |
| 634 | vPort = new DefaultVirtualPort(networkId, device, portNumber, realizedBy); |
| 635 | virtualPortSet.add(vPort); |
| 636 | networkIdVirtualPortSetMap.put(networkId, virtualPortSet); |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 637 | notifyDelegate(new VirtualNetworkEvent(VirtualNetworkEvent.Type.VIRTUAL_PORT_UPDATED, |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 638 | networkId, device, vPort)); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | @Override |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 642 | public void removePort(NetworkId networkId, DeviceId deviceId, PortNumber portNumber) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 643 | checkState(networkExists(networkId), "The network has not been added."); |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 644 | VirtualDevice device = deviceIdVirtualDeviceMap.get(deviceId); |
| 645 | checkNotNull(device, "The device has not been created for deviceId: " |
| 646 | + deviceId); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 647 | |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 648 | if (networkIdVirtualPortSetMap.get(networkId) == null) { |
| 649 | log.warn("No port has been created for NetworkId: {}", networkId); |
| 650 | return; |
| 651 | } |
| 652 | |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 653 | 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 Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 660 | if (!virtualPortSet.isEmpty()) { |
| 661 | AtomicBoolean portRemoved = new AtomicBoolean(false); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 662 | networkIdVirtualPortSetMap.compute(networkId, (id, existingVirtualPorts) -> { |
| 663 | if (existingVirtualPorts == null || existingVirtualPorts.isEmpty()) { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 664 | return new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 665 | } else { |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 666 | portRemoved.set(true); |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 667 | return new HashSet<>(Sets.difference(existingVirtualPorts, virtualPortSet)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 668 | } |
| 669 | }); |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 670 | if (portRemoved.get()) { |
| 671 | virtualPortSet.forEach(virtualPort -> notifyDelegate( |
| 672 | new VirtualNetworkEvent(VirtualNetworkEvent.Type.VIRTUAL_PORT_REMOVED, |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 673 | networkId, device, virtualPort) |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 674 | )); |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 675 | |
| 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 Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 699 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 700 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | @Override |
| 704 | public Set<VirtualNetwork> getNetworks(TenantId tenantId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 705 | 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 Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | @Override |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 714 | public VirtualNetwork getNetwork(NetworkId networkId) { |
| 715 | return networkIdVirtualNetworkMap.get(networkId); |
| 716 | } |
| 717 | |
| 718 | @Override |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 719 | public Set<VirtualDevice> getDevices(NetworkId networkId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 720 | 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 Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | @Override |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 730 | 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 Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 741 | public Set<VirtualLink> getLinks(NetworkId networkId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 742 | checkState(networkExists(networkId), "The network has not been added."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 743 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId); |
| 744 | if (virtualLinkSet == null) { |
| 745 | virtualLinkSet = new HashSet<>(); |
| 746 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 747 | return ImmutableSet.copyOf(virtualLinkSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 750 | @Override |
| 751 | public VirtualLink getLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst) { |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 752 | 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 Huang | 7362e67 | 2017-04-19 10:00:11 +0800 | [diff] [blame] | 759 | 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 Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 766 | virtualLink = link; |
| 767 | break; |
| 768 | } |
| 769 | } |
| 770 | return virtualLink; |
| 771 | } |
| 772 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 773 | @Override |
| 774 | public Set<VirtualPort> getPorts(NetworkId networkId, DeviceId deviceId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 775 | checkState(networkExists(networkId), "The network has not been added."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 776 | Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId); |
| 777 | if (virtualPortSet == null) { |
| 778 | virtualPortSet = new HashSet<>(); |
| 779 | } |
| 780 | |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 781 | if (deviceId == null) { |
| 782 | return ImmutableSet.copyOf(virtualPortSet); |
| 783 | } |
| 784 | |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 785 | 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 Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 792 | } |
| 793 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 794 | @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 Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 873 | /** |
| 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 Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 897 | 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 Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 905 | @Override |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 906 | public void event(MapEvent<K, V> event) { |
| 907 | checkNotNull(event.key()); |
| 908 | VirtualNetworkEvent vnetEvent = null; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 909 | switch (event.type()) { |
| 910 | case INSERT: |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 911 | vnetEvent = createEvent.apply(event.type(), event.newValue().value()); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 912 | break; |
| 913 | case UPDATE: |
| 914 | if ((event.oldValue().value() != null) && (event.newValue().value() == null)) { |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 915 | vnetEvent = createEvent.apply(MapEvent.Type.REMOVE, event.oldValue().value()); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 916 | } else { |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 917 | vnetEvent = createEvent.apply(event.type(), event.newValue().value()); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 918 | } |
| 919 | break; |
| 920 | case REMOVE: |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 921 | if (event.oldValue() != null) { |
| 922 | vnetEvent = createEvent.apply(event.type(), event.oldValue().value()); |
| 923 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 924 | break; |
| 925 | default: |
| 926 | log.error("Unsupported event type: " + event.type()); |
| 927 | } |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 928 | if (vnetEvent != null) { |
| 929 | notifyDelegate(vnetEvent); |
| 930 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 931 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 932 | } |
| 933 | } |