blob: f32514334621a72160d7254ef72393a38d01bccf [file] [log] [blame]
Hyunsun Mooneaf75e62016-09-27 16:40:23 -07001/*
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 */
16package org.opencord.cordvtn.impl;
17
Hyunsun Moon5c143952016-10-19 18:34:46 -070018import com.google.common.collect.ImmutableSet;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
Hyunsun Moond5af96f2016-10-24 11:39:56 -070025import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070027import org.onosproject.event.ListenerRegistry;
28import org.onosproject.net.Host;
29import org.onosproject.net.HostId;
Hyunsun Moond5af96f2016-10-24 11:39:56 -070030import org.onosproject.net.config.NetworkConfigService;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070031import org.onosproject.net.host.HostService;
Hyunsun Moond5af96f2016-10-24 11:39:56 -070032import org.opencord.cordvtn.api.Constants;
33import org.opencord.cordvtn.api.config.CordVtnConfig;
34import org.opencord.cordvtn.api.config.OpenStackConfig;
35import org.opencord.cordvtn.api.config.XosConfig;
Hyunsun Moonfd5a24e2016-10-19 19:15:48 -070036import org.opencord.cordvtn.api.core.CordVtnAdminService;
37import org.opencord.cordvtn.api.core.CordVtnService;
38import org.opencord.cordvtn.api.core.CordVtnStore;
39import org.opencord.cordvtn.api.core.CordVtnStoreDelegate;
40import org.opencord.cordvtn.api.instance.Instance;
41import org.opencord.cordvtn.api.net.NetworkId;
42import org.opencord.cordvtn.api.net.NetworkService;
43import org.opencord.cordvtn.api.net.PortId;
44import org.opencord.cordvtn.api.net.ServiceNetwork;
45import org.opencord.cordvtn.api.net.ServiceNetworkService;
46import org.opencord.cordvtn.api.net.ServicePort;
47import org.opencord.cordvtn.api.net.SubnetId;
48import org.opencord.cordvtn.api.net.VtnNetwork;
49import org.opencord.cordvtn.api.net.VtnNetworkEvent;
50import org.opencord.cordvtn.api.net.VtnNetworkListener;
51import org.opencord.cordvtn.api.net.VtnPort;
Hyunsun Moond5af96f2016-10-24 11:39:56 -070052import org.opencord.cordvtn.impl.external.OpenStackNetworking;
53import org.opencord.cordvtn.impl.external.XosServiceNetworking;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070054import org.openstack4j.model.network.Network;
55import org.openstack4j.model.network.Port;
56import org.openstack4j.model.network.Subnet;
57import org.slf4j.Logger;
58
59import java.util.Objects;
60import java.util.Optional;
61import java.util.Set;
Hyunsun Moond2e4a462016-10-24 12:22:55 -070062import java.util.stream.Collectors;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070063
64import static com.google.common.base.Preconditions.checkNotNull;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070065import static org.slf4j.LoggerFactory.getLogger;
66
67/**
68 * Provides implementation of administering and interfacing VTN networks.
69 */
70@Component(immediate = true)
71@Service
72public class CordVtnManager extends ListenerRegistry<VtnNetworkEvent, VtnNetworkListener>
Hyunsun Moon5c143952016-10-19 18:34:46 -070073 implements CordVtnAdminService, CordVtnService, NetworkService,
74 ServiceNetworkService {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070075
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 Mooneaf75e62016-09-27 16:40:23 -070099 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 Moond5af96f2016-10-24 11:39:56 -0700108 protected NetworkConfigService configService;
109
110 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
111 protected CoreService coreService;
112
113 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700114 protected HostService hostService;
115
116 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
117 protected CordVtnStore store;
118
Hyunsun Moond5af96f2016-10-24 11:39:56 -0700119 private final CordVtnStoreDelegate delegate = new InternalCordVtnStoreDelegate();
120 private ApplicationId appId;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700121
122 @Activate
123 protected void activate() {
Hyunsun Moond5af96f2016-10-24 11:39:56 -0700124 appId = coreService.registerApplication(Constants.CORDVTN_APP_ID);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700125 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 Moond5af96f2016-10-24 11:39:56 -0700136 public void purgeStates() {
137 store.clear();
138 }
139
140 @Override
141 public void syncStates() {
142 syncNetwork();
143 syncServiceNetwork();
144 }
145
146 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700147 public void createServiceNetwork(ServiceNetwork serviceNet) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700148 checkNotNull(serviceNet, ERR_NULL_SERVICE_NET);
149 synchronized (this) {
Hyunsun Moon5c143952016-10-19 18:34:46 -0700150 Network network = store.network(serviceNet.id());
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700151 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 Moon5c143952016-10-19 18:34:46 -0700164 if (store.network(provider.id()) == null) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700165 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 Moon5c143952016-10-19 18:34:46 -0700176 public void updateServiceNetwork(ServiceNetwork serviceNet) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700177 checkNotNull(serviceNet, ERR_NULL_SERVICE_NET);
178 synchronized (this) {
Hyunsun Moon5c143952016-10-19 18:34:46 -0700179 VtnNetwork existing = store.vtnNetwork(serviceNet.id());
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700180 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 Moon5c143952016-10-19 18:34:46 -0700194 public void removeServiceNetwork(NetworkId netId) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700195 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 Moon5c143952016-10-19 18:34:46 -0700202 public void createServicePort(ServicePort servicePort) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700203 checkNotNull(servicePort, ERR_NULL_SERVICE_PORT);
204 synchronized (this) {
Hyunsun Moon5c143952016-10-19 18:34:46 -0700205 Port port = store.port(servicePort.id());
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700206 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 Moon5c143952016-10-19 18:34:46 -0700216 public void updateServicePort(ServicePort servicePort) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700217 checkNotNull(servicePort, ERR_NULL_SERVICE_PORT);
218 synchronized (this) {
Hyunsun Moon5c143952016-10-19 18:34:46 -0700219 VtnPort vtnPort = store.vtnPort(servicePort.id());
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700220 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 Moon5c143952016-10-19 18:34:46 -0700230 public void removeServicePort(PortId portId) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700231 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 Moon5c143952016-10-19 18:34:46 -0700262 if (store.network(NetworkId.of(port.getNetworkId())) == null) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700263 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 Moon5c143952016-10-19 18:34:46 -0700275 if (store.network(NetworkId.of(port.getNetworkId())) == null) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700276 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 Moon5c143952016-10-19 18:34:46 -0700292 removeServicePort(portId);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700293 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 Moon5c143952016-10-19 18:34:46 -0700302 if (store.network(NetworkId.of(subnet.getNetworkId())) == null) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700303 final String error = ERR_SYNC + subnet.getNetworkId() + ERR_NOT_FOUND;
304 throw new IllegalStateException(error);
305 }
306
Hyunsun Moon7331de82016-10-24 17:21:29 -0700307 Subnet existing = getSubnet(NetworkId.of(subnet.getNetworkId()));
308 if (existing != null && !Objects.equals(existing.getId(), subnet.getId())) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700309 // 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 Moon5c143952016-10-19 18:34:46 -0700322 if (store.network(NetworkId.of(subnet.getNetworkId())) == null) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700323 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 Moon5c143952016-10-19 18:34:46 -0700336 removeServiceNetwork(NetworkId.of(store.subnet(subnetId).getNetworkId()));
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700337 store.removeSubnet(subnetId);
338 log.info(String.format(MSG_SUBNET, REMOVED, subnetId));
339 }
340 }
341
342 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700343 public VtnNetwork vtnNetwork(NetworkId netId) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700344 checkNotNull(netId, ERR_NULL_NET_ID);
345
346 // return default VTN network if the network and subnet exist
Hyunsun Moon5c143952016-10-19 18:34:46 -0700347 VtnNetwork vtnNet = store.vtnNetwork(netId);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700348 return vtnNet == null ? getDefaultVtnNetwork(netId) : vtnNet;
349 }
350
351 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700352 public Set<VtnNetwork> vtnNetworks() {
Hyunsun Moond2e4a462016-10-24 12:22:55 -0700353 Set<VtnNetwork> vtnNetworks = networks().stream()
354 .map(net -> vtnNetwork(NetworkId.of(net.getId())))
355 .collect(Collectors.toSet());
356 return ImmutableSet.copyOf(vtnNetworks);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700357 }
358
359 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700360 public VtnPort vtnPort(PortId portId) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700361 checkNotNull(portId, ERR_NULL_PORT_ID);
362
363 // return default VTN port if the port exists
Hyunsun Moon5c143952016-10-19 18:34:46 -0700364 VtnPort vtnPort = store.vtnPort(portId);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700365 return vtnPort == null ? getDefaultPort(portId) : vtnPort;
366 }
367
368 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700369 public VtnPort vtnPort(String portName) {
370 Optional<Port> port = store.ports()
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700371 .stream()
372 .filter(p -> p.getId().contains(portName.substring(3)))
373 .findFirst();
374 if (!port.isPresent()) {
375 return null;
376 }
Hyunsun Moon5c143952016-10-19 18:34:46 -0700377 return vtnPort(PortId.of(port.get().getId()));
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700378 }
379
380 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700381 public Set<VtnPort> vtnPorts() {
Hyunsun Moond2e4a462016-10-24 12:22:55 -0700382 Set<VtnPort> vtnPorts = ports().stream()
383 .map(port -> vtnPort(PortId.of(port.getId())))
384 .collect(Collectors.toSet());
385 return ImmutableSet.copyOf(vtnPorts);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700386 }
387
388 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700389 public ServiceNetwork serviceNetwork(NetworkId netId) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700390 checkNotNull(netId, ERR_NULL_NET_ID);
Hyunsun Moon5c143952016-10-19 18:34:46 -0700391 return store.vtnNetwork(netId);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700392 }
393
394 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700395 public Set<ServiceNetwork> serviceNetworks() {
Hyunsun Moond2e4a462016-10-24 12:22:55 -0700396 return ImmutableSet.copyOf(store.vtnNetworks());
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700397 }
398
399 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700400 public ServicePort servicePort(PortId portId) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700401 checkNotNull(portId, ERR_NULL_PORT_ID);
Hyunsun Moon5c143952016-10-19 18:34:46 -0700402 return store.vtnPort(portId);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700403 }
404
405 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700406 public Set<ServicePort> servicePorts() {
Hyunsun Moond2e4a462016-10-24 12:22:55 -0700407 return ImmutableSet.copyOf(store.vtnPorts());
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700408 }
409
410 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700411 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 Moond2e4a462016-10-24 12:22:55 -0700418 return ImmutableSet.copyOf(store.networks());
Hyunsun Moon5c143952016-10-19 18:34:46 -0700419 }
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 Moond2e4a462016-10-24 12:22:55 -0700429 return ImmutableSet.copyOf(store.ports());
Hyunsun Moon5c143952016-10-19 18:34:46 -0700430 }
431
432 @Override
433 public Subnet subnet(SubnetId subnetId) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700434 checkNotNull(subnetId, ERR_NULL_SUBNET_ID);
Hyunsun Moon5c143952016-10-19 18:34:46 -0700435 return store.subnet(subnetId);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700436 }
437
438 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700439 public Set<Subnet> subnets() {
Hyunsun Moond2e4a462016-10-24 12:22:55 -0700440 return ImmutableSet.copyOf(store.subnets());
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700441 }
442
Hyunsun Moond5af96f2016-10-24 11:39:56 -0700443 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 Moon5c143952016-10-19 18:34:46 -0700481 private Instance getInstance(PortId portId) {
482 VtnPort vtnPort = vtnPort(portId);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700483 if (vtnPort == null) {
484 final String error = "Failed to build VTN port for " + portId.id();
485 throw new IllegalStateException(error);
486 }
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700487 Host host = hostService.getHost(HostId.hostId(vtnPort.mac()));
488 if (host == null) {
489 return null;
490 }
491 return Instance.of(host);
492 }
493
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700494 private VtnNetwork getDefaultVtnNetwork(NetworkId netId) {
Hyunsun Moon5c143952016-10-19 18:34:46 -0700495 Network network = network(netId);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700496 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 Moon5c143952016-10-19 18:34:46 -0700504 Port port = port(portId);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700505 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 Moon5c143952016-10-19 18:34:46 -0700513 Optional<Subnet> subnet = subnets().stream()
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700514 .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}