Yuta HIGUCHI | a9ae6e6 | 2018-02-26 12:56:20 -0800 | [diff] [blame] | 1 | /* |
| 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 Casellas | 390efe9 | 2018-03-01 11:45:27 +0100 | [diff] [blame] | 15 | * |
| 16 | * |
| 17 | * This work was partially supported by EC H2020 project METRO-HAUL (761727). |
| 18 | * Contact: Ramon Casellas <ramon.casellas@cttc.es> |
Yuta HIGUCHI | a9ae6e6 | 2018-02-26 12:56:20 -0800 | [diff] [blame] | 19 | */ |
Ramon Casellas | 390efe9 | 2018-03-01 11:45:27 +0100 | [diff] [blame] | 20 | |
Yuta HIGUCHI | a9ae6e6 | 2018-02-26 12:56:20 -0800 | [diff] [blame] | 21 | package org.onosproject.odtn.impl; |
| 22 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 23 | import org.osgi.service.component.annotations.Activate; |
| 24 | import org.osgi.service.component.annotations.Component; |
| 25 | import org.osgi.service.component.annotations.Deactivate; |
| 26 | import org.osgi.service.component.annotations.Reference; |
| 27 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 28 | |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 29 | import org.onosproject.net.ConnectPoint; |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 30 | import org.onosproject.net.device.DeviceEvent; |
| 31 | import org.onosproject.net.device.DeviceListener; |
| 32 | import org.onosproject.net.device.DeviceService; |
| 33 | import org.onosproject.net.Port; |
hiroki | b8ddc3f | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 34 | import org.onosproject.net.DeviceId; |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 35 | import org.onosproject.net.Link; |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 36 | |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 37 | import org.onosproject.net.config.ConfigFactory; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 38 | import org.onosproject.net.config.NetworkConfigEvent; |
| 39 | import org.onosproject.net.config.NetworkConfigListener; |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 40 | import org.onosproject.net.config.NetworkConfigRegistry; |
Yuta HIGUCHI | a9ae6e6 | 2018-02-26 12:56:20 -0800 | [diff] [blame] | 41 | import org.onosproject.net.config.NetworkConfigService; |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 42 | |
| 43 | import static com.google.common.base.Preconditions.checkNotNull; |
| 44 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_ADDED; |
| 45 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REMOVED; |
| 46 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UPDATED; |
| 47 | import static org.onosproject.net.config.basics.SubjectFactories.CONNECT_POINT_SUBJECT_FACTORY; |
| 48 | |
| 49 | |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 50 | import org.onosproject.net.link.LinkEvent; |
| 51 | import org.onosproject.net.link.LinkListener; |
| 52 | import org.onosproject.net.link.LinkService; |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 53 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 54 | import org.onosproject.odtn.TapiResolver; |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 55 | import org.onosproject.odtn.TapiConnectivityConfig; |
| 56 | import org.onosproject.odtn.TapiConnectivityService; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 57 | import org.onosproject.odtn.TapiTopologyManager; |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 58 | import org.onosproject.odtn.config.TerminalDeviceConfig; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 59 | import org.onosproject.odtn.internal.DcsBasedTapiCommonRpc; |
| 60 | import org.onosproject.odtn.internal.DcsBasedTapiConnectivityRpc; |
hiroki | b8ddc3f | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 61 | import org.onosproject.odtn.internal.DefaultOdtnTerminalDeviceDriver; |
Yuta HIGUCHI | a9ae6e6 | 2018-02-26 12:56:20 -0800 | [diff] [blame] | 62 | import org.slf4j.Logger; |
| 63 | import org.slf4j.LoggerFactory; |
| 64 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 65 | // DCS / onos-yang-tools |
| 66 | import org.onosproject.config.DynamicConfigEvent; |
| 67 | import org.onosproject.config.DynamicConfigListener; |
| 68 | import org.onosproject.config.DynamicConfigService; |
Ramon Casellas | 390efe9 | 2018-03-01 11:45:27 +0100 | [diff] [blame] | 69 | import org.onosproject.yang.model.RpcRegistry; |
Ramon Casellas | 390efe9 | 2018-03-01 11:45:27 +0100 | [diff] [blame] | 70 | |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 71 | import static org.onosproject.config.DynamicConfigEvent.Type.NODE_ADDED; |
| 72 | import static org.onosproject.config.DynamicConfigEvent.Type.NODE_DELETED; |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 73 | import org.onosproject.core.ApplicationId; |
| 74 | import org.onosproject.core.CoreService; |
| 75 | import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView; |
| 76 | |
| 77 | import org.onosproject.net.intent.IntentService; |
| 78 | import org.onosproject.net.intent.Intent; |
| 79 | import org.onosproject.net.intent.IntentState; |
| 80 | import org.onosproject.net.intent.IntentEvent; |
| 81 | import org.onosproject.net.intent.IntentListener; |
| 82 | import static org.onosproject.net.intent.IntentState.FAILED; |
| 83 | import static org.onosproject.net.intent.IntentState.WITHDRAWN; |
| 84 | import org.onosproject.net.intent.Key; |
| 85 | import org.onosproject.net.intent.OpticalConnectivityIntent; |
| 86 | import org.onosproject.net.optical.OchPort; |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 87 | import org.onosproject.net.OduSignalType; |
| 88 | |
| 89 | import java.util.Objects; |
| 90 | import java.util.concurrent.CountDownLatch; |
| 91 | import java.util.concurrent.TimeUnit; |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 92 | |
Yuta HIGUCHI | a9ae6e6 | 2018-02-26 12:56:20 -0800 | [diff] [blame] | 93 | /** |
| 94 | * OSGi Component for ODTN Service application. |
| 95 | */ |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 96 | @Component(immediate = true, service = TapiConnectivityService.class) |
| 97 | public class ServiceApplicationComponent implements TapiConnectivityService { |
Yuta HIGUCHI | a9ae6e6 | 2018-02-26 12:56:20 -0800 | [diff] [blame] | 98 | |
| 99 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 100 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 102 | protected DynamicConfigService dynConfigService; |
Yuta HIGUCHI | a9ae6e6 | 2018-02-26 12:56:20 -0800 | [diff] [blame] | 103 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 105 | protected DeviceService deviceService; |
| 106 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 108 | protected IntentService intentService; |
| 109 | |
| 110 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 111 | protected LinkService linkService; |
| 112 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 113 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 114 | protected CoreService coreService; |
| 115 | |
| 116 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Yuta HIGUCHI | a9ae6e6 | 2018-02-26 12:56:20 -0800 | [diff] [blame] | 117 | protected NetworkConfigService netcfgService; |
| 118 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 119 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 120 | protected NetworkConfigRegistry netcfgRegistry; |
| 121 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 122 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Ramon Casellas | 390efe9 | 2018-03-01 11:45:27 +0100 | [diff] [blame] | 123 | protected RpcRegistry rpcRegistry; |
| 124 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 125 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 126 | protected TapiTopologyManager tapiTopologyManager; |
Ramon Casellas | 390efe9 | 2018-03-01 11:45:27 +0100 | [diff] [blame] | 127 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 128 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 129 | protected TapiResolver resolver; |
| 130 | |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 131 | private static final int WITHDRAW_EVENT_TIMEOUT_SECONDS = 5; |
| 132 | |
| 133 | private static final String APP_ID = "org.onosproject.odtn-service"; |
| 134 | |
| 135 | private ApplicationId appId; |
| 136 | |
Ramon Casellas | 390efe9 | 2018-03-01 11:45:27 +0100 | [diff] [blame] | 137 | // Listener for events from the DCS |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 138 | private final DynamicConfigListener dynamicConfigServiceListener = |
| 139 | new InternalDynamicConfigListener(); |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 140 | |
| 141 | private DeviceListener deviceListener = new InternalDeviceListener(); |
| 142 | private final LinkListener linkListener = new InternalLinkListener(); |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 143 | private final NetworkConfigListener netcfgListener = new InternalNetCfgListener(); |
Ramon Casellas | 390efe9 | 2018-03-01 11:45:27 +0100 | [diff] [blame] | 144 | |
| 145 | // Rpc Service for TAPI Connectivity |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 146 | private final DcsBasedTapiConnectivityRpc rpcTapiConnectivity = new DcsBasedTapiConnectivityRpc(); |
| 147 | private final DcsBasedTapiCommonRpc rpcTapiCommon = new DcsBasedTapiCommonRpc(); |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 148 | |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 149 | // FIXME create factory and register for all behaviours |
| 150 | private final ConfigFactory<ConnectPoint, TerminalDeviceConfig> factory = |
| 151 | new ConfigFactory<ConnectPoint, TerminalDeviceConfig>(CONNECT_POINT_SUBJECT_FACTORY, |
| 152 | TerminalDeviceConfig.class, TerminalDeviceConfig.CONFIG_KEY) { |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 153 | |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 154 | @Override |
| 155 | public TerminalDeviceConfig createConfig() { |
| 156 | return new TerminalDeviceConfig(); |
| 157 | } |
| 158 | }; |
| 159 | |
Yuta HIGUCHI | a9ae6e6 | 2018-02-26 12:56:20 -0800 | [diff] [blame] | 160 | @Activate |
| 161 | protected void activate() { |
| 162 | log.info("Started"); |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 163 | appId = coreService.registerApplication(APP_ID); |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 164 | dynConfigService.addListener(dynamicConfigServiceListener); |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 165 | deviceService.addListener(deviceListener); |
| 166 | linkService.addListener(linkListener); |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 167 | netcfgService.addListener(netcfgListener); |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 168 | netcfgRegistry.registerConfigFactory(factory); |
Ramon Casellas | 390efe9 | 2018-03-01 11:45:27 +0100 | [diff] [blame] | 169 | rpcRegistry.registerRpcService(rpcTapiConnectivity); |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 170 | rpcRegistry.registerRpcService(rpcTapiCommon); |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 171 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 172 | rpcTapiConnectivity.init(); |
| 173 | rpcTapiCommon.init(); |
Yuta HIGUCHI | a9ae6e6 | 2018-02-26 12:56:20 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Deactivate |
| 177 | protected void deactivate() { |
| 178 | log.info("Stopped"); |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 179 | rpcRegistry.unregisterRpcService(rpcTapiCommon); |
Ramon Casellas | 390efe9 | 2018-03-01 11:45:27 +0100 | [diff] [blame] | 180 | rpcRegistry.unregisterRpcService(rpcTapiConnectivity); |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 181 | netcfgRegistry.unregisterConfigFactory(factory); |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 182 | netcfgService.removeListener(netcfgListener); |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 183 | linkService.removeListener(linkListener); |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 184 | deviceService.removeListener(deviceListener); |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 185 | dynConfigService.removeListener(dynamicConfigServiceListener); |
Yuta HIGUCHI | a9ae6e6 | 2018-02-26 12:56:20 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Ramon Casellas | 390efe9 | 2018-03-01 11:45:27 +0100 | [diff] [blame] | 188 | |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 189 | /** |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 190 | * Process TAPI Event from NBI. |
| 191 | * |
| 192 | * @param config TAPI Connectivity config for the event |
| 193 | */ |
| 194 | public void processTapiEvent(TapiConnectivityConfig config) { |
| 195 | checkNotNull(config, "Config can't be null"); |
| 196 | Key key = Key.of(config.uuid(), appId); |
| 197 | // Setup the Intent |
| 198 | if (config.isSetup()) { |
| 199 | log.debug("TAPI config: {} to setup intent", config); |
| 200 | Intent intent = createOpticalIntent(config.leftCp(), config.rightCp(), key, appId); |
| 201 | intentService.submit(intent); |
| 202 | } else { |
| 203 | // Release the intent |
| 204 | Intent intent = intentService.getIntent(key); |
| 205 | if (intent == null) { |
| 206 | log.error("Intent for uuid {} does not exist", config.uuid()); |
| 207 | return; |
| 208 | } |
| 209 | log.debug("TAPI config: {} to purge intent {}", config, intent); |
| 210 | CountDownLatch latch = new CountDownLatch(1); |
| 211 | IntentListener listener = new DeleteListener(key, latch); |
| 212 | intentService.addListener(listener); |
| 213 | try { |
| 214 | /* |
| 215 | * RCAS: Note, withdraw is asynchronous. We cannot call purge |
| 216 | * directly, because at this point it remains in the "INSTALLED" |
| 217 | * state. |
| 218 | */ |
| 219 | intentService.withdraw(intent); |
| 220 | |
| 221 | /* |
| 222 | * org.onosproject.onos-core-net - 2.1.0.SNAPSHOT | |
| 223 | * Purge for intent 0x0 is rejected because intent state is INSTALLED |
| 224 | * intentService.purge(intent); |
| 225 | */ |
| 226 | try { |
| 227 | latch.await(WITHDRAW_EVENT_TIMEOUT_SECONDS, TimeUnit.SECONDS); |
| 228 | } catch (InterruptedException e) { |
| 229 | Thread.currentThread().interrupt(); |
| 230 | } |
| 231 | // double check the state |
| 232 | IntentState state = intentService.getIntentState(key); |
| 233 | if (state == WITHDRAWN || state == FAILED) { |
| 234 | intentService.purge(intent); |
| 235 | } |
| 236 | } finally { |
| 237 | intentService.removeListener(listener); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Returns a new optical intent created from the method parameters. |
| 244 | * |
| 245 | * @param ingress ingress description (device/port) |
| 246 | * @param egress egress description (device/port) |
| 247 | * @param key intent key |
| 248 | * @param appId application id. As per Intent class, it cannot be null |
| 249 | * |
| 250 | * @return created intent |
| 251 | */ |
| 252 | protected Intent createOpticalIntent(ConnectPoint ingress, ConnectPoint egress, |
| 253 | Key key, ApplicationId appId) { |
Andrea Campanella | 9da8e72 | 2019-03-12 18:35:39 -0700 | [diff] [blame^] | 254 | |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 255 | if (ingress == null || egress == null) { |
| 256 | log.error("Invalid endpoint(s) for optical intent: ingress {}, egress {}", |
| 257 | ingress, egress); |
| 258 | return null; |
| 259 | } |
| 260 | DeviceService ds = opticalView(deviceService); |
| 261 | Port srcPort = ds.getPort(ingress.deviceId(), ingress.port()); |
| 262 | Port dstPort = ds.getPort(egress.deviceId(), egress.port()); |
| 263 | if (srcPort == null || dstPort == null) { |
| 264 | log.error("Invalid port(s) for optical intent: src {}, dst {}", |
| 265 | srcPort, dstPort); |
| 266 | return null; |
| 267 | } |
| 268 | |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 269 | OduSignalType signalType = ((OchPort) srcPort).signalType(); |
Andrea Campanella | 9da8e72 | 2019-03-12 18:35:39 -0700 | [diff] [blame^] | 270 | return OpticalConnectivityIntent.builder() |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 271 | .appId(appId) |
| 272 | .key(key) |
| 273 | .src(ingress) |
| 274 | .dst(egress) |
| 275 | .signalType(signalType) |
Andrea Campanella | 9da8e72 | 2019-03-12 18:35:39 -0700 | [diff] [blame^] | 276 | .bidirectional(true) //TODO Revisit this. |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 277 | .build(); |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | |
| 281 | /** |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 282 | * Representation of internal listener, listening for device event. |
| 283 | */ |
| 284 | private class InternalDeviceListener implements DeviceListener { |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 285 | /** |
| 286 | * Process an Event from the Device Service. |
| 287 | * |
| 288 | * @param event device event |
| 289 | */ |
| 290 | @Override |
| 291 | public void event(DeviceEvent event) { |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 292 | |
Andrea Campanella | 77e9b33 | 2018-11-29 13:33:19 -0800 | [diff] [blame] | 293 | log.debug("Device event type: {}, subject: {}", event.type(), event.subject()); |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 294 | switch (event.type()) { |
| 295 | case DEVICE_ADDED: |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 296 | tapiTopologyManager.addDevice(event.subject()); |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 297 | break; |
| 298 | case DEVICE_REMOVED: |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 299 | tapiTopologyManager.removeDevice(event.subject()); |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 300 | break; |
| 301 | case PORT_ADDED: |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 302 | tapiTopologyManager.addPort(event.port()); |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 303 | break; |
| 304 | case PORT_REMOVED: |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 305 | tapiTopologyManager.removePort(event.port()); |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 306 | break; |
Ramon Casellas | 03f194f | 2018-11-15 16:06:02 +0100 | [diff] [blame] | 307 | // TODO: Process device / port updated events |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 308 | default: |
Ramon Casellas | 03f194f | 2018-11-15 16:06:02 +0100 | [diff] [blame] | 309 | log.warn("Unprocessed Event {}", event.type()); |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 310 | break; |
| 311 | } |
| 312 | |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Representation of internal listener, listening for link event. |
| 318 | */ |
| 319 | private class InternalLinkListener implements LinkListener { |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 320 | /** |
| 321 | * Process an Event from the Device Service. |
| 322 | * |
| 323 | * @param event link event |
| 324 | */ |
| 325 | @Override |
| 326 | public void event(LinkEvent event) { |
| 327 | Link link = event.subject(); |
| 328 | |
| 329 | switch (event.type()) { |
| 330 | case LINK_ADDED: |
| 331 | tapiTopologyManager.addLink(link); |
| 332 | break; |
| 333 | case LINK_REMOVED: |
| 334 | tapiTopologyManager.removeLink(link); |
| 335 | break; |
| 336 | default: |
Ramon Casellas | 03f194f | 2018-11-15 16:06:02 +0100 | [diff] [blame] | 337 | log.warn("Unknown Event {}", event.type()); |
hiroki | bca3e93 | 2018-05-15 15:25:54 -0700 | [diff] [blame] | 338 | break; |
| 339 | } |
| 340 | } |
| 341 | } |
Ramon Casellas | 390efe9 | 2018-03-01 11:45:27 +0100 | [diff] [blame] | 342 | |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 343 | |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 344 | /** |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 345 | * Representation of internal listener, listening for netcfg event. |
| 346 | */ |
| 347 | private class InternalNetCfgListener implements NetworkConfigListener { |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 348 | /** |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 349 | * Check if the netcfg event should be further processed. |
| 350 | * |
| 351 | * @param event config event |
| 352 | * @return true if event is supported; false otherwise |
| 353 | */ |
| 354 | @Override |
| 355 | public boolean isRelevant(NetworkConfigEvent event) { |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 356 | if (event.type() == CONFIG_ADDED || event.type() == CONFIG_UPDATED) { |
| 357 | if (event.config().orElse(null) instanceof TerminalDeviceConfig) { |
| 358 | return true; |
| 359 | } |
| 360 | } |
| 361 | if (event.type() == CONFIG_REMOVED) { |
| 362 | if (event.prevConfig().orElse(null) instanceof TerminalDeviceConfig) { |
| 363 | return true; |
| 364 | } |
| 365 | } |
| 366 | return false; |
| 367 | } |
| 368 | |
| 369 | /** |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 370 | * Process an Event from the NetCfg Service. |
| 371 | * |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 372 | * @param event event |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 373 | */ |
| 374 | @Override |
| 375 | public void event(NetworkConfigEvent event) { |
Andrea Campanella | 77e9b33 | 2018-11-29 13:33:19 -0800 | [diff] [blame] | 376 | log.debug("Event type: {}, subject: {}", event.type(), event.subject()); |
hiroki | b8ddc3f | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 377 | DeviceId did = ((ConnectPoint) event.subject()).deviceId(); |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 378 | TerminalDeviceConfig config = (TerminalDeviceConfig) event.config().get(); |
hiroki | b8ddc3f | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 379 | DefaultOdtnTerminalDeviceDriver driver = DefaultOdtnTerminalDeviceDriver.create(); |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 380 | log.debug("config: {}", config); |
hiroki | b8ddc3f | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 381 | switch (event.type()) { |
| 382 | case CONFIG_ADDED: |
| 383 | case CONFIG_UPDATED: |
hiroki | b8ddc3f | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 384 | driver.apply(did, config.clientCp().port(), config.subject().port(), config.isEnabled()); |
| 385 | break; |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 386 | |
hiroki | b8ddc3f | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 387 | case CONFIG_REMOVED: |
hiroki | b8ddc3f | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 388 | driver.apply(did, config.clientCp().port(), config.subject().port(), false); |
| 389 | break; |
| 390 | default: |
| 391 | log.error("Unsupported event type."); |
| 392 | } |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 396 | |
| 397 | /** |
| 398 | * Internal listener for tracking the intent deletion events. |
| 399 | */ |
| 400 | private class DeleteListener implements IntentListener { |
| 401 | final Key key; |
| 402 | final CountDownLatch latch; |
| 403 | |
| 404 | /** |
| 405 | * Default constructor. |
| 406 | * |
| 407 | * @param key key |
| 408 | * @param latch count down latch |
| 409 | */ |
| 410 | DeleteListener(Key key, CountDownLatch latch) { |
| 411 | this.key = key; |
| 412 | this.latch = latch; |
| 413 | } |
| 414 | |
| 415 | @Override |
| 416 | public void event(IntentEvent event) { |
| 417 | if (Objects.equals(event.subject().key(), key) && |
| 418 | (event.type() == IntentEvent.Type.WITHDRAWN || |
| 419 | event.type() == IntentEvent.Type.FAILED)) { |
| 420 | latch.countDown(); |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 425 | /** |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 426 | * Representation of internal listener, listening for dynamic config event. |
| 427 | */ |
| 428 | private class InternalDynamicConfigListener implements DynamicConfigListener { |
| 429 | |
| 430 | /** |
| 431 | * Check if the DCS event should be further processed. |
| 432 | * |
| 433 | * @param event config event |
| 434 | * @return true if event is supported; false otherwise |
| 435 | */ |
| 436 | @Override |
| 437 | public boolean isRelevant(DynamicConfigEvent event) { |
| 438 | // Only care about add and delete |
| 439 | if ((event.type() != NODE_ADDED) && |
| 440 | (event.type() != NODE_DELETED)) { |
| 441 | return false; |
| 442 | } |
| 443 | return true; |
| 444 | } |
| 445 | |
| 446 | |
| 447 | /** |
| 448 | * Process an Event from the Dynamic Configuration Store. |
| 449 | * |
| 450 | * @param event config event |
| 451 | */ |
| 452 | @Override |
| 453 | public void event(DynamicConfigEvent event) { |
| 454 | resolver.makeDirty(); |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 455 | } |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 456 | } |
Ramon Casellas | 390efe9 | 2018-03-01 11:45:27 +0100 | [diff] [blame] | 457 | |
Yuta HIGUCHI | a9ae6e6 | 2018-02-26 12:56:20 -0800 | [diff] [blame] | 458 | } |