update deprecated transform and addCallback methods
[groupbasedpolicy.git] / renderers / ios-xe / src / main / java / org / opendaylight / groupbasedpolicy / renderer / ios_xe_provider / impl / manager / NodeManager.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.manager;
10
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;
14
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.function.Function;
18 import java.util.stream.Collectors;
19
20 import javax.annotation.Nonnull;
21 import javax.annotation.Nullable;
22
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;
52
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;
60
61 public class NodeManager {
62
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;
68
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();
73     }
74
75     public void syncNodes(final Node dataAfter, final Node dataBefore) {
76         // New node
77         if (dataBefore == null && dataAfter != null) {
78             createNode(dataAfter);
79         }
80         // Connected/disconnected node
81         if (dataBefore != null && dataAfter != null) {
82             updateNode(dataAfter);
83         }
84         // Removed node
85         if (dataBefore != null && dataAfter == null) {
86             removeNode(dataBefore);
87         }
88     }
89
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) {
94             return;
95         }
96         final ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
97         switch (connectionStatus) {
98             case Connecting: {
99                 LOG.info("Connecting device {} ...", node.getNodeId().getValue());
100                 break;
101             }
102             case Connected: {
103                 resolveConnectedNode(node, netconfNode);
104                 break;
105             }
106             case UnableToConnect: {
107                 LOG.info("Unable to connect device {}", node.getNodeId().getValue());
108                 break;
109             }
110         }
111     }
112
113     /**
114      * Update previously added node. According to actual connection status, appropriate action is performed
115      *
116      * @param node to resolve
117      */
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());
122             return;
123         }
124         final ConnectionStatus afterNodeStatus = netconfNode.getConnectionStatus();
125         if (afterNodeStatus.equals(Connected)) {
126             resolveConnectedNode(node, netconfNode);
127         }
128         if (afterNodeStatus.equals(Connecting)) {
129             LOG.info("Node {} has been disconnected, removing from available nodes", node.getNodeId().getValue());
130             resolveDisconnectedNode(node);
131         }
132         if (afterNodeStatus.equals(UnableToConnect)) {
133             LOG.info("Unable to connect node {}, removing from available nodes", node.getNodeId().getValue());
134             resolveDisconnectedNode(node);
135         }
136     }
137
138     /**
139      * Removes previously added node. This node is also disconnected and removed from available nodes
140      *
141      * @param node to remove
142      */
143     private void removeNode(final Node node) {
144         Futures.addCallback(resolveDisconnectedNode(node), new FutureCallback<Boolean>() {
145             @Override
146             public void onSuccess(@Nullable Boolean result) {
147                 if (Boolean.TRUE.equals(result)) {
148                     LOG.info("Node {} has been removed", node.getNodeId().getValue());
149                 } else {
150                     LOG.warn("Failed to remove node {}", node.getNodeId().getValue());
151                 }
152             }
153
154             @Override
155             public void onFailure(@Nullable Throwable throwable) {
156                 LOG.warn("Exception thrown when removing node... {}", throwable);
157             }
158         }, MoreExecutors.directExecutor());
159     }
160
161     /**
162      * Resolve node with {@link ConnectionStatus#Connected}. This node is reachable and can be added to nodes available
163      * for renderers
164      *
165      * @param node        to add to available nodes
166      * @param netconfNode node's netconf augmentation
167      */
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);
176             return;
177         }
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);
182             return;
183         }
184         Futures.addCallback(nodeWriter.commitToDatastore(dataBroker), new FutureCallback<Boolean>() {
185             @Override
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());
189                 } else {
190                     LOG.warn("Connected node {} has not been resolved", node.getNodeId().getValue());
191                 }
192             }
193
194             @Override
195             public void onFailure(@Nullable Throwable throwable) {
196                 LOG.warn("Exception thrown when resolving node... {}", throwable);
197             }
198         }, MoreExecutors.directExecutor());
199     }
200
201     /**
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
204      *
205      * @param node to remove from available nodes
206      * @return true if removed, false otherwise
207      */
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);
214     }
215
216     /**
217      * Node is remapped as renderer node with instance identifier. Used when reporting status for renderer manager
218      *
219      * @param path node IID
220      * @return {@link RendererNode} object with path
221      */
222     private RendererNode remapNode(final InstanceIdentifier path) {
223         final RendererNodeBuilder rendererNodeBuilder = new RendererNodeBuilder();
224         rendererNodeBuilder.setKey(new RendererNodeKey(path))
225                 .setNodePath(path);
226         return rendererNodeBuilder.build();
227     }
228
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();
233     }
234
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());
239             return false;
240         }
241         if (!capabilityCheck(netconfAugmentation.getAvailableCapabilities().getAvailableCapability())) {
242             LOG.warn("Node {} does not contain all capabilities required by io-xe-renderer",
243                     node.getNodeId().getValue());
244             return false;
245         }
246         return true;
247     }
248
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);
255     }
256
257     @Nullable
258     DataBroker getNodeMountPoint(final InstanceIdentifier mountPointIid) {
259         if (mountPointIid == null) {
260             return null;
261         }
262         final MountPoint mountPoint = ((Function<InstanceIdentifier, MountPoint>) instanceIdentifier -> {
263             Optional<MountPoint> optionalObject = mountService.getMountPoint(mountPointIid);
264             if (optionalObject.isPresent()) {
265                 return optionalObject.get();
266             }
267             LOG.debug("Cannot obtain mountpoint with IID {}", mountPointIid);
268             return null;
269         }).apply(mountPointIid);
270         if (mountPoint == null) {
271             return null;
272         }
273         return ((Function<MountPoint, DataBroker>) mountPointParam -> {
274             Optional<DataBroker> optionalDataBroker = mountPointParam.getService(DataBroker.class);
275             if (optionalDataBroker.isPresent()) {
276                 return optionalDataBroker.get();
277             }
278             LOG.debug("Cannot obtain data broker from mountpoint {}", mountPointParam);
279             return null;
280         }).apply(mountPoint);
281     }
282
283     NodeId getNodeIdByMountpointIid(final InstanceIdentifier mountpointIid) {
284         final NodeKey identifier = (NodeKey) mountpointIid.firstKeyOf(Node.class);
285         return identifier.getNodeId();
286     }
287
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))
293                 .build();
294         final ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
295         final CheckedFuture<Optional<Node>, ReadFailedException> submitFuture =
296                 rTx.read(LogicalDatastoreType.CONFIGURATION, nodeIid);
297         rTx.close();
298         try {
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);
307             }
308         } catch (ReadFailedException e) {
309             LOG.warn("Read node failed {}", nodeId, e);
310         }
311         return java.util.Optional.empty();
312     }
313
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());
318             return null;
319         }
320         return netconfNode;
321     }
322
323     private static class RequiredCapabilities {
324
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";
331
332         /**
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
335          * in list below.
336          *
337          * @return list of string representations of required capabilities
338          */
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);
343         }
344     }
345 }