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