Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.provider.te.topology; |
| 17 | |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 18 | import com.google.common.base.Preconditions; |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 19 | import org.apache.commons.io.IOUtils; |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.incubator.net.config.basics.ConfigException; |
| 28 | import org.onosproject.net.DeviceId; |
| 29 | import org.onosproject.net.config.ConfigFactory; |
| 30 | import org.onosproject.net.config.NetworkConfigEvent; |
| 31 | import org.onosproject.net.config.NetworkConfigListener; |
| 32 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 33 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 34 | import org.onosproject.net.provider.AbstractProvider; |
| 35 | import org.onosproject.net.provider.ProviderId; |
| 36 | import org.onosproject.protocol.rest.RestSBDevice; |
| 37 | import org.onosproject.protocol.restconf.RestConfSBController; |
chengfan | 9d60b6e | 2016-12-01 11:06:39 +0800 | [diff] [blame] | 38 | import org.onosproject.provider.te.utils.DefaultJsonCodec; |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 39 | import org.onosproject.provider.te.utils.RestconfNotificationEventProcessor; |
| 40 | import org.onosproject.provider.te.utils.TeTopologyRestconfEventListener; |
chengfan | 9d60b6e | 2016-12-01 11:06:39 +0800 | [diff] [blame] | 41 | import org.onosproject.provider.te.utils.YangCompositeEncodingImpl; |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 42 | import org.onosproject.tetopology.management.api.TeTopologyProvider; |
| 43 | import org.onosproject.tetopology.management.api.TeTopologyProviderRegistry; |
| 44 | import org.onosproject.tetopology.management.api.TeTopologyProviderService; |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 45 | import org.onosproject.tetopology.management.api.TeTopologyService; |
| 46 | import org.onosproject.tetopology.management.api.link.NetworkLink; |
| 47 | import org.onosproject.tetopology.management.api.link.NetworkLinkKey; |
| 48 | import org.onosproject.tetopology.management.api.node.NetworkNode; |
| 49 | import org.onosproject.tetopology.management.api.node.NetworkNodeKey; |
| 50 | import org.onosproject.teyang.utils.topology.LinkConverter; |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 51 | import org.onosproject.teyang.utils.topology.NetworkConverter; |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 52 | import org.onosproject.teyang.utils.topology.NodeConverter; |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 53 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev20151208.IetfNetwork; |
| 54 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev20151208.ietfnetwork.networks.Network; |
| 55 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev20151208.IetfNetworkTopology; |
| 56 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708.IetfTeTopology; |
Henry Yu | 8ac364b | 2016-12-15 18:24:20 -0500 | [diff] [blame] | 57 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708.ietftetopology |
| 58 | .IetfTeTopologyEvent; |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 59 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708.ietftetopology.TeLinkEvent; |
| 60 | import org.onosproject.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.te.topology.rev20160708.ietftetopology.TeNodeEvent; |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 61 | import org.onosproject.yms.ych.YangCodecHandler; |
| 62 | import org.onosproject.yms.ych.YangProtocolEncodingFormat; |
| 63 | import org.onosproject.yms.ych.YangResourceIdentifierType; |
| 64 | import org.onosproject.yms.ydt.YmsOperationType; |
| 65 | import org.onosproject.yms.ymsm.YmsService; |
| 66 | import org.slf4j.Logger; |
| 67 | |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 68 | import java.io.IOException; |
| 69 | import java.io.InputStream; |
| 70 | import java.io.StringWriter; |
| 71 | import java.nio.charset.StandardCharsets; |
| 72 | import java.util.HashSet; |
| 73 | import java.util.List; |
| 74 | import java.util.Set; |
| 75 | import java.util.concurrent.ExecutorService; |
| 76 | import java.util.concurrent.Executors; |
| 77 | |
| 78 | import static org.onlab.util.Tools.groupedThreads; |
| 79 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_ADDED; |
| 80 | import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UPDATED; |
| 81 | import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY; |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 82 | import static org.onosproject.provider.te.utils.TeTopologyRestconfEventType.TE_TOPOLOGY_LINK_NOTIFICATION; |
| 83 | import static org.onosproject.provider.te.utils.TeTopologyRestconfEventType.TE_TOPOLOGY_NODE_NOTIFICATION; |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 84 | import static org.slf4j.LoggerFactory.getLogger; |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * Provider for IETF TE Topology that use RESTCONF as means of communication. |
| 88 | */ |
| 89 | @Component(immediate = true) |
| 90 | public class TeTopologyRestconfProvider extends AbstractProvider |
| 91 | implements TeTopologyProvider { |
Henry Yu | 8ac364b | 2016-12-15 18:24:20 -0500 | [diff] [blame] | 92 | private static final String APP_NAME = "org.onosproject.teprovider"; |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 93 | private static final String RESTCONF = "restconf"; |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 94 | private static final String PROVIDER = |
| 95 | "org.onosproject.teprovider.restconf.domain"; |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 96 | private static final String IETF_NETWORK_URI = "ietf-network:networks"; |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 97 | private static final String IETF_NETWORKS_PREFIX = |
| 98 | "{\"ietf-network:networks\":"; |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 99 | private static final String TE_LINK_EVENT_PREFIX = |
| 100 | "{\"ietf-te-topology:te-link-event\":"; |
| 101 | private static final String TE_NODE_EVENT_PREFIX = |
| 102 | "{\"ietf-te-topology:te-node-event\":"; |
| 103 | private static final String IETF_NOTIFICATION_URI = "netconf"; |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 104 | private static final String JSON = "json"; |
| 105 | private static final String E_DEVICE_NULL = "Restconf device is null"; |
| 106 | |
| 107 | private final Logger log = getLogger(getClass()); |
| 108 | |
| 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 110 | protected DeviceProviderRegistry deviceProviderRegistry; |
| 111 | |
| 112 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 113 | protected TeTopologyProviderRegistry topologyProviderRegistry; |
| 114 | |
| 115 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 116 | protected RestConfSBController restconfClient; |
| 117 | |
| 118 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 119 | protected NetworkConfigRegistry cfgService; |
| 120 | |
| 121 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 122 | protected CoreService coreService; |
| 123 | |
| 124 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 125 | protected YmsService ymsService; |
| 126 | |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 127 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 128 | protected TeTopologyService teTopologyService; |
| 129 | |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 130 | private YangCodecHandler codecHandler; |
| 131 | |
| 132 | private TeTopologyProviderService topologyProviderService; |
| 133 | |
| 134 | private final ExecutorService executor = |
| 135 | Executors.newFixedThreadPool(5, groupedThreads("onos/restconfsbprovider", |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 136 | "device-installer-%d", log)); |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 137 | |
| 138 | private final ConfigFactory<ApplicationId, RestconfServerConfig> factory = |
| 139 | new ConfigFactory<ApplicationId, RestconfServerConfig>(APP_SUBJECT_FACTORY, |
| 140 | RestconfServerConfig.class, |
| 141 | "restconfDevices", |
| 142 | true) { |
| 143 | @Override |
| 144 | public RestconfServerConfig createConfig() { |
| 145 | return new RestconfServerConfig(); |
| 146 | } |
| 147 | }; |
| 148 | |
| 149 | private final NetworkConfigListener cfgLister = new InternalNetworkConfigListener(); |
| 150 | private ApplicationId appId; |
| 151 | |
| 152 | private Set<DeviceId> addedDevices = new HashSet<>(); |
| 153 | |
| 154 | @Activate |
| 155 | public void activate() { |
| 156 | // Get the codec handler. |
| 157 | codecHandler = ymsService.getYangCodecHandler(); |
| 158 | // Register all three IETF Topology YANG model schema with YMS. |
| 159 | codecHandler.addDeviceSchema(IetfNetwork.class); |
| 160 | codecHandler.addDeviceSchema(IetfNetworkTopology.class); |
| 161 | codecHandler.addDeviceSchema(IetfTeTopology.class); |
| 162 | // Register JSON CODEC functions |
chengfan | 9d60b6e | 2016-12-01 11:06:39 +0800 | [diff] [blame] | 163 | codecHandler.registerOverriddenCodec(new DefaultJsonCodec(ymsService), |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 164 | YangProtocolEncodingFormat.JSON); |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 165 | |
| 166 | appId = coreService.registerApplication(APP_NAME); |
| 167 | topologyProviderService = topologyProviderRegistry.register(this); |
| 168 | cfgService.registerConfigFactory(factory); |
| 169 | cfgService.addListener(cfgLister); |
| 170 | executor.execute(TeTopologyRestconfProvider.this::connectDevices); |
| 171 | log.info("Started"); |
| 172 | } |
| 173 | |
| 174 | @Deactivate |
| 175 | public void deactivate() { |
| 176 | cfgService.removeListener(cfgLister); |
| 177 | restconfClient.getDevices().keySet().forEach(this::deviceRemoved); |
| 178 | topologyProviderRegistry.unregister(this); |
| 179 | cfgService.unregisterConfigFactory(factory); |
| 180 | log.info("Stopped"); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Creates an instance of TeTopologyRestconf provider. |
| 185 | */ |
| 186 | public TeTopologyRestconfProvider() { |
| 187 | super(new ProviderId(RESTCONF, PROVIDER)); |
| 188 | } |
| 189 | |
| 190 | private void deviceAdded(RestSBDevice nodeId) { |
| 191 | Preconditions.checkNotNull(nodeId, E_DEVICE_NULL); |
| 192 | nodeId.setActive(true); |
| 193 | addedDevices.add(nodeId.deviceId()); |
| 194 | } |
| 195 | |
| 196 | private void deviceRemoved(DeviceId deviceId) { |
| 197 | Preconditions.checkNotNull(deviceId, E_DEVICE_NULL); |
| 198 | restconfClient.removeDevice(deviceId); |
| 199 | } |
| 200 | |
| 201 | private void connectDevices() { |
| 202 | |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 203 | RestconfServerConfig cfg = cfgService.getConfig(appId, |
| 204 | RestconfServerConfig.class); |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 205 | try { |
| 206 | if (cfg != null && cfg.getDevicesAddresses() != null) { |
| 207 | //Precomputing the devices to be removed |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 208 | Set<RestSBDevice> toBeRemoved = new HashSet<>(restconfClient. |
| 209 | getDevices().values()); |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 210 | toBeRemoved.removeAll(cfg.getDevicesAddresses()); |
| 211 | //Adding new devices |
| 212 | for (RestSBDevice device : cfg.getDevicesAddresses()) { |
| 213 | device.setActive(false); |
| 214 | restconfClient.addDevice(device); |
| 215 | deviceAdded(device); |
| 216 | } |
| 217 | |
| 218 | //Removing devices not wanted anymore |
| 219 | toBeRemoved.forEach(device -> deviceRemoved(device.deviceId())); |
| 220 | } |
| 221 | } catch (ConfigException e) { |
| 222 | log.error("Configuration error {}", e); |
| 223 | } |
| 224 | |
| 225 | // Discover the topology from RESTCONF server |
| 226 | addedDevices.forEach(this::retrieveTopology); |
| 227 | addedDevices.clear(); |
| 228 | } |
| 229 | |
| 230 | private void retrieveTopology(DeviceId deviceId) { |
| 231 | // Retrieve IETF Network at top level. |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 232 | InputStream jsonStream = restconfClient.get(deviceId, |
| 233 | IETF_NETWORK_URI, |
| 234 | JSON); |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 235 | if (jsonStream == null) { |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 236 | log.warn("Unable to retrieve network Topology from restconf " + |
| 237 | "server {}", deviceId); |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 238 | return; |
| 239 | } |
| 240 | |
| 241 | // Need to convert Input stream to String. |
| 242 | StringWriter writer = new StringWriter(); |
| 243 | try { |
| 244 | IOUtils.copy(jsonStream, writer, StandardCharsets.UTF_8); |
| 245 | } catch (IOException e) { |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 246 | log.warn("There is an exception {} for copy jsonStream to " + |
| 247 | "stringWriter for restconf {}", |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 248 | e.getMessage(), deviceId); |
| 249 | return; |
| 250 | } |
| 251 | String jsonString = writer.toString(); |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 252 | String networkLevelJsonString = removePrefixTagFromJson(jsonString, |
| 253 | IETF_NETWORKS_PREFIX); |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 254 | |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 255 | YangCompositeEncodingImpl yce = |
| 256 | new YangCompositeEncodingImpl(YangResourceIdentifierType.URI, |
| 257 | IETF_NETWORK_URI, |
| 258 | networkLevelJsonString); |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 259 | |
| 260 | Object yo = codecHandler.decode(yce, |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 261 | YangProtocolEncodingFormat.JSON, |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 262 | YmsOperationType.QUERY_REPLY); |
| 263 | |
| 264 | if ((yo == null)) { |
| 265 | log.error("YMS decoder returns {} for restconf {}", yo, deviceId); |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | // YMS returns an ArrayList in a single Object (i.e. yo in this case) |
| 270 | // this means yo is actually an ArrayList of size 1 |
| 271 | IetfNetwork ietfNetwork = ((List<IetfNetwork>) yo).get(0); |
| 272 | |
| 273 | if (ietfNetwork.networks() != null && |
| 274 | ietfNetwork.networks().network() != null) { |
| 275 | //Convert the YO to TE Core data and update TE Core. |
| 276 | for (Network nw : ietfNetwork.networks().network()) { |
| 277 | topologyProviderService.networkUpdated( |
Yixiao Chen | 265b3bb | 2017-01-13 10:17:03 -0500 | [diff] [blame] | 278 | NetworkConverter.yang2TeSubsystemNetwork(nw, ietfNetwork.networks(), deviceId)); |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | //TODO: Uncomment when YMS fixes the issue in NetworkState translation (network-ref) |
| 283 | // org.onosproject.tetopology.management.api.Networks networks = |
| 284 | // NetworkConverter.yang2TeSubsystemNetworks(ietfNetwork.networks(), |
| 285 | // ietfNetwork.networksState()); |
| 286 | // if (networks == null || networks.networks() == null) { |
| 287 | // log.error("Yang2Te returns null for restconf {}", deviceId); |
| 288 | // return; |
| 289 | // } |
| 290 | // for (org.onosproject.tetopology.management.api.Network network : networks.networks()) { |
| 291 | // topologyProviderService.networkUpdated(network); |
| 292 | // } |
| 293 | |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 294 | subscribeRestconfNotification(deviceId); |
| 295 | } |
| 296 | |
| 297 | private void subscribeRestconfNotification(DeviceId deviceId) { |
| 298 | |
| 299 | TeTopologyRestconfEventListener listener = |
| 300 | new TeTopologyRestconfEventListener(); |
| 301 | |
| 302 | listener.addCallbackFunction(TE_TOPOLOGY_LINK_NOTIFICATION, |
| 303 | new InternalLinkEventProcessor()); |
| 304 | listener.addCallbackFunction(TE_TOPOLOGY_NODE_NOTIFICATION, |
| 305 | new InternalNodeEventProcessor()); |
| 306 | |
| 307 | if (!restconfClient.isNotificationEnabled(deviceId)) { |
| 308 | restconfClient.enableNotifications(deviceId, |
| 309 | IETF_NOTIFICATION_URI, |
| 310 | "application/json", |
| 311 | listener); |
| 312 | } else { |
| 313 | restconfClient.addNotificationListener(deviceId, listener); |
| 314 | } |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 315 | } |
| 316 | |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 317 | private String removePrefixTagFromJson(String jsonString, String prefixTag) { |
| 318 | if (jsonString.startsWith(prefixTag)) { |
| 319 | return jsonString.substring(prefixTag.length(), jsonString.length() - 1); |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 320 | } |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 321 | return jsonString; |
| 322 | } |
| 323 | |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 324 | private class InternalLinkEventProcessor implements |
| 325 | RestconfNotificationEventProcessor<String> { |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 326 | |
| 327 | @Override |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 328 | public void processEventPayload(String payload) { |
| 329 | String linkString = removePrefixTagFromJson(payload, |
| 330 | TE_LINK_EVENT_PREFIX); |
| 331 | log.debug("link event={}", linkString); |
| 332 | handleRestconfLinkNotification(linkString); |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | private void handleRestconfLinkNotification(String linkString) { |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 336 | |
Henry Yu | 8ac364b | 2016-12-15 18:24:20 -0500 | [diff] [blame] | 337 | IetfTeTopologyEvent event = convertJson2IetfTeTopologyEvent( |
| 338 | "ietf-te-topology:te-link-event", |
| 339 | linkString); |
| 340 | if (event == null) { |
| 341 | log.error("ERROR: json to YO conversion failure"); |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 342 | return; |
| 343 | } |
| 344 | |
Henry Yu | 8ac364b | 2016-12-15 18:24:20 -0500 | [diff] [blame] | 345 | if (event.type() != IetfTeTopologyEvent.Type.TE_LINK_EVENT) { |
| 346 | log.error("ERROR: wrong YO event type: {}", event.type()); |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | TeLinkEvent teLinkEvent = event.subject().teLinkEvent(); |
| 351 | |
| 352 | log.debug("TeLinkEvent: {}", teLinkEvent); |
| 353 | |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 354 | NetworkLinkKey linkKey = LinkConverter.yangLinkEvent2NetworkLinkKey( |
Henry Yu | 8ac364b | 2016-12-15 18:24:20 -0500 | [diff] [blame] | 355 | teLinkEvent); |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 356 | |
| 357 | NetworkLink networkLink = LinkConverter.yangLinkEvent2NetworkLink( |
Henry Yu | 8ac364b | 2016-12-15 18:24:20 -0500 | [diff] [blame] | 358 | teLinkEvent, |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 359 | teTopologyService); |
| 360 | |
Henry Yu | 8ac364b | 2016-12-15 18:24:20 -0500 | [diff] [blame] | 361 | if (networkLink == null) { |
| 362 | log.error("ERROR: yangLinkEvent2NetworkLink returns null"); |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | log.debug("networkLink: {}", networkLink); |
| 367 | |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 368 | topologyProviderService.linkUpdated(linkKey, networkLink); |
| 369 | } |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 370 | } |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 371 | |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 372 | private class InternalNodeEventProcessor implements |
| 373 | RestconfNotificationEventProcessor<String> { |
Henry Yu | 8ac364b | 2016-12-15 18:24:20 -0500 | [diff] [blame] | 374 | |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 375 | @Override |
| 376 | public void processEventPayload(String payload) { |
| 377 | String nodeString = removePrefixTagFromJson(payload, TE_NODE_EVENT_PREFIX); |
| 378 | log.debug("node event={}", nodeString); |
| 379 | handleRestconfNodeNotification(nodeString); |
Henry Yu | 8ac364b | 2016-12-15 18:24:20 -0500 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | private void handleRestconfNodeNotification(String nodeString) { |
| 383 | |
| 384 | IetfTeTopologyEvent event = convertJson2IetfTeTopologyEvent( |
| 385 | "ietf-te-topology:te-node-event", |
| 386 | nodeString); |
| 387 | |
| 388 | if (event == null) { |
| 389 | log.error("ERROR: json to YO conversion failure"); |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 390 | return; |
| 391 | } |
| 392 | |
Henry Yu | 8ac364b | 2016-12-15 18:24:20 -0500 | [diff] [blame] | 393 | if (event.type() != IetfTeTopologyEvent.Type.TE_NODE_EVENT) { |
| 394 | log.error("ERROR: wrong YO event type: {}", event.type()); |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | TeNodeEvent teNodeEvent = event.subject().teNodeEvent(); |
| 399 | |
| 400 | log.debug("TeNodeEvent: {}", teNodeEvent); |
| 401 | |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 402 | NetworkNodeKey nodeKey = NodeConverter.yangNodeEvent2NetworkNodeKey( |
Henry Yu | 8ac364b | 2016-12-15 18:24:20 -0500 | [diff] [blame] | 403 | teNodeEvent); |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 404 | |
| 405 | NetworkNode networkNode = NodeConverter.yangNodeEvent2NetworkNode( |
Henry Yu | 8ac364b | 2016-12-15 18:24:20 -0500 | [diff] [blame] | 406 | teNodeEvent, |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 407 | teTopologyService); |
| 408 | |
Henry Yu | 8ac364b | 2016-12-15 18:24:20 -0500 | [diff] [blame] | 409 | if (networkNode == null) { |
| 410 | log.error("ERROR: yangNodeEvent2NetworkNode returns null"); |
| 411 | return; |
| 412 | } |
| 413 | |
Henry Yu | 4b4a7eb | 2016-11-09 20:07:53 -0500 | [diff] [blame] | 414 | topologyProviderService.nodeUpdated(nodeKey, networkNode); |
| 415 | } |
| 416 | } |
Henry Yu | 05dcc21 | 2017-01-05 16:05:26 -0500 | [diff] [blame] | 417 | |
| 418 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 419 | |
| 420 | @Override |
| 421 | public void event(NetworkConfigEvent event) { |
| 422 | executor.execute(TeTopologyRestconfProvider.this::connectDevices); |
| 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public boolean isRelevant(NetworkConfigEvent event) { |
| 427 | return event.configClass().equals(RestconfServerConfig.class) && |
| 428 | (event.type() == CONFIG_ADDED || |
| 429 | event.type() == CONFIG_UPDATED); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | private IetfTeTopologyEvent convertJson2IetfTeTopologyEvent(String uriString, |
| 434 | String jsonBody) { |
| 435 | |
| 436 | YangCompositeEncodingImpl yce = |
| 437 | new YangCompositeEncodingImpl(YangResourceIdentifierType.URI, |
| 438 | uriString, |
| 439 | jsonBody); |
| 440 | Object yo = codecHandler.decode(yce, |
| 441 | YangProtocolEncodingFormat.JSON, |
| 442 | YmsOperationType.NOTIFICATION); |
| 443 | |
| 444 | if (yo == null) { |
| 445 | log.error("YMS decoder error"); |
| 446 | return null; |
| 447 | } |
| 448 | |
| 449 | if (!(yo instanceof IetfTeTopologyEvent)) { |
| 450 | log.error("ERROR: YO is not IetfTeTopologyEvent"); |
| 451 | return null; |
| 452 | } |
| 453 | |
| 454 | return (IetfTeTopologyEvent) yo; |
| 455 | } |
Yixiao Chen | 5ece00f | 2016-09-14 16:23:24 -0400 | [diff] [blame] | 456 | } |