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 static org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus.Connected;
12 import static org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus.Connecting;
13 import static org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus.UnableToConnect;
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.function.Function;
18 import java.util.stream.Collectors;
20 import javax.annotation.Nonnull;
21 import javax.annotation.Nullable;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
25 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
26 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
29 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
30 import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.writer.NodeWriter;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNode;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNodeBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNodeKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionParameters;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.AvailableCapabilities;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 import com.google.common.base.Optional;
54 import com.google.common.base.Preconditions;
55 import com.google.common.util.concurrent.CheckedFuture;
56 import com.google.common.util.concurrent.FutureCallback;
57 import com.google.common.util.concurrent.Futures;
58 import com.google.common.util.concurrent.ListenableFuture;
59 import com.google.common.util.concurrent.MoreExecutors;
61 public class NodeManager {
63 private static final TopologyId TOPOLOGY_ID = new TopologyId("topology-netconf");
64 private static final Logger LOG = LoggerFactory.getLogger(NodeManager.class);
65 private final DataBroker dataBroker;
66 private final MountPointService mountService;
67 private final List<String> requiredCapabilities;
69 public NodeManager(final DataBroker dataBroker, final BindingAwareBroker.ProviderContext session) {
70 this.dataBroker = Preconditions.checkNotNull(dataBroker);
71 mountService = Preconditions.checkNotNull(session.getSALService(MountPointService.class));
72 requiredCapabilities = new RequiredCapabilities().initializeRequiredCapabilities();
75 public void syncNodes(final Node dataAfter, final Node dataBefore) {
77 if (dataBefore == null && dataAfter != null) {
78 createNode(dataAfter);
80 // Connected/disconnected node
81 if (dataBefore != null && dataAfter != null) {
82 updateNode(dataAfter);
85 if (dataBefore != null && dataAfter == null) {
86 removeNode(dataBefore);
90 private void createNode(final Node node) {
91 LOG.info("Registering new node {}", node.getNodeId().getValue());
92 final NetconfNode netconfNode = getNodeAugmentation(node);
93 if (netconfNode == null) {
96 final ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
97 switch (connectionStatus) {
99 LOG.info("Connecting device {} ...", node.getNodeId().getValue());
103 resolveConnectedNode(node, netconfNode);
106 case UnableToConnect: {
107 LOG.info("Unable to connect device {}", node.getNodeId().getValue());
114 * Update previously added node. According to actual connection status, appropriate action is performed
116 * @param node to resolve
118 private void updateNode(final Node node) {
119 final NetconfNode netconfNode = getNodeAugmentation(node);
120 if (netconfNode == null || netconfNode.getConnectionStatus() == null) {
121 LOG.info("Node {} does not contain connection status", node.getNodeId().getValue());
124 final ConnectionStatus afterNodeStatus = netconfNode.getConnectionStatus();
125 if (afterNodeStatus.equals(Connected)) {
126 resolveConnectedNode(node, netconfNode);
128 if (afterNodeStatus.equals(Connecting)) {
129 LOG.info("Node {} has been disconnected, removing from available nodes", node.getNodeId().getValue());
130 resolveDisconnectedNode(node);
132 if (afterNodeStatus.equals(UnableToConnect)) {
133 LOG.info("Unable to connect node {}, removing from available nodes", node.getNodeId().getValue());
134 resolveDisconnectedNode(node);
139 * Removes previously added node. This node is also disconnected and removed from available nodes
141 * @param node to remove
143 private void removeNode(final Node node) {
144 Futures.addCallback(resolveDisconnectedNode(node), new FutureCallback<Boolean>() {
146 public void onSuccess(@Nullable Boolean result) {
147 if (Boolean.TRUE.equals(result)) {
148 LOG.info("Node {} has been removed", node.getNodeId().getValue());
150 LOG.warn("Failed to remove node {}", node.getNodeId().getValue());
155 public void onFailure(@Nullable Throwable throwable) {
156 LOG.warn("Exception thrown when removing node... {}", throwable);
158 }, MoreExecutors.directExecutor());
162 * Resolve node with {@link ConnectionStatus#Connected}. This node is reachable and can be added to nodes available
165 * @param node to add to available nodes
166 * @param netconfNode node's netconf augmentation
168 private void resolveConnectedNode(final Node node, @Nonnull final NetconfNode netconfNode) {
169 final InstanceIdentifier mountPointIid = getMountpointIid(node);
170 // Mountpoint iid == path in renderer-node
171 final RendererNode rendererNode = remapNode(mountPointIid);
172 final NodeWriter nodeWriter = new NodeWriter();
173 nodeWriter.cache(rendererNode);
174 if (!isCapableNetconfDevice(node, netconfNode)) {
175 resolveDisconnectedNode(node);
178 final IpAddress managementIpAddress = netconfNode.getHost().getIpAddress();
179 if (managementIpAddress == null) {
180 LOG.warn("Node {} does not contain management ip address", node.getNodeId().getValue());
181 resolveDisconnectedNode(node);
184 Futures.addCallback(nodeWriter.commitToDatastore(dataBroker), new FutureCallback<Boolean>() {
186 public void onSuccess(@Nullable Boolean result) {
187 if (Boolean.TRUE.equals(result)) {
188 LOG.info("Node {} is ready, added to available nodes for IOS-XE Renderer", node.getNodeId().getValue());
190 LOG.warn("Connected node {} has not been resolved", node.getNodeId().getValue());
195 public void onFailure(@Nullable Throwable throwable) {
196 LOG.warn("Exception thrown when resolving node... {}", throwable);
198 }, MoreExecutors.directExecutor());
202 * Depending on action, this method is called when node is not reachable anymore. Such a node is removed from nodes
203 * available for renderers. Reasons why the node is offline can vary, therefore logging should be handled outside
205 * @param node to remove from available nodes
206 * @return true if removed, false otherwise
208 private ListenableFuture<Boolean> resolveDisconnectedNode(final Node node) {
209 final InstanceIdentifier mountPointIid = getMountpointIid(node);
210 final RendererNode rendererNode = remapNode(mountPointIid);
211 final NodeWriter nodeWriter = new NodeWriter();
212 nodeWriter.cache(rendererNode);
213 return nodeWriter.removeFromDatastore(dataBroker);
217 * Node is remapped as renderer node with instance identifier. Used when reporting status for renderer manager
219 * @param path node IID
220 * @return {@link RendererNode} object with path
222 private RendererNode remapNode(final InstanceIdentifier path) {
223 final RendererNodeBuilder rendererNodeBuilder = new RendererNodeBuilder();
224 rendererNodeBuilder.setKey(new RendererNodeKey(path))
226 return rendererNodeBuilder.build();
229 private InstanceIdentifier getMountpointIid(final Node node) {
230 return InstanceIdentifier.builder(NetworkTopology.class)
231 .child(Topology.class, new TopologyKey(TOPOLOGY_ID))
232 .child(Node.class, new NodeKey(node.getNodeId())).build();
235 private boolean isCapableNetconfDevice(final Node node, @Nonnull final NetconfNode netconfAugmentation) {
236 final AvailableCapabilities available = netconfAugmentation.getAvailableCapabilities();
237 if (available == null || available.getAvailableCapability() == null || available.getAvailableCapability().isEmpty()) {
238 LOG.warn("Node {} does not contain any capabilities", node.getNodeId().getValue());
241 if (!capabilityCheck(netconfAugmentation.getAvailableCapabilities().getAvailableCapability())) {
242 LOG.warn("Node {} does not contain all capabilities required by io-xe-renderer",
243 node.getNodeId().getValue());
249 private boolean capabilityCheck(final List<AvailableCapability> capabilities) {
250 final List<String> availableCapabilities = capabilities.stream()
251 .map(AvailableCapability::getCapability)
252 .collect(Collectors.toList());
253 return requiredCapabilities.stream()
254 .allMatch(availableCapabilities::contains);
258 DataBroker getNodeMountPoint(final InstanceIdentifier mountPointIid) {
259 if (mountPointIid == null) {
262 final MountPoint mountPoint = ((Function<InstanceIdentifier, MountPoint>) instanceIdentifier -> {
263 Optional<MountPoint> optionalObject = mountService.getMountPoint(mountPointIid);
264 if (optionalObject.isPresent()) {
265 return optionalObject.get();
267 LOG.debug("Cannot obtain mountpoint with IID {}", mountPointIid);
269 }).apply(mountPointIid);
270 if (mountPoint == null) {
273 return ((Function<MountPoint, DataBroker>) mountPointParam -> {
274 Optional<DataBroker> optionalDataBroker = mountPointParam.getService(DataBroker.class);
275 if (optionalDataBroker.isPresent()) {
276 return optionalDataBroker.get();
278 LOG.debug("Cannot obtain data broker from mountpoint {}", mountPointParam);
280 }).apply(mountPoint);
283 NodeId getNodeIdByMountpointIid(final InstanceIdentifier mountpointIid) {
284 final NodeKey identifier = (NodeKey) mountpointIid.firstKeyOf(Node.class);
285 return identifier.getNodeId();
288 java.util.Optional<String> getNodeManagementIpByMountPointIid(final InstanceIdentifier<?> mountpointIid) {
289 final NodeId nodeId = getNodeIdByMountpointIid(mountpointIid);
290 final InstanceIdentifier<Node> nodeIid = InstanceIdentifier.builder(NetworkTopology.class)
291 .child(Topology.class, new TopologyKey(new TopologyId(NodeManager.TOPOLOGY_ID)))
292 .child(Node.class, new NodeKey(nodeId))
294 final ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
295 final CheckedFuture<Optional<Node>, ReadFailedException> submitFuture =
296 rTx.read(LogicalDatastoreType.CONFIGURATION, nodeIid);
299 Optional<Node> nodeOptional = submitFuture.checkedGet();
300 if (nodeOptional.isPresent()) {
301 final NetconfNode netconfNode = getNodeAugmentation(nodeOptional.get());
302 return java.util.Optional.ofNullable(netconfNode)
303 .map(NetconfNodeConnectionParameters::getHost)
304 .map(Host::getIpAddress)
305 .map(IpAddress::getIpv4Address)
306 .map(Ipv4Address::getValue);
308 } catch (ReadFailedException e) {
309 LOG.warn("Read node failed {}", nodeId, e);
311 return java.util.Optional.empty();
314 private NetconfNode getNodeAugmentation(final Node node) {
315 final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
316 if (netconfNode == null) {
317 LOG.warn("Node {} is not a netconf device", node.getNodeId().getValue());
323 private static class RequiredCapabilities {
325 private static final String NED ="(urn:ios?revision=2016-03-08)ned";
326 private static final String TAILF_COMMON = "(http://tail-f.com/yang/common?revision=2015-05-22)tailf-common";
327 private static final String TAILF_CLI_EXTENSION = "(http://tail-f.com/yang/common?revision=2015-03-19)tailf-cli-extensions";
328 private static final String TAILF_META_EXTENSION = "(http://tail-f.com/yang/common?revision=2013-11-07)tailf-meta-extensions";
329 private static final String IETF_YANG_TYPES = "(urn:ietf:params:xml:ns:yang:ietf-yang-types?revision=2013-07-15)ietf-yang-types";
330 private static final String IETF_INET_TYPES = "(urn:ietf:params:xml:ns:yang:ietf-inet-types?revision=2013-07-15)ietf-inet-types";
333 * Initialize all common capabilities required by IOS-XE renderer. Any connected node is examined whether it's
334 * an appropriate device to handle configuration created by this renderer. A device has to support all capabilities
337 * @return list of string representations of required capabilities
339 List<String> initializeRequiredCapabilities() {
340 final String capabilityEntries[] = {NED, TAILF_COMMON, TAILF_CLI_EXTENSION, TAILF_META_EXTENSION,
341 IETF_YANG_TYPES, IETF_INET_TYPES};
342 return Arrays.asList(capabilityEntries);