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; |
Yoonseon Han | ffcc32f | 2017-05-03 14:42:17 -0700 | [diff] [blame] | 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; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 26 | import org.onlab.osgi.DefaultServiceDirectory; |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 27 | import org.onlab.osgi.ServiceDirectory; |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 28 | import org.onlab.packet.IpAddress; |
| 29 | import org.onlab.packet.MacAddress; |
| 30 | import org.onlab.packet.VlanId; |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.core.CoreService; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 33 | import org.onosproject.incubator.net.tunnel.TunnelId; |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 34 | import org.onosproject.incubator.net.virtual.DefaultVirtualLink; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 35 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 36 | import org.onosproject.incubator.net.virtual.TenantId; |
| 37 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 38 | import org.onosproject.incubator.net.virtual.VirtualHost; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 39 | import org.onosproject.incubator.net.virtual.VirtualLink; |
| 40 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 41 | import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService; |
| 42 | import org.onosproject.incubator.net.virtual.VirtualNetworkEvent; |
| 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; |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 49 | import org.onosproject.incubator.net.virtual.event.VirtualEvent; |
| 50 | import org.onosproject.incubator.net.virtual.event.VirtualListenerRegistryManager; |
yoonseon | 6b972c3 | 2016-12-06 16:45:03 -0800 | [diff] [blame] | 51 | import org.onosproject.incubator.net.virtual.provider.VirtualNetworkProvider; |
| 52 | import org.onosproject.incubator.net.virtual.provider.VirtualNetworkProviderRegistry; |
| 53 | import org.onosproject.incubator.net.virtual.provider.VirtualNetworkProviderService; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 54 | import org.onosproject.net.ConnectPoint; |
| 55 | import org.onosproject.net.DeviceId; |
Brian Stanke | 7a81b53 | 2016-06-14 15:43:51 -0400 | [diff] [blame] | 56 | import org.onosproject.net.HostId; |
| 57 | import org.onosproject.net.HostLocation; |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 58 | import org.onosproject.net.Link; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 59 | import org.onosproject.net.PortNumber; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 60 | import org.onosproject.net.device.DeviceService; |
Claudine Chiu | 70222ad | 2016-11-17 22:29:20 -0500 | [diff] [blame] | 61 | import org.onosproject.net.flow.FlowRuleService; |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 62 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 63 | import org.onosproject.net.group.GroupService; |
Claudine Chiu | 30a8a2a | 2016-07-14 17:09:04 -0400 | [diff] [blame] | 64 | import org.onosproject.net.host.HostService; |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 65 | import org.onosproject.net.intent.IntentService; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 66 | import org.onosproject.net.link.LinkService; |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 67 | import org.onosproject.net.packet.PacketService; |
Thomas Vachuska | 3d62fd7 | 2015-09-25 14:58:13 -0700 | [diff] [blame] | 68 | import org.onosproject.net.provider.AbstractListenerProviderRegistry; |
| 69 | import org.onosproject.net.provider.AbstractProviderService; |
Claudine Chiu | 45920dd | 2016-07-28 19:19:46 +0000 | [diff] [blame] | 70 | import org.onosproject.net.topology.PathService; |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 71 | import org.onosproject.net.topology.TopologyService; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 72 | import org.slf4j.Logger; |
| 73 | import org.slf4j.LoggerFactory; |
| 74 | |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 75 | import java.util.Map; |
Claudine Chiu | e7f6b91 | 2017-02-24 14:13:30 -0500 | [diff] [blame] | 76 | import java.util.Objects; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 77 | import java.util.Set; |
| 78 | |
| 79 | import static com.google.common.base.Preconditions.checkNotNull; |
| 80 | |
| 81 | /** |
| 82 | * Implementation of the virtual network service. |
| 83 | */ |
| 84 | @Component(immediate = true) |
| 85 | @Service |
| 86 | public class VirtualNetworkManager |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 87 | extends AbstractListenerProviderRegistry<VirtualNetworkEvent, |
| 88 | VirtualNetworkListener, VirtualNetworkProvider, VirtualNetworkProviderService> |
| 89 | implements VirtualNetworkService, VirtualNetworkAdminService, |
| 90 | VirtualNetworkProviderRegistry { |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 91 | |
| 92 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 93 | |
| 94 | private static final String TENANT_NULL = "Tenant ID cannot be null"; |
| 95 | private static final String NETWORK_NULL = "Network ID cannot be null"; |
| 96 | private static final String DEVICE_NULL = "Device ID cannot be null"; |
| 97 | private static final String LINK_POINT_NULL = "Link end-point cannot be null"; |
| 98 | |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 99 | private static final String VIRTUAL_NETWORK_APP_ID_STRING = |
| 100 | "org.onosproject.virtual-network"; |
| 101 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 102 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 103 | protected VirtualNetworkStore store; |
| 104 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 105 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 106 | protected IntentService intentService; |
| 107 | |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 108 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 109 | protected CoreService coreService; |
| 110 | |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 111 | private VirtualNetworkStoreDelegate delegate = this::post; |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 112 | |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 113 | private ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 114 | private ApplicationId appId; |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 115 | |
Thomas Vachuska | 3d62fd7 | 2015-09-25 14:58:13 -0700 | [diff] [blame] | 116 | // 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] | 117 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 118 | /** |
| 119 | * Only used for Junit test methods outside of this package. |
| 120 | * |
| 121 | * @param store virtual network store |
| 122 | */ |
| 123 | public void setStore(VirtualNetworkStore store) { |
| 124 | this.store = store; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Only used for Junit test methods outside of this package. |
| 129 | * |
| 130 | * @param intentService intent service |
| 131 | */ |
| 132 | |
| 133 | public void setIntentService(IntentService intentService) { |
| 134 | this.intentService = intentService; |
| 135 | } |
| 136 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 137 | @Activate |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 138 | public void activate() { |
Brian Stanke | 0e5c94e | 2016-03-08 11:20:04 -0500 | [diff] [blame] | 139 | eventDispatcher.addSink(VirtualNetworkEvent.class, listenerRegistry); |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 140 | eventDispatcher.addSink(VirtualEvent.class, |
| 141 | VirtualListenerRegistryManager.getInstance()); |
Yoonseon Han | b14461b | 2017-03-07 14:08:01 +0900 | [diff] [blame] | 142 | store.setDelegate(delegate); |
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); |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 151 | eventDispatcher.removeSink(VirtualEvent.class); |
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 | |
Yoonseon Han | ffcc32f | 2017-05-03 14:42:17 -0700 | [diff] [blame] | 356 | @Override |
| 357 | public Set<DeviceId> getPhysicalDevices(NetworkId networkId, |
| 358 | VirtualDevice virtualDevice) { |
| 359 | checkNotNull(networkId, "Network ID cannot be null"); |
| 360 | checkNotNull(virtualDevice, "Virtual device cannot be null"); |
| 361 | Set<VirtualPort> virtualPortSet = |
| 362 | getVirtualPorts(networkId, virtualDevice.id()); |
| 363 | Set<DeviceId> physicalDeviceSet = Sets.newConcurrentHashSet(); |
| 364 | |
| 365 | virtualPortSet.forEach(virtualPort -> { |
| 366 | if (virtualPort.realizedBy() != null) { |
| 367 | physicalDeviceSet.add(virtualPort.realizedBy().deviceId()); |
| 368 | } |
| 369 | }); |
| 370 | |
| 371 | return physicalDeviceSet; |
| 372 | } |
| 373 | |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 374 | private final Map<ServiceKey, VnetService> networkServices = Maps.newConcurrentMap(); |
| 375 | |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 376 | @Override |
Yoonseon Han | b14461b | 2017-03-07 14:08:01 +0900 | [diff] [blame] | 377 | @SuppressWarnings("unchecked") |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 378 | public <T> T get(NetworkId networkId, Class<T> serviceClass) { |
| 379 | checkNotNull(networkId, NETWORK_NULL); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 380 | ServiceKey serviceKey = networkServiceKey(networkId, serviceClass); |
| 381 | VnetService service = lookup(serviceKey); |
| 382 | if (service == null) { |
| 383 | service = create(serviceKey); |
| 384 | } |
| 385 | return (T) service; |
| 386 | } |
| 387 | |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 388 | @Override |
| 389 | public ApplicationId getVirtualNetworkApplicationId(NetworkId networkId) { |
| 390 | return appId; |
| 391 | } |
| 392 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 393 | /** |
| 394 | * Returns the Vnet service matching the service key. |
| 395 | * |
| 396 | * @param serviceKey service key |
| 397 | * @return vnet service |
| 398 | */ |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 399 | private VnetService lookup(ServiceKey serviceKey) { |
| 400 | return networkServices.get(serviceKey); |
| 401 | } |
| 402 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 403 | /** |
| 404 | * Creates a new service key using the specified network identifier and service class. |
| 405 | * |
| 406 | * @param networkId network identifier |
| 407 | * @param serviceClass service class |
| 408 | * @param <T> type of service |
| 409 | * @return service key |
| 410 | */ |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 411 | private <T> ServiceKey networkServiceKey(NetworkId networkId, Class<T> serviceClass) { |
| 412 | return new ServiceKey(networkId, serviceClass); |
| 413 | } |
| 414 | |
| 415 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 416 | /** |
| 417 | * Create a new vnet service instance. |
| 418 | * |
| 419 | * @param serviceKey service key |
| 420 | * @return vnet service |
| 421 | */ |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 422 | private VnetService create(ServiceKey serviceKey) { |
| 423 | VirtualNetwork network = getVirtualNetwork(serviceKey.networkId()); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 424 | checkNotNull(network, NETWORK_NULL); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 425 | |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 426 | VnetService service; |
| 427 | if (serviceKey.serviceClass.equals(DeviceService.class)) { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 428 | service = new VirtualNetworkDeviceManager(this, network.id()); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 429 | } else if (serviceKey.serviceClass.equals(LinkService.class)) { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 430 | service = new VirtualNetworkLinkManager(this, network.id()); |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 431 | } else if (serviceKey.serviceClass.equals(TopologyService.class)) { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 432 | service = new VirtualNetworkTopologyManager(this, network.id()); |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 433 | } else if (serviceKey.serviceClass.equals(IntentService.class)) { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 434 | service = new VirtualNetworkIntentManager(this, network.id()); |
Claudine Chiu | 30a8a2a | 2016-07-14 17:09:04 -0400 | [diff] [blame] | 435 | } else if (serviceKey.serviceClass.equals(HostService.class)) { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 436 | service = new VirtualNetworkHostManager(this, network.id()); |
Claudine Chiu | 45920dd | 2016-07-28 19:19:46 +0000 | [diff] [blame] | 437 | } else if (serviceKey.serviceClass.equals(PathService.class)) { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 438 | service = new VirtualNetworkPathManager(this, network.id()); |
Claudine Chiu | 70222ad | 2016-11-17 22:29:20 -0500 | [diff] [blame] | 439 | } else if (serviceKey.serviceClass.equals(FlowRuleService.class)) { |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 440 | service = new VirtualNetworkFlowRuleManager(this, network.id()); |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 441 | } else if (serviceKey.serviceClass.equals(PacketService.class)) { |
| 442 | service = new VirtualNetworkPacketManager(this, network.id()); |
yoonseon | dc3210d | 2017-01-25 16:03:10 -0800 | [diff] [blame] | 443 | } else if (serviceKey.serviceClass.equals(GroupService.class)) { |
| 444 | service = new VirtualNetworkGroupManager(this, network.id()); |
yoonseon | 86bebed | 2017-02-03 15:23:57 -0800 | [diff] [blame] | 445 | } else if (serviceKey.serviceClass.equals(FlowObjectiveService.class)) { |
| 446 | service = new VirtualNetworkFlowObjectiveManager(this, network.id()); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 447 | } else { |
| 448 | return null; |
| 449 | } |
| 450 | networkServices.put(serviceKey, service); |
| 451 | return service; |
| 452 | } |
| 453 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 454 | /** |
| 455 | * Service key class. |
| 456 | */ |
Claudine Chiu | 9a18f49 | 2017-02-27 15:57:06 -0500 | [diff] [blame] | 457 | private static class ServiceKey { |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 458 | final NetworkId networkId; |
| 459 | final Class serviceClass; |
| 460 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 461 | /** |
| 462 | * Constructor for service key. |
| 463 | * |
| 464 | * @param networkId network identifier |
| 465 | * @param serviceClass service class |
| 466 | */ |
Yoonseon Han | b14461b | 2017-03-07 14:08:01 +0900 | [diff] [blame] | 467 | ServiceKey(NetworkId networkId, Class serviceClass) { |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 468 | |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 469 | checkNotNull(networkId, NETWORK_NULL); |
| 470 | this.networkId = networkId; |
| 471 | this.serviceClass = serviceClass; |
| 472 | } |
| 473 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 474 | /** |
| 475 | * Returns the network identifier. |
| 476 | * |
| 477 | * @return network identifier |
| 478 | */ |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 479 | public NetworkId networkId() { |
| 480 | return networkId; |
| 481 | } |
| 482 | |
Brian Stanke | 11f6d53 | 2016-07-05 16:17:59 -0400 | [diff] [blame] | 483 | /** |
| 484 | * Returns the service class. |
| 485 | * |
| 486 | * @return service class |
| 487 | */ |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 488 | public Class serviceClass() { |
| 489 | return serviceClass; |
| 490 | } |
Claudine Chiu | e7f6b91 | 2017-02-24 14:13:30 -0500 | [diff] [blame] | 491 | |
| 492 | @Override |
| 493 | public int hashCode() { |
| 494 | return Objects.hash(networkId, serviceClass); |
| 495 | } |
| 496 | |
| 497 | @Override |
| 498 | public boolean equals(Object obj) { |
| 499 | if (this == obj) { |
| 500 | return true; |
| 501 | } |
| 502 | if (obj instanceof ServiceKey) { |
| 503 | ServiceKey that = (ServiceKey) obj; |
| 504 | return Objects.equals(this.networkId, that.networkId) && |
| 505 | Objects.equals(this.serviceClass, that.serviceClass); |
| 506 | } |
| 507 | return false; |
| 508 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Thomas Vachuska | 3d62fd7 | 2015-09-25 14:58:13 -0700 | [diff] [blame] | 511 | @Override |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 512 | protected VirtualNetworkProviderService |
| 513 | createProviderService(VirtualNetworkProvider provider) { |
Thomas Vachuska | 3d62fd7 | 2015-09-25 14:58:13 -0700 | [diff] [blame] | 514 | return new InternalVirtualNetworkProviderService(provider); |
| 515 | } |
| 516 | |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 517 | /** |
| 518 | * Service issued to registered virtual network providers so that they |
| 519 | * can interact with the core. |
| 520 | */ |
Thomas Vachuska | 3d62fd7 | 2015-09-25 14:58:13 -0700 | [diff] [blame] | 521 | private class InternalVirtualNetworkProviderService |
| 522 | extends AbstractProviderService<VirtualNetworkProvider> |
| 523 | implements VirtualNetworkProviderService { |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 524 | /** |
| 525 | * Constructor. |
| 526 | * @param provider virtual network provider |
| 527 | */ |
Thomas Vachuska | 3d62fd7 | 2015-09-25 14:58:13 -0700 | [diff] [blame] | 528 | InternalVirtualNetworkProviderService(VirtualNetworkProvider provider) { |
| 529 | super(provider); |
| 530 | } |
Brian Stanke | 4d57988 | 2016-04-22 13:28:46 -0400 | [diff] [blame] | 531 | |
| 532 | @Override |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 533 | public void topologyChanged(Set<Set<ConnectPoint>> clusters) { |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 534 | Set<TenantId> tenantIds = getTenantIds(); |
| 535 | tenantIds.forEach(tenantId -> { |
| 536 | Set<VirtualNetwork> virtualNetworks = getVirtualNetworks(tenantId); |
| 537 | |
| 538 | virtualNetworks.forEach(virtualNetwork -> { |
| 539 | Set<VirtualLink> virtualLinks = getVirtualLinks(virtualNetwork.id()); |
| 540 | |
| 541 | virtualLinks.forEach(virtualLink -> { |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 542 | if (isVirtualLinkInCluster(virtualNetwork.id(), |
| 543 | virtualLink, clusters)) { |
| 544 | store.updateLink(virtualLink, virtualLink.tunnelId(), |
| 545 | Link.State.ACTIVE); |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 546 | } else { |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 547 | store.updateLink(virtualLink, virtualLink.tunnelId(), |
| 548 | Link.State.INACTIVE); |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 549 | } |
| 550 | }); |
| 551 | }); |
| 552 | }); |
| 553 | } |
| 554 | |
| 555 | /** |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 556 | * Determines if the virtual link (both source and destination connect point) |
| 557 | * is in a cluster. |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 558 | * |
| 559 | * @param networkId virtual network identifier |
| 560 | * @param virtualLink virtual link |
| 561 | * @param clusters topology clusters |
| 562 | * @return true if the virtual link is in a cluster. |
| 563 | */ |
| 564 | private boolean isVirtualLinkInCluster(NetworkId networkId, VirtualLink virtualLink, |
| 565 | Set<Set<ConnectPoint>> clusters) { |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 566 | ConnectPoint srcPhysicalCp = |
| 567 | mapVirtualToPhysicalPort(networkId, virtualLink.src()); |
| 568 | ConnectPoint dstPhysicalCp = |
| 569 | mapVirtualToPhysicalPort(networkId, virtualLink.dst()); |
Brian Stanke | fb61df4 | 2016-07-25 11:47:51 -0400 | [diff] [blame] | 570 | |
| 571 | final boolean[] foundSrc = {false}; |
| 572 | final boolean[] foundDst = {false}; |
| 573 | clusters.forEach(connectPoints -> { |
| 574 | connectPoints.forEach(connectPoint -> { |
| 575 | if (connectPoint.equals(srcPhysicalCp)) { |
| 576 | foundSrc[0] = true; |
| 577 | } else if (connectPoint.equals(dstPhysicalCp)) { |
| 578 | foundDst[0] = true; |
| 579 | } |
| 580 | }); |
| 581 | if (foundSrc[0] && foundDst[0]) { |
| 582 | return; |
| 583 | } |
| 584 | }); |
| 585 | return foundSrc[0] && foundDst[0]; |
| 586 | } |
| 587 | |
| 588 | @Override |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 589 | public void tunnelUp(NetworkId networkId, ConnectPoint src, |
| 590 | ConnectPoint dst, TunnelId tunnelId) { |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 591 | ConnectPoint srcVirtualCp = mapPhysicalToVirtualToPort(networkId, src); |
| 592 | ConnectPoint dstVirtualCp = mapPhysicalToVirtualToPort(networkId, dst); |
| 593 | if ((srcVirtualCp == null) || (dstVirtualCp == null)) { |
| 594 | log.error("Src or dst virtual connection point was not found."); |
| 595 | } |
| 596 | |
| 597 | VirtualLink virtualLink = store.getLink(networkId, srcVirtualCp, dstVirtualCp); |
| 598 | if (virtualLink != null) { |
| 599 | store.updateLink(virtualLink, tunnelId, Link.State.ACTIVE); |
| 600 | } |
Brian Stanke | 4d57988 | 2016-04-22 13:28:46 -0400 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | @Override |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 604 | public void tunnelDown(NetworkId networkId, ConnectPoint src, |
| 605 | ConnectPoint dst, TunnelId tunnelId) { |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 606 | ConnectPoint srcVirtualCp = mapPhysicalToVirtualToPort(networkId, src); |
| 607 | ConnectPoint dstVirtualCp = mapPhysicalToVirtualToPort(networkId, dst); |
| 608 | if ((srcVirtualCp == null) || (dstVirtualCp == null)) { |
| 609 | log.error("Src or dst virtual connection point was not found."); |
| 610 | } |
Brian Stanke | 4d57988 | 2016-04-22 13:28:46 -0400 | [diff] [blame] | 611 | |
Brian Stanke | 612cebf | 2016-05-02 10:21:33 -0400 | [diff] [blame] | 612 | VirtualLink virtualLink = store.getLink(networkId, srcVirtualCp, dstVirtualCp); |
| 613 | if (virtualLink != null) { |
| 614 | store.updateLink(virtualLink, tunnelId, Link.State.INACTIVE); |
| 615 | } |
Brian Stanke | 4d57988 | 2016-04-22 13:28:46 -0400 | [diff] [blame] | 616 | } |
Thomas Vachuska | 3d62fd7 | 2015-09-25 14:58:13 -0700 | [diff] [blame] | 617 | } |
Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame] | 618 | } |