2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.manager;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
15 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
17 import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.util.NodeWriter;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.RendererName;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNodeBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNodeKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import java.util.Arrays;
35 import java.util.HashMap;
36 import java.util.List;
39 import static org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus.Connected;
40 import static org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus.Connecting;
41 import static org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus.UnableToConnect;
43 public class NodeManager {
45 public static final RendererName iosXeRenderer = new RendererName("ios-xe-renderer");
46 private static final TopologyId TOPOLOGY_ID = new TopologyId("topology-netconf");
47 private static final Logger LOG = LoggerFactory.getLogger(NodeManager.class);
48 private static final Map<InstanceIdentifier, DataBroker> netconfNodeCache = new HashMap<>();
49 private final DataBroker dataBroker;
50 private final MountPointService mountService;
51 private final List<String> requiredCapabilities;
53 public NodeManager(final DataBroker dataBroker, BindingAwareBroker.ProviderContext session) {
54 this.dataBroker = Preconditions.checkNotNull(dataBroker);
55 mountService = Preconditions.checkNotNull(session.getSALService(MountPointService.class));
56 requiredCapabilities = new RequiredCapabilities().initializeRequiredCapabilities();
59 static DataBroker getDataBrokerFromCache(InstanceIdentifier iid) {
60 return netconfNodeCache.get(iid); // TODO read from DS
63 public void syncNodes(Node dataAfter, Node dataBefore) {
65 if (dataBefore == null && dataAfter != null) {
66 createNode(dataAfter);
68 // Connected/disconnected node
69 if (dataBefore != null && dataAfter != null) {
70 updateNode(dataAfter);
73 if (dataBefore != null && dataAfter == null) {
74 removeNode(dataBefore);
78 private void createNode(Node node) {
79 LOG.info("Registering new node {}", node.getNodeId().getValue());
80 NetconfNode netconfNode = getNodeAugmentation(node);
81 if (netconfNode == null) {
84 NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
85 switch (connectionStatus) {
87 LOG.info("Connecting device {} ...", node.getNodeId().getValue());
91 resolveConnectedNode(node, netconfNode);
92 LOG.info("Node {} is ready, added to available nodes for IOS-XE Renderer", node.getNodeId().getValue());
94 case UnableToConnect: {
95 LOG.info("Unable to connect device {}", node.getNodeId().getValue());
101 private void updateNode(Node node) {
102 NetconfNode netconfNode = getNodeAugmentation(node);
103 if (netconfNode == null || netconfNode.getConnectionStatus() == null) {
106 NetconfNodeConnectionStatus.ConnectionStatus afterNodeStatus = netconfNode.getConnectionStatus();
107 if (afterNodeStatus.equals(Connected)) {
108 resolveConnectedNode(node, netconfNode);
109 LOG.info("Node {} is ready, added to available nodes for IOS-XE Renderer", node.getNodeId().getValue());
111 if (afterNodeStatus.equals(Connecting)) {
112 resolveDisconnectedNode(node);
113 LOG.info("Node {} has been disconnected, removing from available nodes", node.getNodeId().getValue());
115 if (afterNodeStatus.equals(UnableToConnect)) {
116 resolveDisconnectedNode(node);
117 LOG.info("Unable to connect node {}, removing from available nodes", node.getNodeId().getValue());
121 private void removeNode(Node node) {
122 resolveDisconnectedNode(node);
123 LOG.info("Node {} has been removed", node.getNodeId().getValue());
126 private void resolveConnectedNode(Node node, NetconfNode netconfNode) {
127 InstanceIdentifier mountPointIid = getMountpointIid(node);
128 // Mountpoint iid == path in renderer-node
129 RendererNode rendererNode = remapNode(mountPointIid);
130 DataBroker mountpoint = getNodeMountPoint(mountPointIid);
131 NodeWriter nodeWriter = new NodeWriter();
132 nodeWriter.cache(rendererNode);
133 if (isCapableNetconfDevice(node, netconfNode)) {
136 nodeWriter.commitToDatastore(dataBroker);
137 netconfNodeCache.put(mountPointIid, mountpoint);
140 private void resolveDisconnectedNode(Node node) {
141 InstanceIdentifier mountPointIid = getMountpointIid(node);
142 RendererNode rendererNode = remapNode(mountPointIid);
143 NodeWriter nodeWriter = new NodeWriter();
144 nodeWriter.cache(rendererNode);
145 nodeWriter.removeFromDatastore(dataBroker);
146 netconfNodeCache.remove(mountPointIid);
149 private RendererNode remapNode(InstanceIdentifier path) {
150 RendererNodeBuilder rendererNodeBuilder = new RendererNodeBuilder();
151 rendererNodeBuilder.setKey(new RendererNodeKey(path))
153 return rendererNodeBuilder.build();
156 private InstanceIdentifier getMountpointIid(Node node) {
157 return InstanceIdentifier.builder(NetworkTopology.class)
158 .child(Topology.class, new TopologyKey(TOPOLOGY_ID))
159 .child(Node.class, new NodeKey(node.getNodeId())).build();
162 private boolean isCapableNetconfDevice(Node node, NetconfNode netconfAugmentation) {
163 if (netconfAugmentation.getAvailableCapabilities() == null ||
164 netconfAugmentation.getAvailableCapabilities().getAvailableCapability() == null ||
165 netconfAugmentation.getAvailableCapabilities().getAvailableCapability().isEmpty()) {
166 LOG.warn("Node {} does not contain any capabilities", node.getNodeId().getValue());
169 if (!capabilityCheck(netconfAugmentation.getAvailableCapabilities().getAvailableCapability())) {
170 LOG.warn("Node {} does not contain all capabilities required by io-xe-renderer",
171 node.getNodeId().getValue());
177 private boolean capabilityCheck(final List<String> capabilities) {
178 for (String requiredCapability : requiredCapabilities) {
179 if (!capabilities.contains(requiredCapability)) {
186 private DataBroker getNodeMountPoint(InstanceIdentifier mountPointIid) {
187 Optional<MountPoint> optionalObject = mountService.getMountPoint(mountPointIid);
188 MountPoint mountPoint;
189 if (optionalObject.isPresent()) {
190 mountPoint = optionalObject.get();
191 if (mountPoint != null) {
192 Optional<DataBroker> optionalDataBroker = mountPoint.getService(DataBroker.class);
193 if (optionalDataBroker.isPresent()) {
194 return optionalDataBroker.get();
196 LOG.debug("Cannot obtain data broker from mountpoint {}", mountPoint);
199 LOG.debug("Cannot obtain mountpoint with IID {}", mountPointIid);
205 private NetconfNode getNodeAugmentation(Node node) {
206 NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
207 if (netconfNode == null) {
208 LOG.warn("Node {} is not a netconf device", node.getNodeId().getValue());
214 private class RequiredCapabilities {
216 private static final String ned = "(urn:ios?revision=2016-03-08)ned";
217 private static final String tailfCommon = "(http://tail-f.com/yang/common?revision=2015-05-22)tailf-common";
218 private static final String tailfCliExtension = "(http://tail-f.com/yang/common?revision=2015-03-19)tailf-cli-extensions";
219 private static final String tailfMetaExtension = "(http://tail-f.com/yang/common?revision=2013-11-07)tailf-meta-extensions";
220 private static final String ietfYangTypes = "(urn:ietf:params:xml:ns:yang:ietf-yang-types?revision=2013-07-15)ietf-yang-types";
221 private static final String ietfInetTypes = "(urn:ietf:params:xml:ns:yang:ietf-inet-types?revision=2013-07-15)ietf-inet-types";
224 * Initialize all common capabilities required by IOS-XE renderer. Any connected node is examined whether it's
225 * an appropriate device to handle configuration created by this renderer. A device must support all capabilities
228 * @return list of string representations of required capabilities
230 List<String> initializeRequiredCapabilities() {
231 String writableDataStore = "urn:ietf:params:netconf:capability:writable-running:1.0";
232 String capabilityEntries[] = {ned, tailfCommon, tailfCliExtension, tailfMetaExtension, ietfYangTypes,
233 ietfInetTypes, writableDataStore};
234 return Arrays.asList(capabilityEntries);