Merge "remove redundant parent data with last child"
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / manager / VppNodeManager.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.vpp.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
14 import javax.annotation.Nullable;
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.concurrent.Callable;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.Executors;
21 import java.util.concurrent.Future;
22 import java.util.stream.Collectors;
23 import com.google.common.base.Optional;
24 import com.google.common.base.Preconditions;
25 import com.google.common.util.concurrent.CheckedFuture;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
28 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
29 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
32 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
33 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppIidFactory;
34 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNode;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNodeBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNodeKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 public class VppNodeManager {
53
54     private static final short DURATION = 3000;
55     private static final TopologyId TOPOLOGY_ID = new TopologyId("topology-netconf");
56     private static final Logger LOG = LoggerFactory.getLogger(VppNodeManager.class);
57     private static final String V3PO_CAPABILITY = "(urn:opendaylight:params:xml:ns:yang:v3po?revision=2016-12-14)v3po";
58     private static final String INTERFACES_CAPABILITY = "(urn:ietf:params:xml:ns:yang:ietf-interfaces?revision=2014-05-08)ietf-interfaces";
59     private static final NodeId CONTROLLER_CONFIG_NODE = new NodeId("controller-config");
60     private final DataBroker dataBroker;
61     private final List<String> requiredCapabilities;
62     private final MountPointService mountService;
63
64     public VppNodeManager(final DataBroker dataBroker, final BindingAwareBroker.ProviderContext session) {
65         this.dataBroker = Preconditions.checkNotNull(dataBroker);
66         this.mountService = Preconditions.checkNotNull(session.getSALService(MountPointService.class));
67         requiredCapabilities = initializeRequiredCapabilities();
68     }
69
70     /**
71      * Synchronizes nodes to DataStore based on their modification state which results in
72      * create/update/remove of Node.
73      */
74     public void syncNodes(Node dataAfter, Node dataBefore) {
75         if (isControllerConfigNode(dataAfter, dataBefore)) {
76             LOG.trace("{} is ignored by VPP-renderer", CONTROLLER_CONFIG_NODE);
77             return;
78         }
79         // New node
80         if (dataBefore == null && dataAfter != null) {
81             createNode(dataAfter);
82         }
83         // Connected/disconnected node
84         if (dataBefore != null && dataAfter != null) {
85             updateNode(dataAfter);
86         }
87         // Removed node
88         if (dataBefore != null && dataAfter == null) {
89             removeNode(dataBefore);
90         }
91     }
92
93     private boolean isControllerConfigNode(Node dataAfter, Node dataBefore) {
94         if (dataAfter != null) {
95             return CONTROLLER_CONFIG_NODE.equals(dataAfter.getNodeId());
96         }
97         return CONTROLLER_CONFIG_NODE.equals(dataBefore.getNodeId());
98     }
99
100     private void createNode(Node node) {
101         LOG.info("Registering new node {}", node.getNodeId().getValue());
102         NetconfNode netconfNode = getNodeAugmentation(node);
103         if (netconfNode == null) {
104             return;
105         }
106         NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
107         switch (connectionStatus) {
108             case Connecting:
109                 LOG.info("Connecting device {} ...", node.getNodeId().getValue());
110                 break;
111             case Connected:
112                 resolveConnectedNode(node, netconfNode);
113                 break;
114             default:
115                 break;
116         }
117     }
118
119     private void updateNode(Node node) {
120         LOG.info("Updating node {}", node.getNodeId());
121         NetconfNode netconfNode = getNodeAugmentation(node);
122         if (netconfNode == null || netconfNode.getConnectionStatus() == null) {
123             return;
124         }
125         NetconfNodeConnectionStatus.ConnectionStatus afterNodeStatus = netconfNode.getConnectionStatus();
126         if (afterNodeStatus.equals(Connected)) {
127             resolveConnectedNode(node, netconfNode);
128         }
129         if (afterNodeStatus.equals(Connecting)) {
130             resolveDisconnectedNode(node);
131             LOG.info("Node {} is disconnected, removing from available nodes", node.getNodeId().getValue());
132         }
133     }
134
135     private void removeNode(Node node) {
136         resolveDisconnectedNode(node);
137         LOG.info("Node {} is removed", node.getNodeId().getValue());
138     }
139
140     private void resolveConnectedNode(Node node, NetconfNode netconfNode) {
141         InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
142         RendererNode rendererNode = remapNode(mountPointIid);
143         if (!isCapableNetconfDevice(node, netconfNode)) {
144             LOG.warn("Node {} is not connected.", node.getNodeId().getValue());
145             return;
146         }
147         final DataBroker mountpoint = getNodeMountPoint(mountPointIid);
148         if (mountpoint == null) {
149             LOG.warn("Mountpoint not available for node {}", node.getNodeId().getValue());
150             return;
151         }
152         final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
153         wTx.put(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodeIid(rendererNode), rendererNode, true);
154         DataStoreHelper.submitToDs(wTx);
155         LOG.info("Node {} is capable and ready.", node.getNodeId().getValue());
156     }
157
158     private void resolveDisconnectedNode(Node node) {
159         InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
160         RendererNode rendererNode = remapNode(mountPointIid);
161         final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
162         wTx.delete(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodeIid(rendererNode));
163         CheckedFuture<Void, TransactionCommitFailedException> submitFuture = wTx.submit();
164         try {
165             submitFuture.checkedGet();
166         } catch (TransactionCommitFailedException e) {
167             LOG.error("Write transaction failed to {}", e.getMessage());
168         } catch (Exception e) {
169             LOG.error("Failed to .. {}", e.getMessage());
170         }
171     }
172
173     @Nullable
174     private DataBroker getNodeMountPoint(InstanceIdentifier<Node> mountPointIid) {
175         final Future<Optional<MountPoint>> futureOptionalObject = getMountpointFromSal(mountPointIid);
176         try {
177             final Optional<MountPoint> optionalObject = futureOptionalObject.get();
178             LOG.debug("Optional mountpoint object: {}", optionalObject);
179             MountPoint mountPoint;
180             if (optionalObject.isPresent()) {
181                 mountPoint = optionalObject.get();
182                 if (mountPoint != null) {
183                     Optional<DataBroker> optionalDataBroker = mountPoint.getService(DataBroker.class);
184                     if (optionalDataBroker.isPresent()) {
185                         return optionalDataBroker.get();
186                     } else {
187                         LOG.warn("Cannot obtain data broker from mountpoint {}", mountPoint);
188                     }
189                 } else {
190                     LOG.warn("Cannot obtain mountpoint with IID {}", mountPointIid);
191                 }
192             }
193             return null;
194         } catch (ExecutionException | InterruptedException e) {
195             LOG.warn("Unable to obtain mountpoint ... {}", e);
196             return null;
197         }
198     }
199
200     private RendererNode remapNode(InstanceIdentifier<Node> path) {
201         RendererNodeBuilder rendererNodeBuilder = new RendererNodeBuilder();
202         rendererNodeBuilder.setKey(new RendererNodeKey(path)).setNodePath(path);
203         return rendererNodeBuilder.build();
204     }
205
206     private InstanceIdentifier<Node> getMountpointIid(Node node) {
207         return InstanceIdentifier.builder(NetworkTopology.class)
208             .child(Topology.class, new TopologyKey(TOPOLOGY_ID))
209             .child(Node.class, new NodeKey(node.getNodeId()))
210             .build();
211     }
212
213     private boolean isCapableNetconfDevice(Node node, NetconfNode netconfAugmentation) {
214         if (netconfAugmentation.getAvailableCapabilities() == null
215                 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability() == null
216                 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability().isEmpty()) {
217             LOG.warn("Node {} does not contain any capabilities", node.getNodeId().getValue());
218             return false;
219         }
220         if (!capabilityCheck(netconfAugmentation.getAvailableCapabilities().getAvailableCapability())) {
221             LOG.warn("Node {} does not contain all capabilities required by vpp-renderer", node.getNodeId().getValue());
222             return false;
223         }
224         return true;
225     }
226
227     private boolean capabilityCheck(final List<AvailableCapability> capabilities) {
228         final List<String> availableCapabilities = capabilities.stream()
229                 .map(AvailableCapability::getCapability)
230                 .collect(Collectors.toList());
231         return requiredCapabilities.stream()
232                 .allMatch(availableCapabilities::contains);
233     }
234
235     private NetconfNode getNodeAugmentation(Node node) {
236         NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
237         if (netconfNode == null) {
238             LOG.warn("Node {} is not a netconf device", node.getNodeId().getValue());
239             return null;
240         }
241         return netconfNode;
242     }
243
244     /**
245      * Initialize all common capabilities required by VPP renderer. Any connected node is examined
246      * whether it's
247      * an appropriate device to handle configuration created by this renderer. A device must support
248      * all capabilities
249      * in list below.
250      *
251      * @return list of string representations of required capabilities
252      */
253     private List<String> initializeRequiredCapabilities() {
254         // Required device capabilities
255
256         String[] capabilityEntries = {V3PO_CAPABILITY, INTERFACES_CAPABILITY};
257         return Arrays.asList(capabilityEntries);
258     }
259
260     // TODO bug 7699
261     // This works as a workaround for mountpoint registration in cluster. If application is registered on different
262     // node as netconf service, it obtains mountpoint registered by SlaveSalFacade (instead of MasterSalFacade). However
263     // this service registers mountpoint a moment later then connectionStatus is set to "Connected". If NodeManager hits
264     // state where device is connected but mountpoint is not yet available, try to get it again in a while
265     private Future<Optional<MountPoint>> getMountpointFromSal(final InstanceIdentifier<Node> iid) {
266         final ExecutorService executorService = Executors.newSingleThreadExecutor();
267         final Callable<Optional<MountPoint>> task = () -> {
268             byte attempt = 0;
269             do {
270                 try {
271                     final Optional<MountPoint> optionalMountpoint = mountService.getMountPoint(iid);
272                     if (optionalMountpoint.isPresent()) {
273                         return optionalMountpoint;
274                     }
275                     LOG.warn("Mountpoint {} is not registered yet", iid);
276                     Thread.sleep(DURATION);
277                 } catch (InterruptedException e) {
278                     LOG.warn("Thread interrupted to ", e);
279                 }
280                 attempt ++;
281             } while (attempt <= 3);
282             return Optional.absent();
283         };
284         return executorService.submit(task);
285     }
286 }