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 | 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; |
| 58 | import org.onosproject.net.intent.IntentData; |
| 59 | import org.onosproject.net.intent.IntentState; |
| 60 | import org.onosproject.net.intent.Key; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 61 | import org.onosproject.store.AbstractStore; |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 62 | import org.onosproject.store.serializers.KryoNamespaces; |
| 63 | import org.onosproject.store.service.ConsistentMap; |
| 64 | import org.onosproject.store.service.DistributedSet; |
| 65 | import org.onosproject.store.service.MapEvent; |
| 66 | import org.onosproject.store.service.MapEventListener; |
| 67 | import org.onosproject.store.service.Serializer; |
| 68 | import org.onosproject.store.service.SetEvent; |
| 69 | import org.onosproject.store.service.SetEventListener; |
| 70 | import org.onosproject.store.service.StorageService; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 71 | import org.onosproject.store.service.WallClockTimestamp; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 72 | import org.slf4j.Logger; |
| 73 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 74 | import java.util.HashSet; |
| 75 | import java.util.Map; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 76 | import java.util.Set; |
| 77 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 78 | import static com.google.common.base.Preconditions.checkNotNull; |
| 79 | import static com.google.common.base.Preconditions.checkState; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 80 | import static org.slf4j.LoggerFactory.getLogger; |
| 81 | |
| 82 | /** |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 83 | * Implementation of the virtual network store. |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 84 | */ |
| 85 | @Component(immediate = true) |
| 86 | @Service |
| 87 | public class DistributedVirtualNetworkStore |
| 88 | extends AbstractStore<VirtualNetworkEvent, VirtualNetworkStoreDelegate> |
| 89 | implements VirtualNetworkStore { |
| 90 | |
| 91 | private final Logger log = getLogger(getClass()); |
| 92 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 94 | protected StorageService storageService; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 95 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 97 | protected CoreService coreService; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 98 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 99 | private IdGenerator idGenerator; |
| 100 | |
| 101 | // Track tenants by ID |
| 102 | private DistributedSet<TenantId> tenantIdSet; |
| 103 | |
| 104 | // Listener for tenant events |
| 105 | private final SetEventListener<TenantId> setListener = new InternalSetListener(); |
| 106 | |
| 107 | // Track virtual networks by network Id |
| 108 | private ConsistentMap<NetworkId, VirtualNetwork> networkIdVirtualNetworkConsistentMap; |
| 109 | private Map<NetworkId, VirtualNetwork> networkIdVirtualNetworkMap; |
| 110 | |
| 111 | // Listener for virtual network events |
| 112 | private final MapEventListener<NetworkId, VirtualNetwork> virtualMapListener = new InternalMapListener(); |
| 113 | |
| 114 | // Track virtual network IDs by tenant Id |
| 115 | private ConsistentMap<TenantId, Set<NetworkId>> tenantIdNetworkIdSetConsistentMap; |
| 116 | private Map<TenantId, Set<NetworkId>> tenantIdNetworkIdSetMap; |
| 117 | |
| 118 | // Track virtual devices by device Id |
| 119 | private ConsistentMap<DeviceId, VirtualDevice> deviceIdVirtualDeviceConsistentMap; |
| 120 | private Map<DeviceId, VirtualDevice> deviceIdVirtualDeviceMap; |
| 121 | |
| 122 | // Track device IDs by network Id |
| 123 | private ConsistentMap<NetworkId, Set<DeviceId>> networkIdDeviceIdSetConsistentMap; |
| 124 | private Map<NetworkId, Set<DeviceId>> networkIdDeviceIdSetMap; |
| 125 | |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 126 | // Track virtual hosts by host Id |
| 127 | private ConsistentMap<HostId, VirtualHost> hostIdVirtualHostConsistentMap; |
| 128 | private Map<HostId, VirtualHost> hostIdVirtualHostMap; |
| 129 | |
| 130 | // Track host IDs by network Id |
| 131 | private ConsistentMap<NetworkId, Set<HostId>> networkIdHostIdSetConsistentMap; |
| 132 | private Map<NetworkId, Set<HostId>> networkIdHostIdSetMap; |
| 133 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 134 | // Track virtual links by network Id |
| 135 | private ConsistentMap<NetworkId, Set<VirtualLink>> networkIdVirtualLinkSetConsistentMap; |
| 136 | private Map<NetworkId, Set<VirtualLink>> networkIdVirtualLinkSetMap; |
| 137 | |
| 138 | // Track virtual ports by network Id |
| 139 | private ConsistentMap<NetworkId, Set<VirtualPort>> networkIdVirtualPortSetConsistentMap; |
| 140 | private Map<NetworkId, Set<VirtualPort>> networkIdVirtualPortSetMap; |
| 141 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 142 | // Track intent key to intent data |
| 143 | private ConsistentMap<Key, IntentData> intentKeyIntentDataConsistentMap; |
| 144 | private Map<Key, IntentData> intentKeyIntentDataMap; |
| 145 | |
| 146 | // Track intent ID to TunnelIds |
| 147 | private ConsistentMap<Key, Set<TunnelId>> intentKeyTunnelIdSetConsistentMap; |
| 148 | private Map<Key, Set<TunnelId>> intentKeyTunnelIdSetMap; |
| 149 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 150 | private static final Serializer SERIALIZER = Serializer |
| 151 | .using(new KryoNamespace.Builder().register(KryoNamespaces.API) |
| 152 | .register(TenantId.class) |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 153 | .register(NetworkId.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 154 | .register(VirtualNetwork.class) |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 155 | .register(DefaultVirtualNetwork.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 156 | .register(VirtualDevice.class) |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 157 | .register(DefaultVirtualDevice.class) |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 158 | .register(VirtualHost.class) |
| 159 | .register(DefaultVirtualHost.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 160 | .register(VirtualLink.class) |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 161 | .register(DefaultVirtualLink.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 162 | .register(VirtualPort.class) |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 163 | .register(DefaultVirtualPort.class) |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 164 | .register(Device.class) |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 165 | .register(TunnelId.class) |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 166 | .register(IntentData.class) |
| 167 | .register(VirtualNetworkIntent.class) |
| 168 | .register(WallClockTimestamp.class) |
HIGUCHI Yuta | 03666a3 | 2016-05-18 11:49:09 -0700 | [diff] [blame] | 169 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 170 | .build()); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 171 | |
| 172 | /** |
| 173 | * Distributed network store service activate method. |
| 174 | */ |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 175 | @Activate |
| 176 | public void activate() { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 177 | idGenerator = coreService.getIdGenerator(VirtualNetworkService.VIRTUAL_NETWORK_TOPIC); |
| 178 | |
| 179 | tenantIdSet = storageService.<TenantId>setBuilder() |
| 180 | .withSerializer(SERIALIZER) |
| 181 | .withName("onos-tenantId") |
| 182 | .withRelaxedReadConsistency() |
| 183 | .build() |
| 184 | .asDistributedSet(); |
| 185 | tenantIdSet.addListener(setListener); |
| 186 | |
| 187 | networkIdVirtualNetworkConsistentMap = storageService.<NetworkId, VirtualNetwork>consistentMapBuilder() |
| 188 | .withSerializer(SERIALIZER) |
| 189 | .withName("onos-networkId-virtualnetwork") |
| 190 | .withRelaxedReadConsistency() |
| 191 | .build(); |
| 192 | networkIdVirtualNetworkConsistentMap.addListener(virtualMapListener); |
| 193 | networkIdVirtualNetworkMap = networkIdVirtualNetworkConsistentMap.asJavaMap(); |
| 194 | |
| 195 | tenantIdNetworkIdSetConsistentMap = storageService.<TenantId, Set<NetworkId>>consistentMapBuilder() |
| 196 | .withSerializer(SERIALIZER) |
| 197 | .withName("onos-tenantId-networkIds") |
| 198 | .withRelaxedReadConsistency() |
| 199 | .build(); |
| 200 | tenantIdNetworkIdSetMap = tenantIdNetworkIdSetConsistentMap.asJavaMap(); |
| 201 | |
| 202 | deviceIdVirtualDeviceConsistentMap = storageService.<DeviceId, VirtualDevice>consistentMapBuilder() |
| 203 | .withSerializer(SERIALIZER) |
| 204 | .withName("onos-deviceId-virtualdevice") |
| 205 | .withRelaxedReadConsistency() |
| 206 | .build(); |
| 207 | deviceIdVirtualDeviceMap = deviceIdVirtualDeviceConsistentMap.asJavaMap(); |
| 208 | |
| 209 | networkIdDeviceIdSetConsistentMap = storageService.<NetworkId, Set<DeviceId>>consistentMapBuilder() |
| 210 | .withSerializer(SERIALIZER) |
| 211 | .withName("onos-networkId-deviceIds") |
| 212 | .withRelaxedReadConsistency() |
| 213 | .build(); |
| 214 | networkIdDeviceIdSetMap = networkIdDeviceIdSetConsistentMap.asJavaMap(); |
| 215 | |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 216 | hostIdVirtualHostConsistentMap = storageService.<HostId, VirtualHost>consistentMapBuilder() |
| 217 | .withSerializer(SERIALIZER) |
| 218 | .withName("onos-hostId-virtualhost") |
| 219 | .withRelaxedReadConsistency() |
| 220 | .build(); |
| 221 | hostIdVirtualHostMap = hostIdVirtualHostConsistentMap.asJavaMap(); |
| 222 | |
| 223 | networkIdHostIdSetConsistentMap = storageService.<NetworkId, Set<HostId>>consistentMapBuilder() |
| 224 | .withSerializer(SERIALIZER) |
| 225 | .withName("onos-networkId-hostIds") |
| 226 | .withRelaxedReadConsistency() |
| 227 | .build(); |
| 228 | networkIdHostIdSetMap = networkIdHostIdSetConsistentMap.asJavaMap(); |
| 229 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 230 | networkIdVirtualLinkSetConsistentMap = storageService.<NetworkId, Set<VirtualLink>>consistentMapBuilder() |
| 231 | .withSerializer(SERIALIZER) |
| 232 | .withName("onos-networkId-virtuallinks") |
| 233 | .withRelaxedReadConsistency() |
| 234 | .build(); |
| 235 | networkIdVirtualLinkSetMap = networkIdVirtualLinkSetConsistentMap.asJavaMap(); |
| 236 | |
| 237 | networkIdVirtualPortSetConsistentMap = storageService.<NetworkId, Set<VirtualPort>>consistentMapBuilder() |
| 238 | .withSerializer(SERIALIZER) |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 239 | .withName("onos-networkId-virtualports") |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 240 | .withRelaxedReadConsistency() |
| 241 | .build(); |
| 242 | networkIdVirtualPortSetMap = networkIdVirtualPortSetConsistentMap.asJavaMap(); |
| 243 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 244 | intentKeyTunnelIdSetConsistentMap = storageService.<Key, Set<TunnelId>>consistentMapBuilder() |
| 245 | .withSerializer(SERIALIZER) |
| 246 | .withName("onos-intentKey-tunnelIds") |
| 247 | .withRelaxedReadConsistency() |
| 248 | .build(); |
| 249 | intentKeyTunnelIdSetMap = intentKeyTunnelIdSetConsistentMap.asJavaMap(); |
| 250 | |
| 251 | intentKeyIntentDataConsistentMap = storageService.<Key, IntentData>consistentMapBuilder() |
| 252 | .withSerializer(SERIALIZER) |
| 253 | .withName("onos-intentKey-intentData") |
| 254 | .withRelaxedReadConsistency() |
| 255 | .build(); |
| 256 | intentKeyIntentDataMap = intentKeyIntentDataConsistentMap.asJavaMap(); |
| 257 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 258 | log.info("Started"); |
| 259 | } |
| 260 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 261 | /** |
| 262 | * Distributed network store service deactivate method. |
| 263 | */ |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 264 | @Deactivate |
| 265 | public void deactivate() { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 266 | tenantIdSet.removeListener(setListener); |
| 267 | networkIdVirtualNetworkConsistentMap.removeListener(virtualMapListener); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 268 | log.info("Stopped"); |
| 269 | } |
| 270 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 271 | /** |
| 272 | * This method is used for Junit tests to set the CoreService instance, which |
| 273 | * is required to set the IdGenerator instance. |
| 274 | * |
| 275 | * @param coreService core service instance |
| 276 | */ |
| 277 | public void setCoreService(CoreService coreService) { |
| 278 | this.coreService = coreService; |
| 279 | } |
| 280 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 281 | @Override |
| 282 | public void addTenantId(TenantId tenantId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 283 | tenantIdSet.add(tenantId); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | @Override |
| 287 | public void removeTenantId(TenantId tenantId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 288 | tenantIdSet.remove(tenantId); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | @Override |
| 292 | public Set<TenantId> getTenantIds() { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 293 | return ImmutableSet.copyOf(tenantIdSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public VirtualNetwork addNetwork(TenantId tenantId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 298 | |
| 299 | checkState(tenantIdSet.contains(tenantId), "The tenant has not been registered. " + tenantId.id()); |
| 300 | VirtualNetwork virtualNetwork = new DefaultVirtualNetwork(genNetworkId(), tenantId); |
| 301 | //TODO update both maps in one transaction. |
| 302 | networkIdVirtualNetworkMap.put(virtualNetwork.id(), virtualNetwork); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 303 | |
| 304 | Set<NetworkId> networkIdSet = tenantIdNetworkIdSetMap.get(tenantId); |
| 305 | if (networkIdSet == null) { |
| 306 | networkIdSet = new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 307 | } |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 308 | networkIdSet.add(virtualNetwork.id()); |
| 309 | tenantIdNetworkIdSetMap.put(tenantId, networkIdSet); |
| 310 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 311 | return virtualNetwork; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 314 | /** |
| 315 | * Returns a new network identifier from a virtual network block of identifiers. |
| 316 | * |
| 317 | * @return NetworkId network identifier |
| 318 | */ |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 319 | private NetworkId genNetworkId() { |
Kenji HIKICHI | cfdf91b | 2016-05-25 13:04:45 +0900 | [diff] [blame] | 320 | NetworkId networkId; |
| 321 | do { |
| 322 | networkId = NetworkId.networkId(idGenerator.getNewId()); |
| 323 | } while (!networkId.isVirtualNetworkId()); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 324 | |
Kenji HIKICHI | cfdf91b | 2016-05-25 13:04:45 +0900 | [diff] [blame] | 325 | return networkId; |
| 326 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 327 | |
| 328 | @Override |
| 329 | public void removeNetwork(NetworkId networkId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 330 | // Make sure that the virtual network exists before attempting to remove it. |
| 331 | if (networkExists(networkId)) { |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 332 | //TODO update both maps in one transaction. |
| 333 | |
| 334 | VirtualNetwork virtualNetwork = networkIdVirtualNetworkMap.remove(networkId); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 335 | if (virtualNetwork == null) { |
| 336 | return; |
| 337 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 338 | TenantId tenantId = virtualNetwork.tenantId(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 339 | |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 340 | Set<NetworkId> networkIdSet = new HashSet<>(); |
| 341 | tenantIdNetworkIdSetMap.get(tenantId).forEach(networkId1 -> { |
| 342 | if (networkId1.id().equals(networkId.id())) { |
| 343 | networkIdSet.add(networkId1); |
| 344 | } |
| 345 | }); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 346 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 347 | tenantIdNetworkIdSetMap.compute(virtualNetwork.tenantId(), (id, existingNetworkIds) -> { |
| 348 | if (existingNetworkIds == null || existingNetworkIds.isEmpty()) { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 349 | return new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 350 | } else { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 351 | return new HashSet<>(Sets.difference(existingNetworkIds, networkIdSet)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 352 | } |
| 353 | }); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Returns if the network identifier exists. |
| 359 | * |
| 360 | * @param networkId network identifier |
| 361 | * @return true if the network identifier exists, false otherwise. |
| 362 | */ |
| 363 | private boolean networkExists(NetworkId networkId) { |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 364 | checkNotNull(networkId, "The network identifier cannot be null."); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 365 | return (networkIdVirtualNetworkMap.containsKey(networkId)); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | @Override |
| 369 | public VirtualDevice addDevice(NetworkId networkId, DeviceId deviceId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 370 | checkState(networkExists(networkId), "The network has not been added."); |
| 371 | Set<DeviceId> deviceIdSet = networkIdDeviceIdSetMap.get(networkId); |
| 372 | if (deviceIdSet == null) { |
| 373 | deviceIdSet = new HashSet<>(); |
| 374 | } |
| 375 | VirtualDevice virtualDevice = new DefaultVirtualDevice(networkId, deviceId); |
| 376 | //TODO update both maps in one transaction. |
| 377 | deviceIdVirtualDeviceMap.put(deviceId, virtualDevice); |
| 378 | deviceIdSet.add(deviceId); |
| 379 | networkIdDeviceIdSetMap.put(networkId, deviceIdSet); |
| 380 | return virtualDevice; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | @Override |
| 384 | public void removeDevice(NetworkId networkId, DeviceId deviceId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 385 | checkState(networkExists(networkId), "The network has not been added."); |
| 386 | //TODO update both maps in one transaction. |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 387 | |
| 388 | Set<DeviceId> deviceIdSet = new HashSet<>(); |
| 389 | networkIdDeviceIdSetMap.get(networkId).forEach(deviceId1 -> { |
| 390 | if (deviceId1.equals(deviceId)) { |
| 391 | deviceIdSet.add(deviceId1); |
| 392 | } |
| 393 | }); |
| 394 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 395 | if (deviceIdSet != null) { |
| 396 | networkIdDeviceIdSetMap.compute(networkId, (id, existingDeviceIds) -> { |
| 397 | if (existingDeviceIds == null || existingDeviceIds.isEmpty()) { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 398 | return new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 399 | } else { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 400 | return new HashSet<>(Sets.difference(existingDeviceIds, deviceIdSet)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 401 | } |
| 402 | }); |
| 403 | |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 404 | deviceIdVirtualDeviceMap.remove(deviceId); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 405 | } |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 406 | //TODO remove virtual links and ports when removing the virtual device |
| 407 | } |
| 408 | |
| 409 | @Override |
| 410 | public VirtualHost addHost(NetworkId networkId, HostId hostId, MacAddress mac, |
| 411 | VlanId vlan, HostLocation location, Set<IpAddress> ips) { |
| 412 | checkState(networkExists(networkId), "The network has not been added."); |
| 413 | Set<HostId> hostIdSet = networkIdHostIdSetMap.get(networkId); |
| 414 | if (hostIdSet == null) { |
| 415 | hostIdSet = new HashSet<>(); |
| 416 | } |
| 417 | VirtualHost virtualhost = new DefaultVirtualHost(networkId, hostId, mac, vlan, location, ips); |
| 418 | //TODO update both maps in one transaction. |
| 419 | hostIdVirtualHostMap.put(hostId, virtualhost); |
| 420 | hostIdSet.add(hostId); |
| 421 | networkIdHostIdSetMap.put(networkId, hostIdSet); |
| 422 | return virtualhost; |
| 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public void removeHost(NetworkId networkId, HostId hostId) { |
| 427 | checkState(networkExists(networkId), "The network has not been added."); |
| 428 | //TODO update both maps in one transaction. |
| 429 | |
| 430 | Set<HostId> hostIdSet = new HashSet<>(); |
| 431 | networkIdHostIdSetMap.get(networkId).forEach(hostId1 -> { |
| 432 | if (hostId1.equals(hostId)) { |
| 433 | hostIdSet.add(hostId1); |
| 434 | } |
| 435 | }); |
| 436 | |
| 437 | if (hostIdSet != null) { |
| 438 | networkIdHostIdSetMap.compute(networkId, (id, existingHostIds) -> { |
| 439 | if (existingHostIds == null || existingHostIds.isEmpty()) { |
| 440 | return new HashSet<>(); |
| 441 | } else { |
| 442 | return new HashSet<>(Sets.difference(existingHostIds, hostIdSet)); |
| 443 | } |
| 444 | }); |
| 445 | |
| 446 | hostIdVirtualHostMap.remove(hostId); |
| 447 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | @Override |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 451 | public VirtualLink addLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst, |
| 452 | Link.State state, TunnelId realizedBy) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 453 | checkState(networkExists(networkId), "The network has not been added."); |
| 454 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId); |
| 455 | if (virtualLinkSet == null) { |
| 456 | virtualLinkSet = new HashSet<>(); |
| 457 | } |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 458 | // validate that the link does not already exist in this network |
| 459 | checkState(getLink(networkId, src, dst) == null, "The virtual link already exists"); |
| 460 | |
| 461 | VirtualLink virtualLink = DefaultVirtualLink.builder() |
| 462 | .networkId(networkId) |
| 463 | .src(src) |
| 464 | .dst(dst) |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 465 | .state(state) |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 466 | .tunnelId(realizedBy) |
| 467 | .build(); |
| 468 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 469 | virtualLinkSet.add(virtualLink); |
| 470 | networkIdVirtualLinkSetMap.put(networkId, virtualLinkSet); |
| 471 | return virtualLink; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | @Override |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 475 | public void updateLink(VirtualLink virtualLink, TunnelId tunnelId, Link.State state) { |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 476 | checkState(networkExists(virtualLink.networkId()), "The network has not been added."); |
| 477 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(virtualLink.networkId()); |
| 478 | if (virtualLinkSet == null) { |
| 479 | virtualLinkSet = new HashSet<>(); |
| 480 | } |
| 481 | virtualLinkSet.remove(virtualLink); |
| 482 | |
| 483 | VirtualLink newVirtualLink = DefaultVirtualLink.builder() |
| 484 | .networkId(virtualLink.networkId()) |
| 485 | .src(virtualLink.src()) |
| 486 | .dst(virtualLink.dst()) |
| 487 | .tunnelId(tunnelId) |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 488 | .state(state) |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 489 | .build(); |
| 490 | |
| 491 | virtualLinkSet.add(newVirtualLink); |
| 492 | networkIdVirtualLinkSetMap.put(newVirtualLink.networkId(), virtualLinkSet); |
| 493 | } |
| 494 | |
| 495 | @Override |
| 496 | public VirtualLink removeLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 497 | checkState(networkExists(networkId), "The network has not been added."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 498 | |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 499 | final VirtualLink virtualLink = getLink(networkId, src, dst); |
| 500 | if (virtualLink == null) { |
| 501 | return null; |
| 502 | } |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 503 | Set<VirtualLink> virtualLinkSet = new HashSet<>(); |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 504 | virtualLinkSet.add(virtualLink); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 505 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 506 | if (virtualLinkSet != null) { |
| 507 | networkIdVirtualLinkSetMap.compute(networkId, (id, existingVirtualLinks) -> { |
| 508 | if (existingVirtualLinks == null || existingVirtualLinks.isEmpty()) { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 509 | return new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 510 | } else { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 511 | return new HashSet<>(Sets.difference(existingVirtualLinks, virtualLinkSet)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 512 | } |
| 513 | }); |
| 514 | } |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 515 | return virtualLink; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | @Override |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame^] | 519 | public VirtualPort addPort(NetworkId networkId, DeviceId deviceId, |
| 520 | PortNumber portNumber, ConnectPoint realizedBy) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 521 | checkState(networkExists(networkId), "The network has not been added."); |
| 522 | Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame^] | 523 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 524 | if (virtualPortSet == null) { |
| 525 | virtualPortSet = new HashSet<>(); |
| 526 | } |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame^] | 527 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 528 | Device device = deviceIdVirtualDeviceMap.get(deviceId); |
| 529 | checkNotNull(device, "The device has not been created for deviceId: " + deviceId); |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame^] | 530 | |
| 531 | boolean exist = virtualPortSet.stream().anyMatch( |
| 532 | p -> p.element().id().equals(deviceId) && |
| 533 | p.number().equals(portNumber)); |
| 534 | checkState(!exist, "The requested Port Number is already in use"); |
| 535 | |
| 536 | VirtualPort virtualPort = new DefaultVirtualPort(networkId, device, |
| 537 | portNumber, realizedBy); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 538 | virtualPortSet.add(virtualPort); |
| 539 | networkIdVirtualPortSetMap.put(networkId, virtualPortSet); |
| 540 | return virtualPort; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | @Override |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame^] | 544 | public void bindPort(NetworkId networkId, DeviceId deviceId, |
| 545 | PortNumber portNumber, ConnectPoint realizedBy) { |
| 546 | |
| 547 | Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap |
| 548 | .get(networkId); |
| 549 | |
| 550 | VirtualPort vPort = virtualPortSet.stream().filter( |
| 551 | p -> p.element().id().equals(deviceId) && |
| 552 | p.number().equals(portNumber)).findFirst().get(); |
| 553 | checkNotNull(vPort, "The virtual port has not been added."); |
| 554 | |
| 555 | Device device = deviceIdVirtualDeviceMap.get(deviceId); |
| 556 | checkNotNull(device, "The device has not been created for deviceId: " |
| 557 | + deviceId); |
| 558 | |
| 559 | virtualPortSet.remove(vPort); |
| 560 | vPort = new DefaultVirtualPort(networkId, device, portNumber, realizedBy); |
| 561 | virtualPortSet.add(vPort); |
| 562 | networkIdVirtualPortSetMap.put(networkId, virtualPortSet); |
| 563 | } |
| 564 | |
| 565 | @Override |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 566 | public void removePort(NetworkId networkId, DeviceId deviceId, PortNumber portNumber) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 567 | checkState(networkExists(networkId), "The network has not been added."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 568 | |
| 569 | Set<VirtualPort> virtualPortSet = new HashSet<>(); |
| 570 | networkIdVirtualPortSetMap.get(networkId).forEach(port -> { |
| 571 | if (port.element().id().equals(deviceId) && port.number().equals(portNumber)) { |
| 572 | virtualPortSet.add(port); |
| 573 | } |
| 574 | }); |
| 575 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 576 | if (virtualPortSet != null) { |
| 577 | networkIdVirtualPortSetMap.compute(networkId, (id, existingVirtualPorts) -> { |
| 578 | if (existingVirtualPorts == null || existingVirtualPorts.isEmpty()) { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 579 | return new HashSet<>(); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 580 | } else { |
HIGUCHI Yuta | c0f5045 | 2016-05-13 19:26:05 -0700 | [diff] [blame] | 581 | return new HashSet<>(Sets.difference(existingVirtualPorts, virtualPortSet)); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 582 | } |
| 583 | }); |
| 584 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | @Override |
| 588 | public Set<VirtualNetwork> getNetworks(TenantId tenantId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 589 | Set<NetworkId> networkIdSet = tenantIdNetworkIdSetMap.get(tenantId); |
| 590 | Set<VirtualNetwork> virtualNetworkSet = new HashSet<>(); |
| 591 | if (networkIdSet != null) { |
| 592 | networkIdSet.forEach(networkId -> virtualNetworkSet.add(networkIdVirtualNetworkMap.get(networkId))); |
| 593 | } |
| 594 | return ImmutableSet.copyOf(virtualNetworkSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | @Override |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 598 | public VirtualNetwork getNetwork(NetworkId networkId) { |
| 599 | return networkIdVirtualNetworkMap.get(networkId); |
| 600 | } |
| 601 | |
| 602 | @Override |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 603 | public Set<VirtualDevice> getDevices(NetworkId networkId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 604 | checkState(networkExists(networkId), "The network has not been added."); |
| 605 | Set<DeviceId> deviceIdSet = networkIdDeviceIdSetMap.get(networkId); |
| 606 | Set<VirtualDevice> virtualDeviceSet = new HashSet<>(); |
| 607 | if (deviceIdSet != null) { |
| 608 | deviceIdSet.forEach(deviceId -> virtualDeviceSet.add(deviceIdVirtualDeviceMap.get(deviceId))); |
| 609 | } |
| 610 | return ImmutableSet.copyOf(virtualDeviceSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | @Override |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 614 | public Set<VirtualHost> getHosts(NetworkId networkId) { |
| 615 | checkState(networkExists(networkId), "The network has not been added."); |
| 616 | Set<HostId> hostIdSet = networkIdHostIdSetMap.get(networkId); |
| 617 | Set<VirtualHost> virtualHostSet = new HashSet<>(); |
| 618 | if (hostIdSet != null) { |
| 619 | hostIdSet.forEach(hostId -> virtualHostSet.add(hostIdVirtualHostMap.get(hostId))); |
| 620 | } |
| 621 | return ImmutableSet.copyOf(virtualHostSet); |
| 622 | } |
| 623 | |
| 624 | @Override |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 625 | public Set<VirtualLink> getLinks(NetworkId networkId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 626 | checkState(networkExists(networkId), "The network has not been added."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 627 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId); |
| 628 | if (virtualLinkSet == null) { |
| 629 | virtualLinkSet = new HashSet<>(); |
| 630 | } |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 631 | return ImmutableSet.copyOf(virtualLinkSet); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 634 | @Override |
| 635 | public VirtualLink getLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst) { |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 636 | Set<VirtualLink> virtualLinkSet = networkIdVirtualLinkSetMap.get(networkId); |
| 637 | if (virtualLinkSet == null) { |
| 638 | return null; |
| 639 | } |
| 640 | |
| 641 | VirtualLink virtualLink = null; |
| 642 | for (VirtualLink link : virtualLinkSet) { |
| 643 | if (link.src().equals(src) && link.dst().equals(dst)) { |
| 644 | virtualLink = link; |
| 645 | break; |
| 646 | } |
| 647 | } |
| 648 | return virtualLink; |
| 649 | } |
| 650 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 651 | @Override |
| 652 | public Set<VirtualPort> getPorts(NetworkId networkId, DeviceId deviceId) { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 653 | checkState(networkExists(networkId), "The network has not been added."); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 654 | Set<VirtualPort> virtualPortSet = networkIdVirtualPortSetMap.get(networkId); |
| 655 | if (virtualPortSet == null) { |
| 656 | virtualPortSet = new HashSet<>(); |
| 657 | } |
| 658 | |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 659 | if (deviceId == null) { |
| 660 | return ImmutableSet.copyOf(virtualPortSet); |
| 661 | } |
| 662 | |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 663 | Set<VirtualPort> portSet = new HashSet<>(); |
| 664 | virtualPortSet.forEach(virtualPort -> { |
| 665 | if (virtualPort.element().id().equals(deviceId)) { |
| 666 | portSet.add(virtualPort); |
| 667 | } |
| 668 | }); |
| 669 | return ImmutableSet.copyOf(portSet); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 670 | } |
| 671 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 672 | @Override |
| 673 | public synchronized void addOrUpdateIntent(Intent intent, IntentState state) { |
| 674 | checkNotNull(intent, "Intent cannot be null"); |
| 675 | IntentData intentData = removeIntent(intent.key()); |
| 676 | if (intentData == null) { |
| 677 | intentData = new IntentData(intent, state, new WallClockTimestamp(System.currentTimeMillis())); |
| 678 | } else { |
| 679 | intentData = new IntentData(intent, state, intentData.version()); |
| 680 | } |
| 681 | intentKeyIntentDataMap.put(intent.key(), intentData); |
| 682 | } |
| 683 | |
| 684 | @Override |
| 685 | public IntentData removeIntent(Key intentKey) { |
| 686 | checkNotNull(intentKey, "Intent key cannot be null"); |
| 687 | return intentKeyIntentDataMap.remove(intentKey); |
| 688 | } |
| 689 | |
| 690 | @Override |
| 691 | public void addTunnelId(Intent intent, TunnelId tunnelId) { |
| 692 | // Add the tunnelId to the intent key set map |
| 693 | Set<TunnelId> tunnelIdSet = intentKeyTunnelIdSetMap.remove(intent.key()); |
| 694 | if (tunnelIdSet == null) { |
| 695 | tunnelIdSet = new HashSet<>(); |
| 696 | } |
| 697 | tunnelIdSet.add(tunnelId); |
| 698 | intentKeyTunnelIdSetMap.put(intent.key(), tunnelIdSet); |
| 699 | } |
| 700 | |
| 701 | @Override |
| 702 | public Set<TunnelId> getTunnelIds(Intent intent) { |
| 703 | Set<TunnelId> tunnelIdSet = intentKeyTunnelIdSetMap.get(intent.key()); |
| 704 | return tunnelIdSet == null ? new HashSet<TunnelId>() : ImmutableSet.copyOf(tunnelIdSet); |
| 705 | } |
| 706 | |
| 707 | @Override |
| 708 | public void removeTunnelId(Intent intent, TunnelId tunnelId) { |
| 709 | Set<TunnelId> tunnelIdSet = new HashSet<>(); |
| 710 | intentKeyTunnelIdSetMap.get(intent.key()).forEach(tunnelId1 -> { |
| 711 | if (tunnelId1.equals(tunnelId)) { |
| 712 | tunnelIdSet.add(tunnelId); |
| 713 | } |
| 714 | }); |
| 715 | |
| 716 | if (!tunnelIdSet.isEmpty()) { |
| 717 | intentKeyTunnelIdSetMap.compute(intent.key(), (key, existingTunnelIds) -> { |
| 718 | if (existingTunnelIds == null || existingTunnelIds.isEmpty()) { |
| 719 | return new HashSet<>(); |
| 720 | } else { |
| 721 | return new HashSet<>(Sets.difference(existingTunnelIds, tunnelIdSet)); |
| 722 | } |
| 723 | }); |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | @Override |
| 728 | public Set<Intent> getIntents() { |
| 729 | Set<Intent> intents = new HashSet<>(); |
| 730 | intentKeyIntentDataMap.values().forEach(intentData -> intents.add(intentData.intent())); |
| 731 | return ImmutableSet.copyOf(intents); |
| 732 | } |
| 733 | |
| 734 | @Override |
| 735 | public Intent getIntent(Key key) { |
| 736 | IntentData intentData = intentKeyIntentDataMap.get(key); |
| 737 | return intentData == null ? null : intentData.intent(); |
| 738 | } |
| 739 | |
| 740 | @Override |
| 741 | public Set<IntentData> getIntentData() { |
| 742 | return ImmutableSet.copyOf(intentKeyIntentDataMap.values()); |
| 743 | } |
| 744 | |
| 745 | @Override |
| 746 | public IntentData getIntentData(Key key) { |
| 747 | IntentData intentData = intentKeyIntentDataMap.get(key); |
| 748 | return intentData == null ? null : new IntentData(intentData); |
| 749 | } |
| 750 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 751 | /** |
| 752 | * Listener class to map listener set events to the virtual network events. |
| 753 | */ |
| 754 | private class InternalSetListener implements SetEventListener<TenantId> { |
| 755 | @Override |
| 756 | public void event(SetEvent<TenantId> event) { |
| 757 | VirtualNetworkEvent.Type type = null; |
| 758 | switch (event.type()) { |
| 759 | case ADD: |
| 760 | type = VirtualNetworkEvent.Type.TENANT_REGISTERED; |
| 761 | break; |
| 762 | case REMOVE: |
| 763 | type = VirtualNetworkEvent.Type.TENANT_UNREGISTERED; |
| 764 | break; |
| 765 | default: |
| 766 | log.error("Unsupported event type: " + event.type()); |
| 767 | } |
| 768 | notifyDelegate(new VirtualNetworkEvent(type, null)); |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | /** |
| 773 | * Listener class to map listener map events to the virtual network events. |
| 774 | */ |
| 775 | private class InternalMapListener implements MapEventListener<NetworkId, VirtualNetwork> { |
| 776 | @Override |
| 777 | public void event(MapEvent<NetworkId, VirtualNetwork> event) { |
| 778 | NetworkId networkId = checkNotNull(event.key()); |
| 779 | VirtualNetworkEvent.Type type = null; |
| 780 | switch (event.type()) { |
| 781 | case INSERT: |
| 782 | type = VirtualNetworkEvent.Type.NETWORK_ADDED; |
| 783 | break; |
| 784 | case UPDATE: |
| 785 | if ((event.oldValue().value() != null) && (event.newValue().value() == null)) { |
| 786 | type = VirtualNetworkEvent.Type.NETWORK_REMOVED; |
| 787 | } else { |
| 788 | type = VirtualNetworkEvent.Type.NETWORK_UPDATED; |
| 789 | } |
| 790 | break; |
| 791 | case REMOVE: |
| 792 | type = VirtualNetworkEvent.Type.NETWORK_REMOVED; |
| 793 | break; |
| 794 | default: |
| 795 | log.error("Unsupported event type: " + event.type()); |
| 796 | } |
| 797 | notifyDelegate(new VirtualNetworkEvent(type, networkId)); |
| 798 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 799 | } |
| 800 | } |