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; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 58 | import org.onosproject.net.intent.Key; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 59 | import org.onosproject.store.AbstractStore; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 60 | import org.onosproject.store.serializers.KryoNamespaces; |
| 61 | import org.onosproject.store.service.ConsistentMap; |
| 62 | import org.onosproject.store.service.DistributedSet; |
| 63 | import org.onosproject.store.service.MapEvent; |
| 64 | import org.onosproject.store.service.MapEventListener; |
| 65 | import org.onosproject.store.service.Serializer; |
| 66 | import org.onosproject.store.service.SetEvent; |
| 67 | import org.onosproject.store.service.SetEventListener; |
| 68 | import org.onosproject.store.service.StorageService; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 69 | import org.onosproject.store.service.WallClockTimestamp; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 70 | import org.slf4j.Logger; |
| 71 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 72 | import java.util.HashSet; |
| 73 | import java.util.Map; |
Yoonseon Han | aa25605 | 2017-05-08 16:25:38 -0700 | [diff] [blame] | 74 | import java.util.Objects; |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 75 | import java.util.Optional; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 76 | import java.util.Set; |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 77 | import java.util.concurrent.atomic.AtomicBoolean; |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 78 | import java.util.function.BiFunction; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 79 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 80 | import static com.google.common.base.Preconditions.checkNotNull; |
| 81 | import static com.google.common.base.Preconditions.checkState; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 82 | import static org.slf4j.LoggerFactory.getLogger; |
| 83 | |
| 84 | /** |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 85 | * Implementation of the virtual network store. |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 86 | */ |
| 87 | @Component(immediate = true) |
| 88 | @Service |
| 89 | public class DistributedVirtualNetworkStore |
| 90 | extends AbstractStore<VirtualNetworkEvent, VirtualNetworkStoreDelegate> |
| 91 | implements VirtualNetworkStore { |
| 92 | |
| 93 | private final Logger log = getLogger(getClass()); |
| 94 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 96 | protected StorageService storageService; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 97 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 99 | protected CoreService coreService; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 100 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 101 | private IdGenerator idGenerator; |
| 102 | |
| 103 | // Track tenants by ID |
| 104 | private DistributedSet<TenantId> tenantIdSet; |
| 105 | |
| 106 | // Listener for tenant events |
| 107 | private final SetEventListener<TenantId> setListener = new InternalSetListener(); |
| 108 | |
| 109 | // Track virtual networks by network Id |
| 110 | private ConsistentMap<NetworkId, VirtualNetwork> networkIdVirtualNetworkConsistentMap; |
| 111 | private Map<NetworkId, VirtualNetwork> networkIdVirtualNetworkMap; |
| 112 | |
| 113 | // Listener for virtual network events |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 114 | private final MapEventListener<NetworkId, VirtualNetwork> virtualNetworkMapListener = |
| 115 | new InternalMapListener<>((mapEventType, virtualNetwork) -> { |
| 116 | VirtualNetworkEvent.Type eventType = |
sangyun-han | 3c3e99e | 2017-02-08 15:30:53 +0900 | [diff] [blame] | 117 | mapEventType.equals(MapEvent.Type.INSERT) |
| 118 | ? VirtualNetworkEvent.Type.NETWORK_ADDED : |
| 119 | mapEventType.equals(MapEvent.Type.UPDATE) |
| 120 | ? VirtualNetworkEvent.Type.NETWORK_UPDATED : |
| 121 | mapEventType.equals(MapEvent.Type.REMOVE) |
| 122 | ? VirtualNetworkEvent.Type.NETWORK_REMOVED : null; |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 123 | return eventType == null ? null : new VirtualNetworkEvent(eventType, virtualNetwork.id()); |
| 124 | }); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 125 | |
sangyun-han | 3c3e99e | 2017-02-08 15:30:53 +0900 | [diff] [blame] | 126 | // Listener for virtual device events |
Yoonseon Han | aa25605 | 2017-05-08 16:25:38 -0700 | [diff] [blame] | 127 | private final MapEventListener<VirtualDeviceId, VirtualDevice> virtualDeviceMapListener = |
sangyun-han | 3c3e99e | 2017-02-08 15:30:53 +0900 | [diff] [blame] | 128 | new InternalMapListener<>((mapEventType, virtualDevice) -> { |
| 129 | VirtualNetworkEvent.Type eventType = |
| 130 | mapEventType.equals(MapEvent.Type.INSERT) |
| 131 | ? VirtualNetworkEvent.Type.VIRTUAL_DEVICE_ADDED : |
| 132 | mapEventType.equals(MapEvent.Type.UPDATE) |
| 133 | ? VirtualNetworkEvent.Type.VIRTUAL_DEVICE_UPDATED : |
| 134 | mapEventType.equals(MapEvent.Type.REMOVE) |
| 135 | ? VirtualNetworkEvent.Type.VIRTUAL_DEVICE_REMOVED : null; |
| 136 | return eventType == null ? null : |
| 137 | new VirtualNetworkEvent(eventType, virtualDevice.networkId(), virtualDevice); |
| 138 | }); |
| 139 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 140 | // Track virtual network IDs by tenant Id |
| 141 | private ConsistentMap<TenantId, Set<NetworkId>> tenantIdNetworkIdSetConsistentMap; |
| 142 | private Map<TenantId, Set<NetworkId>> tenantIdNetworkIdSetMap; |
| 143 | |
| 144 | // Track virtual devices by device Id |
Yoonseon Han | aa25605 | 2017-05-08 16:25:38 -0700 | [diff] [blame] | 145 | private ConsistentMap<VirtualDeviceId, VirtualDevice> deviceIdVirtualDeviceConsistentMap; |
| 146 | private Map<VirtualDeviceId, VirtualDevice> deviceIdVirtualDeviceMap; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 147 | |
| 148 | // Track device IDs by network Id |
| 149 | private ConsistentMap<NetworkId, Set<DeviceId>> networkIdDeviceIdSetConsistentMap; |
| 150 | private Map<NetworkId, Set<DeviceId>> networkIdDeviceIdSetMap; |
| 151 | |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 152 | // Track virtual hosts by host Id |
| 153 | private ConsistentMap<HostId, VirtualHost> hostIdVirtualHostConsistentMap; |
| 154 | private Map<HostId, VirtualHost> hostIdVirtualHostMap; |
| 155 | |
| 156 | // Track host IDs by network Id |
| 157 | private ConsistentMap<NetworkId, Set<HostId>> networkIdHostIdSetConsistentMap; |
| 158 | private Map<NetworkId, Set<HostId>> networkIdHostIdSetMap; |
| 159 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 160 | // Track virtual links by network Id |
| 161 | private ConsistentMap<NetworkId, Set<VirtualLink>> networkIdVirtualLinkSetConsistentMap; |
| 162 | private Map<NetworkId, Set<VirtualLink>> networkIdVirtualLinkSetMap; |
| 163 | |
| 164 | // Track virtual ports by network Id |
| 165 | private ConsistentMap<NetworkId, Set<VirtualPort>> networkIdVirtualPortSetConsistentMap; |
| 166 | private Map<NetworkId, Set<VirtualPort>> networkIdVirtualPortSetMap; |
| 167 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 168 | // Track intent ID to TunnelIds |
| 169 | private ConsistentMap<Key, Set<TunnelId>> intentKeyTunnelIdSetConsistentMap; |
| 170 | private Map<Key, Set<TunnelId>> intentKeyTunnelIdSetMap; |
| 171 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 172 | private static final Serializer SERIALIZER = Serializer |
| 173 | .using(new KryoNamespace.Builder().register(KryoNamespaces.API) |
| 174 | .register(TenantId.class) |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 175 | .register(NetworkId.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 176 | .register(VirtualNetwork.class) |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 177 | .register(DefaultVirtualNetwork.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 178 | .register(VirtualDevice.class) |
Yoonseon Han | aa25605 | 2017-05-08 16:25:38 -0700 | [diff] [blame] | 179 | .register(VirtualDeviceId.class) |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 180 | .register(DefaultVirtualDevice.class) |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 181 | .register(VirtualHost.class) |
| 182 | .register(DefaultVirtualHost.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 183 | .register(VirtualLink.class) |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 184 | .register(DefaultVirtualLink.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 185 | .register(VirtualPort.class) |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 186 | .register(DefaultVirtualPort.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 187 | .register(Device.class) |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 188 | .register(TunnelId.class) |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 189 | .register(VirtualNetworkIntent.class) |
| 190 | .register(WallClockTimestamp.class) |
HIGUCHI Yuta | 03666a3 | 2016-05-18 11:49:09 -0700 | [diff] [blame] | 191 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 192 | .build()); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 193 | |
| 194 | /** |
| 195 | * Distributed network store service activate method. |
| 196 | */ |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 197 | @Activate |
| 198 | public void activate() { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 199 | idGenerator = coreService.getIdGenerator(VirtualNetworkService.VIRTUAL_NETWORK_TOPIC); |
| 200 | |
| 201 | tenantIdSet = storageService.<TenantId>setBuilder() |
| 202 | .withSerializer(SERIALIZER) |
| 203 | .withName("onos-tenantId") |
| 204 | .withRelaxedReadConsistency() |
| 205 | .build() |
| 206 | .asDistributedSet(); |
| 207 | tenantIdSet.addListener(setListener); |
| 208 | |
| 209 | networkIdVirtualNetworkConsistentMap = storageService.<NetworkId, VirtualNetwork>consistentMapBuilder() |
| 210 | .withSerializer(SERIALIZER) |
| 211 | .withName("onos-networkId-virtualnetwork") |
| 212 | .withRelaxedReadConsistency() |
| 213 | .build(); |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 214 | networkIdVirtualNetworkConsistentMap.addListener(virtualNetworkMapListener); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 215 | networkIdVirtualNetworkMap = networkIdVirtualNetworkConsistentMap.asJavaMap(); |
| 216 | |
| 217 | tenantIdNetworkIdSetConsistentMap = storageService.<TenantId, Set<NetworkId>>consistentMapBuilder() |
| 218 | .withSerializer(SERIALIZER) |
| 219 | .withName("onos-tenantId-networkIds") |
| 220 | .withRelaxedReadConsistency() |
| 221 | .build(); |
| 222 | tenantIdNetworkIdSetMap = tenantIdNetworkIdSetConsistentMap.asJavaMap(); |
| 223 | |
Yoonseon Han | aa25605 | 2017-05-08 16:25:38 -0700 | [diff] [blame] | 224 | deviceIdVirtualDeviceConsistentMap = storageService.<VirtualDeviceId, VirtualDevice>consistentMapBuilder() |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 225 | .withSerializer(SERIALIZER) |
| 226 | .withName("onos-deviceId-virtualdevice") |
| 227 | .withRelaxedReadConsistency() |
| 228 | .build(); |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 229 | deviceIdVirtualDeviceConsistentMap.addListener(virtualDeviceMapListener); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 230 | deviceIdVirtualDeviceMap = deviceIdVirtualDeviceConsistentMap.asJavaMap(); |
| 231 | |
| 232 | networkIdDeviceIdSetConsistentMap = storageService.<NetworkId, Set<DeviceId>>consistentMapBuilder() |
| 233 | .withSerializer(SERIALIZER) |
| 234 | .withName("onos-networkId-deviceIds") |
| 235 | .withRelaxedReadConsistency() |
| 236 | .build(); |
| 237 | networkIdDeviceIdSetMap = networkIdDeviceIdSetConsistentMap.asJavaMap(); |
| 238 | |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 239 | hostIdVirtualHostConsistentMap = storageService.<HostId, VirtualHost>consistentMapBuilder() |
| 240 | .withSerializer(SERIALIZER) |
| 241 | .withName("onos-hostId-virtualhost") |
| 242 | .withRelaxedReadConsistency() |
| 243 | .build(); |
| 244 | hostIdVirtualHostMap = hostIdVirtualHostConsistentMap.asJavaMap(); |
| 245 | |
| 246 | networkIdHostIdSetConsistentMap = storageService.<NetworkId, Set<HostId>>consistentMapBuilder() |
| 247 | .withSerializer(SERIALIZER) |
| 248 | .withName("onos-networkId-hostIds") |
| 249 | .withRelaxedReadConsistency() |
| 250 | .build(); |
| 251 | networkIdHostIdSetMap = networkIdHostIdSetConsistentMap.asJavaMap(); |
| 252 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 253 | networkIdVirtualLinkSetConsistentMap = storageService.<NetworkId, Set<VirtualLink>>consistentMapBuilder() |
| 254 | .withSerializer(SERIALIZER) |
| 255 | .withName("onos-networkId-virtuallinks") |
| 256 | .withRelaxedReadConsistency() |
| 257 | .build(); |
| 258 | networkIdVirtualLinkSetMap = networkIdVirtualLinkSetConsistentMap.asJavaMap(); |
| 259 | |
| 260 | networkIdVirtualPortSetConsistentMap = storageService.<NetworkId, Set<VirtualPort>>consistentMapBuilder() |
| 261 | .withSerializer(SERIALIZER) |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 262 | .withName("onos-networkId-virtualports") |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 263 | .withRelaxedReadConsistency() |
| 264 | .build(); |
| 265 | networkIdVirtualPortSetMap = networkIdVirtualPortSetConsistentMap.asJavaMap(); |
| 266 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 267 | intentKeyTunnelIdSetConsistentMap = storageService.<Key, Set<TunnelId>>consistentMapBuilder() |
| 268 | .withSerializer(SERIALIZER) |
| 269 | .withName("onos-intentKey-tunnelIds") |
| 270 | .withRelaxedReadConsistency() |
| 271 | .build(); |
| 272 | intentKeyTunnelIdSetMap = intentKeyTunnelIdSetConsistentMap.asJavaMap(); |
| 273 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 274 | log.info("Started"); |
| 275 | } |
| 276 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 277 | /** |
| 278 | * Distributed network store service deactivate method. |
| 279 | */ |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 280 | @Deactivate |
| 281 | public void deactivate() { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 282 | tenantIdSet.removeListener(setListener); |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 283 | networkIdVirtualNetworkConsistentMap.removeListener(virtualNetworkMapListener); |
| 284 | deviceIdVirtualDeviceConsistentMap.removeListener(virtualDeviceMapListener); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 285 | log.info("Stopped"); |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public void addTenantId(TenantId tenantId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 290 | tenantIdSet.add(tenantId); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public void removeTenantId(TenantId tenantId) { |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 295 | //Remove all the virtual networks of this tenant |
| 296 | Set<VirtualNetwork> networkIdSet = getNetworks(tenantId); |
| 297 | if (networkIdSet != null) { |
| 298 | networkIdSet.forEach(virtualNetwork -> removeNetwork(virtualNetwork.id())); |
| 299 | } |
| 300 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 301 | tenantIdSet.remove(tenantId); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public Set<TenantId> getTenantIds() { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 306 | return ImmutableSet.copyOf(tenantIdSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | @Override |
| 310 | public VirtualNetwork addNetwork(TenantId tenantId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 311 | |
| 312 | checkState(tenantIdSet.contains(tenantId), "The tenant has not been registered. " + tenantId.id()); |
| 313 | VirtualNetwork virtualNetwork = new DefaultVirtualNetwork(genNetworkId(), tenantId); |
| 314 | //TODO update both maps in one transaction. |
| 315 | networkIdVirtualNetworkMap.put(virtualNetwork.id(), virtualNetwork); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 316 | |
| 317 | Set<NetworkId> networkIdSet = tenantIdNetworkIdSetMap.get(tenantId); |
| 318 | if (networkIdSet == null) { |
| 319 | networkIdSet = new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 320 | } |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 321 | networkIdSet.add(virtualNetwork.id()); |
| 322 | tenantIdNetworkIdSetMap.put(tenantId, networkIdSet); |
| 323 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 324 | return virtualNetwork; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 327 | /** |
| 328 | * Returns a new network identifier from a virtual network block of identifiers. |
| 329 | * |
| 330 | * @return NetworkId network identifier |
| 331 | */ |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 332 | private NetworkId genNetworkId() { |
Kenji HIKICHI | cfdf91b | 2016-05-25 13:04:45 +0900 | [diff] [blame] | 333 | NetworkId networkId; |
| 334 | do { |
| 335 | networkId = NetworkId.networkId(idGenerator.getNewId()); |
| 336 | } while (!networkId.isVirtualNetworkId()); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 337 | |
Kenji HIKICHI | cfdf91b | 2016-05-25 13:04:45 +0900 | [diff] [blame] | 338 | return networkId; |
| 339 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 340 | |
| 341 | @Override |
| 342 | public void removeNetwork(NetworkId networkId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 343 | // Make sure that the virtual network exists before attempting to remove it. |
Claudine Chiu | 70e2db8 | 2017-05-04 09:53:11 -0400 | [diff] [blame] | 344 | checkState(networkExists(networkId), "The network does not exist."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 345 | |
Claudine Chiu | 70e2db8 | 2017-05-04 09:53:11 -0400 | [diff] [blame] | 346 | //Remove all the devices of this network |
| 347 | Set<VirtualDevice> deviceSet = getDevices(networkId); |
| 348 | if (deviceSet != null) { |
| 349 | deviceSet.forEach(virtualDevice -> removeDevice(networkId, virtualDevice.id())); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 350 | } |
Claudine Chiu | 70e2db8 | 2017-05-04 09:53:11 -0400 | [diff] [blame] | 351 | //TODO update both maps in one transaction. |
| 352 | |
| 353 | VirtualNetwork virtualNetwork = networkIdVirtualNetworkMap.remove(networkId); |
| 354 | if (virtualNetwork == null) { |
| 355 | return; |
| 356 | } |
| 357 | TenantId tenantId = virtualNetwork.tenantId(); |
| 358 | |
| 359 | Set<NetworkId> networkIdSet = new HashSet<>(); |
| 360 | tenantIdNetworkIdSetMap.get(tenantId).forEach(networkId1 -> { |
| 361 | if (networkId1.id().equals(networkId.id())) { |
| 362 | networkIdSet.add(networkId1); |
| 363 | } |
| 364 | }); |
| 365 | |
| 366 | tenantIdNetworkIdSetMap.compute(virtualNetwork.tenantId(), (id, existingNetworkIds) -> { |
| 367 | if (existingNetworkIds == null || existingNetworkIds.isEmpty()) { |
| 368 | return new HashSet<>(); |
| 369 | } else { |
| 370 | return new HashSet<>(Sets.difference(existingNetworkIds, networkIdSet)); |
| 371 | } |
| 372 | }); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Returns if the network identifier exists. |
| 377 | * |
| 378 | * @param networkId network identifier |
| 379 | * @return true if the network identifier exists, false otherwise. |
| 380 | */ |
| 381 | private boolean networkExists(NetworkId networkId) { |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 382 | checkNotNull(networkId, "The network identifier cannot be null."); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 383 | return (networkIdVirtualNetworkMap.containsKey(networkId)); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | @Override |
| 387 | public VirtualDevice addDevice(NetworkId networkId, DeviceId deviceId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 388 | checkState(networkExists(networkId), "The network has not been added."); |
Yoonseon Han | aa25605 | 2017-05-08 16:25:38 -0700 | [diff] [blame] | 389 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 390 | Set<DeviceId> deviceIdSet = networkIdDeviceIdSetMap.get(networkId); |
| 391 | if (deviceIdSet == null) { |
| 392 | deviceIdSet = new HashSet<>(); |
| 393 | } |
Yoonseon Han | aa25605 | 2017-05-08 16:25:38 -0700 | [diff] [blame] | 394 | |
| 395 | checkState(!deviceIdSet.contains(deviceId), "The device already exists."); |
| 396 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 397 | VirtualDevice virtualDevice = new DefaultVirtualDevice(networkId, deviceId); |
| 398 | //TODO update both maps in one transaction. |
Yoonseon Han | aa25605 | 2017-05-08 16:25:38 -0700 | [diff] [blame] | 399 | deviceIdVirtualDeviceMap.put(new VirtualDeviceId(networkId, deviceId), virtualDevice); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 400 | deviceIdSet.add(deviceId); |
| 401 | networkIdDeviceIdSetMap.put(networkId, deviceIdSet); |
| 402 | return virtualDevice; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | @Override |
| 406 | public void removeDevice(NetworkId networkId, DeviceId deviceId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 407 | checkState(networkExists(networkId), "The network has not been added."); |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 408 | //Remove all the virtual ports of the this device |
| 409 | Set<VirtualPort> virtualPorts = getPorts(networkId, deviceId); |
| 410 | if (virtualPorts != null) { |
| 411 | virtualPorts.forEach(virtualPort -> removePort(networkId, deviceId, virtualPort.number())); |
| 412 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 413 | //TODO update both maps in one transaction. |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 414 | |
| 415 | Set<DeviceId> deviceIdSet = new HashSet<>(); |
| 416 | networkIdDeviceIdSetMap.get(networkId).forEach(deviceId1 -> { |
| 417 | if (deviceId1.equals(deviceId)) { |
| 418 | deviceIdSet.add(deviceId1); |
| 419 | } |
| 420 | }); |
| 421 | |
Yoonseon Han | aa25605 | 2017-05-08 16:25:38 -0700 | [diff] [blame] | 422 | if (!deviceIdSet.isEmpty()) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 423 | networkIdDeviceIdSetMap.compute(networkId, (id, existingDeviceIds) -> { |
| 424 | if (existingDeviceIds == null || existingDeviceIds.isEmpty()) { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 425 | return new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 426 | } else { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 427 | return new HashSet<>(Sets.difference(existingDeviceIds, deviceIdSet)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 428 | } |
| 429 | }); |
| 430 | |
Yoonseon Han | aa25605 | 2017-05-08 16:25:38 -0700 | [diff] [blame] | 431 | deviceIdVirtualDeviceMap.remove(new VirtualDeviceId(networkId, deviceId)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 432 | } |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public VirtualHost addHost(NetworkId networkId, HostId hostId, MacAddress mac, |
| 437 | VlanId vlan, HostLocation location, Set<IpAddress> ips) { |
| 438 | checkState(networkExists(networkId), "The network has not been added."); |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 439 | checkState(virtualPortExists(networkId, location.deviceId(), location.port()), |
| 440 | "The virtual port has not been created."); |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 441 | Set<HostId> hostIdSet = networkIdHostIdSetMap.get(networkId); |
| 442 | if (hostIdSet == null) { |
| 443 | hostIdSet = new HashSet<>(); |
| 444 | } |
| 445 | VirtualHost virtualhost = new DefaultVirtualHost(networkId, hostId, mac, vlan, location, ips); |
| 446 | //TODO update both maps in one transaction. |
| 447 | hostIdVirtualHostMap.put(hostId, virtualhost); |
| 448 | hostIdSet.add(hostId); |
| 449 | networkIdHostIdSetMap.put(networkId, hostIdSet); |
| 450 | return virtualhost; |
| 451 | } |
| 452 | |
| 453 | @Override |
| 454 | public void removeHost(NetworkId networkId, HostId hostId) { |
| 455 | checkState(networkExists(networkId), "The network has not been added."); |
| 456 | //TODO update both maps in one transaction. |
| 457 | |
| 458 | Set<HostId> hostIdSet = new HashSet<>(); |
| 459 | networkIdHostIdSetMap.get(networkId).forEach(hostId1 -> { |
| 460 | if (hostId1.equals(hostId)) { |
| 461 | hostIdSet.add(hostId1); |
| 462 | } |
| 463 | }); |
| 464 | |
| 465 | if (hostIdSet != null) { |
| 466 | networkIdHostIdSetMap.compute(networkId, (id, existingHostIds) -> { |
| 467 | if (existingHostIds == null || existingHostIds.isEmpty()) { |
| 468 | return new HashSet<>(); |
| 469 | } else { |
| 470 | return new HashSet<>(Sets.difference(existingHostIds, hostIdSet)); |
| 471 | } |
| 472 | }); |
| 473 | |
| 474 | hostIdVirtualHostMap.remove(hostId); |
| 475 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 478 | /** |
| 479 | * Returns if the given virtual port exists. |
| 480 | * |
| 481 | * @param networkId network identifier |
| 482 | * @param deviceId virtual device Id |
| 483 | * @param portNumber virtual port number |
| 484 | * @return true if the virtual port exists, false otherwise. |
| 485 | */ |
| 486 | private boolean virtualPortExists(NetworkId networkId, DeviceId deviceId, PortNumber portNumber) { |
| 487 | Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId); |
| 488 | if (virtualPortSet != null) { |
| 489 | return virtualPortSet.stream().anyMatch( |
| 490 | p -> p.element().id().equals(deviceId) && |
| 491 | p.number().equals(portNumber)); |
| 492 | } else { |
| 493 | return false; |
| 494 | } |
| 495 | } |
| 496 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 497 | @Override |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 498 | public VirtualLink addLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst, |
| 499 | Link.State state, TunnelId realizedBy) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 500 | checkState(networkExists(networkId), "The network has not been added."); |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 501 | checkState(virtualPortExists(networkId, src.deviceId(), src.port()), |
| 502 | "The source virtual port has not been added."); |
| 503 | checkState(virtualPortExists(networkId, dst.deviceId(), dst.port()), |
| 504 | "The destination virtual port has not been added."); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 505 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId); |
| 506 | if (virtualLinkSet == null) { |
| 507 | virtualLinkSet = new HashSet<>(); |
| 508 | } |
Harold Huang | 7362e67 | 2017-04-19 10:00:11 +0800 | [diff] [blame] | 509 | |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 510 | // validate that the link does not already exist in this network |
Harold Huang | 7362e67 | 2017-04-19 10:00:11 +0800 | [diff] [blame] | 511 | checkState(getLink(networkId, src, dst) == null, |
| 512 | "The virtual link already exists"); |
| 513 | checkState(getLink(networkId, src, null) == null, |
| 514 | "The source connection point has been used by another link"); |
| 515 | checkState(getLink(networkId, null, dst) == null, |
| 516 | "The destination connection point has been used by another link"); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 517 | |
| 518 | VirtualLink virtualLink = DefaultVirtualLink.builder() |
| 519 | .networkId(networkId) |
| 520 | .src(src) |
| 521 | .dst(dst) |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 522 | .state(state) |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 523 | .tunnelId(realizedBy) |
| 524 | .build(); |
| 525 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 526 | virtualLinkSet.add(virtualLink); |
| 527 | networkIdVirtualLinkSetMap.put(networkId, virtualLinkSet); |
| 528 | return virtualLink; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | @Override |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 532 | public void updateLink(VirtualLink virtualLink, TunnelId tunnelId, Link.State state) { |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 533 | checkState(networkExists(virtualLink.networkId()), "The network has not been added."); |
| 534 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(virtualLink.networkId()); |
| 535 | if (virtualLinkSet == null) { |
| 536 | virtualLinkSet = new HashSet<>(); |
Harold Huang | 7362e67 | 2017-04-19 10:00:11 +0800 | [diff] [blame] | 537 | networkIdVirtualLinkSetMap.put(virtualLink.networkId(), virtualLinkSet); |
| 538 | log.warn("The updated virtual link {} has not been added", virtualLink); |
| 539 | return; |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 540 | } |
Harold Huang | 7362e67 | 2017-04-19 10:00:11 +0800 | [diff] [blame] | 541 | if (!virtualLinkSet.remove(virtualLink)) { |
| 542 | log.warn("The updated virtual link {} does not exist", virtualLink); |
| 543 | return; |
| 544 | } |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 545 | |
| 546 | VirtualLink newVirtualLink = DefaultVirtualLink.builder() |
| 547 | .networkId(virtualLink.networkId()) |
| 548 | .src(virtualLink.src()) |
| 549 | .dst(virtualLink.dst()) |
| 550 | .tunnelId(tunnelId) |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 551 | .state(state) |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 552 | .build(); |
| 553 | |
| 554 | virtualLinkSet.add(newVirtualLink); |
| 555 | networkIdVirtualLinkSetMap.put(newVirtualLink.networkId(), virtualLinkSet); |
| 556 | } |
| 557 | |
| 558 | @Override |
| 559 | public VirtualLink removeLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 560 | checkState(networkExists(networkId), "The network has not been added."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 561 | |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 562 | final VirtualLink virtualLink = getLink(networkId, src, dst); |
| 563 | if (virtualLink == null) { |
Harold Huang | 7362e67 | 2017-04-19 10:00:11 +0800 | [diff] [blame] | 564 | 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] | 565 | return null; |
| 566 | } |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 567 | Set<VirtualLink> virtualLinkSet = new HashSet<>(); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 568 | virtualLinkSet.add(virtualLink); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 569 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 570 | if (virtualLinkSet != null) { |
| 571 | networkIdVirtualLinkSetMap.compute(networkId, (id, existingVirtualLinks) -> { |
| 572 | if (existingVirtualLinks == null || existingVirtualLinks.isEmpty()) { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 573 | return new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 574 | } else { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 575 | return new HashSet<>(Sets.difference(existingVirtualLinks, virtualLinkSet)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 576 | } |
| 577 | }); |
| 578 | } |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 579 | return virtualLink; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | @Override |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 583 | public VirtualPort addPort(NetworkId networkId, DeviceId deviceId, |
| 584 | PortNumber portNumber, ConnectPoint realizedBy) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 585 | checkState(networkExists(networkId), "The network has not been added."); |
| 586 | Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 587 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 588 | if (virtualPortSet == null) { |
| 589 | virtualPortSet = new HashSet<>(); |
| 590 | } |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 591 | |
Yoonseon Han | aa25605 | 2017-05-08 16:25:38 -0700 | [diff] [blame] | 592 | VirtualDevice device = deviceIdVirtualDeviceMap.get(new VirtualDeviceId(networkId, deviceId)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 593 | checkNotNull(device, "The device has not been created for deviceId: " + deviceId); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 594 | |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 595 | checkState(!virtualPortExists(networkId, deviceId, portNumber), |
| 596 | "The requested Port Number has been added."); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 597 | |
| 598 | VirtualPort virtualPort = new DefaultVirtualPort(networkId, device, |
| 599 | portNumber, realizedBy); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 600 | virtualPortSet.add(virtualPort); |
| 601 | networkIdVirtualPortSetMap.put(networkId, virtualPortSet); |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 602 | notifyDelegate(new VirtualNetworkEvent(VirtualNetworkEvent.Type.VIRTUAL_PORT_ADDED, |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 603 | networkId, device, virtualPort)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 604 | return virtualPort; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | @Override |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 608 | public void bindPort(NetworkId networkId, DeviceId deviceId, |
| 609 | PortNumber portNumber, ConnectPoint realizedBy) { |
| 610 | |
| 611 | Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap |
| 612 | .get(networkId); |
| 613 | |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 614 | Optional<VirtualPort> virtualPortOptional = virtualPortSet.stream().filter( |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 615 | p -> p.element().id().equals(deviceId) && |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 616 | p.number().equals(portNumber)).findFirst(); |
| 617 | checkState(virtualPortOptional.isPresent(), "The virtual port has not been added."); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 618 | |
Yoonseon Han | aa25605 | 2017-05-08 16:25:38 -0700 | [diff] [blame] | 619 | VirtualDevice device = deviceIdVirtualDeviceMap.get(new VirtualDeviceId(networkId, deviceId)); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 620 | checkNotNull(device, "The device has not been created for deviceId: " |
| 621 | + deviceId); |
| 622 | |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 623 | VirtualPort vPort = virtualPortOptional.get(); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 624 | virtualPortSet.remove(vPort); |
| 625 | vPort = new DefaultVirtualPort(networkId, device, portNumber, realizedBy); |
| 626 | virtualPortSet.add(vPort); |
| 627 | networkIdVirtualPortSetMap.put(networkId, virtualPortSet); |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 628 | notifyDelegate(new VirtualNetworkEvent(VirtualNetworkEvent.Type.VIRTUAL_PORT_UPDATED, |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 629 | networkId, device, vPort)); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | @Override |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 633 | public void removePort(NetworkId networkId, DeviceId deviceId, PortNumber portNumber) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 634 | checkState(networkExists(networkId), "The network has not been added."); |
Yoonseon Han | aa25605 | 2017-05-08 16:25:38 -0700 | [diff] [blame] | 635 | VirtualDevice device = deviceIdVirtualDeviceMap.get(new VirtualDeviceId(networkId, deviceId)); |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 636 | checkNotNull(device, "The device has not been created for deviceId: " |
| 637 | + deviceId); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 638 | |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 639 | if (networkIdVirtualPortSetMap.get(networkId) == null) { |
| 640 | log.warn("No port has been created for NetworkId: {}", networkId); |
| 641 | return; |
| 642 | } |
| 643 | |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 644 | Set<VirtualPort> virtualPortSet = new HashSet<>(); |
| 645 | networkIdVirtualPortSetMap.get(networkId).forEach(port -> { |
| 646 | if (port.element().id().equals(deviceId) && port.number().equals(portNumber)) { |
| 647 | virtualPortSet.add(port); |
| 648 | } |
| 649 | }); |
| 650 | |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 651 | if (!virtualPortSet.isEmpty()) { |
| 652 | AtomicBoolean portRemoved = new AtomicBoolean(false); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 653 | networkIdVirtualPortSetMap.compute(networkId, (id, existingVirtualPorts) -> { |
| 654 | if (existingVirtualPorts == null || existingVirtualPorts.isEmpty()) { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 655 | return new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 656 | } else { |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 657 | portRemoved.set(true); |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 658 | return new HashSet<>(Sets.difference(existingVirtualPorts, virtualPortSet)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 659 | } |
| 660 | }); |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 661 | if (portRemoved.get()) { |
| 662 | virtualPortSet.forEach(virtualPort -> notifyDelegate( |
| 663 | new VirtualNetworkEvent(VirtualNetworkEvent.Type.VIRTUAL_PORT_REMOVED, |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 664 | networkId, device, virtualPort) |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 665 | )); |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 666 | |
| 667 | //Remove all the virtual links connected to this virtual port |
| 668 | Set<VirtualLink> existingVirtualLinks = networkIdVirtualLinkSetMap.get(networkId); |
| 669 | if (existingVirtualLinks != null && !existingVirtualLinks.isEmpty()) { |
| 670 | Set<VirtualLink> virtualLinkSet = new HashSet<>(); |
| 671 | ConnectPoint cp = new ConnectPoint(deviceId, portNumber); |
| 672 | existingVirtualLinks.forEach(virtualLink -> { |
| 673 | if (virtualLink.src().equals(cp) || virtualLink.dst().equals(cp)) { |
| 674 | virtualLinkSet.add(virtualLink); |
| 675 | } |
| 676 | }); |
| 677 | virtualLinkSet.forEach(virtualLink -> |
| 678 | removeLink(networkId, virtualLink.src(), virtualLink.dst())); |
| 679 | } |
| 680 | |
| 681 | //Remove all the hosts connected to this virtual port |
| 682 | Set<HostId> hostIdSet = new HashSet<>(); |
| 683 | hostIdVirtualHostMap.forEach((hostId, virtualHost) -> { |
| 684 | if (virtualHost.location().deviceId().equals(deviceId) && |
| 685 | virtualHost.location().port().equals(portNumber)) { |
| 686 | hostIdSet.add(hostId); |
| 687 | } |
| 688 | }); |
| 689 | hostIdSet.forEach(hostId -> removeHost(networkId, hostId)); |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 690 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 691 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | @Override |
| 695 | public Set<VirtualNetwork> getNetworks(TenantId tenantId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 696 | Set<NetworkId> networkIdSet = tenantIdNetworkIdSetMap.get(tenantId); |
| 697 | Set<VirtualNetwork> virtualNetworkSet = new HashSet<>(); |
| 698 | if (networkIdSet != null) { |
Harold Huang | e86d35f | 2017-06-14 15:15:18 +0800 | [diff] [blame] | 699 | networkIdSet.forEach(networkId -> { |
| 700 | if (networkIdVirtualNetworkMap.get(networkId) != null) { |
| 701 | virtualNetworkSet.add(networkIdVirtualNetworkMap.get(networkId)); |
| 702 | } |
| 703 | }); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 704 | } |
| 705 | return ImmutableSet.copyOf(virtualNetworkSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | @Override |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 709 | public VirtualNetwork getNetwork(NetworkId networkId) { |
| 710 | return networkIdVirtualNetworkMap.get(networkId); |
| 711 | } |
| 712 | |
| 713 | @Override |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 714 | public Set<VirtualDevice> getDevices(NetworkId networkId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 715 | checkState(networkExists(networkId), "The network has not been added."); |
| 716 | Set<DeviceId> deviceIdSet = networkIdDeviceIdSetMap.get(networkId); |
| 717 | Set<VirtualDevice> virtualDeviceSet = new HashSet<>(); |
| 718 | if (deviceIdSet != null) { |
Yoonseon Han | aa25605 | 2017-05-08 16:25:38 -0700 | [diff] [blame] | 719 | deviceIdSet.forEach(deviceId -> virtualDeviceSet.add( |
| 720 | deviceIdVirtualDeviceMap.get(new VirtualDeviceId(networkId, deviceId)))); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 721 | } |
| 722 | return ImmutableSet.copyOf(virtualDeviceSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | @Override |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 726 | public Set<VirtualHost> getHosts(NetworkId networkId) { |
| 727 | checkState(networkExists(networkId), "The network has not been added."); |
| 728 | Set<HostId> hostIdSet = networkIdHostIdSetMap.get(networkId); |
| 729 | Set<VirtualHost> virtualHostSet = new HashSet<>(); |
| 730 | if (hostIdSet != null) { |
| 731 | hostIdSet.forEach(hostId -> virtualHostSet.add(hostIdVirtualHostMap.get(hostId))); |
| 732 | } |
| 733 | return ImmutableSet.copyOf(virtualHostSet); |
| 734 | } |
| 735 | |
| 736 | @Override |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 737 | public Set<VirtualLink> getLinks(NetworkId networkId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 738 | checkState(networkExists(networkId), "The network has not been added."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 739 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId); |
| 740 | if (virtualLinkSet == null) { |
| 741 | virtualLinkSet = new HashSet<>(); |
| 742 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 743 | return ImmutableSet.copyOf(virtualLinkSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 744 | } |
| 745 | |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 746 | @Override |
| 747 | public VirtualLink getLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst) { |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 748 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId); |
| 749 | if (virtualLinkSet == null) { |
| 750 | return null; |
| 751 | } |
| 752 | |
| 753 | VirtualLink virtualLink = null; |
| 754 | for (VirtualLink link : virtualLinkSet) { |
Harold Huang | 7362e67 | 2017-04-19 10:00:11 +0800 | [diff] [blame] | 755 | if (src == null && link.dst().equals(dst)) { |
| 756 | virtualLink = link; |
| 757 | break; |
| 758 | } else if (dst == null && link.src().equals(src)) { |
| 759 | virtualLink = link; |
| 760 | break; |
| 761 | } else if (link.src().equals(src) && link.dst().equals(dst)) { |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 762 | virtualLink = link; |
| 763 | break; |
| 764 | } |
| 765 | } |
| 766 | return virtualLink; |
| 767 | } |
| 768 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 769 | @Override |
| 770 | public Set<VirtualPort> getPorts(NetworkId networkId, DeviceId deviceId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 771 | checkState(networkExists(networkId), "The network has not been added."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 772 | Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId); |
| 773 | if (virtualPortSet == null) { |
| 774 | virtualPortSet = new HashSet<>(); |
| 775 | } |
| 776 | |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 777 | if (deviceId == null) { |
| 778 | return ImmutableSet.copyOf(virtualPortSet); |
| 779 | } |
| 780 | |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 781 | Set<VirtualPort> portSet = new HashSet<>(); |
| 782 | virtualPortSet.forEach(virtualPort -> { |
| 783 | if (virtualPort.element().id().equals(deviceId)) { |
| 784 | portSet.add(virtualPort); |
| 785 | } |
| 786 | }); |
| 787 | return ImmutableSet.copyOf(portSet); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 788 | } |
| 789 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 790 | @Override |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 791 | public void addTunnelId(Intent intent, TunnelId tunnelId) { |
| 792 | // Add the tunnelId to the intent key set map |
| 793 | Set<TunnelId> tunnelIdSet = intentKeyTunnelIdSetMap.remove(intent.key()); |
| 794 | if (tunnelIdSet == null) { |
| 795 | tunnelIdSet = new HashSet<>(); |
| 796 | } |
| 797 | tunnelIdSet.add(tunnelId); |
| 798 | intentKeyTunnelIdSetMap.put(intent.key(), tunnelIdSet); |
| 799 | } |
| 800 | |
| 801 | @Override |
| 802 | public Set<TunnelId> getTunnelIds(Intent intent) { |
| 803 | Set<TunnelId> tunnelIdSet = intentKeyTunnelIdSetMap.get(intent.key()); |
| 804 | return tunnelIdSet == null ? new HashSet<TunnelId>() : ImmutableSet.copyOf(tunnelIdSet); |
| 805 | } |
| 806 | |
| 807 | @Override |
| 808 | public void removeTunnelId(Intent intent, TunnelId tunnelId) { |
| 809 | Set<TunnelId> tunnelIdSet = new HashSet<>(); |
| 810 | intentKeyTunnelIdSetMap.get(intent.key()).forEach(tunnelId1 -> { |
| 811 | if (tunnelId1.equals(tunnelId)) { |
| 812 | tunnelIdSet.add(tunnelId); |
| 813 | } |
| 814 | }); |
| 815 | |
| 816 | if (!tunnelIdSet.isEmpty()) { |
| 817 | intentKeyTunnelIdSetMap.compute(intent.key(), (key, existingTunnelIds) -> { |
| 818 | if (existingTunnelIds == null || existingTunnelIds.isEmpty()) { |
| 819 | return new HashSet<>(); |
| 820 | } else { |
| 821 | return new HashSet<>(Sets.difference(existingTunnelIds, tunnelIdSet)); |
| 822 | } |
| 823 | }); |
| 824 | } |
| 825 | } |
| 826 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 827 | /** |
| 828 | * Listener class to map listener set events to the virtual network events. |
| 829 | */ |
| 830 | private class InternalSetListener implements SetEventListener<TenantId> { |
| 831 | @Override |
| 832 | public void event(SetEvent<TenantId> event) { |
| 833 | VirtualNetworkEvent.Type type = null; |
| 834 | switch (event.type()) { |
| 835 | case ADD: |
| 836 | type = VirtualNetworkEvent.Type.TENANT_REGISTERED; |
| 837 | break; |
| 838 | case REMOVE: |
| 839 | type = VirtualNetworkEvent.Type.TENANT_UNREGISTERED; |
| 840 | break; |
| 841 | default: |
| 842 | log.error("Unsupported event type: " + event.type()); |
| 843 | } |
| 844 | notifyDelegate(new VirtualNetworkEvent(type, null)); |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | /** |
| 849 | * Listener class to map listener map events to the virtual network events. |
| 850 | */ |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 851 | private class InternalMapListener<K, V> implements MapEventListener<K, V> { |
| 852 | |
| 853 | private final BiFunction<MapEvent.Type, V, VirtualNetworkEvent> createEvent; |
| 854 | |
| 855 | InternalMapListener(BiFunction<MapEvent.Type, V, VirtualNetworkEvent> createEvent) { |
| 856 | this.createEvent = createEvent; |
| 857 | } |
| 858 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 859 | @Override |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 860 | public void event(MapEvent<K, V> event) { |
| 861 | checkNotNull(event.key()); |
| 862 | VirtualNetworkEvent vnetEvent = null; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 863 | switch (event.type()) { |
| 864 | case INSERT: |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 865 | vnetEvent = createEvent.apply(event.type(), event.newValue().value()); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 866 | break; |
| 867 | case UPDATE: |
| 868 | if ((event.oldValue().value() != null) && (event.newValue().value() == null)) { |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 869 | vnetEvent = createEvent.apply(MapEvent.Type.REMOVE, event.oldValue().value()); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 870 | } else { |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 871 | vnetEvent = createEvent.apply(event.type(), event.newValue().value()); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 872 | } |
| 873 | break; |
| 874 | case REMOVE: |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 875 | if (event.oldValue() != null) { |
| 876 | vnetEvent = createEvent.apply(event.type(), event.oldValue().value()); |
| 877 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 878 | break; |
| 879 | default: |
| 880 | log.error("Unsupported event type: " + event.type()); |
| 881 | } |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 882 | if (vnetEvent != null) { |
| 883 | notifyDelegate(vnetEvent); |
| 884 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 885 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 886 | } |
Yoonseon Han | aa25605 | 2017-05-08 16:25:38 -0700 | [diff] [blame] | 887 | |
| 888 | /** |
| 889 | * A wrapper class to isolate device id from other virtual networks. |
| 890 | */ |
| 891 | |
| 892 | private static class VirtualDeviceId { |
| 893 | |
| 894 | NetworkId networkId; |
| 895 | DeviceId deviceId; |
| 896 | |
| 897 | public VirtualDeviceId(NetworkId networkId, DeviceId deviceId) { |
| 898 | this.networkId = networkId; |
| 899 | this.deviceId = deviceId; |
| 900 | } |
| 901 | |
| 902 | public NetworkId getNetworkId() { |
| 903 | return networkId; |
| 904 | } |
| 905 | |
| 906 | public DeviceId getDeviceId() { |
| 907 | return deviceId; |
| 908 | } |
| 909 | |
| 910 | @Override |
| 911 | public int hashCode() { |
| 912 | return Objects.hash(networkId, deviceId); |
| 913 | } |
| 914 | |
| 915 | @Override |
| 916 | public boolean equals(Object obj) { |
| 917 | if (this == obj) { |
| 918 | return true; |
| 919 | } |
| 920 | |
| 921 | if (obj instanceof VirtualDeviceId) { |
| 922 | VirtualDeviceId that = (VirtualDeviceId) obj; |
| 923 | return this.deviceId.equals(that.deviceId) && |
| 924 | this.networkId.equals(that.networkId); |
| 925 | } |
| 926 | return false; |
| 927 | } |
| 928 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 929 | } |