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 | |
Ray Milkey | fe0e085 | 2018-01-18 11:14:05 -0800 | [diff] [blame] | 465 | networkIdHostIdSetMap.compute(networkId, (id, existingHostIds) -> { |
| 466 | if (existingHostIds == null || existingHostIds.isEmpty()) { |
| 467 | return new HashSet<>(); |
| 468 | } else { |
| 469 | return new HashSet<>(Sets.difference(existingHostIds, hostIdSet)); |
| 470 | } |
| 471 | }); |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 472 | |
Ray Milkey | fe0e085 | 2018-01-18 11:14:05 -0800 | [diff] [blame] | 473 | hostIdVirtualHostMap.remove(hostId); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 476 | /** |
| 477 | * Returns if the given virtual port exists. |
| 478 | * |
| 479 | * @param networkId network identifier |
| 480 | * @param deviceId virtual device Id |
| 481 | * @param portNumber virtual port number |
| 482 | * @return true if the virtual port exists, false otherwise. |
| 483 | */ |
| 484 | private boolean virtualPortExists(NetworkId networkId, DeviceId deviceId, PortNumber portNumber) { |
| 485 | Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId); |
| 486 | if (virtualPortSet != null) { |
| 487 | return virtualPortSet.stream().anyMatch( |
| 488 | p -> p.element().id().equals(deviceId) && |
| 489 | p.number().equals(portNumber)); |
| 490 | } else { |
| 491 | return false; |
| 492 | } |
| 493 | } |
| 494 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 495 | @Override |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 496 | public VirtualLink addLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst, |
| 497 | Link.State state, TunnelId realizedBy) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 498 | checkState(networkExists(networkId), "The network has not been added."); |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 499 | checkState(virtualPortExists(networkId, src.deviceId(), src.port()), |
| 500 | "The source virtual port has not been added."); |
| 501 | checkState(virtualPortExists(networkId, dst.deviceId(), dst.port()), |
| 502 | "The destination virtual port has not been added."); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 503 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId); |
| 504 | if (virtualLinkSet == null) { |
| 505 | virtualLinkSet = new HashSet<>(); |
| 506 | } |
Harold Huang | 7362e67 | 2017-04-19 10:00:11 +0800 | [diff] [blame] | 507 | |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 508 | // validate that the link does not already exist in this network |
Harold Huang | 7362e67 | 2017-04-19 10:00:11 +0800 | [diff] [blame] | 509 | checkState(getLink(networkId, src, dst) == null, |
| 510 | "The virtual link already exists"); |
| 511 | checkState(getLink(networkId, src, null) == null, |
| 512 | "The source connection point has been used by another link"); |
| 513 | checkState(getLink(networkId, null, dst) == null, |
| 514 | "The destination connection point has been used by another link"); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 515 | |
| 516 | VirtualLink virtualLink = DefaultVirtualLink.builder() |
| 517 | .networkId(networkId) |
| 518 | .src(src) |
| 519 | .dst(dst) |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 520 | .state(state) |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 521 | .tunnelId(realizedBy) |
| 522 | .build(); |
| 523 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 524 | virtualLinkSet.add(virtualLink); |
| 525 | networkIdVirtualLinkSetMap.put(networkId, virtualLinkSet); |
| 526 | return virtualLink; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | @Override |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 530 | public void updateLink(VirtualLink virtualLink, TunnelId tunnelId, Link.State state) { |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 531 | checkState(networkExists(virtualLink.networkId()), "The network has not been added."); |
| 532 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(virtualLink.networkId()); |
| 533 | if (virtualLinkSet == null) { |
| 534 | virtualLinkSet = new HashSet<>(); |
Harold Huang | 7362e67 | 2017-04-19 10:00:11 +0800 | [diff] [blame] | 535 | networkIdVirtualLinkSetMap.put(virtualLink.networkId(), virtualLinkSet); |
| 536 | log.warn("The updated virtual link {} has not been added", virtualLink); |
| 537 | return; |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 538 | } |
Harold Huang | 7362e67 | 2017-04-19 10:00:11 +0800 | [diff] [blame] | 539 | if (!virtualLinkSet.remove(virtualLink)) { |
| 540 | log.warn("The updated virtual link {} does not exist", virtualLink); |
| 541 | return; |
| 542 | } |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 543 | |
| 544 | VirtualLink newVirtualLink = DefaultVirtualLink.builder() |
| 545 | .networkId(virtualLink.networkId()) |
| 546 | .src(virtualLink.src()) |
| 547 | .dst(virtualLink.dst()) |
| 548 | .tunnelId(tunnelId) |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 549 | .state(state) |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 550 | .build(); |
| 551 | |
| 552 | virtualLinkSet.add(newVirtualLink); |
| 553 | networkIdVirtualLinkSetMap.put(newVirtualLink.networkId(), virtualLinkSet); |
| 554 | } |
| 555 | |
| 556 | @Override |
| 557 | public VirtualLink removeLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 558 | checkState(networkExists(networkId), "The network has not been added."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 559 | |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 560 | final VirtualLink virtualLink = getLink(networkId, src, dst); |
| 561 | if (virtualLink == null) { |
Harold Huang | 7362e67 | 2017-04-19 10:00:11 +0800 | [diff] [blame] | 562 | 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] | 563 | return null; |
| 564 | } |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 565 | Set<VirtualLink> virtualLinkSet = new HashSet<>(); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 566 | virtualLinkSet.add(virtualLink); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 567 | |
Ray Milkey | fe0e085 | 2018-01-18 11:14:05 -0800 | [diff] [blame] | 568 | networkIdVirtualLinkSetMap.compute(networkId, (id, existingVirtualLinks) -> { |
| 569 | if (existingVirtualLinks == null || existingVirtualLinks.isEmpty()) { |
| 570 | return new HashSet<>(); |
| 571 | } else { |
| 572 | return new HashSet<>(Sets.difference(existingVirtualLinks, virtualLinkSet)); |
| 573 | } |
| 574 | }); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 575 | return virtualLink; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | @Override |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 579 | public VirtualPort addPort(NetworkId networkId, DeviceId deviceId, |
| 580 | PortNumber portNumber, ConnectPoint realizedBy) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 581 | checkState(networkExists(networkId), "The network has not been added."); |
| 582 | Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 583 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 584 | if (virtualPortSet == null) { |
| 585 | virtualPortSet = new HashSet<>(); |
| 586 | } |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 587 | |
Yoonseon Han | aa25605 | 2017-05-08 16:25:38 -0700 | [diff] [blame] | 588 | VirtualDevice device = deviceIdVirtualDeviceMap.get(new VirtualDeviceId(networkId, deviceId)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 589 | checkNotNull(device, "The device has not been created for deviceId: " + deviceId); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 590 | |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 591 | checkState(!virtualPortExists(networkId, deviceId, portNumber), |
| 592 | "The requested Port Number has been added."); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 593 | |
| 594 | VirtualPort virtualPort = new DefaultVirtualPort(networkId, device, |
| 595 | portNumber, realizedBy); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 596 | virtualPortSet.add(virtualPort); |
| 597 | networkIdVirtualPortSetMap.put(networkId, virtualPortSet); |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 598 | notifyDelegate(new VirtualNetworkEvent(VirtualNetworkEvent.Type.VIRTUAL_PORT_ADDED, |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 599 | networkId, device, virtualPort)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 600 | return virtualPort; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | @Override |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 604 | public void bindPort(NetworkId networkId, DeviceId deviceId, |
| 605 | PortNumber portNumber, ConnectPoint realizedBy) { |
| 606 | |
| 607 | Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap |
| 608 | .get(networkId); |
| 609 | |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 610 | Optional<VirtualPort> virtualPortOptional = virtualPortSet.stream().filter( |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 611 | p -> p.element().id().equals(deviceId) && |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 612 | p.number().equals(portNumber)).findFirst(); |
| 613 | checkState(virtualPortOptional.isPresent(), "The virtual port has not been added."); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 614 | |
Yoonseon Han | aa25605 | 2017-05-08 16:25:38 -0700 | [diff] [blame] | 615 | VirtualDevice device = deviceIdVirtualDeviceMap.get(new VirtualDeviceId(networkId, deviceId)); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 616 | checkNotNull(device, "The device has not been created for deviceId: " |
| 617 | + deviceId); |
| 618 | |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 619 | VirtualPort vPort = virtualPortOptional.get(); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 620 | virtualPortSet.remove(vPort); |
| 621 | vPort = new DefaultVirtualPort(networkId, device, portNumber, realizedBy); |
| 622 | virtualPortSet.add(vPort); |
| 623 | networkIdVirtualPortSetMap.put(networkId, virtualPortSet); |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 624 | notifyDelegate(new VirtualNetworkEvent(VirtualNetworkEvent.Type.VIRTUAL_PORT_UPDATED, |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 625 | networkId, device, vPort)); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | @Override |
Claudine Chiu | 579969d | 2017-10-06 14:32:18 -0400 | [diff] [blame] | 629 | public void updatePortState(NetworkId networkId, DeviceId deviceId, |
| 630 | PortNumber portNumber, boolean isEnabled) { |
| 631 | checkState(networkExists(networkId), "No network with NetworkId %s exists.", networkId); |
| 632 | |
| 633 | VirtualDevice device = deviceIdVirtualDeviceMap.get(new VirtualDeviceId(networkId, deviceId)); |
| 634 | checkNotNull(device, "No device %s exists in NetworkId: %s", deviceId, networkId); |
| 635 | |
| 636 | Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId); |
| 637 | checkNotNull(virtualPortSet, "No port has been created for NetworkId: %s", networkId); |
| 638 | |
| 639 | Optional<VirtualPort> virtualPortOptional = virtualPortSet.stream().filter( |
| 640 | p -> p.element().id().equals(deviceId) && |
| 641 | p.number().equals(portNumber)).findFirst(); |
| 642 | checkState(virtualPortOptional.isPresent(), "The virtual port has not been added."); |
| 643 | |
| 644 | VirtualPort oldPort = virtualPortOptional.get(); |
| 645 | if (oldPort.isEnabled() == isEnabled) { |
| 646 | log.debug("No change in port state - port not updated"); |
| 647 | return; |
| 648 | } |
| 649 | VirtualPort newPort = new DefaultVirtualPort(networkId, device, portNumber, isEnabled, |
| 650 | oldPort.realizedBy()); |
| 651 | virtualPortSet.remove(oldPort); |
| 652 | virtualPortSet.add(newPort); |
| 653 | networkIdVirtualPortSetMap.put(networkId, virtualPortSet); |
| 654 | notifyDelegate(new VirtualNetworkEvent(VirtualNetworkEvent.Type.VIRTUAL_PORT_UPDATED, |
| 655 | networkId, device, newPort)); |
| 656 | log.debug("port state changed from {} to {}", oldPort.isEnabled(), isEnabled); |
| 657 | } |
| 658 | |
| 659 | @Override |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 660 | public void removePort(NetworkId networkId, DeviceId deviceId, PortNumber portNumber) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 661 | checkState(networkExists(networkId), "The network has not been added."); |
Yoonseon Han | aa25605 | 2017-05-08 16:25:38 -0700 | [diff] [blame] | 662 | VirtualDevice device = deviceIdVirtualDeviceMap.get(new VirtualDeviceId(networkId, deviceId)); |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 663 | checkNotNull(device, "The device has not been created for deviceId: " |
| 664 | + deviceId); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 665 | |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 666 | if (networkIdVirtualPortSetMap.get(networkId) == null) { |
| 667 | log.warn("No port has been created for NetworkId: {}", networkId); |
| 668 | return; |
| 669 | } |
| 670 | |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 671 | Set<VirtualPort> virtualPortSet = new HashSet<>(); |
| 672 | networkIdVirtualPortSetMap.get(networkId).forEach(port -> { |
| 673 | if (port.element().id().equals(deviceId) && port.number().equals(portNumber)) { |
| 674 | virtualPortSet.add(port); |
| 675 | } |
| 676 | }); |
| 677 | |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 678 | if (!virtualPortSet.isEmpty()) { |
| 679 | AtomicBoolean portRemoved = new AtomicBoolean(false); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 680 | networkIdVirtualPortSetMap.compute(networkId, (id, existingVirtualPorts) -> { |
| 681 | if (existingVirtualPorts == null || existingVirtualPorts.isEmpty()) { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 682 | return new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 683 | } else { |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 684 | portRemoved.set(true); |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 685 | return new HashSet<>(Sets.difference(existingVirtualPorts, virtualPortSet)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 686 | } |
| 687 | }); |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 688 | if (portRemoved.get()) { |
| 689 | virtualPortSet.forEach(virtualPort -> notifyDelegate( |
| 690 | new VirtualNetworkEvent(VirtualNetworkEvent.Type.VIRTUAL_PORT_REMOVED, |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 691 | networkId, device, virtualPort) |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 692 | )); |
Harold Huang | b7d6b8e | 2017-04-03 17:13:33 +0800 | [diff] [blame] | 693 | |
| 694 | //Remove all the virtual links connected to this virtual port |
| 695 | Set<VirtualLink> existingVirtualLinks = networkIdVirtualLinkSetMap.get(networkId); |
| 696 | if (existingVirtualLinks != null && !existingVirtualLinks.isEmpty()) { |
| 697 | Set<VirtualLink> virtualLinkSet = new HashSet<>(); |
| 698 | ConnectPoint cp = new ConnectPoint(deviceId, portNumber); |
| 699 | existingVirtualLinks.forEach(virtualLink -> { |
| 700 | if (virtualLink.src().equals(cp) || virtualLink.dst().equals(cp)) { |
| 701 | virtualLinkSet.add(virtualLink); |
| 702 | } |
| 703 | }); |
| 704 | virtualLinkSet.forEach(virtualLink -> |
| 705 | removeLink(networkId, virtualLink.src(), virtualLink.dst())); |
| 706 | } |
| 707 | |
| 708 | //Remove all the hosts connected to this virtual port |
| 709 | Set<HostId> hostIdSet = new HashSet<>(); |
| 710 | hostIdVirtualHostMap.forEach((hostId, virtualHost) -> { |
| 711 | if (virtualHost.location().deviceId().equals(deviceId) && |
| 712 | virtualHost.location().port().equals(portNumber)) { |
| 713 | hostIdSet.add(hostId); |
| 714 | } |
| 715 | }); |
| 716 | hostIdSet.forEach(hostId -> removeHost(networkId, hostId)); |
Claudine Chiu | 7f872a7 | 2016-12-16 13:51:39 -0500 | [diff] [blame] | 717 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 718 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | @Override |
| 722 | public Set<VirtualNetwork> getNetworks(TenantId tenantId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 723 | Set<NetworkId> networkIdSet = tenantIdNetworkIdSetMap.get(tenantId); |
| 724 | Set<VirtualNetwork> virtualNetworkSet = new HashSet<>(); |
| 725 | if (networkIdSet != null) { |
Harold Huang | e86d35f | 2017-06-14 15:15:18 +0800 | [diff] [blame] | 726 | networkIdSet.forEach(networkId -> { |
| 727 | if (networkIdVirtualNetworkMap.get(networkId) != null) { |
| 728 | virtualNetworkSet.add(networkIdVirtualNetworkMap.get(networkId)); |
| 729 | } |
| 730 | }); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 731 | } |
| 732 | return ImmutableSet.copyOf(virtualNetworkSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | @Override |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 736 | public VirtualNetwork getNetwork(NetworkId networkId) { |
| 737 | return networkIdVirtualNetworkMap.get(networkId); |
| 738 | } |
| 739 | |
| 740 | @Override |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 741 | public Set<VirtualDevice> getDevices(NetworkId networkId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 742 | checkState(networkExists(networkId), "The network has not been added."); |
| 743 | Set<DeviceId> deviceIdSet = networkIdDeviceIdSetMap.get(networkId); |
| 744 | Set<VirtualDevice> virtualDeviceSet = new HashSet<>(); |
| 745 | if (deviceIdSet != null) { |
Yoonseon Han | aa25605 | 2017-05-08 16:25:38 -0700 | [diff] [blame] | 746 | deviceIdSet.forEach(deviceId -> virtualDeviceSet.add( |
| 747 | deviceIdVirtualDeviceMap.get(new VirtualDeviceId(networkId, deviceId)))); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 748 | } |
| 749 | return ImmutableSet.copyOf(virtualDeviceSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | @Override |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 753 | public Set<VirtualHost> getHosts(NetworkId networkId) { |
| 754 | checkState(networkExists(networkId), "The network has not been added."); |
| 755 | Set<HostId> hostIdSet = networkIdHostIdSetMap.get(networkId); |
| 756 | Set<VirtualHost> virtualHostSet = new HashSet<>(); |
| 757 | if (hostIdSet != null) { |
| 758 | hostIdSet.forEach(hostId -> virtualHostSet.add(hostIdVirtualHostMap.get(hostId))); |
| 759 | } |
| 760 | return ImmutableSet.copyOf(virtualHostSet); |
| 761 | } |
| 762 | |
| 763 | @Override |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 764 | public Set<VirtualLink> getLinks(NetworkId networkId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 765 | checkState(networkExists(networkId), "The network has not been added."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 766 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId); |
| 767 | if (virtualLinkSet == null) { |
| 768 | virtualLinkSet = new HashSet<>(); |
| 769 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 770 | return ImmutableSet.copyOf(virtualLinkSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 773 | @Override |
| 774 | public VirtualLink getLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst) { |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 775 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId); |
| 776 | if (virtualLinkSet == null) { |
| 777 | return null; |
| 778 | } |
| 779 | |
| 780 | VirtualLink virtualLink = null; |
| 781 | for (VirtualLink link : virtualLinkSet) { |
Harold Huang | 7362e67 | 2017-04-19 10:00:11 +0800 | [diff] [blame] | 782 | if (src == null && link.dst().equals(dst)) { |
| 783 | virtualLink = link; |
| 784 | break; |
| 785 | } else if (dst == null && link.src().equals(src)) { |
| 786 | virtualLink = link; |
| 787 | break; |
| 788 | } else if (link.src().equals(src) && link.dst().equals(dst)) { |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 789 | virtualLink = link; |
| 790 | break; |
| 791 | } |
| 792 | } |
| 793 | return virtualLink; |
| 794 | } |
| 795 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 796 | @Override |
| 797 | public Set<VirtualPort> getPorts(NetworkId networkId, DeviceId deviceId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 798 | checkState(networkExists(networkId), "The network has not been added."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 799 | Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId); |
| 800 | if (virtualPortSet == null) { |
| 801 | virtualPortSet = new HashSet<>(); |
| 802 | } |
| 803 | |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 804 | if (deviceId == null) { |
| 805 | return ImmutableSet.copyOf(virtualPortSet); |
| 806 | } |
| 807 | |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 808 | Set<VirtualPort> portSet = new HashSet<>(); |
| 809 | virtualPortSet.forEach(virtualPort -> { |
| 810 | if (virtualPort.element().id().equals(deviceId)) { |
| 811 | portSet.add(virtualPort); |
| 812 | } |
| 813 | }); |
| 814 | return ImmutableSet.copyOf(portSet); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 815 | } |
| 816 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 817 | @Override |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 818 | public void addTunnelId(Intent intent, TunnelId tunnelId) { |
| 819 | // Add the tunnelId to the intent key set map |
| 820 | Set<TunnelId> tunnelIdSet = intentKeyTunnelIdSetMap.remove(intent.key()); |
| 821 | if (tunnelIdSet == null) { |
| 822 | tunnelIdSet = new HashSet<>(); |
| 823 | } |
| 824 | tunnelIdSet.add(tunnelId); |
| 825 | intentKeyTunnelIdSetMap.put(intent.key(), tunnelIdSet); |
| 826 | } |
| 827 | |
| 828 | @Override |
| 829 | public Set<TunnelId> getTunnelIds(Intent intent) { |
| 830 | Set<TunnelId> tunnelIdSet = intentKeyTunnelIdSetMap.get(intent.key()); |
| 831 | return tunnelIdSet == null ? new HashSet<TunnelId>() : ImmutableSet.copyOf(tunnelIdSet); |
| 832 | } |
| 833 | |
| 834 | @Override |
| 835 | public void removeTunnelId(Intent intent, TunnelId tunnelId) { |
| 836 | Set<TunnelId> tunnelIdSet = new HashSet<>(); |
| 837 | intentKeyTunnelIdSetMap.get(intent.key()).forEach(tunnelId1 -> { |
| 838 | if (tunnelId1.equals(tunnelId)) { |
| 839 | tunnelIdSet.add(tunnelId); |
| 840 | } |
| 841 | }); |
| 842 | |
| 843 | if (!tunnelIdSet.isEmpty()) { |
| 844 | intentKeyTunnelIdSetMap.compute(intent.key(), (key, existingTunnelIds) -> { |
| 845 | if (existingTunnelIds == null || existingTunnelIds.isEmpty()) { |
| 846 | return new HashSet<>(); |
| 847 | } else { |
| 848 | return new HashSet<>(Sets.difference(existingTunnelIds, tunnelIdSet)); |
| 849 | } |
| 850 | }); |
| 851 | } |
| 852 | } |
| 853 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 854 | /** |
| 855 | * Listener class to map listener set events to the virtual network events. |
| 856 | */ |
| 857 | private class InternalSetListener implements SetEventListener<TenantId> { |
| 858 | @Override |
| 859 | public void event(SetEvent<TenantId> event) { |
| 860 | VirtualNetworkEvent.Type type = null; |
| 861 | switch (event.type()) { |
| 862 | case ADD: |
| 863 | type = VirtualNetworkEvent.Type.TENANT_REGISTERED; |
| 864 | break; |
| 865 | case REMOVE: |
| 866 | type = VirtualNetworkEvent.Type.TENANT_UNREGISTERED; |
| 867 | break; |
| 868 | default: |
| 869 | log.error("Unsupported event type: " + event.type()); |
| 870 | } |
| 871 | notifyDelegate(new VirtualNetworkEvent(type, null)); |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | /** |
| 876 | * Listener class to map listener map events to the virtual network events. |
| 877 | */ |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 878 | private class InternalMapListener<K, V> implements MapEventListener<K, V> { |
| 879 | |
| 880 | private final BiFunction<MapEvent.Type, V, VirtualNetworkEvent> createEvent; |
| 881 | |
| 882 | InternalMapListener(BiFunction<MapEvent.Type, V, VirtualNetworkEvent> createEvent) { |
| 883 | this.createEvent = createEvent; |
| 884 | } |
| 885 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 886 | @Override |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 887 | public void event(MapEvent<K, V> event) { |
| 888 | checkNotNull(event.key()); |
| 889 | VirtualNetworkEvent vnetEvent = null; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 890 | switch (event.type()) { |
| 891 | case INSERT: |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 892 | vnetEvent = createEvent.apply(event.type(), event.newValue().value()); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 893 | break; |
| 894 | case UPDATE: |
| 895 | if ((event.oldValue().value() != null) && (event.newValue().value() == null)) { |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 896 | vnetEvent = createEvent.apply(MapEvent.Type.REMOVE, event.oldValue().value()); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 897 | } else { |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 898 | vnetEvent = createEvent.apply(event.type(), event.newValue().value()); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 899 | } |
| 900 | break; |
| 901 | case REMOVE: |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 902 | if (event.oldValue() != null) { |
| 903 | vnetEvent = createEvent.apply(event.type(), event.oldValue().value()); |
| 904 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 905 | break; |
| 906 | default: |
| 907 | log.error("Unsupported event type: " + event.type()); |
| 908 | } |
Claudine Chiu | 945828d | 2016-11-21 12:47:07 -0500 | [diff] [blame] | 909 | if (vnetEvent != null) { |
| 910 | notifyDelegate(vnetEvent); |
| 911 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 912 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 913 | } |
Yoonseon Han | aa25605 | 2017-05-08 16:25:38 -0700 | [diff] [blame] | 914 | |
| 915 | /** |
| 916 | * A wrapper class to isolate device id from other virtual networks. |
| 917 | */ |
| 918 | |
| 919 | private static class VirtualDeviceId { |
| 920 | |
| 921 | NetworkId networkId; |
| 922 | DeviceId deviceId; |
| 923 | |
| 924 | public VirtualDeviceId(NetworkId networkId, DeviceId deviceId) { |
| 925 | this.networkId = networkId; |
| 926 | this.deviceId = deviceId; |
| 927 | } |
| 928 | |
| 929 | public NetworkId getNetworkId() { |
| 930 | return networkId; |
| 931 | } |
| 932 | |
| 933 | public DeviceId getDeviceId() { |
| 934 | return deviceId; |
| 935 | } |
| 936 | |
| 937 | @Override |
| 938 | public int hashCode() { |
| 939 | return Objects.hash(networkId, deviceId); |
| 940 | } |
| 941 | |
| 942 | @Override |
| 943 | public boolean equals(Object obj) { |
| 944 | if (this == obj) { |
| 945 | return true; |
| 946 | } |
| 947 | |
| 948 | if (obj instanceof VirtualDeviceId) { |
| 949 | VirtualDeviceId that = (VirtualDeviceId) obj; |
| 950 | return this.deviceId.equals(that.deviceId) && |
| 951 | this.networkId.equals(that.networkId); |
| 952 | } |
| 953 | return false; |
| 954 | } |
| 955 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 956 | } |