Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.incubator.store.virtual.impl; |
| 17 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Sets; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Service; |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame^] | 26 | import org.onlab.packet.IpAddress; |
| 27 | import org.onlab.packet.MacAddress; |
| 28 | import org.onlab.packet.VlanId; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 29 | import org.onlab.util.KryoNamespace; |
| 30 | import org.onosproject.core.CoreService; |
| 31 | import org.onosproject.core.IdGenerator; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 32 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 33 | import org.onosproject.incubator.net.virtual.DefaultVirtualDevice; |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame^] | 34 | import org.onosproject.incubator.net.virtual.DefaultVirtualHost; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 35 | import org.onosproject.incubator.net.virtual.DefaultVirtualLink; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 36 | import org.onosproject.incubator.net.virtual.DefaultVirtualNetwork; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 37 | import org.onosproject.incubator.net.virtual.DefaultVirtualPort; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 38 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 39 | import org.onosproject.incubator.net.virtual.TenantId; |
| 40 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame^] | 41 | import org.onosproject.incubator.net.virtual.VirtualHost; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 42 | import org.onosproject.incubator.net.virtual.VirtualLink; |
| 43 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 44 | import org.onosproject.incubator.net.virtual.VirtualNetworkEvent; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 45 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 46 | import org.onosproject.incubator.net.virtual.VirtualNetworkStore; |
| 47 | import org.onosproject.incubator.net.virtual.VirtualNetworkStoreDelegate; |
| 48 | import org.onosproject.incubator.net.virtual.VirtualPort; |
| 49 | import org.onosproject.net.ConnectPoint; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 50 | import org.onosproject.net.Device; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 51 | import org.onosproject.net.DeviceId; |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame^] | 52 | import org.onosproject.net.HostId; |
| 53 | import org.onosproject.net.HostLocation; |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 54 | import org.onosproject.net.Link; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 55 | import org.onosproject.net.Port; |
| 56 | import org.onosproject.net.PortNumber; |
| 57 | import org.onosproject.store.AbstractStore; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 58 | import org.onosproject.store.serializers.KryoNamespaces; |
| 59 | import org.onosproject.store.service.ConsistentMap; |
| 60 | import org.onosproject.store.service.DistributedSet; |
| 61 | import org.onosproject.store.service.MapEvent; |
| 62 | import org.onosproject.store.service.MapEventListener; |
| 63 | import org.onosproject.store.service.Serializer; |
| 64 | import org.onosproject.store.service.SetEvent; |
| 65 | import org.onosproject.store.service.SetEventListener; |
| 66 | import org.onosproject.store.service.StorageService; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 67 | import org.slf4j.Logger; |
| 68 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 69 | import java.util.HashSet; |
| 70 | import java.util.Map; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 71 | import java.util.Set; |
| 72 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 73 | import static com.google.common.base.Preconditions.checkNotNull; |
| 74 | import static com.google.common.base.Preconditions.checkState; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 75 | import static org.slf4j.LoggerFactory.getLogger; |
| 76 | |
| 77 | /** |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 78 | * Implementation of the virtual network store. |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 79 | */ |
| 80 | @Component(immediate = true) |
| 81 | @Service |
| 82 | public class DistributedVirtualNetworkStore |
| 83 | extends AbstractStore<VirtualNetworkEvent, VirtualNetworkStoreDelegate> |
| 84 | implements VirtualNetworkStore { |
| 85 | |
| 86 | private final Logger log = getLogger(getClass()); |
| 87 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 89 | protected StorageService storageService; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 90 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 92 | protected CoreService coreService; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 93 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 94 | private IdGenerator idGenerator; |
| 95 | |
| 96 | // Track tenants by ID |
| 97 | private DistributedSet<TenantId> tenantIdSet; |
| 98 | |
| 99 | // Listener for tenant events |
| 100 | private final SetEventListener<TenantId> setListener = new InternalSetListener(); |
| 101 | |
| 102 | // Track virtual networks by network Id |
| 103 | private ConsistentMap<NetworkId, VirtualNetwork> networkIdVirtualNetworkConsistentMap; |
| 104 | private Map<NetworkId, VirtualNetwork> networkIdVirtualNetworkMap; |
| 105 | |
| 106 | // Listener for virtual network events |
| 107 | private final MapEventListener<NetworkId, VirtualNetwork> virtualMapListener = new InternalMapListener(); |
| 108 | |
| 109 | // Track virtual network IDs by tenant Id |
| 110 | private ConsistentMap<TenantId, Set<NetworkId>> tenantIdNetworkIdSetConsistentMap; |
| 111 | private Map<TenantId, Set<NetworkId>> tenantIdNetworkIdSetMap; |
| 112 | |
| 113 | // Track virtual devices by device Id |
| 114 | private ConsistentMap<DeviceId, VirtualDevice> deviceIdVirtualDeviceConsistentMap; |
| 115 | private Map<DeviceId, VirtualDevice> deviceIdVirtualDeviceMap; |
| 116 | |
| 117 | // Track device IDs by network Id |
| 118 | private ConsistentMap<NetworkId, Set<DeviceId>> networkIdDeviceIdSetConsistentMap; |
| 119 | private Map<NetworkId, Set<DeviceId>> networkIdDeviceIdSetMap; |
| 120 | |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame^] | 121 | // Track virtual hosts by host Id |
| 122 | private ConsistentMap<HostId, VirtualHost> hostIdVirtualHostConsistentMap; |
| 123 | private Map<HostId, VirtualHost> hostIdVirtualHostMap; |
| 124 | |
| 125 | // Track host IDs by network Id |
| 126 | private ConsistentMap<NetworkId, Set<HostId>> networkIdHostIdSetConsistentMap; |
| 127 | private Map<NetworkId, Set<HostId>> networkIdHostIdSetMap; |
| 128 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 129 | // Track virtual links by network Id |
| 130 | private ConsistentMap<NetworkId, Set<VirtualLink>> networkIdVirtualLinkSetConsistentMap; |
| 131 | private Map<NetworkId, Set<VirtualLink>> networkIdVirtualLinkSetMap; |
| 132 | |
| 133 | // Track virtual ports by network Id |
| 134 | private ConsistentMap<NetworkId, Set<VirtualPort>> networkIdVirtualPortSetConsistentMap; |
| 135 | private Map<NetworkId, Set<VirtualPort>> networkIdVirtualPortSetMap; |
| 136 | |
| 137 | private static final Serializer SERIALIZER = Serializer |
| 138 | .using(new KryoNamespace.Builder().register(KryoNamespaces.API) |
| 139 | .register(TenantId.class) |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 140 | .register(NetworkId.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 141 | .register(VirtualNetwork.class) |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 142 | .register(DefaultVirtualNetwork.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 143 | .register(VirtualDevice.class) |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 144 | .register(DefaultVirtualDevice.class) |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame^] | 145 | .register(VirtualHost.class) |
| 146 | .register(DefaultVirtualHost.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 147 | .register(VirtualLink.class) |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 148 | .register(DefaultVirtualLink.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 149 | .register(VirtualPort.class) |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 150 | .register(DefaultVirtualPort.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 151 | .register(Device.class) |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 152 | .register(TunnelId.class) |
HIGUCHI Yuta | 03666a3 | 2016-05-18 11:49:09 -0700 | [diff] [blame] | 153 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 154 | .build("VirtualNetworkStore")); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 155 | |
| 156 | /** |
| 157 | * Distributed network store service activate method. |
| 158 | */ |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 159 | @Activate |
| 160 | public void activate() { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 161 | idGenerator = coreService.getIdGenerator(VirtualNetworkService.VIRTUAL_NETWORK_TOPIC); |
| 162 | |
| 163 | tenantIdSet = storageService.<TenantId>setBuilder() |
| 164 | .withSerializer(SERIALIZER) |
| 165 | .withName("onos-tenantId") |
| 166 | .withRelaxedReadConsistency() |
| 167 | .build() |
| 168 | .asDistributedSet(); |
| 169 | tenantIdSet.addListener(setListener); |
| 170 | |
| 171 | networkIdVirtualNetworkConsistentMap = storageService.<NetworkId, VirtualNetwork>consistentMapBuilder() |
| 172 | .withSerializer(SERIALIZER) |
| 173 | .withName("onos-networkId-virtualnetwork") |
| 174 | .withRelaxedReadConsistency() |
| 175 | .build(); |
| 176 | networkIdVirtualNetworkConsistentMap.addListener(virtualMapListener); |
| 177 | networkIdVirtualNetworkMap = networkIdVirtualNetworkConsistentMap.asJavaMap(); |
| 178 | |
| 179 | tenantIdNetworkIdSetConsistentMap = storageService.<TenantId, Set<NetworkId>>consistentMapBuilder() |
| 180 | .withSerializer(SERIALIZER) |
| 181 | .withName("onos-tenantId-networkIds") |
| 182 | .withRelaxedReadConsistency() |
| 183 | .build(); |
| 184 | tenantIdNetworkIdSetMap = tenantIdNetworkIdSetConsistentMap.asJavaMap(); |
| 185 | |
| 186 | deviceIdVirtualDeviceConsistentMap = storageService.<DeviceId, VirtualDevice>consistentMapBuilder() |
| 187 | .withSerializer(SERIALIZER) |
| 188 | .withName("onos-deviceId-virtualdevice") |
| 189 | .withRelaxedReadConsistency() |
| 190 | .build(); |
| 191 | deviceIdVirtualDeviceMap = deviceIdVirtualDeviceConsistentMap.asJavaMap(); |
| 192 | |
| 193 | networkIdDeviceIdSetConsistentMap = storageService.<NetworkId, Set<DeviceId>>consistentMapBuilder() |
| 194 | .withSerializer(SERIALIZER) |
| 195 | .withName("onos-networkId-deviceIds") |
| 196 | .withRelaxedReadConsistency() |
| 197 | .build(); |
| 198 | networkIdDeviceIdSetMap = networkIdDeviceIdSetConsistentMap.asJavaMap(); |
| 199 | |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame^] | 200 | hostIdVirtualHostConsistentMap = storageService.<HostId, VirtualHost>consistentMapBuilder() |
| 201 | .withSerializer(SERIALIZER) |
| 202 | .withName("onos-hostId-virtualhost") |
| 203 | .withRelaxedReadConsistency() |
| 204 | .build(); |
| 205 | hostIdVirtualHostMap = hostIdVirtualHostConsistentMap.asJavaMap(); |
| 206 | |
| 207 | networkIdHostIdSetConsistentMap = storageService.<NetworkId, Set<HostId>>consistentMapBuilder() |
| 208 | .withSerializer(SERIALIZER) |
| 209 | .withName("onos-networkId-hostIds") |
| 210 | .withRelaxedReadConsistency() |
| 211 | .build(); |
| 212 | networkIdHostIdSetMap = networkIdHostIdSetConsistentMap.asJavaMap(); |
| 213 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 214 | networkIdVirtualLinkSetConsistentMap = storageService.<NetworkId, Set<VirtualLink>>consistentMapBuilder() |
| 215 | .withSerializer(SERIALIZER) |
| 216 | .withName("onos-networkId-virtuallinks") |
| 217 | .withRelaxedReadConsistency() |
| 218 | .build(); |
| 219 | networkIdVirtualLinkSetMap = networkIdVirtualLinkSetConsistentMap.asJavaMap(); |
| 220 | |
| 221 | networkIdVirtualPortSetConsistentMap = storageService.<NetworkId, Set<VirtualPort>>consistentMapBuilder() |
| 222 | .withSerializer(SERIALIZER) |
| 223 | .withName("onos-networkId-virtualportss") |
| 224 | .withRelaxedReadConsistency() |
| 225 | .build(); |
| 226 | networkIdVirtualPortSetMap = networkIdVirtualPortSetConsistentMap.asJavaMap(); |
| 227 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 228 | log.info("Started"); |
| 229 | } |
| 230 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 231 | /** |
| 232 | * Distributed network store service deactivate method. |
| 233 | */ |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 234 | @Deactivate |
| 235 | public void deactivate() { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 236 | tenantIdSet.removeListener(setListener); |
| 237 | networkIdVirtualNetworkConsistentMap.removeListener(virtualMapListener); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 238 | log.info("Stopped"); |
| 239 | } |
| 240 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 241 | /** |
| 242 | * This method is used for Junit tests to set the CoreService instance, which |
| 243 | * is required to set the IdGenerator instance. |
| 244 | * |
| 245 | * @param coreService core service instance |
| 246 | */ |
| 247 | public void setCoreService(CoreService coreService) { |
| 248 | this.coreService = coreService; |
| 249 | } |
| 250 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 251 | @Override |
| 252 | public void addTenantId(TenantId tenantId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 253 | tenantIdSet.add(tenantId); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public void removeTenantId(TenantId tenantId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 258 | tenantIdSet.remove(tenantId); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public Set<TenantId> getTenantIds() { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 263 | return ImmutableSet.copyOf(tenantIdSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public VirtualNetwork addNetwork(TenantId tenantId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 268 | |
| 269 | checkState(tenantIdSet.contains(tenantId), "The tenant has not been registered. " + tenantId.id()); |
| 270 | VirtualNetwork virtualNetwork = new DefaultVirtualNetwork(genNetworkId(), tenantId); |
| 271 | //TODO update both maps in one transaction. |
| 272 | networkIdVirtualNetworkMap.put(virtualNetwork.id(), virtualNetwork); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 273 | |
| 274 | Set<NetworkId> networkIdSet = tenantIdNetworkIdSetMap.get(tenantId); |
| 275 | if (networkIdSet == null) { |
| 276 | networkIdSet = new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 277 | } |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 278 | networkIdSet.add(virtualNetwork.id()); |
| 279 | tenantIdNetworkIdSetMap.put(tenantId, networkIdSet); |
| 280 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 281 | return virtualNetwork; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 284 | /** |
| 285 | * Returns a new network identifier from a virtual network block of identifiers. |
| 286 | * |
| 287 | * @return NetworkId network identifier |
| 288 | */ |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 289 | private NetworkId genNetworkId() { |
Kenji HIKICHI | cfdf91b | 2016-05-25 13:04:45 +0900 | [diff] [blame] | 290 | NetworkId networkId; |
| 291 | do { |
| 292 | networkId = NetworkId.networkId(idGenerator.getNewId()); |
| 293 | } while (!networkId.isVirtualNetworkId()); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 294 | |
Kenji HIKICHI | cfdf91b | 2016-05-25 13:04:45 +0900 | [diff] [blame] | 295 | return networkId; |
| 296 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 297 | |
| 298 | @Override |
| 299 | public void removeNetwork(NetworkId networkId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 300 | // Make sure that the virtual network exists before attempting to remove it. |
| 301 | if (networkExists(networkId)) { |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 302 | //TODO update both maps in one transaction. |
| 303 | |
| 304 | VirtualNetwork virtualNetwork = networkIdVirtualNetworkMap.remove(networkId); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 305 | if (virtualNetwork == null) { |
| 306 | return; |
| 307 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 308 | TenantId tenantId = virtualNetwork.tenantId(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 309 | |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 310 | Set<NetworkId> networkIdSet = new HashSet<>(); |
| 311 | tenantIdNetworkIdSetMap.get(tenantId).forEach(networkId1 -> { |
| 312 | if (networkId1.id().equals(networkId.id())) { |
| 313 | networkIdSet.add(networkId1); |
| 314 | } |
| 315 | }); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 316 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 317 | tenantIdNetworkIdSetMap.compute(virtualNetwork.tenantId(), (id, existingNetworkIds) -> { |
| 318 | if (existingNetworkIds == null || existingNetworkIds.isEmpty()) { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 319 | return new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 320 | } else { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 321 | return new HashSet<>(Sets.difference(existingNetworkIds, networkIdSet)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 322 | } |
| 323 | }); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Returns if the network identifier exists. |
| 329 | * |
| 330 | * @param networkId network identifier |
| 331 | * @return true if the network identifier exists, false otherwise. |
| 332 | */ |
| 333 | private boolean networkExists(NetworkId networkId) { |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 334 | checkNotNull(networkId, "The network identifier cannot be null."); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 335 | return (networkIdVirtualNetworkMap.containsKey(networkId)); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public VirtualDevice addDevice(NetworkId networkId, DeviceId deviceId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 340 | checkState(networkExists(networkId), "The network has not been added."); |
| 341 | Set<DeviceId> deviceIdSet = networkIdDeviceIdSetMap.get(networkId); |
| 342 | if (deviceIdSet == null) { |
| 343 | deviceIdSet = new HashSet<>(); |
| 344 | } |
| 345 | VirtualDevice virtualDevice = new DefaultVirtualDevice(networkId, deviceId); |
| 346 | //TODO update both maps in one transaction. |
| 347 | deviceIdVirtualDeviceMap.put(deviceId, virtualDevice); |
| 348 | deviceIdSet.add(deviceId); |
| 349 | networkIdDeviceIdSetMap.put(networkId, deviceIdSet); |
| 350 | return virtualDevice; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | @Override |
| 354 | public void removeDevice(NetworkId networkId, DeviceId deviceId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 355 | checkState(networkExists(networkId), "The network has not been added."); |
| 356 | //TODO update both maps in one transaction. |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 357 | |
| 358 | Set<DeviceId> deviceIdSet = new HashSet<>(); |
| 359 | networkIdDeviceIdSetMap.get(networkId).forEach(deviceId1 -> { |
| 360 | if (deviceId1.equals(deviceId)) { |
| 361 | deviceIdSet.add(deviceId1); |
| 362 | } |
| 363 | }); |
| 364 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 365 | if (deviceIdSet != null) { |
| 366 | networkIdDeviceIdSetMap.compute(networkId, (id, existingDeviceIds) -> { |
| 367 | if (existingDeviceIds == null || existingDeviceIds.isEmpty()) { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 368 | return new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 369 | } else { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 370 | return new HashSet<>(Sets.difference(existingDeviceIds, deviceIdSet)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 371 | } |
| 372 | }); |
| 373 | |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 374 | deviceIdVirtualDeviceMap.remove(deviceId); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 375 | } |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame^] | 376 | //TODO remove virtual links and ports when removing the virtual device |
| 377 | } |
| 378 | |
| 379 | @Override |
| 380 | public VirtualHost addHost(NetworkId networkId, HostId hostId, MacAddress mac, |
| 381 | VlanId vlan, HostLocation location, Set<IpAddress> ips) { |
| 382 | checkState(networkExists(networkId), "The network has not been added."); |
| 383 | Set<HostId> hostIdSet = networkIdHostIdSetMap.get(networkId); |
| 384 | if (hostIdSet == null) { |
| 385 | hostIdSet = new HashSet<>(); |
| 386 | } |
| 387 | VirtualHost virtualhost = new DefaultVirtualHost(networkId, hostId, mac, vlan, location, ips); |
| 388 | //TODO update both maps in one transaction. |
| 389 | hostIdVirtualHostMap.put(hostId, virtualhost); |
| 390 | hostIdSet.add(hostId); |
| 391 | networkIdHostIdSetMap.put(networkId, hostIdSet); |
| 392 | return virtualhost; |
| 393 | } |
| 394 | |
| 395 | @Override |
| 396 | public void removeHost(NetworkId networkId, HostId hostId) { |
| 397 | checkState(networkExists(networkId), "The network has not been added."); |
| 398 | //TODO update both maps in one transaction. |
| 399 | |
| 400 | Set<HostId> hostIdSet = new HashSet<>(); |
| 401 | networkIdHostIdSetMap.get(networkId).forEach(hostId1 -> { |
| 402 | if (hostId1.equals(hostId)) { |
| 403 | hostIdSet.add(hostId1); |
| 404 | } |
| 405 | }); |
| 406 | |
| 407 | if (hostIdSet != null) { |
| 408 | networkIdHostIdSetMap.compute(networkId, (id, existingHostIds) -> { |
| 409 | if (existingHostIds == null || existingHostIds.isEmpty()) { |
| 410 | return new HashSet<>(); |
| 411 | } else { |
| 412 | return new HashSet<>(Sets.difference(existingHostIds, hostIdSet)); |
| 413 | } |
| 414 | }); |
| 415 | |
| 416 | hostIdVirtualHostMap.remove(hostId); |
| 417 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | @Override |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 421 | public VirtualLink addLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst, |
| 422 | Link.State state, TunnelId realizedBy) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 423 | checkState(networkExists(networkId), "The network has not been added."); |
| 424 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId); |
| 425 | if (virtualLinkSet == null) { |
| 426 | virtualLinkSet = new HashSet<>(); |
| 427 | } |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 428 | // validate that the link does not already exist in this network |
| 429 | checkState(getLink(networkId, src, dst) == null, "The virtual link already exists"); |
| 430 | |
| 431 | VirtualLink virtualLink = DefaultVirtualLink.builder() |
| 432 | .networkId(networkId) |
| 433 | .src(src) |
| 434 | .dst(dst) |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 435 | .state(state) |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 436 | .tunnelId(realizedBy) |
| 437 | .build(); |
| 438 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 439 | virtualLinkSet.add(virtualLink); |
| 440 | networkIdVirtualLinkSetMap.put(networkId, virtualLinkSet); |
| 441 | return virtualLink; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | @Override |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 445 | public void updateLink(VirtualLink virtualLink, TunnelId tunnelId, Link.State state) { |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 446 | checkState(networkExists(virtualLink.networkId()), "The network has not been added."); |
| 447 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(virtualLink.networkId()); |
| 448 | if (virtualLinkSet == null) { |
| 449 | virtualLinkSet = new HashSet<>(); |
| 450 | } |
| 451 | virtualLinkSet.remove(virtualLink); |
| 452 | |
| 453 | VirtualLink newVirtualLink = DefaultVirtualLink.builder() |
| 454 | .networkId(virtualLink.networkId()) |
| 455 | .src(virtualLink.src()) |
| 456 | .dst(virtualLink.dst()) |
| 457 | .tunnelId(tunnelId) |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 458 | .state(state) |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 459 | .build(); |
| 460 | |
| 461 | virtualLinkSet.add(newVirtualLink); |
| 462 | networkIdVirtualLinkSetMap.put(newVirtualLink.networkId(), virtualLinkSet); |
| 463 | } |
| 464 | |
| 465 | @Override |
| 466 | public VirtualLink removeLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 467 | checkState(networkExists(networkId), "The network has not been added."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 468 | |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 469 | final VirtualLink virtualLink = getLink(networkId, src, dst); |
| 470 | if (virtualLink == null) { |
| 471 | return null; |
| 472 | } |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 473 | Set<VirtualLink> virtualLinkSet = new HashSet<>(); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 474 | virtualLinkSet.add(virtualLink); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 475 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 476 | if (virtualLinkSet != null) { |
| 477 | networkIdVirtualLinkSetMap.compute(networkId, (id, existingVirtualLinks) -> { |
| 478 | if (existingVirtualLinks == null || existingVirtualLinks.isEmpty()) { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 479 | return new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 480 | } else { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 481 | return new HashSet<>(Sets.difference(existingVirtualLinks, virtualLinkSet)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 482 | } |
| 483 | }); |
| 484 | } |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 485 | return virtualLink; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | @Override |
| 489 | public VirtualPort addPort(NetworkId networkId, DeviceId deviceId, PortNumber portNumber, Port realizedBy) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 490 | checkState(networkExists(networkId), "The network has not been added."); |
| 491 | Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId); |
| 492 | if (virtualPortSet == null) { |
| 493 | virtualPortSet = new HashSet<>(); |
| 494 | } |
| 495 | Device device = deviceIdVirtualDeviceMap.get(deviceId); |
| 496 | checkNotNull(device, "The device has not been created for deviceId: " + deviceId); |
| 497 | VirtualPort virtualPort = new DefaultVirtualPort(networkId, device, portNumber, realizedBy); |
| 498 | virtualPortSet.add(virtualPort); |
| 499 | networkIdVirtualPortSetMap.put(networkId, virtualPortSet); |
| 500 | return virtualPort; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | @Override |
| 504 | public void removePort(NetworkId networkId, DeviceId deviceId, PortNumber portNumber) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 505 | checkState(networkExists(networkId), "The network has not been added."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 506 | |
| 507 | Set<VirtualPort> virtualPortSet = new HashSet<>(); |
| 508 | networkIdVirtualPortSetMap.get(networkId).forEach(port -> { |
| 509 | if (port.element().id().equals(deviceId) && port.number().equals(portNumber)) { |
| 510 | virtualPortSet.add(port); |
| 511 | } |
| 512 | }); |
| 513 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 514 | if (virtualPortSet != null) { |
| 515 | networkIdVirtualPortSetMap.compute(networkId, (id, existingVirtualPorts) -> { |
| 516 | if (existingVirtualPorts == null || existingVirtualPorts.isEmpty()) { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 517 | return new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 518 | } else { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 519 | return new HashSet<>(Sets.difference(existingVirtualPorts, virtualPortSet)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 520 | } |
| 521 | }); |
| 522 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | @Override |
| 526 | public Set<VirtualNetwork> getNetworks(TenantId tenantId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 527 | Set<NetworkId> networkIdSet = tenantIdNetworkIdSetMap.get(tenantId); |
| 528 | Set<VirtualNetwork> virtualNetworkSet = new HashSet<>(); |
| 529 | if (networkIdSet != null) { |
| 530 | networkIdSet.forEach(networkId -> virtualNetworkSet.add(networkIdVirtualNetworkMap.get(networkId))); |
| 531 | } |
| 532 | return ImmutableSet.copyOf(virtualNetworkSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | @Override |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 536 | public VirtualNetwork getNetwork(NetworkId networkId) { |
| 537 | return networkIdVirtualNetworkMap.get(networkId); |
| 538 | } |
| 539 | |
| 540 | @Override |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 541 | public Set<VirtualDevice> getDevices(NetworkId networkId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 542 | checkState(networkExists(networkId), "The network has not been added."); |
| 543 | Set<DeviceId> deviceIdSet = networkIdDeviceIdSetMap.get(networkId); |
| 544 | Set<VirtualDevice> virtualDeviceSet = new HashSet<>(); |
| 545 | if (deviceIdSet != null) { |
| 546 | deviceIdSet.forEach(deviceId -> virtualDeviceSet.add(deviceIdVirtualDeviceMap.get(deviceId))); |
| 547 | } |
| 548 | return ImmutableSet.copyOf(virtualDeviceSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | @Override |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame^] | 552 | public Set<VirtualHost> getHosts(NetworkId networkId) { |
| 553 | checkState(networkExists(networkId), "The network has not been added."); |
| 554 | Set<HostId> hostIdSet = networkIdHostIdSetMap.get(networkId); |
| 555 | Set<VirtualHost> virtualHostSet = new HashSet<>(); |
| 556 | if (hostIdSet != null) { |
| 557 | hostIdSet.forEach(hostId -> virtualHostSet.add(hostIdVirtualHostMap.get(hostId))); |
| 558 | } |
| 559 | return ImmutableSet.copyOf(virtualHostSet); |
| 560 | } |
| 561 | |
| 562 | @Override |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 563 | public Set<VirtualLink> getLinks(NetworkId networkId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 564 | checkState(networkExists(networkId), "The network has not been added."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 565 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId); |
| 566 | if (virtualLinkSet == null) { |
| 567 | virtualLinkSet = new HashSet<>(); |
| 568 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 569 | return ImmutableSet.copyOf(virtualLinkSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 572 | @Override |
| 573 | public VirtualLink getLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst) { |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 574 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId); |
| 575 | if (virtualLinkSet == null) { |
| 576 | return null; |
| 577 | } |
| 578 | |
| 579 | VirtualLink virtualLink = null; |
| 580 | for (VirtualLink link : virtualLinkSet) { |
| 581 | if (link.src().equals(src) && link.dst().equals(dst)) { |
| 582 | virtualLink = link; |
| 583 | break; |
| 584 | } |
| 585 | } |
| 586 | return virtualLink; |
| 587 | } |
| 588 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 589 | @Override |
| 590 | public Set<VirtualPort> getPorts(NetworkId networkId, DeviceId deviceId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 591 | checkState(networkExists(networkId), "The network has not been added."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 592 | Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId); |
| 593 | if (virtualPortSet == null) { |
| 594 | virtualPortSet = new HashSet<>(); |
| 595 | } |
| 596 | |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 597 | if (deviceId == null) { |
| 598 | return ImmutableSet.copyOf(virtualPortSet); |
| 599 | } |
| 600 | |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 601 | Set<VirtualPort> portSet = new HashSet<>(); |
| 602 | virtualPortSet.forEach(virtualPort -> { |
| 603 | if (virtualPort.element().id().equals(deviceId)) { |
| 604 | portSet.add(virtualPort); |
| 605 | } |
| 606 | }); |
| 607 | return ImmutableSet.copyOf(portSet); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /** |
| 611 | * Listener class to map listener set events to the virtual network events. |
| 612 | */ |
| 613 | private class InternalSetListener implements SetEventListener<TenantId> { |
| 614 | @Override |
| 615 | public void event(SetEvent<TenantId> event) { |
| 616 | VirtualNetworkEvent.Type type = null; |
| 617 | switch (event.type()) { |
| 618 | case ADD: |
| 619 | type = VirtualNetworkEvent.Type.TENANT_REGISTERED; |
| 620 | break; |
| 621 | case REMOVE: |
| 622 | type = VirtualNetworkEvent.Type.TENANT_UNREGISTERED; |
| 623 | break; |
| 624 | default: |
| 625 | log.error("Unsupported event type: " + event.type()); |
| 626 | } |
| 627 | notifyDelegate(new VirtualNetworkEvent(type, null)); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * Listener class to map listener map events to the virtual network events. |
| 633 | */ |
| 634 | private class InternalMapListener implements MapEventListener<NetworkId, VirtualNetwork> { |
| 635 | @Override |
| 636 | public void event(MapEvent<NetworkId, VirtualNetwork> event) { |
| 637 | NetworkId networkId = checkNotNull(event.key()); |
| 638 | VirtualNetworkEvent.Type type = null; |
| 639 | switch (event.type()) { |
| 640 | case INSERT: |
| 641 | type = VirtualNetworkEvent.Type.NETWORK_ADDED; |
| 642 | break; |
| 643 | case UPDATE: |
| 644 | if ((event.oldValue().value() != null) && (event.newValue().value() == null)) { |
| 645 | type = VirtualNetworkEvent.Type.NETWORK_REMOVED; |
| 646 | } else { |
| 647 | type = VirtualNetworkEvent.Type.NETWORK_UPDATED; |
| 648 | } |
| 649 | break; |
| 650 | case REMOVE: |
| 651 | type = VirtualNetworkEvent.Type.NETWORK_REMOVED; |
| 652 | break; |
| 653 | default: |
| 654 | log.error("Unsupported event type: " + event.type()); |
| 655 | } |
| 656 | notifyDelegate(new VirtualNetworkEvent(type, networkId)); |
| 657 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 658 | } |
| 659 | } |