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;
60 public class NodeManager {
62 private static final TopologyId TOPOLOGY_ID = new TopologyId("topology-netconf");
63 private static final Logger LOG = LoggerFactory.getLogger(NodeManager.class);
64 private final DataBroker dataBroker;
65 private final MountPointService mountService;
66 private final List<String> requiredCapabilities;
68 public NodeManager(final DataBroker dataBroker, final BindingAwareBroker.ProviderContext session) {
69 this.dataBroker = Preconditions.checkNotNull(dataBroker);
70 mountService = Preconditions.checkNotNull(session.getSALService(MountPointService.class));
71 requiredCapabilities = new RequiredCapabilities().initializeRequiredCapabilities();
74 public void syncNodes(final Node dataAfter, final Node dataBefore) {
76 if (dataBefore == null && dataAfter != null) {
77 createNode(dataAfter);
79 // Connected/disconnected node
80 if (dataBefore != null && dataAfter != null) {
81 updateNode(dataAfter);
84 if (dataBefore != null && dataAfter == null) {
85 removeNode(dataBefore);
89 private void createNode(final Node node) {
90 LOG.info("Registering new node {}", node.getNodeId().getValue());
91 final NetconfNode netconfNode = getNodeAugmentation(node);
92 if (netconfNode == null) {
95 final ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
96 switch (connectionStatus) {
98 LOG.info("Connecting device {} ...", node.getNodeId().getValue());
102 resolveConnectedNode(node, netconfNode);
105 case UnableToConnect: {
106 LOG.info("Unable to connect device {}", node.getNodeId().getValue());
113 * Update previously added node. According to actual connection status, appropriate action is performed
115 * @param node to resolve
117 private void updateNode(final Node node) {
118 final NetconfNode netconfNode = getNodeAugmentation(node);
119 if (netconfNode == null || netconfNode.getConnectionStatus() == null) {
120 LOG.info("Node {} does not contain connection status", node.getNodeId().getValue());
123 final ConnectionStatus afterNodeStatus = netconfNode.getConnectionStatus();
124 if (afterNodeStatus.equals(Connected)) {
125 resolveConnectedNode(node, netconfNode);
127 if (afterNodeStatus.equals(Connecting)) {
128 LOG.info("Node {} has been disconnected, removing from available nodes", node.getNodeId().getValue());
129 resolveDisconnectedNode(node);
131 if (afterNodeStatus.equals(UnableToConnect)) {
132 LOG.info("Unable to connect node {}, removing from available nodes", node.getNodeId().getValue());
133 resolveDisconnectedNode(node);
138 * Removes previously added node. This node is also disconnected and removed from available nodes
140 * @param node to remove
142 private void removeNode(final Node node) {
143 Futures.addCallback(resolveDisconnectedNode(node), new FutureCallback<Boolean>() {
145 public void onSuccess(@Nullable Boolean result) {
146 if (Boolean.TRUE.equals(result)) {
147 LOG.info("Node {} has been removed", node.getNodeId().getValue());
149 LOG.warn("Failed to remove node {}", node.getNodeId().getValue());
154 public void onFailure(@Nullable Throwable throwable) {
155 LOG.warn("Exception thrown when removing node... {}", throwable);
161 * Resolve node with {@link ConnectionStatus#Connected}. This node is reachable and can be added to nodes available
164 * @param node to add to available nodes
165 * @param netconfNode node's netconf augmentation
167 private void resolveConnectedNode(final Node node, @Nonnull final NetconfNode netconfNode) {
168 final InstanceIdentifier mountPointIid = getMountpointIid(node);
169 // Mountpoint iid == path in renderer-node
170 final RendererNode rendererNode = remapNode(mountPointIid);
171 final NodeWriter nodeWriter = new NodeWriter();
172 nodeWriter.cache(rendererNode);
173 if (!isCapableNetconfDevice(node, netconfNode)) {
174 resolveDisconnectedNode(node);
177 final IpAddress managementIpAddress = netconfNode.getHost().getIpAddress();
178 if (managementIpAddress == null) {
179 LOG.warn("Node {} does not contain management ip address", node.getNodeId().getValue());
180 resolveDisconnectedNode(node);
183 Futures.addCallback(nodeWriter.commitToDatastore(dataBroker), new FutureCallback<Boolean>() {
185 public void onSuccess(@Nullable Boolean result) {
186 if (Boolean.TRUE.equals(result)) {
187 LOG.info("Node {} is ready, added to available nodes for IOS-XE Renderer", node.getNodeId().getValue());
189 LOG.warn("Connected node {} has not been resolved", node.getNodeId().getValue());
194 public void onFailure(@Nullable Throwable throwable) {
195 LOG.warn("Exception thrown when resolving node... {}", throwable);
201 * Depending on action, this method is called when node is not reachable anymore. Such a node is removed from nodes
202 * available for renderers. Reasons why the node is offline can vary, therefore logging should be handled outside
204 * @param node to remove from available nodes
205 * @return true if removed, false otherwise
207 private ListenableFuture<Boolean> resolveDisconnectedNode(final Node node) {
208 final InstanceIdentifier mountPointIid = getMountpointIid(node);
209 final RendererNode rendererNode = remapNode(mountPointIid);
210 final NodeWriter nodeWriter = new NodeWriter();
211 nodeWriter.cache(rendererNode);
212 return nodeWriter.removeFromDatastore(dataBroker);
216 * Node is remapped as renderer node with instance identifier. Used when reporting status for renderer manager
218 * @param path node IID
219 * @return {@link RendererNode} object with path
221 private RendererNode remapNode(final InstanceIdentifier path) {
222 final RendererNodeBuilder rendererNodeBuilder = new RendererNodeBuilder();
223 rendererNodeBuilder.setKey(new RendererNodeKey(path))
225 return rendererNodeBuilder.build();
228 private InstanceIdentifier getMountpointIid(final Node node) {
229 return InstanceIdentifier.builder(NetworkTopology.class)
230 .child(Topology.class, new TopologyKey(TOPOLOGY_ID))
231 .child(Node.class, new NodeKey(node.getNodeId())).build();
234 private boolean isCapableNetconfDevice(final Node node, @Nonnull final NetconfNode netconfAugmentation) {
235 final AvailableCapabilities available = netconfAugmentation.getAvailableCapabilities();
236 if (available == null || available.getAvailableCapability() == null || available.getAvailableCapability().isEmpty()) {
237 LOG.warn("Node {} does not contain any capabilities", node.getNodeId().getValue());
240 if (!capabilityCheck(netconfAugmentation.getAvailableCapabilities().getAvailableCapability())) {
241 LOG.warn("Node {} does not contain all capabilities required by io-xe-renderer",
242 node.getNodeId().getValue());
248 private boolean capabilityCheck(final List<AvailableCapability> capabilities) {
249 final List<String> availableCapabilities = capabilities.stream()
250 .map(AvailableCapability::getCapability)
251 .collect(Collectors.toList());
252 return requiredCapabilities.stream()
253 .allMatch(availableCapabilities::contains);
257 DataBroker getNodeMountPoint(final InstanceIdentifier mountPointIid) {
258 if (mountPointIid == null) {
261 final MountPoint mountPoint = ((Function<InstanceIdentifier, MountPoint>) instanceIdentifier -> {
262 Optional<MountPoint> optionalObject = mountService.getMountPoint(mountPointIid);
263 if (optionalObject.isPresent()) {
264 return optionalObject.get();
266 LOG.debug("Cannot obtain mountpoint with IID {}", mountPointIid);
268 }).apply(mountPointIid);
269 if (mountPoint == null) {
272 return ((Function<MountPoint, DataBroker>) mountPointParam -> {
273 Optional<DataBroker> optionalDataBroker = mountPointParam.getService(DataBroker.class);
274 if (optionalDataBroker.isPresent()) {
275 return optionalDataBroker.get();
277 LOG.debug("Cannot obtain data broker from mountpoint {}", mountPointParam);
279 }).apply(mountPoint);
282 NodeId getNodeIdByMountpointIid(final InstanceIdentifier mountpointIid) {
283 final NodeKey identifier = (NodeKey) mountpointIid.firstKeyOf(Node.class);
284 return identifier.getNodeId();
287 java.util.Optional<String> getNodeManagementIpByMountPointIid(final InstanceIdentifier<?> mountpointIid) {
288 final NodeId nodeId = getNodeIdByMountpointIid(mountpointIid);
289 final InstanceIdentifier<Node> nodeIid = InstanceIdentifier.builder(NetworkTopology.class)
290 .child(Topology.class, new TopologyKey(new TopologyId(NodeManager.TOPOLOGY_ID)))
291 .child(Node.class, new NodeKey(nodeId))
293 final ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
294 final CheckedFuture<Optional<Node>, ReadFailedException> submitFuture =
295 rTx.read(LogicalDatastoreType.CONFIGURATION, nodeIid);
298 Optional<Node> nodeOptional = submitFuture.checkedGet();
299 if (nodeOptional.isPresent()) {
300 final NetconfNode netconfNode = getNodeAugmentation(nodeOptional.get());
301 return java.util.Optional.ofNullable(netconfNode)
302 .map(NetconfNodeConnectionParameters::getHost)
303 .map(Host::getIpAddress)
304 .map(IpAddress::getIpv4Address)
305 .map(Ipv4Address::getValue);
307 } catch (ReadFailedException e) {
308 LOG.warn("Read node failed {}", nodeId, e);
310 return java.util.Optional.empty();
313 private NetconfNode getNodeAugmentation(final Node node) {
314 final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
315 if (netconfNode == null) {
316 LOG.warn("Node {} is not a netconf device", node.getNodeId().getValue());
322 private static class RequiredCapabilities {
324 private static final String NED ="(urn:ios?revision=2016-03-08)ned";
325 private static final String TAILF_COMMON = "(http://tail-f.com/yang/common?revision=2015-05-22)tailf-common";
326 private static final String TAILF_CLI_EXTENSION = "(http://tail-f.com/yang/common?revision=2015-03-19)tailf-cli-extensions";
327 private static final String TAILF_META_EXTENSION = "(http://tail-f.com/yang/common?revision=2013-11-07)tailf-meta-extensions";
328 private static final String IETF_YANG_TYPES = "(urn:ietf:params:xml:ns:yang:ietf-yang-types?revision=2013-07-15)ietf-yang-types";
329 private static final String IETF_INET_TYPES = "(urn:ietf:params:xml:ns:yang:ietf-inet-types?revision=2013-07-15)ietf-inet-types";
332 * Initialize all common capabilities required by IOS-XE renderer. Any connected node is examined whether it's
333 * an appropriate device to handle configuration created by this renderer. A device has to support all capabilities
336 * @return list of string representations of required capabilities
338 List<String> initializeRequiredCapabilities() {
339 final String capabilityEntries[] = {NED, TAILF_COMMON, TAILF_CLI_EXTENSION, TAILF_META_EXTENSION,
340 IETF_YANG_TYPES, IETF_INET_TYPES};
341 return Arrays.asList(capabilityEntries);