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