Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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.opencord.cordvtn.impl; |
| 17 | |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -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; |
Hyunsun Moon | d5af96f | 2016-10-24 11:39:56 -0700 | [diff] [blame] | 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 27 | import org.onosproject.event.ListenerRegistry; |
| 28 | import org.onosproject.net.Host; |
| 29 | import org.onosproject.net.HostId; |
Hyunsun Moon | d5af96f | 2016-10-24 11:39:56 -0700 | [diff] [blame] | 30 | import org.onosproject.net.config.NetworkConfigService; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 31 | import org.onosproject.net.host.HostService; |
Hyunsun Moon | d5af96f | 2016-10-24 11:39:56 -0700 | [diff] [blame] | 32 | import org.opencord.cordvtn.api.Constants; |
| 33 | import org.opencord.cordvtn.api.config.CordVtnConfig; |
| 34 | import org.opencord.cordvtn.api.config.OpenStackConfig; |
| 35 | import org.opencord.cordvtn.api.config.XosConfig; |
Hyunsun Moon | fd5a24e | 2016-10-19 19:15:48 -0700 | [diff] [blame] | 36 | import org.opencord.cordvtn.api.core.CordVtnAdminService; |
| 37 | import org.opencord.cordvtn.api.core.CordVtnService; |
| 38 | import org.opencord.cordvtn.api.core.CordVtnStore; |
| 39 | import org.opencord.cordvtn.api.core.CordVtnStoreDelegate; |
| 40 | import org.opencord.cordvtn.api.instance.Instance; |
| 41 | import org.opencord.cordvtn.api.net.NetworkId; |
| 42 | import org.opencord.cordvtn.api.net.NetworkService; |
| 43 | import org.opencord.cordvtn.api.net.PortId; |
| 44 | import org.opencord.cordvtn.api.net.ServiceNetwork; |
| 45 | import org.opencord.cordvtn.api.net.ServiceNetworkService; |
| 46 | import org.opencord.cordvtn.api.net.ServicePort; |
| 47 | import org.opencord.cordvtn.api.net.SubnetId; |
| 48 | import org.opencord.cordvtn.api.net.VtnNetwork; |
| 49 | import org.opencord.cordvtn.api.net.VtnNetworkEvent; |
| 50 | import org.opencord.cordvtn.api.net.VtnNetworkListener; |
| 51 | import org.opencord.cordvtn.api.net.VtnPort; |
Hyunsun Moon | d5af96f | 2016-10-24 11:39:56 -0700 | [diff] [blame] | 52 | import org.opencord.cordvtn.impl.external.OpenStackNetworking; |
| 53 | import org.opencord.cordvtn.impl.external.XosServiceNetworking; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 54 | import org.openstack4j.model.network.Network; |
| 55 | import org.openstack4j.model.network.Port; |
| 56 | import org.openstack4j.model.network.Subnet; |
| 57 | import org.slf4j.Logger; |
| 58 | |
| 59 | import java.util.Objects; |
| 60 | import java.util.Optional; |
| 61 | import java.util.Set; |
Hyunsun Moon | d2e4a46 | 2016-10-24 12:22:55 -0700 | [diff] [blame^] | 62 | import java.util.stream.Collectors; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 63 | |
| 64 | import static com.google.common.base.Preconditions.checkNotNull; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 65 | import static org.slf4j.LoggerFactory.getLogger; |
| 66 | |
| 67 | /** |
| 68 | * Provides implementation of administering and interfacing VTN networks. |
| 69 | */ |
| 70 | @Component(immediate = true) |
| 71 | @Service |
| 72 | public class CordVtnManager extends ListenerRegistry<VtnNetworkEvent, VtnNetworkListener> |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 73 | implements CordVtnAdminService, CordVtnService, NetworkService, |
| 74 | ServiceNetworkService { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 75 | |
| 76 | protected final Logger log = getLogger(getClass()); |
| 77 | |
| 78 | private static final String MSG_SERVICE_NET = "VTN network %s %s"; |
| 79 | private static final String MSG_SERVICE_PORT = "VTN port %s %s"; |
| 80 | private static final String MSG_NET = "Network %s %s"; |
| 81 | private static final String MSG_PORT = "Port %s %s"; |
| 82 | private static final String MSG_SUBNET = "Subnet %s %s"; |
| 83 | |
| 84 | private static final String CREATED = "created"; |
| 85 | private static final String UPDATED = "updated"; |
| 86 | private static final String REMOVED = "removed"; |
| 87 | |
| 88 | private static final String ERR_NULL_SERVICE_PORT = "Service port cannot be null"; |
| 89 | private static final String ERR_NULL_SERVICE_NET = "Service network cannot be null"; |
| 90 | private static final String ERR_NULL_PORT = "Port cannot be null"; |
| 91 | private static final String ERR_NULL_NET = "Network cannot be null"; |
| 92 | private static final String ERR_NULL_SUBNET = "Subnet cannot be null"; |
| 93 | private static final String ERR_NULL_PORT_ID = "Port ID cannot be null"; |
| 94 | private static final String ERR_NULL_NET_ID = "Network ID cannot be null"; |
| 95 | private static final String ERR_NULL_SUBNET_ID = "Subnet ID cannot be null"; |
| 96 | |
| 97 | private static final String ERR_SYNC = "VTN store is out of sync: "; |
| 98 | private static final String ERR_NOT_FOUND = " does not exist"; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 99 | private static final String ERR_IN_USE_PORT = "There are ports still in use on the network %s"; |
| 100 | private static final String ERR_SUBNET_DUPLICATE = "Subnet already exists for network %s"; |
| 101 | |
| 102 | private static final String PORT = "port "; |
| 103 | private static final String NETWORK = "network "; |
| 104 | private static final String SUBNET = "subnet for "; |
| 105 | private static final String PROVIDER = "provider "; |
| 106 | |
| 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | d5af96f | 2016-10-24 11:39:56 -0700 | [diff] [blame] | 108 | protected NetworkConfigService configService; |
| 109 | |
| 110 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 111 | protected CoreService coreService; |
| 112 | |
| 113 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 114 | protected HostService hostService; |
| 115 | |
| 116 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 117 | protected CordVtnStore store; |
| 118 | |
Hyunsun Moon | d5af96f | 2016-10-24 11:39:56 -0700 | [diff] [blame] | 119 | private final CordVtnStoreDelegate delegate = new InternalCordVtnStoreDelegate(); |
| 120 | private ApplicationId appId; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 121 | |
| 122 | @Activate |
| 123 | protected void activate() { |
Hyunsun Moon | d5af96f | 2016-10-24 11:39:56 -0700 | [diff] [blame] | 124 | appId = coreService.registerApplication(Constants.CORDVTN_APP_ID); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 125 | store.setDelegate(delegate); |
| 126 | log.info("Started"); |
| 127 | } |
| 128 | |
| 129 | @Deactivate |
| 130 | protected void deactivate() { |
| 131 | store.unsetDelegate(delegate); |
| 132 | log.info("Stopped"); |
| 133 | } |
| 134 | |
| 135 | @Override |
Hyunsun Moon | d5af96f | 2016-10-24 11:39:56 -0700 | [diff] [blame] | 136 | public void purgeStates() { |
| 137 | store.clear(); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void syncStates() { |
| 142 | syncNetwork(); |
| 143 | syncServiceNetwork(); |
| 144 | } |
| 145 | |
| 146 | @Override |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 147 | public void createServiceNetwork(ServiceNetwork serviceNet) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 148 | checkNotNull(serviceNet, ERR_NULL_SERVICE_NET); |
| 149 | synchronized (this) { |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 150 | Network network = store.network(serviceNet.id()); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 151 | if (network == null) { |
| 152 | final String error = ERR_SYNC + NETWORK + serviceNet.id() + ERR_NOT_FOUND; |
| 153 | throw new IllegalStateException(error); |
| 154 | } |
| 155 | |
| 156 | Subnet subnet = getSubnet(serviceNet.id()); |
| 157 | if (subnet == null) { |
| 158 | final String error = ERR_SYNC + SUBNET + serviceNet.id() + ERR_NOT_FOUND; |
| 159 | throw new IllegalStateException(error); |
| 160 | } |
| 161 | |
| 162 | // TODO check VTN network instead of network |
| 163 | serviceNet.providers().stream().forEach(provider -> { |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 164 | if (store.network(provider.id()) == null) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 165 | final String error = ERR_SYNC + PROVIDER + provider.id() + ERR_NOT_FOUND; |
| 166 | throw new IllegalStateException(error); |
| 167 | } |
| 168 | }); |
| 169 | |
| 170 | store.createVtnNetwork(VtnNetwork.of(network, subnet, serviceNet)); |
| 171 | log.info(String.format(MSG_SERVICE_NET, CREATED, serviceNet.id())); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | @Override |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 176 | public void updateServiceNetwork(ServiceNetwork serviceNet) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 177 | checkNotNull(serviceNet, ERR_NULL_SERVICE_NET); |
| 178 | synchronized (this) { |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 179 | VtnNetwork existing = store.vtnNetwork(serviceNet.id()); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 180 | if (existing == null) { |
| 181 | final String error = ERR_SYNC + NETWORK + serviceNet.id() + ERR_NOT_FOUND; |
| 182 | throw new IllegalStateException(error); |
| 183 | } |
| 184 | // only providers update is allowed |
| 185 | VtnNetwork updated = VtnNetwork.builder(existing) |
| 186 | .providers(serviceNet.providers()) |
| 187 | .build(); |
| 188 | store.updateVtnNetwork(updated); |
| 189 | log.info(String.format(MSG_SERVICE_NET, UPDATED, serviceNet.id())); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | @Override |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 194 | public void removeServiceNetwork(NetworkId netId) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 195 | checkNotNull(netId, ERR_NULL_NET_ID); |
| 196 | // TODO check if the network still exists? |
| 197 | store.removeVtnNetwork(netId); |
| 198 | log.info(String.format(MSG_SERVICE_NET, REMOVED, netId)); |
| 199 | } |
| 200 | |
| 201 | @Override |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 202 | public void createServicePort(ServicePort servicePort) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 203 | checkNotNull(servicePort, ERR_NULL_SERVICE_PORT); |
| 204 | synchronized (this) { |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 205 | Port port = store.port(servicePort.id()); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 206 | if (port == null) { |
| 207 | final String error = ERR_SYNC + PORT + servicePort.id() + ERR_NOT_FOUND; |
| 208 | throw new IllegalStateException(error); |
| 209 | } |
| 210 | store.createVtnPort(VtnPort.of(port, servicePort)); |
| 211 | log.info(String.format(MSG_SERVICE_PORT, CREATED, servicePort.id())); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | @Override |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 216 | public void updateServicePort(ServicePort servicePort) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 217 | checkNotNull(servicePort, ERR_NULL_SERVICE_PORT); |
| 218 | synchronized (this) { |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 219 | VtnPort vtnPort = store.vtnPort(servicePort.id()); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 220 | if (vtnPort == null) { |
| 221 | final String error = ERR_SYNC + PORT + servicePort.id() + ERR_NOT_FOUND; |
| 222 | throw new IllegalStateException(error); |
| 223 | } |
| 224 | store.updateVtnPort(VtnPort.of(vtnPort, servicePort)); |
| 225 | log.info(String.format(MSG_SERVICE_PORT, UPDATED, servicePort.id())); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | @Override |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 230 | public void removeServicePort(PortId portId) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 231 | checkNotNull(portId, ERR_NULL_PORT_ID); |
| 232 | store.removeVtnPort(portId); |
| 233 | log.info(String.format(MSG_SERVICE_PORT, REMOVED, portId)); |
| 234 | } |
| 235 | |
| 236 | @Override |
| 237 | public void createNetwork(Network network) { |
| 238 | checkNotNull(network, ERR_NULL_NET); |
| 239 | store.createNetwork(network); |
| 240 | log.info(String.format(MSG_NET, CREATED, network.getId())); |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public void updateNetwork(Network network) { |
| 245 | checkNotNull(network, ERR_NULL_NET); |
| 246 | store.updateNetwork(network); |
| 247 | log.info(String.format(MSG_NET, UPDATED, network.getId())); |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public void removeNetwork(NetworkId netId) { |
| 252 | checkNotNull(netId, ERR_NULL_NET_ID); |
| 253 | // FIXME Neutron removes network anyway even if there's an exception here |
| 254 | store.removeNetwork(netId); |
| 255 | log.info(String.format(MSG_NET, REMOVED, netId)); |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public void createPort(Port port) { |
| 260 | checkNotNull(port, ERR_NULL_PORT); |
| 261 | synchronized (this) { |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 262 | if (store.network(NetworkId.of(port.getNetworkId())) == null) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 263 | final String error = ERR_SYNC + port.getNetworkId() + ERR_NOT_FOUND; |
| 264 | throw new IllegalStateException(error); |
| 265 | } |
| 266 | store.createPort(port); |
| 267 | log.info(String.format(MSG_PORT, CREATED, port.getId())); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public void updatePort(Port port) { |
| 273 | checkNotNull(port, ERR_NULL_PORT); |
| 274 | synchronized (this) { |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 275 | if (store.network(NetworkId.of(port.getNetworkId())) == null) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 276 | final String error = ERR_SYNC + port.getNetworkId() + ERR_NOT_FOUND; |
| 277 | throw new IllegalStateException(error); |
| 278 | } |
| 279 | store.updatePort(port); |
| 280 | log.info(String.format(MSG_PORT, UPDATED, port.getId())); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | @Override |
| 285 | public void removePort(PortId portId) { |
| 286 | checkNotNull(portId, ERR_NULL_PORT_ID); |
| 287 | synchronized (this) { |
| 288 | if (getInstance(portId) != null) { |
| 289 | final String error = String.format(ERR_IN_USE_PORT, portId); |
| 290 | throw new IllegalStateException(error); |
| 291 | } |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 292 | removeServicePort(portId); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 293 | store.removePort(portId); |
| 294 | log.info(String.format(MSG_PORT, REMOVED, portId)); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public void createSubnet(Subnet subnet) { |
| 300 | checkNotNull(subnet, ERR_NULL_SUBNET); |
| 301 | synchronized (this) { |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 302 | if (store.network(NetworkId.of(subnet.getNetworkId())) == null) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 303 | final String error = ERR_SYNC + subnet.getNetworkId() + ERR_NOT_FOUND; |
| 304 | throw new IllegalStateException(error); |
| 305 | } |
| 306 | |
Hyunsun Moon | 7331de8 | 2016-10-24 17:21:29 -0700 | [diff] [blame] | 307 | Subnet existing = getSubnet(NetworkId.of(subnet.getNetworkId())); |
| 308 | if (existing != null && !Objects.equals(existing.getId(), subnet.getId())) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 309 | // CORD does not allow multiple subnets for a network |
| 310 | final String error = String.format(ERR_SUBNET_DUPLICATE, subnet.getNetworkId()); |
| 311 | throw new IllegalStateException(error); |
| 312 | } |
| 313 | store.createSubnet(subnet); |
| 314 | log.info(String.format(MSG_SUBNET, CREATED, subnet.getId())); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public void updateSubnet(Subnet subnet) { |
| 320 | checkNotNull(subnet, ERR_NULL_SUBNET); |
| 321 | synchronized (this) { |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 322 | if (store.network(NetworkId.of(subnet.getNetworkId())) == null) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 323 | final String error = ERR_SYNC + subnet.getNetworkId() + ERR_NOT_FOUND; |
| 324 | throw new IllegalStateException(error); |
| 325 | } |
| 326 | store.updateSubnet(subnet); |
| 327 | log.info(String.format(MSG_SUBNET, UPDATED, subnet.getId())); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | public void removeSubnet(SubnetId subnetId) { |
| 333 | checkNotNull(subnetId, ERR_NULL_SUBNET_ID); |
| 334 | // FIXME Neutron removes network anyway even if there's an exception here |
| 335 | synchronized (this) { |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 336 | removeServiceNetwork(NetworkId.of(store.subnet(subnetId).getNetworkId())); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 337 | store.removeSubnet(subnetId); |
| 338 | log.info(String.format(MSG_SUBNET, REMOVED, subnetId)); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | @Override |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 343 | public VtnNetwork vtnNetwork(NetworkId netId) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 344 | checkNotNull(netId, ERR_NULL_NET_ID); |
| 345 | |
| 346 | // return default VTN network if the network and subnet exist |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 347 | VtnNetwork vtnNet = store.vtnNetwork(netId); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 348 | return vtnNet == null ? getDefaultVtnNetwork(netId) : vtnNet; |
| 349 | } |
| 350 | |
| 351 | @Override |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 352 | public Set<VtnNetwork> vtnNetworks() { |
Hyunsun Moon | d2e4a46 | 2016-10-24 12:22:55 -0700 | [diff] [blame^] | 353 | Set<VtnNetwork> vtnNetworks = networks().stream() |
| 354 | .map(net -> vtnNetwork(NetworkId.of(net.getId()))) |
| 355 | .collect(Collectors.toSet()); |
| 356 | return ImmutableSet.copyOf(vtnNetworks); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | @Override |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 360 | public VtnPort vtnPort(PortId portId) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 361 | checkNotNull(portId, ERR_NULL_PORT_ID); |
| 362 | |
| 363 | // return default VTN port if the port exists |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 364 | VtnPort vtnPort = store.vtnPort(portId); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 365 | return vtnPort == null ? getDefaultPort(portId) : vtnPort; |
| 366 | } |
| 367 | |
| 368 | @Override |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 369 | public VtnPort vtnPort(String portName) { |
| 370 | Optional<Port> port = store.ports() |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 371 | .stream() |
| 372 | .filter(p -> p.getId().contains(portName.substring(3))) |
| 373 | .findFirst(); |
| 374 | if (!port.isPresent()) { |
| 375 | return null; |
| 376 | } |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 377 | return vtnPort(PortId.of(port.get().getId())); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | @Override |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 381 | public Set<VtnPort> vtnPorts() { |
Hyunsun Moon | d2e4a46 | 2016-10-24 12:22:55 -0700 | [diff] [blame^] | 382 | Set<VtnPort> vtnPorts = ports().stream() |
| 383 | .map(port -> vtnPort(PortId.of(port.getId()))) |
| 384 | .collect(Collectors.toSet()); |
| 385 | return ImmutableSet.copyOf(vtnPorts); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | @Override |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 389 | public ServiceNetwork serviceNetwork(NetworkId netId) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 390 | checkNotNull(netId, ERR_NULL_NET_ID); |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 391 | return store.vtnNetwork(netId); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | @Override |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 395 | public Set<ServiceNetwork> serviceNetworks() { |
Hyunsun Moon | d2e4a46 | 2016-10-24 12:22:55 -0700 | [diff] [blame^] | 396 | return ImmutableSet.copyOf(store.vtnNetworks()); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | @Override |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 400 | public ServicePort servicePort(PortId portId) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 401 | checkNotNull(portId, ERR_NULL_PORT_ID); |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 402 | return store.vtnPort(portId); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | @Override |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 406 | public Set<ServicePort> servicePorts() { |
Hyunsun Moon | d2e4a46 | 2016-10-24 12:22:55 -0700 | [diff] [blame^] | 407 | return ImmutableSet.copyOf(store.vtnPorts()); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | @Override |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 411 | public Network network(NetworkId netId) { |
| 412 | checkNotNull(netId, ERR_NULL_NET_ID); |
| 413 | return store.network(netId); |
| 414 | } |
| 415 | |
| 416 | @Override |
| 417 | public Set<Network> networks() { |
Hyunsun Moon | d2e4a46 | 2016-10-24 12:22:55 -0700 | [diff] [blame^] | 418 | return ImmutableSet.copyOf(store.networks()); |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | @Override |
| 422 | public Port port(PortId portId) { |
| 423 | checkNotNull(portId, ERR_NULL_PORT_ID); |
| 424 | return store.port(portId); |
| 425 | } |
| 426 | |
| 427 | @Override |
| 428 | public Set<Port> ports() { |
Hyunsun Moon | d2e4a46 | 2016-10-24 12:22:55 -0700 | [diff] [blame^] | 429 | return ImmutableSet.copyOf(store.ports()); |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | @Override |
| 433 | public Subnet subnet(SubnetId subnetId) { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 434 | checkNotNull(subnetId, ERR_NULL_SUBNET_ID); |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 435 | return store.subnet(subnetId); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | @Override |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 439 | public Set<Subnet> subnets() { |
Hyunsun Moon | d2e4a46 | 2016-10-24 12:22:55 -0700 | [diff] [blame^] | 440 | return ImmutableSet.copyOf(store.subnets()); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Hyunsun Moon | d5af96f | 2016-10-24 11:39:56 -0700 | [diff] [blame] | 443 | private void syncNetwork() { |
| 444 | CordVtnConfig config = configService.getConfig(appId, CordVtnConfig.class); |
| 445 | if (config == null) { |
| 446 | final String error = "Failed to read network configurations"; |
| 447 | throw new IllegalArgumentException(error); |
| 448 | } |
| 449 | |
| 450 | OpenStackConfig osConfig = config.openStackConfig(); |
| 451 | NetworkService netService = OpenStackNetworking.builder() |
| 452 | .endpoint(osConfig.endpoint()) |
| 453 | .tenant(osConfig.tenant()) |
| 454 | .user(osConfig.user()) |
| 455 | .password(osConfig.password()) |
| 456 | .build(); |
| 457 | |
| 458 | netService.networks().forEach(this::createNetwork); |
| 459 | netService.subnets().forEach(this::createSubnet); |
| 460 | netService.ports().forEach(this::createPort); |
| 461 | } |
| 462 | |
| 463 | private void syncServiceNetwork() { |
| 464 | CordVtnConfig config = configService.getConfig(appId, CordVtnConfig.class); |
| 465 | if (config == null) { |
| 466 | final String error = "Failed to read network configurations"; |
| 467 | throw new IllegalArgumentException(error); |
| 468 | } |
| 469 | |
| 470 | XosConfig xosConfig = config.xosConfig(); |
| 471 | ServiceNetworkService snetService = XosServiceNetworking.builder() |
| 472 | .endpoint(xosConfig.endpoint()) |
| 473 | .user(xosConfig.user()) |
| 474 | .password(xosConfig.password()) |
| 475 | .build(); |
| 476 | |
| 477 | snetService.serviceNetworks().forEach(this::createServiceNetwork); |
| 478 | snetService.servicePorts().forEach(this::createServicePort); |
| 479 | } |
| 480 | |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 481 | private Instance getInstance(PortId portId) { |
| 482 | VtnPort vtnPort = vtnPort(portId); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 483 | if (vtnPort == null) { |
| 484 | final String error = "Failed to build VTN port for " + portId.id(); |
| 485 | throw new IllegalStateException(error); |
| 486 | } |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 487 | Host host = hostService.getHost(HostId.hostId(vtnPort.mac())); |
| 488 | if (host == null) { |
| 489 | return null; |
| 490 | } |
| 491 | return Instance.of(host); |
| 492 | } |
| 493 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 494 | private VtnNetwork getDefaultVtnNetwork(NetworkId netId) { |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 495 | Network network = network(netId); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 496 | Subnet subnet = getSubnet(netId); |
| 497 | if (network == null || subnet == null) { |
| 498 | return null; |
| 499 | } |
| 500 | return VtnNetwork.of(network, subnet, null); |
| 501 | } |
| 502 | |
| 503 | private VtnPort getDefaultPort(PortId portId) { |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 504 | Port port = port(portId); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 505 | if (port == null) { |
| 506 | return null; |
| 507 | } |
| 508 | return VtnPort.of(port, null); |
| 509 | } |
| 510 | |
| 511 | private Subnet getSubnet(NetworkId netId) { |
| 512 | // TODO fix networking-onos to send Network UPDATE when subnet created |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 513 | Optional<Subnet> subnet = subnets().stream() |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 514 | .filter(s -> Objects.equals(s.getNetworkId(), netId.id())) |
| 515 | .findFirst(); |
| 516 | return subnet.orElse(null); |
| 517 | } |
| 518 | |
| 519 | private class InternalCordVtnStoreDelegate implements CordVtnStoreDelegate { |
| 520 | |
| 521 | @Override |
| 522 | public void notify(VtnNetworkEvent event) { |
| 523 | if (event != null) { |
| 524 | log.trace("send service network event {}", event); |
| 525 | process(event); |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | } |