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