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.net.virtual.impl; |
| 17 | |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 18 | import com.google.common.collect.Maps; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 25 | import org.onlab.osgi.DefaultServiceDirectory; |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 26 | import org.onlab.osgi.ServiceDirectory; |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 27 | import org.onlab.packet.IpAddress; |
| 28 | import org.onlab.packet.MacAddress; |
| 29 | import org.onlab.packet.VlanId; |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame^] | 30 | import org.onosproject.core.ApplicationId; |
| 31 | import org.onosproject.core.CoreService; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 32 | import org.onosproject.incubator.net.tunnel.TunnelId; |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 33 | import org.onosproject.incubator.net.virtual.DefaultVirtualLink; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 34 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 35 | import org.onosproject.incubator.net.virtual.TenantId; |
| 36 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 37 | import org.onosproject.incubator.net.virtual.VirtualHost; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 38 | import org.onosproject.incubator.net.virtual.VirtualLink; |
| 39 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 40 | import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService; |
| 41 | import org.onosproject.incubator.net.virtual.VirtualNetworkEvent; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 42 | import org.onosproject.incubator.net.virtual.VirtualNetworkIntent; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 43 | import org.onosproject.incubator.net.virtual.VirtualNetworkListener; |
| 44 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
| 45 | import org.onosproject.incubator.net.virtual.VirtualNetworkStore; |
| 46 | import org.onosproject.incubator.net.virtual.VirtualNetworkStoreDelegate; |
| 47 | import org.onosproject.incubator.net.virtual.VirtualPort; |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 48 | import org.onosproject.incubator.net.virtual.VnetService; |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 49 | import org.onosproject.incubator.net.virtual.provider.VirtualNetworkProvider; |
| 50 | import org.onosproject.incubator.net.virtual.provider.VirtualNetworkProviderRegistry; |
| 51 | import org.onosproject.incubator.net.virtual.provider.VirtualNetworkProviderService; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 52 | import org.onosproject.net.ConnectPoint; |
| 53 | import org.onosproject.net.DeviceId; |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 54 | import org.onosproject.net.HostId; |
| 55 | import org.onosproject.net.HostLocation; |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 56 | import org.onosproject.net.Link; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 57 | import org.onosproject.net.PortNumber; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 58 | import org.onosproject.net.device.DeviceService; |
Claudine Chiu | 70222ad | 2016-11-17 22:29:20 -0500 | [diff] [blame] | 59 | import org.onosproject.net.flow.FlowRuleService; |
Claudine Chiu | 30a8a2a | 2016-07-14 17:09:04 -0400 | [diff] [blame] | 60 | import org.onosproject.net.host.HostService; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 61 | import org.onosproject.net.intent.IntentEvent; |
| 62 | import org.onosproject.net.intent.IntentListener; |
| 63 | import org.onosproject.net.intent.IntentService; |
| 64 | import org.onosproject.net.intent.IntentState; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 65 | import org.onosproject.net.link.LinkService; |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame^] | 66 | import org.onosproject.net.packet.PacketService; |
Thomas Vachuska | 3d62fd7 | 2015-09-25 14:58:13 -0700 | [diff] [blame] | 67 | import org.onosproject.net.provider.AbstractListenerProviderRegistry; |
| 68 | import org.onosproject.net.provider.AbstractProviderService; |
Claudine Chiu | 45920dd | 2016-07-28 19:19:46 +0000 | [diff] [blame] | 69 | import org.onosproject.net.topology.PathService; |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 70 | import org.onosproject.net.topology.TopologyService; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 71 | import org.slf4j.Logger; |
| 72 | import org.slf4j.LoggerFactory; |
| 73 | |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 74 | import java.util.Map; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 75 | import java.util.Set; |
| 76 | |
| 77 | import static com.google.common.base.Preconditions.checkNotNull; |
| 78 | |
| 79 | /** |
| 80 | * Implementation of the virtual network service. |
| 81 | */ |
| 82 | @Component(immediate = true) |
| 83 | @Service |
| 84 | public class VirtualNetworkManager |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 85 | extends AbstractListenerProviderRegistry<VirtualNetworkEvent, |
| 86 | VirtualNetworkListener, VirtualNetworkProvider, VirtualNetworkProviderService> |
| 87 | implements VirtualNetworkService, VirtualNetworkAdminService, |
| 88 | VirtualNetworkProviderRegistry { |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 89 | |
| 90 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 91 | |
| 92 | private static final String TENANT_NULL = "Tenant ID cannot be null"; |
| 93 | private static final String NETWORK_NULL = "Network ID cannot be null"; |
| 94 | private static final String DEVICE_NULL = "Device ID cannot be null"; |
| 95 | private static final String LINK_POINT_NULL = "Link end-point cannot be null"; |
| 96 | |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame^] | 97 | private static final String VIRTUAL_NETWORK_APP_ID_STRING = |
| 98 | "org.onosproject.virtual-network"; |
| 99 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 101 | protected VirtualNetworkStore store; |
| 102 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 104 | protected IntentService intentService; |
| 105 | |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame^] | 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 107 | protected CoreService coreService; |
| 108 | |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 109 | private final InternalVirtualIntentListener intentListener = |
| 110 | new InternalVirtualIntentListener(); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 111 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 112 | private VirtualNetworkStoreDelegate delegate = this::post; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 113 | |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 114 | private ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame^] | 115 | private ApplicationId appId; |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 116 | |
Thomas Vachuska | 3d62fd7 | 2015-09-25 14:58:13 -0700 | [diff] [blame] | 117 | // TODO: figure out how to coordinate "implementation" of a virtual network in a cluster |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 118 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 119 | /** |
| 120 | * Only used for Junit test methods outside of this package. |
| 121 | * |
| 122 | * @param store virtual network store |
| 123 | */ |
| 124 | public void setStore(VirtualNetworkStore store) { |
| 125 | this.store = store; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Only used for Junit test methods outside of this package. |
| 130 | * |
| 131 | * @param intentService intent service |
| 132 | */ |
| 133 | |
| 134 | public void setIntentService(IntentService intentService) { |
| 135 | this.intentService = intentService; |
| 136 | } |
| 137 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 138 | @Activate |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 139 | public void activate() { |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 140 | store.setDelegate(delegate); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 141 | eventDispatcher.addSink(VirtualNetworkEvent.class, listenerRegistry); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 142 | intentService.addListener(intentListener); |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame^] | 143 | appId = coreService.registerApplication(VIRTUAL_NETWORK_APP_ID_STRING); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 144 | log.info("Started"); |
| 145 | } |
| 146 | |
| 147 | @Deactivate |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 148 | public void deactivate() { |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 149 | store.unsetDelegate(delegate); |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 150 | eventDispatcher.removeSink(VirtualNetworkEvent.class); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 151 | intentService.removeListener(intentListener); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 152 | log.info("Stopped"); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void registerTenantId(TenantId tenantId) { |
| 157 | checkNotNull(tenantId, TENANT_NULL); |
| 158 | store.addTenantId(tenantId); |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public void unregisterTenantId(TenantId tenantId) { |
| 163 | checkNotNull(tenantId, TENANT_NULL); |
| 164 | store.removeTenantId(tenantId); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public Set<TenantId> getTenantIds() { |
| 169 | return store.getTenantIds(); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public VirtualNetwork createVirtualNetwork(TenantId tenantId) { |
| 174 | checkNotNull(tenantId, TENANT_NULL); |
| 175 | return store.addNetwork(tenantId); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public void removeVirtualNetwork(NetworkId networkId) { |
| 180 | checkNotNull(networkId, NETWORK_NULL); |
| 181 | store.removeNetwork(networkId); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public VirtualDevice createVirtualDevice(NetworkId networkId, DeviceId deviceId) { |
| 186 | checkNotNull(networkId, NETWORK_NULL); |
| 187 | checkNotNull(deviceId, DEVICE_NULL); |
| 188 | return store.addDevice(networkId, deviceId); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public void removeVirtualDevice(NetworkId networkId, DeviceId deviceId) { |
| 193 | checkNotNull(networkId, NETWORK_NULL); |
| 194 | checkNotNull(deviceId, DEVICE_NULL); |
| 195 | store.removeDevice(networkId, deviceId); |
| 196 | } |
| 197 | |
| 198 | @Override |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 199 | public VirtualHost createVirtualHost(NetworkId networkId, HostId hostId, |
| 200 | MacAddress mac, VlanId vlan, |
| 201 | HostLocation location, Set<IpAddress> ips) { |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 202 | checkNotNull(networkId, NETWORK_NULL); |
| 203 | checkNotNull(hostId, DEVICE_NULL); |
| 204 | return store.addHost(networkId, hostId, mac, vlan, location, ips); |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public void removeVirtualHost(NetworkId networkId, HostId hostId) { |
| 209 | checkNotNull(networkId, NETWORK_NULL); |
| 210 | checkNotNull(hostId, DEVICE_NULL); |
| 211 | store.removeHost(networkId, hostId); |
| 212 | } |
| 213 | |
| 214 | @Override |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 215 | public VirtualLink createVirtualLink(NetworkId networkId, |
Brian Stanke | 9a10897 | 2016-04-11 15:25:17 -0400 | [diff] [blame] | 216 | ConnectPoint src, ConnectPoint dst) { |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 217 | checkNotNull(networkId, NETWORK_NULL); |
| 218 | checkNotNull(src, LINK_POINT_NULL); |
| 219 | checkNotNull(dst, LINK_POINT_NULL); |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 220 | ConnectPoint physicalSrc = mapVirtualToPhysicalPort(networkId, src); |
| 221 | checkNotNull(physicalSrc, LINK_POINT_NULL); |
| 222 | ConnectPoint physicalDst = mapVirtualToPhysicalPort(networkId, dst); |
| 223 | checkNotNull(physicalDst, LINK_POINT_NULL); |
| 224 | |
| 225 | VirtualNetworkProvider provider = getProvider(DefaultVirtualLink.PID); |
| 226 | Link.State state = Link.State.INACTIVE; |
| 227 | if (provider != null) { |
| 228 | boolean traversable = provider.isTraversable(physicalSrc, physicalDst); |
| 229 | state = traversable ? Link.State.ACTIVE : Link.State.INACTIVE; |
| 230 | } |
| 231 | return store.addLink(networkId, src, dst, state, null); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Maps the virtual connect point to a physical connect point. |
| 236 | * |
| 237 | * @param networkId network identifier |
| 238 | * @param virtualCp virtual connect point |
| 239 | * @return physical connect point |
| 240 | */ |
| 241 | private ConnectPoint mapVirtualToPhysicalPort(NetworkId networkId, |
| 242 | ConnectPoint virtualCp) { |
| 243 | Set<VirtualPort> ports = store.getPorts(networkId, virtualCp.deviceId()); |
| 244 | for (VirtualPort port : ports) { |
| 245 | if (port.number().equals(virtualCp.port())) { |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 246 | return new ConnectPoint(port.realizedBy().deviceId(), |
| 247 | port.realizedBy().port()); |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | return null; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 253 | /** |
| 254 | * Maps the physical connect point to a virtual connect point. |
| 255 | * |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 256 | * @param networkId network identifier |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 257 | * @param physicalCp physical connect point |
| 258 | * @return virtual connect point |
| 259 | */ |
| 260 | private ConnectPoint mapPhysicalToVirtualToPort(NetworkId networkId, |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 261 | ConnectPoint physicalCp) { |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 262 | Set<VirtualPort> ports = store.getPorts(networkId, null); |
| 263 | for (VirtualPort port : ports) { |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 264 | if (port.realizedBy().deviceId().equals(physicalCp.elementId()) && |
| 265 | port.realizedBy().port().equals(physicalCp.port())) { |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 266 | return new ConnectPoint(port.element().id(), port.number()); |
| 267 | } |
| 268 | } |
| 269 | return null; |
| 270 | } |
| 271 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 272 | @Override |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 273 | public void removeVirtualLink(NetworkId networkId, ConnectPoint src, |
| 274 | ConnectPoint dst) { |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 275 | checkNotNull(networkId, NETWORK_NULL); |
| 276 | checkNotNull(src, LINK_POINT_NULL); |
| 277 | checkNotNull(dst, LINK_POINT_NULL); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 278 | store.removeLink(networkId, src, dst); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public VirtualPort createVirtualPort(NetworkId networkId, DeviceId deviceId, |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 283 | PortNumber portNumber, ConnectPoint realizedBy) { |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 284 | checkNotNull(networkId, NETWORK_NULL); |
| 285 | checkNotNull(deviceId, DEVICE_NULL); |
| 286 | checkNotNull(portNumber, "Port description cannot be null"); |
| 287 | return store.addPort(networkId, deviceId, portNumber, realizedBy); |
| 288 | } |
| 289 | |
| 290 | @Override |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 291 | public void bindVirtualPort(NetworkId networkId, DeviceId deviceId, |
| 292 | PortNumber portNumber, ConnectPoint realizedBy) { |
| 293 | checkNotNull(networkId, NETWORK_NULL); |
| 294 | checkNotNull(deviceId, DEVICE_NULL); |
| 295 | checkNotNull(portNumber, "Port description cannot be null"); |
| 296 | checkNotNull(realizedBy, "Physical port description cannot be null"); |
| 297 | |
| 298 | store.bindPort(networkId, deviceId, portNumber, realizedBy); |
| 299 | } |
| 300 | |
| 301 | @Override |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 302 | public void removeVirtualPort(NetworkId networkId, DeviceId deviceId, |
| 303 | PortNumber portNumber) { |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 304 | checkNotNull(networkId, NETWORK_NULL); |
| 305 | checkNotNull(deviceId, DEVICE_NULL); |
| 306 | checkNotNull(portNumber, "Port number cannot be null"); |
| 307 | store.removePort(networkId, deviceId, portNumber); |
| 308 | } |
| 309 | |
| 310 | @Override |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 311 | public ServiceDirectory getServiceDirectory() { |
| 312 | return serviceDirectory; |
| 313 | } |
| 314 | |
| 315 | @Override |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 316 | public Set<VirtualNetwork> getVirtualNetworks(TenantId tenantId) { |
| 317 | checkNotNull(tenantId, TENANT_NULL); |
| 318 | return store.getNetworks(tenantId); |
| 319 | } |
| 320 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 321 | /** |
| 322 | * Returns the virtual network matching the network identifier. |
| 323 | * |
| 324 | * @param networkId network identifier |
| 325 | * @return virtual network |
| 326 | */ |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 327 | private VirtualNetwork getVirtualNetwork(NetworkId networkId) { |
| 328 | checkNotNull(networkId, NETWORK_NULL); |
| 329 | return store.getNetwork(networkId); |
| 330 | } |
| 331 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 332 | @Override |
| 333 | public Set<VirtualDevice> getVirtualDevices(NetworkId networkId) { |
| 334 | checkNotNull(networkId, NETWORK_NULL); |
| 335 | return store.getDevices(networkId); |
| 336 | } |
| 337 | |
| 338 | @Override |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 339 | public Set<VirtualHost> getVirtualHosts(NetworkId networkId) { |
| 340 | checkNotNull(networkId, NETWORK_NULL); |
| 341 | return store.getHosts(networkId); |
| 342 | } |
| 343 | |
| 344 | @Override |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 345 | public Set<VirtualLink> getVirtualLinks(NetworkId networkId) { |
| 346 | checkNotNull(networkId, NETWORK_NULL); |
| 347 | return store.getLinks(networkId); |
| 348 | } |
| 349 | |
| 350 | @Override |
| 351 | public Set<VirtualPort> getVirtualPorts(NetworkId networkId, DeviceId deviceId) { |
| 352 | checkNotNull(networkId, NETWORK_NULL); |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 353 | return store.getPorts(networkId, deviceId); |
| 354 | } |
| 355 | |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 356 | private final Map<ServiceKey, VnetService> networkServices = Maps.newConcurrentMap(); |
| 357 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 358 | @Override |
| 359 | public <T> T get(NetworkId networkId, Class<T> serviceClass) { |
| 360 | checkNotNull(networkId, NETWORK_NULL); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 361 | ServiceKey serviceKey = networkServiceKey(networkId, serviceClass); |
| 362 | VnetService service = lookup(serviceKey); |
| 363 | if (service == null) { |
| 364 | service = create(serviceKey); |
| 365 | } |
| 366 | return (T) service; |
| 367 | } |
| 368 | |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame^] | 369 | @Override |
| 370 | public ApplicationId getVirtualNetworkApplicationId(NetworkId networkId) { |
| 371 | return appId; |
| 372 | } |
| 373 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 374 | /** |
| 375 | * Returns the Vnet service matching the service key. |
| 376 | * |
| 377 | * @param serviceKey service key |
| 378 | * @return vnet service |
| 379 | */ |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 380 | private VnetService lookup(ServiceKey serviceKey) { |
| 381 | return networkServices.get(serviceKey); |
| 382 | } |
| 383 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 384 | /** |
| 385 | * Creates a new service key using the specified network identifier and service class. |
| 386 | * |
| 387 | * @param networkId network identifier |
| 388 | * @param serviceClass service class |
| 389 | * @param <T> type of service |
| 390 | * @return service key |
| 391 | */ |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 392 | private <T> ServiceKey networkServiceKey(NetworkId networkId, Class<T> serviceClass) { |
| 393 | return new ServiceKey(networkId, serviceClass); |
| 394 | } |
| 395 | |
| 396 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 397 | /** |
| 398 | * Create a new vnet service instance. |
| 399 | * |
| 400 | * @param serviceKey service key |
| 401 | * @return vnet service |
| 402 | */ |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 403 | private VnetService create(ServiceKey serviceKey) { |
| 404 | VirtualNetwork network = getVirtualNetwork(serviceKey.networkId()); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 405 | checkNotNull(network, NETWORK_NULL); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 406 | |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 407 | VnetService service; |
| 408 | if (serviceKey.serviceClass.equals(DeviceService.class)) { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 409 | service = new VirtualNetworkDeviceManager(this, network.id()); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 410 | } else if (serviceKey.serviceClass.equals(LinkService.class)) { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 411 | service = new VirtualNetworkLinkManager(this, network.id()); |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 412 | } else if (serviceKey.serviceClass.equals(TopologyService.class)) { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 413 | service = new VirtualNetworkTopologyManager(this, network.id()); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 414 | } else if (serviceKey.serviceClass.equals(IntentService.class)) { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 415 | service = new VirtualNetworkIntentManager(this, network.id()); |
Claudine Chiu | 30a8a2a | 2016-07-14 17:09:04 -0400 | [diff] [blame] | 416 | } else if (serviceKey.serviceClass.equals(HostService.class)) { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 417 | service = new VirtualNetworkHostManager(this, network.id()); |
Claudine Chiu | 45920dd | 2016-07-28 19:19:46 +0000 | [diff] [blame] | 418 | } else if (serviceKey.serviceClass.equals(PathService.class)) { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 419 | service = new VirtualNetworkPathManager(this, network.id()); |
Claudine Chiu | 70222ad | 2016-11-17 22:29:20 -0500 | [diff] [blame] | 420 | } else if (serviceKey.serviceClass.equals(FlowRuleService.class)) { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 421 | service = new VirtualNetworkFlowRuleManager(this, network.id()); |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame^] | 422 | } else if (serviceKey.serviceClass.equals(PacketService.class)) { |
| 423 | service = new VirtualNetworkPacketManager(this, network.id()); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 424 | } else { |
| 425 | return null; |
| 426 | } |
| 427 | networkServices.put(serviceKey, service); |
| 428 | return service; |
| 429 | } |
| 430 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 431 | /** |
| 432 | * Service key class. |
| 433 | */ |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 434 | private class ServiceKey { |
| 435 | final NetworkId networkId; |
| 436 | final Class serviceClass; |
| 437 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 438 | /** |
| 439 | * Constructor for service key. |
| 440 | * |
| 441 | * @param networkId network identifier |
| 442 | * @param serviceClass service class |
| 443 | */ |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 444 | public ServiceKey(NetworkId networkId, Class serviceClass) { |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 445 | |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 446 | checkNotNull(networkId, NETWORK_NULL); |
| 447 | this.networkId = networkId; |
| 448 | this.serviceClass = serviceClass; |
| 449 | } |
| 450 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 451 | /** |
| 452 | * Returns the network identifier. |
| 453 | * |
| 454 | * @return network identifier |
| 455 | */ |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 456 | public NetworkId networkId() { |
| 457 | return networkId; |
| 458 | } |
| 459 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 460 | /** |
| 461 | * Returns the service class. |
| 462 | * |
| 463 | * @return service class |
| 464 | */ |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 465 | public Class serviceClass() { |
| 466 | return serviceClass; |
| 467 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 470 | /** |
| 471 | * Internal intent event listener. |
| 472 | */ |
| 473 | private class InternalVirtualIntentListener implements IntentListener { |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 474 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 475 | @Override |
| 476 | public void event(IntentEvent event) { |
| 477 | |
| 478 | // Ignore intent events that are not relevant. |
| 479 | if (!isRelevant(event)) { |
| 480 | return; |
| 481 | } |
| 482 | |
| 483 | VirtualNetworkIntent intent = (VirtualNetworkIntent) event.subject(); |
| 484 | |
| 485 | switch (event.type()) { |
| 486 | case INSTALL_REQ: |
| 487 | store.addOrUpdateIntent(intent, IntentState.INSTALL_REQ); |
| 488 | break; |
| 489 | case INSTALLED: |
| 490 | store.addOrUpdateIntent(intent, IntentState.INSTALLED); |
| 491 | break; |
| 492 | case WITHDRAW_REQ: |
| 493 | store.addOrUpdateIntent(intent, IntentState.WITHDRAW_REQ); |
| 494 | break; |
| 495 | case WITHDRAWN: |
| 496 | store.addOrUpdateIntent(intent, IntentState.WITHDRAWN); |
| 497 | break; |
| 498 | case FAILED: |
| 499 | store.addOrUpdateIntent(intent, IntentState.FAILED); |
| 500 | break; |
| 501 | case CORRUPT: |
| 502 | store.addOrUpdateIntent(intent, IntentState.CORRUPT); |
| 503 | break; |
| 504 | case PURGED: |
| 505 | store.removeIntent(intent.key()); |
| 506 | default: |
| 507 | break; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | @Override |
| 512 | public boolean isRelevant(IntentEvent event) { |
| 513 | if (event.subject() instanceof VirtualNetworkIntent) { |
| 514 | return true; |
| 515 | } |
| 516 | return false; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | |
Thomas Vachuska | 3d62fd7 | 2015-09-25 14:58:13 -0700 | [diff] [blame] | 521 | @Override |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 522 | protected VirtualNetworkProviderService |
| 523 | createProviderService(VirtualNetworkProvider provider) { |
Thomas Vachuska | 3d62fd7 | 2015-09-25 14:58:13 -0700 | [diff] [blame] | 524 | return new InternalVirtualNetworkProviderService(provider); |
| 525 | } |
| 526 | |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 527 | |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 528 | /** |
| 529 | * Service issued to registered virtual network providers so that they |
| 530 | * can interact with the core. |
| 531 | */ |
Thomas Vachuska | 3d62fd7 | 2015-09-25 14:58:13 -0700 | [diff] [blame] | 532 | private class InternalVirtualNetworkProviderService |
| 533 | extends AbstractProviderService<VirtualNetworkProvider> |
| 534 | implements VirtualNetworkProviderService { |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 535 | /** |
| 536 | * Constructor. |
| 537 | * @param provider virtual network provider |
| 538 | */ |
Thomas Vachuska | 3d62fd7 | 2015-09-25 14:58:13 -0700 | [diff] [blame] | 539 | InternalVirtualNetworkProviderService(VirtualNetworkProvider provider) { |
| 540 | super(provider); |
| 541 | } |
Brian Stanke | 4d57988 | 2016-04-22 13:28:46 -0400 | [diff] [blame] | 542 | |
| 543 | @Override |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 544 | public void topologyChanged(Set<Set<ConnectPoint>> clusters) { |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 545 | Set<TenantId> tenantIds = getTenantIds(); |
| 546 | tenantIds.forEach(tenantId -> { |
| 547 | Set<VirtualNetwork> virtualNetworks = getVirtualNetworks(tenantId); |
| 548 | |
| 549 | virtualNetworks.forEach(virtualNetwork -> { |
| 550 | Set<VirtualLink> virtualLinks = getVirtualLinks(virtualNetwork.id()); |
| 551 | |
| 552 | virtualLinks.forEach(virtualLink -> { |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 553 | if (isVirtualLinkInCluster(virtualNetwork.id(), |
| 554 | virtualLink, clusters)) { |
| 555 | store.updateLink(virtualLink, virtualLink.tunnelId(), |
| 556 | Link.State.ACTIVE); |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 557 | } else { |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 558 | store.updateLink(virtualLink, virtualLink.tunnelId(), |
| 559 | Link.State.INACTIVE); |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 560 | } |
| 561 | }); |
| 562 | }); |
| 563 | }); |
| 564 | } |
| 565 | |
| 566 | /** |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 567 | * Determines if the virtual link (both source and destination connect point) |
| 568 | * is in a cluster. |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 569 | * |
| 570 | * @param networkId virtual network identifier |
| 571 | * @param virtualLink virtual link |
| 572 | * @param clusters topology clusters |
| 573 | * @return true if the virtual link is in a cluster. |
| 574 | */ |
| 575 | private boolean isVirtualLinkInCluster(NetworkId networkId, VirtualLink virtualLink, |
| 576 | Set<Set<ConnectPoint>> clusters) { |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 577 | ConnectPoint srcPhysicalCp = |
| 578 | mapVirtualToPhysicalPort(networkId, virtualLink.src()); |
| 579 | ConnectPoint dstPhysicalCp = |
| 580 | mapVirtualToPhysicalPort(networkId, virtualLink.dst()); |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 581 | |
| 582 | final boolean[] foundSrc = {false}; |
| 583 | final boolean[] foundDst = {false}; |
| 584 | clusters.forEach(connectPoints -> { |
| 585 | connectPoints.forEach(connectPoint -> { |
| 586 | if (connectPoint.equals(srcPhysicalCp)) { |
| 587 | foundSrc[0] = true; |
| 588 | } else if (connectPoint.equals(dstPhysicalCp)) { |
| 589 | foundDst[0] = true; |
| 590 | } |
| 591 | }); |
| 592 | if (foundSrc[0] && foundDst[0]) { |
| 593 | return; |
| 594 | } |
| 595 | }); |
| 596 | return foundSrc[0] && foundDst[0]; |
| 597 | } |
| 598 | |
| 599 | @Override |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 600 | public void tunnelUp(NetworkId networkId, ConnectPoint src, |
| 601 | ConnectPoint dst, TunnelId tunnelId) { |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 602 | ConnectPoint srcVirtualCp = mapPhysicalToVirtualToPort(networkId, src); |
| 603 | ConnectPoint dstVirtualCp = mapPhysicalToVirtualToPort(networkId, dst); |
| 604 | if ((srcVirtualCp == null) || (dstVirtualCp == null)) { |
| 605 | log.error("Src or dst virtual connection point was not found."); |
| 606 | } |
| 607 | |
| 608 | VirtualLink virtualLink = store.getLink(networkId, srcVirtualCp, dstVirtualCp); |
| 609 | if (virtualLink != null) { |
| 610 | store.updateLink(virtualLink, tunnelId, Link.State.ACTIVE); |
| 611 | } |
Brian Stanke | 4d57988 | 2016-04-22 13:28:46 -0400 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | @Override |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 615 | public void tunnelDown(NetworkId networkId, ConnectPoint src, |
| 616 | ConnectPoint dst, TunnelId tunnelId) { |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 617 | ConnectPoint srcVirtualCp = mapPhysicalToVirtualToPort(networkId, src); |
| 618 | ConnectPoint dstVirtualCp = mapPhysicalToVirtualToPort(networkId, dst); |
| 619 | if ((srcVirtualCp == null) || (dstVirtualCp == null)) { |
| 620 | log.error("Src or dst virtual connection point was not found."); |
| 621 | } |
Brian Stanke | 4d57988 | 2016-04-22 13:28:46 -0400 | [diff] [blame] | 622 | |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 623 | VirtualLink virtualLink = store.getLink(networkId, srcVirtualCp, dstVirtualCp); |
| 624 | if (virtualLink != null) { |
| 625 | store.updateLink(virtualLink, tunnelId, Link.State.INACTIVE); |
| 626 | } |
Brian Stanke | 4d57988 | 2016-04-22 13:28:46 -0400 | [diff] [blame] | 627 | } |
Thomas Vachuska | 3d62fd7 | 2015-09-25 14:58:13 -0700 | [diff] [blame] | 628 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 629 | } |