blob: e296f1b4d7a6064617323b35dc91952c562f2328 [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
hirokifca084b2018-06-02 08:29:42 -0700164 log.info("Device event type: {}", event.type());
165 log.info("Device event subject: {}", event.subject());
hirokibca3e932018-05-15 15:25:54 -0700166 switch (event.type()) {
167 case DEVICE_ADDED:
hirokiec18d3a2018-05-16 15:27:37 -0700168 tapiTopologyManager.addDevice(event.subject());
hirokibca3e932018-05-15 15:25:54 -0700169 break;
170 case DEVICE_REMOVED:
hirokiec18d3a2018-05-16 15:27:37 -0700171 tapiTopologyManager.removeDevice(event.subject());
hirokibca3e932018-05-15 15:25:54 -0700172 break;
173 case PORT_ADDED:
hirokiec18d3a2018-05-16 15:27:37 -0700174 tapiTopologyManager.addPort(event.port());
hirokibca3e932018-05-15 15:25:54 -0700175 break;
176 case PORT_REMOVED:
hirokiec18d3a2018-05-16 15:27:37 -0700177 tapiTopologyManager.removePort(event.port());
hirokibca3e932018-05-15 15:25:54 -0700178 break;
Ramon Casellas03f194f2018-11-15 16:06:02 +0100179 // TODO: Process device / port updated events
hirokibca3e932018-05-15 15:25:54 -0700180 default:
Ramon Casellas03f194f2018-11-15 16:06:02 +0100181 log.warn("Unprocessed Event {}", event.type());
hirokibca3e932018-05-15 15:25:54 -0700182 break;
183 }
184
185 }
186 }
187
188 /**
189 * Representation of internal listener, listening for link event.
190 */
191 private class InternalLinkListener implements LinkListener {
192
193 /**
194 * Process an Event from the Device Service.
195 *
196 * @param event link event
197 */
198 @Override
199 public void event(LinkEvent event) {
200 Link link = event.subject();
201
202 switch (event.type()) {
203 case LINK_ADDED:
204 tapiTopologyManager.addLink(link);
205 break;
206 case LINK_REMOVED:
207 tapiTopologyManager.removeLink(link);
208 break;
209 default:
Ramon Casellas03f194f2018-11-15 16:06:02 +0100210 log.warn("Unknown Event {}", event.type());
hirokibca3e932018-05-15 15:25:54 -0700211 break;
212 }
213 }
214 }
Ramon Casellas390efe92018-03-01 11:45:27 +0100215
hiroki684aa2f2018-05-19 20:48:49 -0700216 /**
hirokif4ed5212018-05-26 22:39:38 -0700217 * Representation of internal listener, listening for netcfg event.
218 */
219 private class InternalNetCfgListener implements NetworkConfigListener {
220
221 /**
hirokifca084b2018-06-02 08:29:42 -0700222 * Check if the netcfg event should be further processed.
223 *
224 * @param event config event
225 * @return true if event is supported; false otherwise
226 */
227 @Override
228 public boolean isRelevant(NetworkConfigEvent event) {
229
230 if (event.type() == CONFIG_ADDED || event.type() == CONFIG_UPDATED) {
231 if (event.config().orElse(null) instanceof TerminalDeviceConfig) {
232 return true;
233 }
234 }
235 if (event.type() == CONFIG_REMOVED) {
236 if (event.prevConfig().orElse(null) instanceof TerminalDeviceConfig) {
237 return true;
238 }
239 }
240 return false;
241 }
242
243 /**
hirokif4ed5212018-05-26 22:39:38 -0700244 * Process an Event from the NetCfg Service.
245 *
hirokifca084b2018-06-02 08:29:42 -0700246 * @param event event
hirokif4ed5212018-05-26 22:39:38 -0700247 */
248 @Override
249 public void event(NetworkConfigEvent event) {
hirokifca084b2018-06-02 08:29:42 -0700250
hirokif4ed5212018-05-26 22:39:38 -0700251 log.info("type: {}", event.type());
hirokifca084b2018-06-02 08:29:42 -0700252 log.info("subject: {}", event.subject());
hirokib8ddc3f2018-06-02 08:29:42 -0700253 DeviceId did = ((ConnectPoint) event.subject()).deviceId();
254
255 DefaultOdtnTerminalDeviceDriver driver = DefaultOdtnTerminalDeviceDriver.create();
256 TerminalDeviceConfig config;
257
258 switch (event.type()) {
259 case CONFIG_ADDED:
260 case CONFIG_UPDATED:
261 config = (TerminalDeviceConfig) event.config().get();
262 log.info("config: {}", config);
263 driver.apply(did, config.clientCp().port(), config.subject().port(), config.isEnabled());
264 break;
265 case CONFIG_REMOVED:
266 config = (TerminalDeviceConfig) event.prevConfig().get();
267 log.info("config: {}", config);
268 driver.apply(did, config.clientCp().port(), config.subject().port(), false);
269 break;
270 default:
271 log.error("Unsupported event type.");
272 }
hirokif4ed5212018-05-26 22:39:38 -0700273 }
274 }
275
hirokif4ed5212018-05-26 22:39:38 -0700276 /**
hiroki684aa2f2018-05-19 20:48:49 -0700277 * Representation of internal listener, listening for dynamic config event.
278 */
279 private class InternalDynamicConfigListener implements DynamicConfigListener {
280
281 /**
282 * Check if the DCS event should be further processed.
283 *
284 * @param event config event
285 * @return true if event is supported; false otherwise
286 */
287 @Override
288 public boolean isRelevant(DynamicConfigEvent event) {
289 // Only care about add and delete
290 if ((event.type() != NODE_ADDED) &&
291 (event.type() != NODE_DELETED)) {
292 return false;
293 }
294 return true;
295 }
296
297
298 /**
299 * Process an Event from the Dynamic Configuration Store.
300 *
301 * @param event config event
302 */
303 @Override
304 public void event(DynamicConfigEvent event) {
305 resolver.makeDirty();
hirokif4ed5212018-05-26 22:39:38 -0700306// ResourceId rsId = event.subject();
307// DataNode node;
308// try {
309// Filter filter = Filter.builder().addCriteria(rsId).build();
310// node = dynConfigService.readNode(rsId, filter);
311// } catch (FailedException e) {
312// node = null;
313// }
314// switch (event.type()) {
315// case NODE_ADDED:
316// onDcsNodeAdded(rsId, node);
317// break;
318//
319// case NODE_DELETED:
320// onDcsNodeDeleted(node);
321// break;
322//
323// default:
324// log.warn("Unknown Event", event.type());
325// break;
326// }
hiroki684aa2f2018-05-19 20:48:49 -0700327 }
328
hirokif4ed5212018-05-26 22:39:38 -0700329// /**
330// * Process the event that a node has been added to the DCS.
331// *
332// * @param rsId ResourceId of the added node
333// * @param node added node. Access the key and value
334// */
335// private void onDcsNodeAdded(ResourceId rsId, DataNode node) {
336//
337// switch (node.type()) {
338// case SINGLE_INSTANCE_NODE:
339// break;
340// case MULTI_INSTANCE_NODE:
341// break;
342// case SINGLE_INSTANCE_LEAF_VALUE_NODE:
343// break;
344// case MULTI_INSTANCE_LEAF_VALUE_NODE:
345// break;
346// default:
347// break;
348// }
349//
350// NodeKey dataNodeKey = node.key();
351// SchemaId schemaId = dataNodeKey.schemaId();
hiroki684aa2f2018-05-19 20:48:49 -0700352
hirokifca084b2018-06-02 08:29:42 -0700353 // Consolidate events
hirokiec18d3a2018-05-16 15:27:37 -0700354// if (!schemaId.namespace().contains("tapi")) {
355// return;
356// }
hirokibca3e932018-05-15 15:25:54 -0700357// log.info("namespace {}", schemaId.namespace());
hirokif4ed5212018-05-26 22:39:38 -0700358// }
hiroki684aa2f2018-05-19 20:48:49 -0700359
hirokif4ed5212018-05-26 22:39:38 -0700360// /**
361// * Process the event that a node has been deleted from the DCS.
362// *
363// * @param dataNode data node
364// */
365// private void onDcsNodeDeleted(DataNode dataNode) {
366// // TODO: Implement release logic
367// }
hiroki684aa2f2018-05-19 20:48:49 -0700368
369 }
Ramon Casellas390efe92018-03-01 11:45:27 +0100370
Yuta HIGUCHIa9ae6e62018-02-26 12:56:20 -0800371}