blob: 9e4bed3d781b2b7cb09c4c367a3fafba33a09eeb [file] [log] [blame]
Yuta HIGUCHIa9ae6e62018-02-26 12:56:20 -08001/*
2 * Copyright 2018-present Open Networking Foundation
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.
Ramon Casellas390efe92018-03-01 11:45:27 +010015 *
16 *
17 * This work was partially supported by EC H2020 project METRO-HAUL (761727).
18 * Contact: Ramon Casellas <ramon.casellas@cttc.es>
Yuta HIGUCHIa9ae6e62018-02-26 12:56:20 -080019 */
Ramon Casellas390efe92018-03-01 11:45:27 +010020
Yuta HIGUCHIa9ae6e62018-02-26 12:56:20 -080021package org.onosproject.odtn.impl;
22
Ray Milkeyd84f89b2018-08-17 14:54:17 -070023import org.osgi.service.component.annotations.Activate;
24import org.osgi.service.component.annotations.Component;
25import org.osgi.service.component.annotations.Deactivate;
26import org.osgi.service.component.annotations.Reference;
27import org.osgi.service.component.annotations.ReferenceCardinality;
hirokifca084b2018-06-02 08:29:42 -070028import org.onosproject.net.ConnectPoint;
hirokib8ddc3f2018-06-02 08:29:42 -070029import org.onosproject.net.DeviceId;
hirokibca3e932018-05-15 15:25:54 -070030import org.onosproject.net.Link;
hirokifca084b2018-06-02 08:29:42 -070031import org.onosproject.net.config.ConfigFactory;
hirokif4ed5212018-05-26 22:39:38 -070032import org.onosproject.net.config.NetworkConfigEvent;
33import org.onosproject.net.config.NetworkConfigListener;
hirokifca084b2018-06-02 08:29:42 -070034import org.onosproject.net.config.NetworkConfigRegistry;
Yuta HIGUCHIa9ae6e62018-02-26 12:56:20 -080035import org.onosproject.net.config.NetworkConfigService;
hirokibca3e932018-05-15 15:25:54 -070036import org.onosproject.net.device.DeviceEvent;
37import org.onosproject.net.device.DeviceListener;
38import org.onosproject.net.device.DeviceService;
39import org.onosproject.net.link.LinkEvent;
40import org.onosproject.net.link.LinkListener;
41import org.onosproject.net.link.LinkService;
hirokif4ed5212018-05-26 22:39:38 -070042import org.onosproject.odtn.TapiResolver;
43import org.onosproject.odtn.TapiTopologyManager;
hirokifca084b2018-06-02 08:29:42 -070044import org.onosproject.odtn.config.TerminalDeviceConfig;
hirokif4ed5212018-05-26 22:39:38 -070045import org.onosproject.odtn.internal.DcsBasedTapiCommonRpc;
46import org.onosproject.odtn.internal.DcsBasedTapiConnectivityRpc;
hiroki684aa2f2018-05-19 20:48:49 -070047import org.onosproject.odtn.internal.DcsBasedTapiDataProducer;
48import org.onosproject.odtn.internal.TapiDataProducer;
hirokib8ddc3f2018-06-02 08:29:42 -070049import org.onosproject.odtn.internal.DefaultOdtnTerminalDeviceDriver;
Yuta HIGUCHIa9ae6e62018-02-26 12:56:20 -080050import org.slf4j.Logger;
51import org.slf4j.LoggerFactory;
52
hirokif4ed5212018-05-26 22:39:38 -070053// DCS / onos-yang-tools
54import org.onosproject.config.DynamicConfigEvent;
55import org.onosproject.config.DynamicConfigListener;
56import org.onosproject.config.DynamicConfigService;
Ramon Casellas390efe92018-03-01 11:45:27 +010057import org.onosproject.yang.model.RpcRegistry;
Ramon Casellas390efe92018-03-01 11:45:27 +010058
hiroki684aa2f2018-05-19 20:48:49 -070059import static org.onosproject.config.DynamicConfigEvent.Type.NODE_ADDED;
60import static org.onosproject.config.DynamicConfigEvent.Type.NODE_DELETED;
hirokifca084b2018-06-02 08:29:42 -070061import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_ADDED;
62import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REMOVED;
63import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UPDATED;
64import static org.onosproject.net.config.basics.SubjectFactories.CONNECT_POINT_SUBJECT_FACTORY;
hiroki684aa2f2018-05-19 20:48:49 -070065
Yuta HIGUCHIa9ae6e62018-02-26 12:56:20 -080066/**
67 * OSGi Component for ODTN Service application.
68 */
69@Component(immediate = true)
70public class ServiceApplicationComponent {
71
72 private final Logger log = LoggerFactory.getLogger(getClass());
73
Ray Milkeyd84f89b2018-08-17 14:54:17 -070074 @Reference(cardinality = ReferenceCardinality.MANDATORY)
hiroki684aa2f2018-05-19 20:48:49 -070075 protected DynamicConfigService dynConfigService;
Yuta HIGUCHIa9ae6e62018-02-26 12:56:20 -080076
Ray Milkeyd84f89b2018-08-17 14:54:17 -070077 @Reference(cardinality = ReferenceCardinality.MANDATORY)
hirokibca3e932018-05-15 15:25:54 -070078 protected DeviceService deviceService;
79
Ray Milkeyd84f89b2018-08-17 14:54:17 -070080 @Reference(cardinality = ReferenceCardinality.MANDATORY)
hirokibca3e932018-05-15 15:25:54 -070081 protected LinkService linkService;
82
Ray Milkeyd84f89b2018-08-17 14:54:17 -070083 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Yuta HIGUCHIa9ae6e62018-02-26 12:56:20 -080084 protected NetworkConfigService netcfgService;
85
Ray Milkeyd84f89b2018-08-17 14:54:17 -070086 @Reference(cardinality = ReferenceCardinality.MANDATORY)
hirokifca084b2018-06-02 08:29:42 -070087 protected NetworkConfigRegistry netcfgRegistry;
88
Ray Milkeyd84f89b2018-08-17 14:54:17 -070089 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Ramon Casellas390efe92018-03-01 11:45:27 +010090 protected RpcRegistry rpcRegistry;
91
Ray Milkeyd84f89b2018-08-17 14:54:17 -070092 @Reference(cardinality = ReferenceCardinality.MANDATORY)
hirokibca3e932018-05-15 15:25:54 -070093 protected TapiTopologyManager tapiTopologyManager;
Ramon Casellas390efe92018-03-01 11:45:27 +010094
Ray Milkeyd84f89b2018-08-17 14:54:17 -070095 @Reference(cardinality = ReferenceCardinality.MANDATORY)
hiroki684aa2f2018-05-19 20:48:49 -070096 protected TapiResolver resolver;
97
Ramon Casellas390efe92018-03-01 11:45:27 +010098 // Listener for events from the DCS
hiroki684aa2f2018-05-19 20:48:49 -070099 private final DynamicConfigListener dynamicConfigServiceListener =
100 new InternalDynamicConfigListener();
hirokibca3e932018-05-15 15:25:54 -0700101
102 private DeviceListener deviceListener = new InternalDeviceListener();
103 private final LinkListener linkListener = new InternalLinkListener();
hirokif4ed5212018-05-26 22:39:38 -0700104 private final NetworkConfigListener netcfgListener = new InternalNetCfgListener();
hiroki684aa2f2018-05-19 20:48:49 -0700105 private TapiDataProducer dataProvider = new DcsBasedTapiDataProducer();
Ramon Casellas390efe92018-03-01 11:45:27 +0100106
107 // Rpc Service for TAPI Connectivity
hirokif4ed5212018-05-26 22:39:38 -0700108 private final DcsBasedTapiConnectivityRpc rpcTapiConnectivity = new DcsBasedTapiConnectivityRpc();
109 private final DcsBasedTapiCommonRpc rpcTapiCommon = new DcsBasedTapiCommonRpc();
hiroki684aa2f2018-05-19 20:48:49 -0700110
hirokifca084b2018-06-02 08:29:42 -0700111 // FIXME create factory and register for all behaviours
112 private final ConfigFactory<ConnectPoint, TerminalDeviceConfig> factory =
113 new ConfigFactory<ConnectPoint, TerminalDeviceConfig>(CONNECT_POINT_SUBJECT_FACTORY,
114 TerminalDeviceConfig.class, TerminalDeviceConfig.CONFIG_KEY) {
115 @Override
116 public TerminalDeviceConfig createConfig() {
117 return new TerminalDeviceConfig();
118 }
119 };
120
Ramon Casellas390efe92018-03-01 11:45:27 +0100121
Yuta HIGUCHIa9ae6e62018-02-26 12:56:20 -0800122 @Activate
123 protected void activate() {
124 log.info("Started");
hiroki684aa2f2018-05-19 20:48:49 -0700125 dynConfigService.addListener(dynamicConfigServiceListener);
hirokibca3e932018-05-15 15:25:54 -0700126 deviceService.addListener(deviceListener);
127 linkService.addListener(linkListener);
hirokif4ed5212018-05-26 22:39:38 -0700128 netcfgService.addListener(netcfgListener);
hirokifca084b2018-06-02 08:29:42 -0700129 netcfgRegistry.registerConfigFactory(factory);
Ramon Casellas390efe92018-03-01 11:45:27 +0100130 rpcRegistry.registerRpcService(rpcTapiConnectivity);
hiroki684aa2f2018-05-19 20:48:49 -0700131 rpcRegistry.registerRpcService(rpcTapiCommon);
hirokifca084b2018-06-02 08:29:42 -0700132
hirokif4ed5212018-05-26 22:39:38 -0700133 rpcTapiConnectivity.init();
134 rpcTapiCommon.init();
Yuta HIGUCHIa9ae6e62018-02-26 12:56:20 -0800135 }
136
Ramon Casellas390efe92018-03-01 11:45:27 +0100137
Yuta HIGUCHIa9ae6e62018-02-26 12:56:20 -0800138 @Deactivate
139 protected void deactivate() {
140 log.info("Stopped");
hiroki684aa2f2018-05-19 20:48:49 -0700141 rpcRegistry.unregisterRpcService(rpcTapiCommon);
Ramon Casellas390efe92018-03-01 11:45:27 +0100142 rpcRegistry.unregisterRpcService(rpcTapiConnectivity);
hirokifca084b2018-06-02 08:29:42 -0700143 netcfgRegistry.unregisterConfigFactory(factory);
hirokif4ed5212018-05-26 22:39:38 -0700144 netcfgService.removeListener(netcfgListener);
hirokibca3e932018-05-15 15:25:54 -0700145 linkService.removeListener(linkListener);
hirokiec18d3a2018-05-16 15:27:37 -0700146 deviceService.removeListener(deviceListener);
hiroki684aa2f2018-05-19 20:48:49 -0700147 dynConfigService.removeListener(dynamicConfigServiceListener);
Yuta HIGUCHIa9ae6e62018-02-26 12:56:20 -0800148 }
149
Ramon Casellas390efe92018-03-01 11:45:27 +0100150
hirokibca3e932018-05-15 15:25:54 -0700151 /**
152 * Representation of internal listener, listening for device event.
153 */
154 private class InternalDeviceListener implements DeviceListener {
Ramon Casellas390efe92018-03-01 11:45:27 +0100155
hirokibca3e932018-05-15 15:25:54 -0700156 /**
157 * Process an Event from the Device Service.
158 *
159 * @param event device event
160 */
161 @Override
162 public void event(DeviceEvent event) {
hirokibca3e932018-05-15 15:25:54 -0700163
Andrea Campanella77e9b332018-11-29 13:33:19 -0800164 log.debug("Device event type: {}, subject: {}", event.type(), event.subject());
hirokibca3e932018-05-15 15:25:54 -0700165 switch (event.type()) {
166 case DEVICE_ADDED:
hirokiec18d3a2018-05-16 15:27:37 -0700167 tapiTopologyManager.addDevice(event.subject());
hirokibca3e932018-05-15 15:25:54 -0700168 break;
169 case DEVICE_REMOVED:
hirokiec18d3a2018-05-16 15:27:37 -0700170 tapiTopologyManager.removeDevice(event.subject());
hirokibca3e932018-05-15 15:25:54 -0700171 break;
172 case PORT_ADDED:
hirokiec18d3a2018-05-16 15:27:37 -0700173 tapiTopologyManager.addPort(event.port());
hirokibca3e932018-05-15 15:25:54 -0700174 break;
175 case PORT_REMOVED:
hirokiec18d3a2018-05-16 15:27:37 -0700176 tapiTopologyManager.removePort(event.port());
hirokibca3e932018-05-15 15:25:54 -0700177 break;
Ramon Casellas03f194f2018-11-15 16:06:02 +0100178 // TODO: Process device / port updated events
hirokibca3e932018-05-15 15:25:54 -0700179 default:
Ramon Casellas03f194f2018-11-15 16:06:02 +0100180 log.warn("Unprocessed Event {}", event.type());
hirokibca3e932018-05-15 15:25:54 -0700181 break;
182 }
183
184 }
185 }
186
187 /**
188 * Representation of internal listener, listening for link event.
189 */
190 private class InternalLinkListener implements LinkListener {
191
192 /**
193 * Process an Event from the Device Service.
194 *
195 * @param event link event
196 */
197 @Override
198 public void event(LinkEvent event) {
199 Link link = event.subject();
200
201 switch (event.type()) {
202 case LINK_ADDED:
203 tapiTopologyManager.addLink(link);
204 break;
205 case LINK_REMOVED:
206 tapiTopologyManager.removeLink(link);
207 break;
208 default:
Ramon Casellas03f194f2018-11-15 16:06:02 +0100209 log.warn("Unknown Event {}", event.type());
hirokibca3e932018-05-15 15:25:54 -0700210 break;
211 }
212 }
213 }
Ramon Casellas390efe92018-03-01 11:45:27 +0100214
hiroki684aa2f2018-05-19 20:48:49 -0700215 /**
hirokif4ed5212018-05-26 22:39:38 -0700216 * Representation of internal listener, listening for netcfg event.
217 */
218 private class InternalNetCfgListener implements NetworkConfigListener {
219
220 /**
hirokifca084b2018-06-02 08:29:42 -0700221 * Check if the netcfg event should be further processed.
222 *
223 * @param event config event
224 * @return true if event is supported; false otherwise
225 */
226 @Override
227 public boolean isRelevant(NetworkConfigEvent event) {
228
229 if (event.type() == CONFIG_ADDED || event.type() == CONFIG_UPDATED) {
230 if (event.config().orElse(null) instanceof TerminalDeviceConfig) {
231 return true;
232 }
233 }
234 if (event.type() == CONFIG_REMOVED) {
235 if (event.prevConfig().orElse(null) instanceof TerminalDeviceConfig) {
236 return true;
237 }
238 }
239 return false;
240 }
241
242 /**
hirokif4ed5212018-05-26 22:39:38 -0700243 * Process an Event from the NetCfg Service.
244 *
hirokifca084b2018-06-02 08:29:42 -0700245 * @param event event
hirokif4ed5212018-05-26 22:39:38 -0700246 */
247 @Override
248 public void event(NetworkConfigEvent event) {
hirokifca084b2018-06-02 08:29:42 -0700249
Andrea Campanella77e9b332018-11-29 13:33:19 -0800250 log.debug("Event type: {}, subject: {}", event.type(), event.subject());
hirokib8ddc3f2018-06-02 08:29:42 -0700251 DeviceId did = ((ConnectPoint) event.subject()).deviceId();
252
253 DefaultOdtnTerminalDeviceDriver driver = DefaultOdtnTerminalDeviceDriver.create();
254 TerminalDeviceConfig config;
255
256 switch (event.type()) {
257 case CONFIG_ADDED:
258 case CONFIG_UPDATED:
259 config = (TerminalDeviceConfig) event.config().get();
Andrea Campanella77e9b332018-11-29 13:33:19 -0800260 log.debug("config: {}", config);
hirokib8ddc3f2018-06-02 08:29:42 -0700261 driver.apply(did, config.clientCp().port(), config.subject().port(), config.isEnabled());
262 break;
263 case CONFIG_REMOVED:
264 config = (TerminalDeviceConfig) event.prevConfig().get();
Andrea Campanella77e9b332018-11-29 13:33:19 -0800265 log.debug("config: {}", config);
hirokib8ddc3f2018-06-02 08:29:42 -0700266 driver.apply(did, config.clientCp().port(), config.subject().port(), false);
267 break;
268 default:
269 log.error("Unsupported event type.");
270 }
hirokif4ed5212018-05-26 22:39:38 -0700271 }
272 }
273
hirokif4ed5212018-05-26 22:39:38 -0700274 /**
hiroki684aa2f2018-05-19 20:48:49 -0700275 * Representation of internal listener, listening for dynamic config event.
276 */
277 private class InternalDynamicConfigListener implements DynamicConfigListener {
278
279 /**
280 * Check if the DCS event should be further processed.
281 *
282 * @param event config event
283 * @return true if event is supported; false otherwise
284 */
285 @Override
286 public boolean isRelevant(DynamicConfigEvent event) {
287 // Only care about add and delete
288 if ((event.type() != NODE_ADDED) &&
289 (event.type() != NODE_DELETED)) {
290 return false;
291 }
292 return true;
293 }
294
295
296 /**
297 * Process an Event from the Dynamic Configuration Store.
298 *
299 * @param event config event
300 */
301 @Override
302 public void event(DynamicConfigEvent event) {
303 resolver.makeDirty();
hirokif4ed5212018-05-26 22:39:38 -0700304// ResourceId rsId = event.subject();
305// DataNode node;
306// try {
307// Filter filter = Filter.builder().addCriteria(rsId).build();
308// node = dynConfigService.readNode(rsId, filter);
309// } catch (FailedException e) {
310// node = null;
311// }
312// switch (event.type()) {
313// case NODE_ADDED:
314// onDcsNodeAdded(rsId, node);
315// break;
316//
317// case NODE_DELETED:
318// onDcsNodeDeleted(node);
319// break;
320//
321// default:
322// log.warn("Unknown Event", event.type());
323// break;
324// }
hiroki684aa2f2018-05-19 20:48:49 -0700325 }
326
hirokif4ed5212018-05-26 22:39:38 -0700327// /**
328// * Process the event that a node has been added to the DCS.
329// *
330// * @param rsId ResourceId of the added node
331// * @param node added node. Access the key and value
332// */
333// private void onDcsNodeAdded(ResourceId rsId, DataNode node) {
334//
335// switch (node.type()) {
336// case SINGLE_INSTANCE_NODE:
337// break;
338// case MULTI_INSTANCE_NODE:
339// break;
340// case SINGLE_INSTANCE_LEAF_VALUE_NODE:
341// break;
342// case MULTI_INSTANCE_LEAF_VALUE_NODE:
343// break;
344// default:
345// break;
346// }
347//
348// NodeKey dataNodeKey = node.key();
349// SchemaId schemaId = dataNodeKey.schemaId();
hiroki684aa2f2018-05-19 20:48:49 -0700350
hirokifca084b2018-06-02 08:29:42 -0700351 // Consolidate events
hirokiec18d3a2018-05-16 15:27:37 -0700352// if (!schemaId.namespace().contains("tapi")) {
353// return;
354// }
hirokibca3e932018-05-15 15:25:54 -0700355// log.info("namespace {}", schemaId.namespace());
hirokif4ed5212018-05-26 22:39:38 -0700356// }
hiroki684aa2f2018-05-19 20:48:49 -0700357
hirokif4ed5212018-05-26 22:39:38 -0700358// /**
359// * Process the event that a node has been deleted from the DCS.
360// *
361// * @param dataNode data node
362// */
363// private void onDcsNodeDeleted(DataNode dataNode) {
364// // TODO: Implement release logic
365// }
hiroki684aa2f2018-05-19 20:48:49 -0700366
367 }
Ramon Casellas390efe92018-03-01 11:45:27 +0100368
Yuta HIGUCHIa9ae6e62018-02-26 12:56:20 -0800369}