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