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 | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 26 | import org.onlab.util.KryoNamespace; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.core.IdGenerator; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 29 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 30 | import org.onosproject.incubator.net.virtual.DefaultVirtualDevice; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 31 | import org.onosproject.incubator.net.virtual.DefaultVirtualLink; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 32 | import org.onosproject.incubator.net.virtual.DefaultVirtualNetwork; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 33 | import org.onosproject.incubator.net.virtual.DefaultVirtualPort; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 34 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 35 | import org.onosproject.incubator.net.virtual.TenantId; |
| 36 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
| 37 | import org.onosproject.incubator.net.virtual.VirtualLink; |
| 38 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 39 | import org.onosproject.incubator.net.virtual.VirtualNetworkEvent; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 40 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 41 | import org.onosproject.incubator.net.virtual.VirtualNetworkStore; |
| 42 | import org.onosproject.incubator.net.virtual.VirtualNetworkStoreDelegate; |
| 43 | import org.onosproject.incubator.net.virtual.VirtualPort; |
| 44 | import org.onosproject.net.ConnectPoint; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 45 | import org.onosproject.net.Device; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 46 | import org.onosproject.net.DeviceId; |
| 47 | import org.onosproject.net.Port; |
| 48 | import org.onosproject.net.PortNumber; |
| 49 | import org.onosproject.store.AbstractStore; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 50 | import org.onosproject.store.serializers.KryoNamespaces; |
| 51 | import org.onosproject.store.service.ConsistentMap; |
| 52 | import org.onosproject.store.service.DistributedSet; |
| 53 | import org.onosproject.store.service.MapEvent; |
| 54 | import org.onosproject.store.service.MapEventListener; |
| 55 | import org.onosproject.store.service.Serializer; |
| 56 | import org.onosproject.store.service.SetEvent; |
| 57 | import org.onosproject.store.service.SetEventListener; |
| 58 | import org.onosproject.store.service.StorageService; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 59 | import org.slf4j.Logger; |
| 60 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 61 | import java.util.HashSet; |
| 62 | import java.util.Map; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 63 | import java.util.Set; |
| 64 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 65 | import static com.google.common.base.Preconditions.checkNotNull; |
| 66 | import static com.google.common.base.Preconditions.checkState; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 67 | import static org.slf4j.LoggerFactory.getLogger; |
| 68 | |
| 69 | /** |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 70 | * Implementation of the virtual network store. |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 71 | */ |
| 72 | @Component(immediate = true) |
| 73 | @Service |
| 74 | public class DistributedVirtualNetworkStore |
| 75 | extends AbstractStore<VirtualNetworkEvent, VirtualNetworkStoreDelegate> |
| 76 | implements VirtualNetworkStore { |
| 77 | |
| 78 | private final Logger log = getLogger(getClass()); |
| 79 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected StorageService storageService; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 82 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 84 | protected CoreService coreService; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 85 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 86 | private IdGenerator idGenerator; |
| 87 | |
| 88 | // Track tenants by ID |
| 89 | private DistributedSet<TenantId> tenantIdSet; |
| 90 | |
| 91 | // Listener for tenant events |
| 92 | private final SetEventListener<TenantId> setListener = new InternalSetListener(); |
| 93 | |
| 94 | // Track virtual networks by network Id |
| 95 | private ConsistentMap<NetworkId, VirtualNetwork> networkIdVirtualNetworkConsistentMap; |
| 96 | private Map<NetworkId, VirtualNetwork> networkIdVirtualNetworkMap; |
| 97 | |
| 98 | // Listener for virtual network events |
| 99 | private final MapEventListener<NetworkId, VirtualNetwork> virtualMapListener = new InternalMapListener(); |
| 100 | |
| 101 | // Track virtual network IDs by tenant Id |
| 102 | private ConsistentMap<TenantId, Set<NetworkId>> tenantIdNetworkIdSetConsistentMap; |
| 103 | private Map<TenantId, Set<NetworkId>> tenantIdNetworkIdSetMap; |
| 104 | |
| 105 | // Track virtual devices by device Id |
| 106 | private ConsistentMap<DeviceId, VirtualDevice> deviceIdVirtualDeviceConsistentMap; |
| 107 | private Map<DeviceId, VirtualDevice> deviceIdVirtualDeviceMap; |
| 108 | |
| 109 | // Track device IDs by network Id |
| 110 | private ConsistentMap<NetworkId, Set<DeviceId>> networkIdDeviceIdSetConsistentMap; |
| 111 | private Map<NetworkId, Set<DeviceId>> networkIdDeviceIdSetMap; |
| 112 | |
| 113 | // Track virtual links by network Id |
| 114 | private ConsistentMap<NetworkId, Set<VirtualLink>> networkIdVirtualLinkSetConsistentMap; |
| 115 | private Map<NetworkId, Set<VirtualLink>> networkIdVirtualLinkSetMap; |
| 116 | |
| 117 | // Track virtual ports by network Id |
| 118 | private ConsistentMap<NetworkId, Set<VirtualPort>> networkIdVirtualPortSetConsistentMap; |
| 119 | private Map<NetworkId, Set<VirtualPort>> networkIdVirtualPortSetMap; |
| 120 | |
| 121 | private static final Serializer SERIALIZER = Serializer |
| 122 | .using(new KryoNamespace.Builder().register(KryoNamespaces.API) |
| 123 | .register(TenantId.class) |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 124 | .register(NetworkId.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 125 | .register(VirtualNetwork.class) |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 126 | .register(DefaultVirtualNetwork.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 127 | .register(VirtualDevice.class) |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 128 | .register(DefaultVirtualDevice.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 129 | .register(VirtualLink.class) |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 130 | .register(DefaultVirtualLink.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 131 | .register(VirtualPort.class) |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 132 | .register(DefaultVirtualPort.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 133 | .register(Device.class) |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 134 | .register(TunnelId.class) |
HIGUCHI Yuta | 03666a3 | 2016-05-18 11:49:09 -0700 | [diff] [blame^] | 135 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 136 | .build("VirtualNetworkStore")); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 137 | |
| 138 | /** |
| 139 | * Distributed network store service activate method. |
| 140 | */ |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 141 | @Activate |
| 142 | public void activate() { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 143 | idGenerator = coreService.getIdGenerator(VirtualNetworkService.VIRTUAL_NETWORK_TOPIC); |
| 144 | |
| 145 | tenantIdSet = storageService.<TenantId>setBuilder() |
| 146 | .withSerializer(SERIALIZER) |
| 147 | .withName("onos-tenantId") |
| 148 | .withRelaxedReadConsistency() |
| 149 | .build() |
| 150 | .asDistributedSet(); |
| 151 | tenantIdSet.addListener(setListener); |
| 152 | |
| 153 | networkIdVirtualNetworkConsistentMap = storageService.<NetworkId, VirtualNetwork>consistentMapBuilder() |
| 154 | .withSerializer(SERIALIZER) |
| 155 | .withName("onos-networkId-virtualnetwork") |
| 156 | .withRelaxedReadConsistency() |
| 157 | .build(); |
| 158 | networkIdVirtualNetworkConsistentMap.addListener(virtualMapListener); |
| 159 | networkIdVirtualNetworkMap = networkIdVirtualNetworkConsistentMap.asJavaMap(); |
| 160 | |
| 161 | tenantIdNetworkIdSetConsistentMap = storageService.<TenantId, Set<NetworkId>>consistentMapBuilder() |
| 162 | .withSerializer(SERIALIZER) |
| 163 | .withName("onos-tenantId-networkIds") |
| 164 | .withRelaxedReadConsistency() |
| 165 | .build(); |
| 166 | tenantIdNetworkIdSetMap = tenantIdNetworkIdSetConsistentMap.asJavaMap(); |
| 167 | |
| 168 | deviceIdVirtualDeviceConsistentMap = storageService.<DeviceId, VirtualDevice>consistentMapBuilder() |
| 169 | .withSerializer(SERIALIZER) |
| 170 | .withName("onos-deviceId-virtualdevice") |
| 171 | .withRelaxedReadConsistency() |
| 172 | .build(); |
| 173 | deviceIdVirtualDeviceMap = deviceIdVirtualDeviceConsistentMap.asJavaMap(); |
| 174 | |
| 175 | networkIdDeviceIdSetConsistentMap = storageService.<NetworkId, Set<DeviceId>>consistentMapBuilder() |
| 176 | .withSerializer(SERIALIZER) |
| 177 | .withName("onos-networkId-deviceIds") |
| 178 | .withRelaxedReadConsistency() |
| 179 | .build(); |
| 180 | networkIdDeviceIdSetMap = networkIdDeviceIdSetConsistentMap.asJavaMap(); |
| 181 | |
| 182 | networkIdVirtualLinkSetConsistentMap = storageService.<NetworkId, Set<VirtualLink>>consistentMapBuilder() |
| 183 | .withSerializer(SERIALIZER) |
| 184 | .withName("onos-networkId-virtuallinks") |
| 185 | .withRelaxedReadConsistency() |
| 186 | .build(); |
| 187 | networkIdVirtualLinkSetMap = networkIdVirtualLinkSetConsistentMap.asJavaMap(); |
| 188 | |
| 189 | networkIdVirtualPortSetConsistentMap = storageService.<NetworkId, Set<VirtualPort>>consistentMapBuilder() |
| 190 | .withSerializer(SERIALIZER) |
| 191 | .withName("onos-networkId-virtualportss") |
| 192 | .withRelaxedReadConsistency() |
| 193 | .build(); |
| 194 | networkIdVirtualPortSetMap = networkIdVirtualPortSetConsistentMap.asJavaMap(); |
| 195 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 196 | log.info("Started"); |
| 197 | } |
| 198 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 199 | /** |
| 200 | * Distributed network store service deactivate method. |
| 201 | */ |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 202 | @Deactivate |
| 203 | public void deactivate() { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 204 | tenantIdSet.removeListener(setListener); |
| 205 | networkIdVirtualNetworkConsistentMap.removeListener(virtualMapListener); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 206 | log.info("Stopped"); |
| 207 | } |
| 208 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 209 | /** |
| 210 | * This method is used for Junit tests to set the CoreService instance, which |
| 211 | * is required to set the IdGenerator instance. |
| 212 | * |
| 213 | * @param coreService core service instance |
| 214 | */ |
| 215 | public void setCoreService(CoreService coreService) { |
| 216 | this.coreService = coreService; |
| 217 | } |
| 218 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 219 | @Override |
| 220 | public void addTenantId(TenantId tenantId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 221 | tenantIdSet.add(tenantId); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | @Override |
| 225 | public void removeTenantId(TenantId tenantId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 226 | tenantIdSet.remove(tenantId); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public Set<TenantId> getTenantIds() { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 231 | return ImmutableSet.copyOf(tenantIdSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | @Override |
| 235 | public VirtualNetwork addNetwork(TenantId tenantId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 236 | |
| 237 | checkState(tenantIdSet.contains(tenantId), "The tenant has not been registered. " + tenantId.id()); |
| 238 | VirtualNetwork virtualNetwork = new DefaultVirtualNetwork(genNetworkId(), tenantId); |
| 239 | //TODO update both maps in one transaction. |
| 240 | networkIdVirtualNetworkMap.put(virtualNetwork.id(), virtualNetwork); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 241 | |
| 242 | Set<NetworkId> networkIdSet = tenantIdNetworkIdSetMap.get(tenantId); |
| 243 | if (networkIdSet == null) { |
| 244 | networkIdSet = new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 245 | } |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 246 | networkIdSet.add(virtualNetwork.id()); |
| 247 | tenantIdNetworkIdSetMap.put(tenantId, networkIdSet); |
| 248 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 249 | return virtualNetwork; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 252 | /** |
| 253 | * Returns a new network identifier from a virtual network block of identifiers. |
| 254 | * |
| 255 | * @return NetworkId network identifier |
| 256 | */ |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 257 | private NetworkId genNetworkId() { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 258 | return NetworkId.networkId(idGenerator.getNewId()); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | |
| 262 | @Override |
| 263 | public void removeNetwork(NetworkId networkId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 264 | // Make sure that the virtual network exists before attempting to remove it. |
| 265 | if (networkExists(networkId)) { |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 266 | //TODO update both maps in one transaction. |
| 267 | |
| 268 | VirtualNetwork virtualNetwork = networkIdVirtualNetworkMap.remove(networkId); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 269 | if (virtualNetwork == null) { |
| 270 | return; |
| 271 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 272 | TenantId tenantId = virtualNetwork.tenantId(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 273 | |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 274 | Set<NetworkId> networkIdSet = new HashSet<>(); |
| 275 | tenantIdNetworkIdSetMap.get(tenantId).forEach(networkId1 -> { |
| 276 | if (networkId1.id().equals(networkId.id())) { |
| 277 | networkIdSet.add(networkId1); |
| 278 | } |
| 279 | }); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 280 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 281 | tenantIdNetworkIdSetMap.compute(virtualNetwork.tenantId(), (id, existingNetworkIds) -> { |
| 282 | if (existingNetworkIds == null || existingNetworkIds.isEmpty()) { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 283 | return new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 284 | } else { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 285 | return new HashSet<>(Sets.difference(existingNetworkIds, networkIdSet)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 286 | } |
| 287 | }); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Returns if the network identifier exists. |
| 293 | * |
| 294 | * @param networkId network identifier |
| 295 | * @return true if the network identifier exists, false otherwise. |
| 296 | */ |
| 297 | private boolean networkExists(NetworkId networkId) { |
| 298 | return (networkIdVirtualNetworkMap.containsKey(networkId)); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | @Override |
| 302 | public VirtualDevice addDevice(NetworkId networkId, DeviceId deviceId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 303 | checkState(networkExists(networkId), "The network has not been added."); |
| 304 | Set<DeviceId> deviceIdSet = networkIdDeviceIdSetMap.get(networkId); |
| 305 | if (deviceIdSet == null) { |
| 306 | deviceIdSet = new HashSet<>(); |
| 307 | } |
| 308 | VirtualDevice virtualDevice = new DefaultVirtualDevice(networkId, deviceId); |
| 309 | //TODO update both maps in one transaction. |
| 310 | deviceIdVirtualDeviceMap.put(deviceId, virtualDevice); |
| 311 | deviceIdSet.add(deviceId); |
| 312 | networkIdDeviceIdSetMap.put(networkId, deviceIdSet); |
| 313 | return virtualDevice; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public void removeDevice(NetworkId networkId, DeviceId deviceId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 318 | checkState(networkExists(networkId), "The network has not been added."); |
| 319 | //TODO update both maps in one transaction. |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 320 | |
| 321 | Set<DeviceId> deviceIdSet = new HashSet<>(); |
| 322 | networkIdDeviceIdSetMap.get(networkId).forEach(deviceId1 -> { |
| 323 | if (deviceId1.equals(deviceId)) { |
| 324 | deviceIdSet.add(deviceId1); |
| 325 | } |
| 326 | }); |
| 327 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 328 | if (deviceIdSet != null) { |
| 329 | networkIdDeviceIdSetMap.compute(networkId, (id, existingDeviceIds) -> { |
| 330 | if (existingDeviceIds == null || existingDeviceIds.isEmpty()) { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 331 | return new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 332 | } else { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 333 | return new HashSet<>(Sets.difference(existingDeviceIds, deviceIdSet)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 334 | } |
| 335 | }); |
| 336 | |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 337 | deviceIdVirtualDeviceMap.remove(deviceId); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 338 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | @Override |
| 342 | public VirtualLink addLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst, TunnelId realizedBy) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 343 | checkState(networkExists(networkId), "The network has not been added."); |
| 344 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId); |
| 345 | if (virtualLinkSet == null) { |
| 346 | virtualLinkSet = new HashSet<>(); |
| 347 | } |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 348 | // validate that the link does not already exist in this network |
| 349 | checkState(getLink(networkId, src, dst) == null, "The virtual link already exists"); |
| 350 | |
| 351 | VirtualLink virtualLink = DefaultVirtualLink.builder() |
| 352 | .networkId(networkId) |
| 353 | .src(src) |
| 354 | .dst(dst) |
| 355 | .tunnelId(realizedBy) |
| 356 | .build(); |
| 357 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 358 | virtualLinkSet.add(virtualLink); |
| 359 | networkIdVirtualLinkSetMap.put(networkId, virtualLinkSet); |
| 360 | return virtualLink; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | @Override |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 364 | public void updateLink(VirtualLink virtualLink, TunnelId tunnelId) { |
| 365 | checkState(networkExists(virtualLink.networkId()), "The network has not been added."); |
| 366 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(virtualLink.networkId()); |
| 367 | if (virtualLinkSet == null) { |
| 368 | virtualLinkSet = new HashSet<>(); |
| 369 | } |
| 370 | virtualLinkSet.remove(virtualLink); |
| 371 | |
| 372 | VirtualLink newVirtualLink = DefaultVirtualLink.builder() |
| 373 | .networkId(virtualLink.networkId()) |
| 374 | .src(virtualLink.src()) |
| 375 | .dst(virtualLink.dst()) |
| 376 | .tunnelId(tunnelId) |
| 377 | .build(); |
| 378 | |
| 379 | virtualLinkSet.add(newVirtualLink); |
| 380 | networkIdVirtualLinkSetMap.put(newVirtualLink.networkId(), virtualLinkSet); |
| 381 | } |
| 382 | |
| 383 | @Override |
| 384 | public VirtualLink removeLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 385 | checkState(networkExists(networkId), "The network has not been added."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 386 | |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 387 | final VirtualLink virtualLink = getLink(networkId, src, dst); |
| 388 | if (virtualLink == null) { |
| 389 | return null; |
| 390 | } |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 391 | Set<VirtualLink> virtualLinkSet = new HashSet<>(); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 392 | virtualLinkSet.add(virtualLink); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 393 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 394 | if (virtualLinkSet != null) { |
| 395 | networkIdVirtualLinkSetMap.compute(networkId, (id, existingVirtualLinks) -> { |
| 396 | if (existingVirtualLinks == null || existingVirtualLinks.isEmpty()) { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 397 | return new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 398 | } else { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 399 | return new HashSet<>(Sets.difference(existingVirtualLinks, virtualLinkSet)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 400 | } |
| 401 | }); |
| 402 | } |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 403 | return virtualLink; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | @Override |
| 407 | public VirtualPort addPort(NetworkId networkId, DeviceId deviceId, PortNumber portNumber, Port realizedBy) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 408 | checkState(networkExists(networkId), "The network has not been added."); |
| 409 | Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId); |
| 410 | if (virtualPortSet == null) { |
| 411 | virtualPortSet = new HashSet<>(); |
| 412 | } |
| 413 | Device device = deviceIdVirtualDeviceMap.get(deviceId); |
| 414 | checkNotNull(device, "The device has not been created for deviceId: " + deviceId); |
| 415 | VirtualPort virtualPort = new DefaultVirtualPort(networkId, device, portNumber, realizedBy); |
| 416 | virtualPortSet.add(virtualPort); |
| 417 | networkIdVirtualPortSetMap.put(networkId, virtualPortSet); |
| 418 | return virtualPort; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | @Override |
| 422 | public void removePort(NetworkId networkId, DeviceId deviceId, PortNumber portNumber) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 423 | checkState(networkExists(networkId), "The network has not been added."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 424 | |
| 425 | Set<VirtualPort> virtualPortSet = new HashSet<>(); |
| 426 | networkIdVirtualPortSetMap.get(networkId).forEach(port -> { |
| 427 | if (port.element().id().equals(deviceId) && port.number().equals(portNumber)) { |
| 428 | virtualPortSet.add(port); |
| 429 | } |
| 430 | }); |
| 431 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 432 | if (virtualPortSet != null) { |
| 433 | networkIdVirtualPortSetMap.compute(networkId, (id, existingVirtualPorts) -> { |
| 434 | if (existingVirtualPorts == null || existingVirtualPorts.isEmpty()) { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 435 | return new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 436 | } else { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 437 | return new HashSet<>(Sets.difference(existingVirtualPorts, virtualPortSet)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 438 | } |
| 439 | }); |
| 440 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | @Override |
| 444 | public Set<VirtualNetwork> getNetworks(TenantId tenantId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 445 | Set<NetworkId> networkIdSet = tenantIdNetworkIdSetMap.get(tenantId); |
| 446 | Set<VirtualNetwork> virtualNetworkSet = new HashSet<>(); |
| 447 | if (networkIdSet != null) { |
| 448 | networkIdSet.forEach(networkId -> virtualNetworkSet.add(networkIdVirtualNetworkMap.get(networkId))); |
| 449 | } |
| 450 | return ImmutableSet.copyOf(virtualNetworkSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | @Override |
| 454 | public Set<VirtualDevice> getDevices(NetworkId networkId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 455 | checkState(networkExists(networkId), "The network has not been added."); |
| 456 | Set<DeviceId> deviceIdSet = networkIdDeviceIdSetMap.get(networkId); |
| 457 | Set<VirtualDevice> virtualDeviceSet = new HashSet<>(); |
| 458 | if (deviceIdSet != null) { |
| 459 | deviceIdSet.forEach(deviceId -> virtualDeviceSet.add(deviceIdVirtualDeviceMap.get(deviceId))); |
| 460 | } |
| 461 | return ImmutableSet.copyOf(virtualDeviceSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | @Override |
| 465 | public Set<VirtualLink> getLinks(NetworkId networkId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 466 | checkState(networkExists(networkId), "The network has not been added."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 467 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId); |
| 468 | if (virtualLinkSet == null) { |
| 469 | virtualLinkSet = new HashSet<>(); |
| 470 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 471 | return ImmutableSet.copyOf(virtualLinkSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 474 | /** |
| 475 | * Returns the virtual link matching the network identifier, source connect point, |
| 476 | * and destination connect point. |
| 477 | * |
| 478 | * @param networkId network identifier |
| 479 | * @param src source connect point |
| 480 | * @param dst destination connect point |
| 481 | * @return virtual link |
| 482 | */ |
| 483 | private VirtualLink getLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst) { |
| 484 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId); |
| 485 | if (virtualLinkSet == null) { |
| 486 | return null; |
| 487 | } |
| 488 | |
| 489 | VirtualLink virtualLink = null; |
| 490 | for (VirtualLink link : virtualLinkSet) { |
| 491 | if (link.src().equals(src) && link.dst().equals(dst)) { |
| 492 | virtualLink = link; |
| 493 | break; |
| 494 | } |
| 495 | } |
| 496 | return virtualLink; |
| 497 | } |
| 498 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 499 | @Override |
| 500 | public Set<VirtualPort> getPorts(NetworkId networkId, DeviceId deviceId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 501 | checkState(networkExists(networkId), "The network has not been added."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 502 | Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId); |
| 503 | if (virtualPortSet == null) { |
| 504 | virtualPortSet = new HashSet<>(); |
| 505 | } |
| 506 | |
| 507 | Set<VirtualPort> portSet = new HashSet<>(); |
| 508 | virtualPortSet.forEach(virtualPort -> { |
| 509 | if (virtualPort.element().id().equals(deviceId)) { |
| 510 | portSet.add(virtualPort); |
| 511 | } |
| 512 | }); |
| 513 | return ImmutableSet.copyOf(portSet); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | /** |
| 517 | * Listener class to map listener set events to the virtual network events. |
| 518 | */ |
| 519 | private class InternalSetListener implements SetEventListener<TenantId> { |
| 520 | @Override |
| 521 | public void event(SetEvent<TenantId> event) { |
| 522 | VirtualNetworkEvent.Type type = null; |
| 523 | switch (event.type()) { |
| 524 | case ADD: |
| 525 | type = VirtualNetworkEvent.Type.TENANT_REGISTERED; |
| 526 | break; |
| 527 | case REMOVE: |
| 528 | type = VirtualNetworkEvent.Type.TENANT_UNREGISTERED; |
| 529 | break; |
| 530 | default: |
| 531 | log.error("Unsupported event type: " + event.type()); |
| 532 | } |
| 533 | notifyDelegate(new VirtualNetworkEvent(type, null)); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Listener class to map listener map events to the virtual network events. |
| 539 | */ |
| 540 | private class InternalMapListener implements MapEventListener<NetworkId, VirtualNetwork> { |
| 541 | @Override |
| 542 | public void event(MapEvent<NetworkId, VirtualNetwork> event) { |
| 543 | NetworkId networkId = checkNotNull(event.key()); |
| 544 | VirtualNetworkEvent.Type type = null; |
| 545 | switch (event.type()) { |
| 546 | case INSERT: |
| 547 | type = VirtualNetworkEvent.Type.NETWORK_ADDED; |
| 548 | break; |
| 549 | case UPDATE: |
| 550 | if ((event.oldValue().value() != null) && (event.newValue().value() == null)) { |
| 551 | type = VirtualNetworkEvent.Type.NETWORK_REMOVED; |
| 552 | } else { |
| 553 | type = VirtualNetworkEvent.Type.NETWORK_UPDATED; |
| 554 | } |
| 555 | break; |
| 556 | case REMOVE: |
| 557 | type = VirtualNetworkEvent.Type.NETWORK_REMOVED; |
| 558 | break; |
| 559 | default: |
| 560 | log.error("Unsupported event type: " + event.type()); |
| 561 | } |
| 562 | notifyDelegate(new VirtualNetworkEvent(type, networkId)); |
| 563 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 564 | } |
| 565 | } |