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