Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 |
Claudine Chiu | 579969d | 2017-10-06 14:32:18 -0400 | [diff] [blame^] | 633 | public void updatePortState(NetworkId networkId, DeviceId deviceId, |
| 634 | PortNumber portNumber, boolean isEnabled) { |
| 635 | checkState(networkExists(networkId), "No network with NetworkId %s exists.", networkId); |
| 636 | |
| 637 | VirtualDevice device = deviceIdVirtualDeviceMap.get(new VirtualDeviceId(networkId, deviceId)); |
| 638 | checkNotNull(device, "No device %s exists in NetworkId: %s", deviceId, networkId); |
| 639 | |
| 640 | Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId); |
| 641 | checkNotNull(virtualPortSet, "No port has been created for NetworkId: %s", networkId); |
| 642 | |
| 643 | Optional<VirtualPort> virtualPortOptional = virtualPortSet.stream().filter( |
| 644 | p -> p.element().id().equals(deviceId) && |
| 645 | p.number().equals(portNumber)).findFirst(); |
| 646 | checkState(virtualPortOptional.isPresent(), "The virtual port has not been added."); |
| 647 | |
| 648 | VirtualPort oldPort = virtualPortOptional.get(); |
| 649 | if (oldPort.isEnabled() == isEnabled) { |
| 650 | log.debug("No change in port state - port not updated"); |
| 651 | return; |
| 652 | } |
| 653 | VirtualPort newPort = new DefaultVirtualPort(networkId, device, portNumber, isEnabled, |
| 654 | oldPort.realizedBy()); |
| 655 | virtualPortSet.remove(oldPort); |
| 656 | virtualPortSet.add(newPort); |
| 657 | networkIdVirtualPortSetMap.put(networkId, virtualPortSet); |
| 658 | notifyDelegate(new VirtualNetworkEvent(VirtualNetworkEvent.Type.VIRTUAL_PORT_UPDATED, |
| 659 | networkId, device, newPort)); |
| 660 | log.debug("port state changed from {} to {}", oldPort.isEnabled(), isEnabled); |
| 661 | } |
| 662 | |
| 663 | @Override |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 664 | public void removePort(NetworkId networkId, DeviceId deviceId, PortNumber portNumber) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 665 | checkState(networkExists(networkId), "The network has not been added."); |
Yoonseon Han | aa25605 | 2017-05-08 16:25:38 -0700 | [diff] [blame] | 666 | VirtualDevice device = deviceIdVirtualDeviceMap.get(new VirtualDeviceId(networkId, deviceId)); |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 667 | checkNotNull(device, "The device has not been created for deviceId: " |
| 668 | + deviceId); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 669 | |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 670 | if (networkIdVirtualPortSetMap.get(networkId) == null) { |
| 671 | log.warn("No port has been created for NetworkId: {}", networkId); |
| 672 | return; |
| 673 | } |
| 674 | |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 675 | Set<VirtualPort> virtualPortSet = new HashSet<>(); |
| 676 | networkIdVirtualPortSetMap.get(networkId).forEach(port -> { |
| 677 | if (port.element().id().equals(deviceId) && port.number().equals(portNumber)) { |
| 678 | virtualPortSet.add(port); |
| 679 | } |
| 680 | }); |
| 681 | |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 682 | if (!virtualPortSet.isEmpty()) { |
| 683 | AtomicBoolean portRemoved = new AtomicBoolean(false); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 684 | networkIdVirtualPortSetMap.compute(networkId, (id, existingVirtualPorts) -> { |
| 685 | if (existingVirtualPorts == null || existingVirtualPorts.isEmpty()) { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 686 | return new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 687 | } else { |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 688 | portRemoved.set(true); |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 689 | return new HashSet<>(Sets.difference(existingVirtualPorts, virtualPortSet)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 690 | } |
| 691 | }); |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 692 | if (portRemoved.get()) { |
| 693 | virtualPortSet.forEach(virtualPort -> notifyDelegate( |
| 694 | new VirtualNetworkEvent(VirtualNetworkEvent.Type.VIRTUAL_PORT_REMOVED, |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 695 | networkId, device, virtualPort) |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 696 | )); |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 697 | |
| 698 | //Remove all the virtual links connected to this virtual port |
| 699 | Set<VirtualLink> existingVirtualLinks = networkIdVirtualLinkSetMap.get(networkId); |
| 700 | if (existingVirtualLinks != null && !existingVirtualLinks.isEmpty()) { |
| 701 | Set<VirtualLink> virtualLinkSet = new HashSet<>(); |
| 702 | ConnectPoint cp = new ConnectPoint(deviceId, portNumber); |
| 703 | existingVirtualLinks.forEach(virtualLink -> { |
| 704 | if (virtualLink.src().equals(cp) || virtualLink.dst().equals(cp)) { |
| 705 | virtualLinkSet.add(virtualLink); |
| 706 | } |
| 707 | }); |
| 708 | virtualLinkSet.forEach(virtualLink -> |
| 709 | removeLink(networkId, virtualLink.src(), virtualLink.dst())); |
| 710 | } |
| 711 | |
| 712 | //Remove all the hosts connected to this virtual port |
| 713 | Set<HostId> hostIdSet = new HashSet<>(); |
| 714 | hostIdVirtualHostMap.forEach((hostId, virtualHost) -> { |
| 715 | if (virtualHost.location().deviceId().equals(deviceId) && |
| 716 | virtualHost.location().port().equals(portNumber)) { |
| 717 | hostIdSet.add(hostId); |
| 718 | } |
| 719 | }); |
| 720 | hostIdSet.forEach(hostId -> removeHost(networkId, hostId)); |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 721 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 722 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | @Override |
| 726 | public Set<VirtualNetwork> getNetworks(TenantId tenantId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 727 | Set<NetworkId> networkIdSet = tenantIdNetworkIdSetMap.get(tenantId); |
| 728 | Set<VirtualNetwork> virtualNetworkSet = new HashSet<>(); |
| 729 | if (networkIdSet != null) { |
Harold Huang | e86d35f | 2017-06-14 15:15:18 +0800 | [diff] [blame] | 730 | networkIdSet.forEach(networkId -> { |
| 731 | if (networkIdVirtualNetworkMap.get(networkId) != null) { |
| 732 | virtualNetworkSet.add(networkIdVirtualNetworkMap.get(networkId)); |
| 733 | } |
| 734 | }); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 735 | } |
| 736 | return ImmutableSet.copyOf(virtualNetworkSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | @Override |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 740 | public VirtualNetwork getNetwork(NetworkId networkId) { |
| 741 | return networkIdVirtualNetworkMap.get(networkId); |
| 742 | } |
| 743 | |
| 744 | @Override |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 745 | public Set<VirtualDevice> getDevices(NetworkId networkId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 746 | checkState(networkExists(networkId), "The network has not been added."); |
| 747 | Set<DeviceId> deviceIdSet = networkIdDeviceIdSetMap.get(networkId); |
| 748 | Set<VirtualDevice> virtualDeviceSet = new HashSet<>(); |
| 749 | if (deviceIdSet != null) { |
Yoonseon Han | aa25605 | 2017-05-08 16:25:38 -0700 | [diff] [blame] | 750 | deviceIdSet.forEach(deviceId -> virtualDeviceSet.add( |
| 751 | deviceIdVirtualDeviceMap.get(new VirtualDeviceId(networkId, deviceId)))); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 752 | } |
| 753 | return ImmutableSet.copyOf(virtualDeviceSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | @Override |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 757 | public Set<VirtualHost> getHosts(NetworkId networkId) { |
| 758 | checkState(networkExists(networkId), "The network has not been added."); |
| 759 | Set<HostId> hostIdSet = networkIdHostIdSetMap.get(networkId); |
| 760 | Set<VirtualHost> virtualHostSet = new HashSet<>(); |
| 761 | if (hostIdSet != null) { |
| 762 | hostIdSet.forEach(hostId -> virtualHostSet.add(hostIdVirtualHostMap.get(hostId))); |
| 763 | } |
| 764 | return ImmutableSet.copyOf(virtualHostSet); |
| 765 | } |
| 766 | |
| 767 | @Override |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 768 | public Set<VirtualLink> getLinks(NetworkId networkId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 769 | checkState(networkExists(networkId), "The network has not been added."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 770 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId); |
| 771 | if (virtualLinkSet == null) { |
| 772 | virtualLinkSet = new HashSet<>(); |
| 773 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 774 | return ImmutableSet.copyOf(virtualLinkSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 777 | @Override |
| 778 | public VirtualLink getLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst) { |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 779 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId); |
| 780 | if (virtualLinkSet == null) { |
| 781 | return null; |
| 782 | } |
| 783 | |
| 784 | VirtualLink virtualLink = null; |
| 785 | for (VirtualLink link : virtualLinkSet) { |
Harold Huang | 7362e67 | 2017-04-19 10:00:11 +0800 | [diff] [blame] | 786 | if (src == null && link.dst().equals(dst)) { |
| 787 | virtualLink = link; |
| 788 | break; |
| 789 | } else if (dst == null && link.src().equals(src)) { |
| 790 | virtualLink = link; |
| 791 | break; |
| 792 | } else if (link.src().equals(src) && link.dst().equals(dst)) { |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 793 | virtualLink = link; |
| 794 | break; |
| 795 | } |
| 796 | } |
| 797 | return virtualLink; |
| 798 | } |
| 799 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 800 | @Override |
| 801 | public Set<VirtualPort> getPorts(NetworkId networkId, DeviceId deviceId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 802 | checkState(networkExists(networkId), "The network has not been added."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 803 | Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId); |
| 804 | if (virtualPortSet == null) { |
| 805 | virtualPortSet = new HashSet<>(); |
| 806 | } |
| 807 | |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 808 | if (deviceId == null) { |
| 809 | return ImmutableSet.copyOf(virtualPortSet); |
| 810 | } |
| 811 | |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 812 | Set<VirtualPort> portSet = new HashSet<>(); |
| 813 | virtualPortSet.forEach(virtualPort -> { |
| 814 | if (virtualPort.element().id().equals(deviceId)) { |
| 815 | portSet.add(virtualPort); |
| 816 | } |
| 817 | }); |
| 818 | return ImmutableSet.copyOf(portSet); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 819 | } |
| 820 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 821 | @Override |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 822 | public void addTunnelId(Intent intent, TunnelId tunnelId) { |
| 823 | // Add the tunnelId to the intent key set map |
| 824 | Set<TunnelId> tunnelIdSet = intentKeyTunnelIdSetMap.remove(intent.key()); |
| 825 | if (tunnelIdSet == null) { |
| 826 | tunnelIdSet = new HashSet<>(); |
| 827 | } |
| 828 | tunnelIdSet.add(tunnelId); |
| 829 | intentKeyTunnelIdSetMap.put(intent.key(), tunnelIdSet); |
| 830 | } |
| 831 | |
| 832 | @Override |
| 833 | public Set<TunnelId> getTunnelIds(Intent intent) { |
| 834 | Set<TunnelId> tunnelIdSet = intentKeyTunnelIdSetMap.get(intent.key()); |
| 835 | return tunnelIdSet == null ? new HashSet<TunnelId>() : ImmutableSet.copyOf(tunnelIdSet); |
| 836 | } |
| 837 | |
| 838 | @Override |
| 839 | public void removeTunnelId(Intent intent, TunnelId tunnelId) { |
| 840 | Set<TunnelId> tunnelIdSet = new HashSet<>(); |
| 841 | intentKeyTunnelIdSetMap.get(intent.key()).forEach(tunnelId1 -> { |
| 842 | if (tunnelId1.equals(tunnelId)) { |
| 843 | tunnelIdSet.add(tunnelId); |
| 844 | } |
| 845 | }); |
| 846 | |
| 847 | if (!tunnelIdSet.isEmpty()) { |
| 848 | intentKeyTunnelIdSetMap.compute(intent.key(), (key, existingTunnelIds) -> { |
| 849 | if (existingTunnelIds == null || existingTunnelIds.isEmpty()) { |
| 850 | return new HashSet<>(); |
| 851 | } else { |
| 852 | return new HashSet<>(Sets.difference(existingTunnelIds, tunnelIdSet)); |
| 853 | } |
| 854 | }); |
| 855 | } |
| 856 | } |
| 857 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 858 | /** |
| 859 | * Listener class to map listener set events to the virtual network events. |
| 860 | */ |
| 861 | private class InternalSetListener implements SetEventListener<TenantId> { |
| 862 | @Override |
| 863 | public void event(SetEvent<TenantId> event) { |
| 864 | VirtualNetworkEvent.Type type = null; |
| 865 | switch (event.type()) { |
| 866 | case ADD: |
| 867 | type = VirtualNetworkEvent.Type.TENANT_REGISTERED; |
| 868 | break; |
| 869 | case REMOVE: |
| 870 | type = VirtualNetworkEvent.Type.TENANT_UNREGISTERED; |
| 871 | break; |
| 872 | default: |
| 873 | log.error("Unsupported event type: " + event.type()); |
| 874 | } |
| 875 | notifyDelegate(new VirtualNetworkEvent(type, null)); |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | /** |
| 880 | * Listener class to map listener map events to the virtual network events. |
| 881 | */ |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 882 | private class InternalMapListener<K, V> implements MapEventListener<K, V> { |
| 883 | |
| 884 | private final BiFunction<MapEvent.Type, V, VirtualNetworkEvent> createEvent; |
| 885 | |
| 886 | InternalMapListener(BiFunction<MapEvent.Type, V, VirtualNetworkEvent> createEvent) { |
| 887 | this.createEvent = createEvent; |
| 888 | } |
| 889 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 890 | @Override |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 891 | public void event(MapEvent<K, V> event) { |
| 892 | checkNotNull(event.key()); |
| 893 | VirtualNetworkEvent vnetEvent = null; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 894 | switch (event.type()) { |
| 895 | case INSERT: |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 896 | vnetEvent = createEvent.apply(event.type(), event.newValue().value()); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 897 | break; |
| 898 | case UPDATE: |
| 899 | if ((event.oldValue().value() != null) && (event.newValue().value() == null)) { |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 900 | vnetEvent = createEvent.apply(MapEvent.Type.REMOVE, event.oldValue().value()); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 901 | } else { |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 902 | vnetEvent = createEvent.apply(event.type(), event.newValue().value()); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 903 | } |
| 904 | break; |
| 905 | case REMOVE: |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 906 | if (event.oldValue() != null) { |
| 907 | vnetEvent = createEvent.apply(event.type(), event.oldValue().value()); |
| 908 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 909 | break; |
| 910 | default: |
| 911 | log.error("Unsupported event type: " + event.type()); |
| 912 | } |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 913 | if (vnetEvent != null) { |
| 914 | notifyDelegate(vnetEvent); |
| 915 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 916 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 917 | } |
Yoonseon Han | aa25605 | 2017-05-08 16:25:38 -0700 | [diff] [blame] | 918 | |
| 919 | /** |
| 920 | * A wrapper class to isolate device id from other virtual networks. |
| 921 | */ |
| 922 | |
| 923 | private static class VirtualDeviceId { |
| 924 | |
| 925 | NetworkId networkId; |
| 926 | DeviceId deviceId; |
| 927 | |
| 928 | public VirtualDeviceId(NetworkId networkId, DeviceId deviceId) { |
| 929 | this.networkId = networkId; |
| 930 | this.deviceId = deviceId; |
| 931 | } |
| 932 | |
| 933 | public NetworkId getNetworkId() { |
| 934 | return networkId; |
| 935 | } |
| 936 | |
| 937 | public DeviceId getDeviceId() { |
| 938 | return deviceId; |
| 939 | } |
| 940 | |
| 941 | @Override |
| 942 | public int hashCode() { |
| 943 | return Objects.hash(networkId, deviceId); |
| 944 | } |
| 945 | |
| 946 | @Override |
| 947 | public boolean equals(Object obj) { |
| 948 | if (this == obj) { |
| 949 | return true; |
| 950 | } |
| 951 | |
| 952 | if (obj instanceof VirtualDeviceId) { |
| 953 | VirtualDeviceId that = (VirtualDeviceId) obj; |
| 954 | return this.deviceId.equals(that.deviceId) && |
| 955 | this.networkId.equals(that.networkId); |
| 956 | } |
| 957 | return false; |
| 958 | } |
| 959 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 960 | } |