Thomas Vachuska | 33979fd | 2015-07-31 11:41:14 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.apache.felix.scr.annotations.Service; |
| 24 | import org.onosproject.event.AbstractListenerManager; |
| 25 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 26 | import org.onosproject.incubator.net.virtual.NetworkId; |
| 27 | import org.onosproject.incubator.net.virtual.TenantId; |
| 28 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
| 29 | import org.onosproject.incubator.net.virtual.VirtualLink; |
| 30 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 31 | import org.onosproject.incubator.net.virtual.VirtualNetworkAdminService; |
| 32 | import org.onosproject.incubator.net.virtual.VirtualNetworkEvent; |
| 33 | import org.onosproject.incubator.net.virtual.VirtualNetworkListener; |
| 34 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
| 35 | import org.onosproject.incubator.net.virtual.VirtualNetworkStore; |
| 36 | import org.onosproject.incubator.net.virtual.VirtualNetworkStoreDelegate; |
| 37 | import org.onosproject.incubator.net.virtual.VirtualPort; |
| 38 | import org.onosproject.net.ConnectPoint; |
| 39 | import org.onosproject.net.DeviceId; |
| 40 | import org.onosproject.net.Port; |
| 41 | import org.onosproject.net.PortNumber; |
| 42 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; |
| 44 | |
| 45 | import java.util.Set; |
| 46 | |
| 47 | import static com.google.common.base.Preconditions.checkNotNull; |
| 48 | |
| 49 | /** |
| 50 | * Implementation of the virtual network service. |
| 51 | */ |
| 52 | @Component(immediate = true) |
| 53 | @Service |
| 54 | public class VirtualNetworkManager |
| 55 | extends AbstractListenerManager<VirtualNetworkEvent, VirtualNetworkListener> |
| 56 | implements VirtualNetworkService, VirtualNetworkAdminService { |
| 57 | |
| 58 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 59 | |
| 60 | private static final String TENANT_NULL = "Tenant ID cannot be null"; |
| 61 | private static final String NETWORK_NULL = "Network ID cannot be null"; |
| 62 | private static final String DEVICE_NULL = "Device ID cannot be null"; |
| 63 | private static final String LINK_POINT_NULL = "Link end-point cannot be null"; |
| 64 | |
| 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected VirtualNetworkStore store; |
| 67 | |
| 68 | private VirtualNetworkStoreDelegate delegate = new InternalStoreDelegate(); |
| 69 | |
| 70 | // TODO: figure out how to coordinate "implementation" of a virtual network |
| 71 | |
| 72 | @Activate |
| 73 | protected void activate() { |
| 74 | store.setDelegate(delegate); |
| 75 | log.info("Started"); |
| 76 | } |
| 77 | |
| 78 | @Deactivate |
| 79 | protected void deactivate() { |
| 80 | store.unsetDelegate(delegate); |
| 81 | log.info("Stopped"); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void registerTenantId(TenantId tenantId) { |
| 86 | checkNotNull(tenantId, TENANT_NULL); |
| 87 | store.addTenantId(tenantId); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void unregisterTenantId(TenantId tenantId) { |
| 92 | checkNotNull(tenantId, TENANT_NULL); |
| 93 | store.removeTenantId(tenantId); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public Set<TenantId> getTenantIds() { |
| 98 | return store.getTenantIds(); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public VirtualNetwork createVirtualNetwork(TenantId tenantId) { |
| 103 | checkNotNull(tenantId, TENANT_NULL); |
| 104 | return store.addNetwork(tenantId); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void removeVirtualNetwork(NetworkId networkId) { |
| 109 | checkNotNull(networkId, NETWORK_NULL); |
| 110 | store.removeNetwork(networkId); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public VirtualDevice createVirtualDevice(NetworkId networkId, DeviceId deviceId) { |
| 115 | checkNotNull(networkId, NETWORK_NULL); |
| 116 | checkNotNull(deviceId, DEVICE_NULL); |
| 117 | return store.addDevice(networkId, deviceId); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void removeVirtualDevice(NetworkId networkId, DeviceId deviceId) { |
| 122 | checkNotNull(networkId, NETWORK_NULL); |
| 123 | checkNotNull(deviceId, DEVICE_NULL); |
| 124 | store.removeDevice(networkId, deviceId); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public VirtualLink createVirtualLink(NetworkId networkId, |
| 129 | ConnectPoint src, ConnectPoint dst, |
| 130 | TunnelId realizedBy) { |
| 131 | checkNotNull(networkId, NETWORK_NULL); |
| 132 | checkNotNull(src, LINK_POINT_NULL); |
| 133 | checkNotNull(dst, LINK_POINT_NULL); |
| 134 | checkNotNull(realizedBy, "Tunnel ID cannot be null"); |
| 135 | return store.addLink(networkId, src, dst, realizedBy); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void removeVirtualLink(NetworkId networkId, ConnectPoint src, ConnectPoint dst) { |
| 140 | checkNotNull(networkId, NETWORK_NULL); |
| 141 | checkNotNull(src, LINK_POINT_NULL); |
| 142 | checkNotNull(dst, LINK_POINT_NULL); |
| 143 | store.removeLink(networkId, src, dst); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public VirtualPort createVirtualPort(NetworkId networkId, DeviceId deviceId, |
| 148 | PortNumber portNumber, Port realizedBy) { |
| 149 | checkNotNull(networkId, NETWORK_NULL); |
| 150 | checkNotNull(deviceId, DEVICE_NULL); |
| 151 | checkNotNull(portNumber, "Port description cannot be null"); |
| 152 | return store.addPort(networkId, deviceId, portNumber, realizedBy); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void removeVirtualPort(NetworkId networkId, DeviceId deviceId, PortNumber portNumber) { |
| 157 | checkNotNull(networkId, NETWORK_NULL); |
| 158 | checkNotNull(deviceId, DEVICE_NULL); |
| 159 | checkNotNull(portNumber, "Port number cannot be null"); |
| 160 | store.removePort(networkId, deviceId, portNumber); |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public Set<VirtualNetwork> getVirtualNetworks(TenantId tenantId) { |
| 165 | checkNotNull(tenantId, TENANT_NULL); |
| 166 | return store.getNetworks(tenantId); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public Set<VirtualDevice> getVirtualDevices(NetworkId networkId) { |
| 171 | checkNotNull(networkId, NETWORK_NULL); |
| 172 | return store.getDevices(networkId); |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public Set<VirtualLink> getVirtualLinks(NetworkId networkId) { |
| 177 | checkNotNull(networkId, NETWORK_NULL); |
| 178 | return store.getLinks(networkId); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public Set<VirtualPort> getVirtualPorts(NetworkId networkId, DeviceId deviceId) { |
| 183 | checkNotNull(networkId, NETWORK_NULL); |
| 184 | checkNotNull(deviceId, DEVICE_NULL); |
| 185 | return store.getPorts(networkId, deviceId); |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public <T> T get(NetworkId networkId, Class<T> serviceClass) { |
| 190 | checkNotNull(networkId, NETWORK_NULL); |
| 191 | return null; |
| 192 | } |
| 193 | |
| 194 | // Auxiliary store delegate to receive notification about changes in |
| 195 | // the virtual network configuration store state - by the store itself. |
| 196 | private class InternalStoreDelegate implements VirtualNetworkStoreDelegate { |
| 197 | @Override |
| 198 | public void notify(VirtualNetworkEvent event) { |
| 199 | post(event); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | } |