blob: ff813e50f839e3aa4c1d4b1fc404ccb019a9f3ea [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
23import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
26import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.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
hiroki684aa2f2018-05-19 20:48:49 -070074 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
75 protected DynamicConfigService dynConfigService;
Yuta HIGUCHIa9ae6e62018-02-26 12:56:20 -080076
77 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
hirokibca3e932018-05-15 15:25:54 -070078 protected DeviceService deviceService;
79
80 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 protected LinkService linkService;
82
83 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Yuta HIGUCHIa9ae6e62018-02-26 12:56:20 -080084 protected NetworkConfigService netcfgService;
85
86 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
hirokifca084b2018-06-02 08:29:42 -070087 protected NetworkConfigRegistry netcfgRegistry;
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Ramon Casellas390efe92018-03-01 11:45:27 +010090 protected RpcRegistry rpcRegistry;
91
92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
hirokibca3e932018-05-15 15:25:54 -070093 protected TapiTopologyManager tapiTopologyManager;
Ramon Casellas390efe92018-03-01 11:45:27 +010094
hiroki684aa2f2018-05-19 20:48:49 -070095 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 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;
179 default:
180 log.warn("Unknown Event", event.type());
181 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:
209 log.warn("Unknown Event", event.type());
210 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
hirokif4ed5212018-05-26 22:39:38 -0700250 log.info("type: {}", event.type());
hirokifca084b2018-06-02 08:29:42 -0700251 log.info("subject: {}", event.subject());
hirokib8ddc3f2018-06-02 08:29:42 -0700252 DeviceId did = ((ConnectPoint) event.subject()).deviceId();
253
254 DefaultOdtnTerminalDeviceDriver driver = DefaultOdtnTerminalDeviceDriver.create();
255 TerminalDeviceConfig config;
256
257 switch (event.type()) {
258 case CONFIG_ADDED:
259 case CONFIG_UPDATED:
260 config = (TerminalDeviceConfig) event.config().get();
261 log.info("config: {}", config);
262 driver.apply(did, config.clientCp().port(), config.subject().port(), config.isEnabled());
263 break;
264 case CONFIG_REMOVED:
265 config = (TerminalDeviceConfig) event.prevConfig().get();
266 log.info("config: {}", config);
267 driver.apply(did, config.clientCp().port(), config.subject().port(), false);
268 break;
269 default:
270 log.error("Unsupported event type.");
271 }
hirokif4ed5212018-05-26 22:39:38 -0700272 }
273 }
274
hirokif4ed5212018-05-26 22:39:38 -0700275 /**
hiroki684aa2f2018-05-19 20:48:49 -0700276 * Representation of internal listener, listening for dynamic config event.
277 */
278 private class InternalDynamicConfigListener implements DynamicConfigListener {
279
280 /**
281 * Check if the DCS event should be further processed.
282 *
283 * @param event config event
284 * @return true if event is supported; false otherwise
285 */
286 @Override
287 public boolean isRelevant(DynamicConfigEvent event) {
288 // Only care about add and delete
289 if ((event.type() != NODE_ADDED) &&
290 (event.type() != NODE_DELETED)) {
291 return false;
292 }
293 return true;
294 }
295
296
297 /**
298 * Process an Event from the Dynamic Configuration Store.
299 *
300 * @param event config event
301 */
302 @Override
303 public void event(DynamicConfigEvent event) {
304 resolver.makeDirty();
hirokif4ed5212018-05-26 22:39:38 -0700305// ResourceId rsId = event.subject();
306// DataNode node;
307// try {
308// Filter filter = Filter.builder().addCriteria(rsId).build();
309// node = dynConfigService.readNode(rsId, filter);
310// } catch (FailedException e) {
311// node = null;
312// }
313// switch (event.type()) {
314// case NODE_ADDED:
315// onDcsNodeAdded(rsId, node);
316// break;
317//
318// case NODE_DELETED:
319// onDcsNodeDeleted(node);
320// break;
321//
322// default:
323// log.warn("Unknown Event", event.type());
324// break;
325// }
hiroki684aa2f2018-05-19 20:48:49 -0700326 }
327
hirokif4ed5212018-05-26 22:39:38 -0700328// /**
329// * Process the event that a node has been added to the DCS.
330// *
331// * @param rsId ResourceId of the added node
332// * @param node added node. Access the key and value
333// */
334// private void onDcsNodeAdded(ResourceId rsId, DataNode node) {
335//
336// switch (node.type()) {
337// case SINGLE_INSTANCE_NODE:
338// break;
339// case MULTI_INSTANCE_NODE:
340// break;
341// case SINGLE_INSTANCE_LEAF_VALUE_NODE:
342// break;
343// case MULTI_INSTANCE_LEAF_VALUE_NODE:
344// break;
345// default:
346// break;
347// }
348//
349// NodeKey dataNodeKey = node.key();
350// SchemaId schemaId = dataNodeKey.schemaId();
hiroki684aa2f2018-05-19 20:48:49 -0700351
hirokifca084b2018-06-02 08:29:42 -0700352 // Consolidate events
hirokiec18d3a2018-05-16 15:27:37 -0700353// if (!schemaId.namespace().contains("tapi")) {
354// return;
355// }
hirokibca3e932018-05-15 15:25:54 -0700356// log.info("namespace {}", schemaId.namespace());
hirokif4ed5212018-05-26 22:39:38 -0700357// }
hiroki684aa2f2018-05-19 20:48:49 -0700358
hirokif4ed5212018-05-26 22:39:38 -0700359// /**
360// * Process the event that a node has been deleted from the DCS.
361// *
362// * @param dataNode data node
363// */
364// private void onDcsNodeDeleted(DataNode dataNode) {
365// // TODO: Implement release logic
366// }
hiroki684aa2f2018-05-19 20:48:49 -0700367
368 }
Ramon Casellas390efe92018-03-01 11:45:27 +0100369
Yuta HIGUCHIa9ae6e62018-02-26 12:56:20 -0800370}