blob: f3dd1f4480545af9e2246bf9526ef27b9fdcd5de [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 Moon0984cbd2016-12-01 17:34:11 -080030import org.onosproject.net.config.ConfigFactory;
31import org.onosproject.net.config.NetworkConfigRegistry;
Hyunsun Moond5af96f2016-10-24 11:39:56 -070032import org.onosproject.net.config.NetworkConfigService;
Hyunsun Moon0984cbd2016-12-01 17:34:11 -080033import org.onosproject.net.config.basics.SubjectFactories;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070034import org.onosproject.net.host.HostService;
Hyunsun Moond5af96f2016-10-24 11:39:56 -070035import org.opencord.cordvtn.api.Constants;
36import org.opencord.cordvtn.api.config.CordVtnConfig;
37import org.opencord.cordvtn.api.config.OpenStackConfig;
38import org.opencord.cordvtn.api.config.XosConfig;
Hyunsun Moonfd5a24e2016-10-19 19:15:48 -070039import org.opencord.cordvtn.api.core.CordVtnAdminService;
40import org.opencord.cordvtn.api.core.CordVtnService;
41import org.opencord.cordvtn.api.core.CordVtnStore;
42import org.opencord.cordvtn.api.core.CordVtnStoreDelegate;
43import org.opencord.cordvtn.api.instance.Instance;
44import org.opencord.cordvtn.api.net.NetworkId;
45import org.opencord.cordvtn.api.net.NetworkService;
46import org.opencord.cordvtn.api.net.PortId;
47import org.opencord.cordvtn.api.net.ServiceNetwork;
48import org.opencord.cordvtn.api.net.ServiceNetworkService;
49import org.opencord.cordvtn.api.net.ServicePort;
50import org.opencord.cordvtn.api.net.SubnetId;
51import org.opencord.cordvtn.api.net.VtnNetwork;
52import org.opencord.cordvtn.api.net.VtnNetworkEvent;
53import org.opencord.cordvtn.api.net.VtnNetworkListener;
54import org.opencord.cordvtn.api.net.VtnPort;
Hyunsun Moond5af96f2016-10-24 11:39:56 -070055import org.opencord.cordvtn.impl.external.OpenStackNetworking;
56import org.opencord.cordvtn.impl.external.XosServiceNetworking;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070057import org.openstack4j.model.network.Network;
58import org.openstack4j.model.network.Port;
59import org.openstack4j.model.network.Subnet;
60import org.slf4j.Logger;
61
62import java.util.Objects;
63import java.util.Optional;
64import java.util.Set;
Hyunsun Moond2e4a462016-10-24 12:22:55 -070065import java.util.stream.Collectors;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070066
67import static com.google.common.base.Preconditions.checkNotNull;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070068import static org.slf4j.LoggerFactory.getLogger;
69
70/**
71 * Provides implementation of administering and interfacing VTN networks.
72 */
73@Component(immediate = true)
74@Service
75public class CordVtnManager extends ListenerRegistry<VtnNetworkEvent, VtnNetworkListener>
Hyunsun Moon5c143952016-10-19 18:34:46 -070076 implements CordVtnAdminService, CordVtnService, NetworkService,
77 ServiceNetworkService {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070078
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 Mooneaf75e62016-09-27 16:40:23 -0700102 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 Moond5af96f2016-10-24 11:39:56 -0700111 protected NetworkConfigService configService;
112
113 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moon0984cbd2016-12-01 17:34:11 -0800114 protected NetworkConfigRegistry configRegistry;
115
116 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moond5af96f2016-10-24 11:39:56 -0700117 protected CoreService coreService;
118
119 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700120 protected HostService hostService;
121
122 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
123 protected CordVtnStore store;
124
Hyunsun Moon0984cbd2016-12-01 17:34:11 -0800125 // 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 Moond5af96f2016-10-24 11:39:56 -0700136 private final CordVtnStoreDelegate delegate = new InternalCordVtnStoreDelegate();
137 private ApplicationId appId;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700138
139 @Activate
140 protected void activate() {
Hyunsun Moond5af96f2016-10-24 11:39:56 -0700141 appId = coreService.registerApplication(Constants.CORDVTN_APP_ID);
Hyunsun Moon0984cbd2016-12-01 17:34:11 -0800142 configRegistry.registerConfigFactory(configFactory);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700143 store.setDelegate(delegate);
144 log.info("Started");
145 }
146
147 @Deactivate
148 protected void deactivate() {
Hyunsun Moon0984cbd2016-12-01 17:34:11 -0800149 configRegistry.unregisterConfigFactory(configFactory);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700150 store.unsetDelegate(delegate);
151 log.info("Stopped");
152 }
153
154 @Override
Hyunsun Moond5af96f2016-10-24 11:39:56 -0700155 public void purgeStates() {
156 store.clear();
157 }
158
159 @Override
160 public void syncStates() {
161 syncNetwork();
162 syncServiceNetwork();
163 }
164
165 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700166 public void createServiceNetwork(ServiceNetwork serviceNet) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700167 checkNotNull(serviceNet, ERR_NULL_SERVICE_NET);
168 synchronized (this) {
Hyunsun Moon5c143952016-10-19 18:34:46 -0700169 Network network = store.network(serviceNet.id());
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700170 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 Moon5c143952016-10-19 18:34:46 -0700183 if (store.network(provider.id()) == null) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700184 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 Moon5c143952016-10-19 18:34:46 -0700195 public void updateServiceNetwork(ServiceNetwork serviceNet) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700196 checkNotNull(serviceNet, ERR_NULL_SERVICE_NET);
197 synchronized (this) {
Hyunsun Moon5c143952016-10-19 18:34:46 -0700198 VtnNetwork existing = store.vtnNetwork(serviceNet.id());
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700199 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 Moon5c143952016-10-19 18:34:46 -0700213 public void removeServiceNetwork(NetworkId netId) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700214 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 Moon5c143952016-10-19 18:34:46 -0700221 public void createServicePort(ServicePort servicePort) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700222 checkNotNull(servicePort, ERR_NULL_SERVICE_PORT);
223 synchronized (this) {
Hyunsun Moon5c143952016-10-19 18:34:46 -0700224 Port port = store.port(servicePort.id());
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700225 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 Moon5c143952016-10-19 18:34:46 -0700235 public void updateServicePort(ServicePort servicePort) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700236 checkNotNull(servicePort, ERR_NULL_SERVICE_PORT);
237 synchronized (this) {
Hyunsun Moon5c143952016-10-19 18:34:46 -0700238 VtnPort vtnPort = store.vtnPort(servicePort.id());
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700239 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 Moon5c143952016-10-19 18:34:46 -0700249 public void removeServicePort(PortId portId) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700250 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 Moon5c143952016-10-19 18:34:46 -0700281 if (store.network(NetworkId.of(port.getNetworkId())) == null) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700282 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 Moon5c143952016-10-19 18:34:46 -0700294 if (store.network(NetworkId.of(port.getNetworkId())) == null) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700295 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 Moon5c143952016-10-19 18:34:46 -0700311 removeServicePort(portId);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700312 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 Moon5c143952016-10-19 18:34:46 -0700321 if (store.network(NetworkId.of(subnet.getNetworkId())) == null) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700322 final String error = ERR_SYNC + subnet.getNetworkId() + ERR_NOT_FOUND;
323 throw new IllegalStateException(error);
324 }
325
Hyunsun Moon7331de82016-10-24 17:21:29 -0700326 Subnet existing = getSubnet(NetworkId.of(subnet.getNetworkId()));
327 if (existing != null && !Objects.equals(existing.getId(), subnet.getId())) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700328 // 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 Moonaf477ac2016-11-09 12:56:00 -0800333 // FIXME update the network as well with the new subnet
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700334 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 Moon5c143952016-10-19 18:34:46 -0700342 if (store.network(NetworkId.of(subnet.getNetworkId())) == null) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700343 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 Moon5c143952016-10-19 18:34:46 -0700356 removeServiceNetwork(NetworkId.of(store.subnet(subnetId).getNetworkId()));
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700357 store.removeSubnet(subnetId);
358 log.info(String.format(MSG_SUBNET, REMOVED, subnetId));
359 }
360 }
361
362 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700363 public VtnNetwork vtnNetwork(NetworkId netId) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700364 checkNotNull(netId, ERR_NULL_NET_ID);
365
366 // return default VTN network if the network and subnet exist
Hyunsun Moon5c143952016-10-19 18:34:46 -0700367 VtnNetwork vtnNet = store.vtnNetwork(netId);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700368 return vtnNet == null ? getDefaultVtnNetwork(netId) : vtnNet;
369 }
370
371 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700372 public Set<VtnNetwork> vtnNetworks() {
Hyunsun Moond2e4a462016-10-24 12:22:55 -0700373 Set<VtnNetwork> vtnNetworks = networks().stream()
Hyunsun Moonaf477ac2016-11-09 12:56:00 -0800374 .filter(net -> vtnNetwork(NetworkId.of(net.getId())) != null)
Hyunsun Moond2e4a462016-10-24 12:22:55 -0700375 .map(net -> vtnNetwork(NetworkId.of(net.getId())))
376 .collect(Collectors.toSet());
377 return ImmutableSet.copyOf(vtnNetworks);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700378 }
379
380 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700381 public VtnPort vtnPort(PortId portId) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700382 checkNotNull(portId, ERR_NULL_PORT_ID);
383
384 // return default VTN port if the port exists
Hyunsun Moon5c143952016-10-19 18:34:46 -0700385 VtnPort vtnPort = store.vtnPort(portId);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700386 return vtnPort == null ? getDefaultPort(portId) : vtnPort;
387 }
388
389 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700390 public VtnPort vtnPort(String portName) {
391 Optional<Port> port = store.ports()
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700392 .stream()
393 .filter(p -> p.getId().contains(portName.substring(3)))
394 .findFirst();
395 if (!port.isPresent()) {
396 return null;
397 }
Hyunsun Moon5c143952016-10-19 18:34:46 -0700398 return vtnPort(PortId.of(port.get().getId()));
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700399 }
400
401 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700402 public Set<VtnPort> vtnPorts() {
Hyunsun Moond2e4a462016-10-24 12:22:55 -0700403 Set<VtnPort> vtnPorts = ports().stream()
Hyunsun Moonaf477ac2016-11-09 12:56:00 -0800404 .filter(port -> vtnPort(PortId.of(port.getId())) != null)
Hyunsun Moond2e4a462016-10-24 12:22:55 -0700405 .map(port -> vtnPort(PortId.of(port.getId())))
406 .collect(Collectors.toSet());
407 return ImmutableSet.copyOf(vtnPorts);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700408 }
409
410 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700411 public ServiceNetwork serviceNetwork(NetworkId netId) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700412 checkNotNull(netId, ERR_NULL_NET_ID);
Hyunsun Moon5c143952016-10-19 18:34:46 -0700413 return store.vtnNetwork(netId);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700414 }
415
416 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700417 public Set<ServiceNetwork> serviceNetworks() {
Hyunsun Moond2e4a462016-10-24 12:22:55 -0700418 return ImmutableSet.copyOf(store.vtnNetworks());
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700419 }
420
421 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700422 public ServicePort servicePort(PortId portId) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700423 checkNotNull(portId, ERR_NULL_PORT_ID);
Hyunsun Moon5c143952016-10-19 18:34:46 -0700424 return store.vtnPort(portId);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700425 }
426
427 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700428 public Set<ServicePort> servicePorts() {
Hyunsun Moond2e4a462016-10-24 12:22:55 -0700429 return ImmutableSet.copyOf(store.vtnPorts());
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700430 }
431
432 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700433 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 Moond2e4a462016-10-24 12:22:55 -0700440 return ImmutableSet.copyOf(store.networks());
Hyunsun Moon5c143952016-10-19 18:34:46 -0700441 }
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 Moond2e4a462016-10-24 12:22:55 -0700451 return ImmutableSet.copyOf(store.ports());
Hyunsun Moon5c143952016-10-19 18:34:46 -0700452 }
453
454 @Override
455 public Subnet subnet(SubnetId subnetId) {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700456 checkNotNull(subnetId, ERR_NULL_SUBNET_ID);
Hyunsun Moon5c143952016-10-19 18:34:46 -0700457 return store.subnet(subnetId);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700458 }
459
460 @Override
Hyunsun Moon5c143952016-10-19 18:34:46 -0700461 public Set<Subnet> subnets() {
Hyunsun Moond2e4a462016-10-24 12:22:55 -0700462 return ImmutableSet.copyOf(store.subnets());
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700463 }
464
Hyunsun Moond5af96f2016-10-24 11:39:56 -0700465 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 Moon5c143952016-10-19 18:34:46 -0700503 private Instance getInstance(PortId portId) {
504 VtnPort vtnPort = vtnPort(portId);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700505 if (vtnPort == null) {
506 final String error = "Failed to build VTN port for " + portId.id();
507 throw new IllegalStateException(error);
508 }
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700509 Host host = hostService.getHost(HostId.hostId(vtnPort.mac()));
510 if (host == null) {
511 return null;
512 }
513 return Instance.of(host);
514 }
515
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700516 private VtnNetwork getDefaultVtnNetwork(NetworkId netId) {
Hyunsun Moon5c143952016-10-19 18:34:46 -0700517 Network network = network(netId);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700518 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 Moon5c143952016-10-19 18:34:46 -0700526 Port port = port(portId);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700527 if (port == null) {
528 return null;
529 }
530 return VtnPort.of(port, null);
531 }
532
533 private Subnet getSubnet(NetworkId netId) {
Hyunsun Moon5c143952016-10-19 18:34:46 -0700534 Optional<Subnet> subnet = subnets().stream()
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700535 .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}