Bug 6743: added futures to InterfaceManager and VppNodeManager
[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 import static org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus.UnableToConnect;
14
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Objects;
21 import java.util.concurrent.Callable;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.ExecutorService;
24 import java.util.concurrent.Executors;
25 import java.util.concurrent.Future;
26 import java.util.stream.Collectors;
27
28 import javax.annotation.Nonnull;
29 import javax.annotation.Nullable;
30
31 import com.google.common.base.Optional;
32 import com.google.common.base.Preconditions;
33 import com.google.common.base.Splitter;
34 import com.google.common.base.Strings;
35 import com.google.common.util.concurrent.CheckedFuture;
36 import com.google.common.util.concurrent.FutureCallback;
37 import com.google.common.util.concurrent.Futures;
38 import com.google.common.util.concurrent.ListenableFuture;
39 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
40 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
41 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
42 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
43 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
44 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
45 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
46 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
47 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
48 import org.opendaylight.groupbasedpolicy.renderer.vpp.nat.NatUtil;
49 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppIidFactory;
50 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppRendererProcessingException;
51 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.EthernetCsmacd;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface1;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNode;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNodeBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNodeKey;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.VppInterfaceAugmentation;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.VppInterfaceAugmentationBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.renderers.renderer.renderer.nodes.renderer.node.PhysicalInterface;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.renderers.renderer.renderer.nodes.renderer.node.PhysicalInterfaceBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.renderers.renderer.renderer.nodes.renderer.node.PhysicalInterfaceKey;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
69 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
75 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
76 import org.slf4j.Logger;
77 import org.slf4j.LoggerFactory;
78
79 import com.google.common.collect.Lists;
80 import com.google.common.collect.Sets;
81
82 public class VppNodeManager {
83
84     private static final short DURATION = 3000;
85     private static final TopologyId TOPOLOGY_ID = new TopologyId("topology-netconf");
86     private static final Logger LOG = LoggerFactory.getLogger(VppNodeManager.class);
87     private static final String V3PO_CAPABILITY = "(urn:opendaylight:params:xml:ns:yang:v3po?revision=2016-12-14)v3po";
88     private static final String INTERFACES_CAPABILITY = "(urn:ietf:params:xml:ns:yang:ietf-interfaces?revision=2014-05-08)ietf-interfaces";
89     private static final NodeId CONTROLLER_CONFIG_NODE = new NodeId("controller-config");
90     private static final String NO_PUBLIC_INT_SPECIFIED = "unspecified";
91     private final Map<NodeId, PhysicalInterfaceKey> extInterfaces = new HashMap<>();
92     private final DataBroker dataBroker;
93     private final List<String> requiredCapabilities;
94     private final MountPointService mountService;
95
96     public VppNodeManager(@Nonnull final DataBroker dataBroker,
97             @Nonnull final BindingAwareBroker.ProviderContext session, @Nullable String physicalInterfaces) {
98         this.dataBroker = Preconditions.checkNotNull(dataBroker);
99         this.mountService = Preconditions.checkNotNull(session.getSALService(MountPointService.class));
100         requiredCapabilities = initializeRequiredCapabilities();
101         if (!Strings.isNullOrEmpty(physicalInterfaces) && !Objects.equals(physicalInterfaces, NO_PUBLIC_INT_SPECIFIED)) {
102             loadPhysicalInterfaces(physicalInterfaces);
103         }
104     }
105
106     /**
107      * Caches list of physical interfaces.
108      */
109     private void loadPhysicalInterfaces(@Nonnull String physicalInterfaces) {
110         for (String intfOnNode : Sets.newConcurrentHashSet(Splitter.on(",").split(physicalInterfaces))) {
111             List<String> entries = Lists.newArrayList(Splitter.on(":").split(intfOnNode));
112             if (entries.size() != 2) {
113                 LOG.warn("Cannot resolve {} initial configuration for physical interfaces.", intfOnNode);
114                 continue;
115             }
116             NodeId nodeId = new NodeId(entries.get(0));
117             PhysicalInterfaceKey infaceKey = new PhysicalInterfaceKey(entries.get(1));
118             LOG.info("Interface " + infaceKey + " on node " + nodeId + "will be considered as external");
119             extInterfaces.put(nodeId, infaceKey);
120         }
121     }
122
123     /**
124      * Synchronizes nodes to DataStore based on their modification state which results in
125      * create/update/remove of Node.
126      */
127     public void syncNodes(final Node dataAfter, final Node dataBefore) {
128         if (isControllerConfigNode(dataAfter, dataBefore)) {
129             LOG.trace("{} is ignored by VPP-renderer", CONTROLLER_CONFIG_NODE);
130             return;
131         }
132         ListenableFuture<String> syncFuture = Futures.immediateFuture(null);
133         // New node
134         if (dataBefore == null && dataAfter != null) {
135             syncFuture = createNode(dataAfter);
136         }
137         // Connected/disconnected node
138         else if (dataBefore != null && dataAfter != null) {
139             syncFuture = updateNode(dataAfter);
140         }
141         // Removed node
142         else if (dataBefore != null) {
143             syncFuture = removeNode(dataBefore);
144         }
145         Futures.addCallback(syncFuture, new FutureCallback<String>() {
146             @Override
147             public void onSuccess(@Nullable String message) {
148                 LOG.info("Node synchronization completed. {} ", message);
149             }
150
151             @Override
152             public void onFailure(@Nonnull Throwable t) {
153                 LOG.warn("Node synchronization failed. Data before: {} after {}", dataBefore, dataAfter);
154             }
155         });
156     }
157
158     private boolean isControllerConfigNode(final Node dataAfter, final Node dataBefore) {
159         if (dataAfter != null) {
160             return CONTROLLER_CONFIG_NODE.equals(dataAfter.getNodeId());
161         }
162         return CONTROLLER_CONFIG_NODE.equals(dataBefore.getNodeId());
163     }
164
165     private ListenableFuture<String> createNode(final Node node) {
166         final String nodeId = node.getNodeId().getValue();
167         LOG.info("Registering new node {}", nodeId);
168         final NetconfNode netconfNode = getNodeAugmentation(node);
169         if (netconfNode == null) {
170             final String message = String.format("Node %s is not an netconf node", nodeId);
171             return Futures.immediateFuture(message);
172         }
173         final NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
174         switch (connectionStatus) {
175             case Connecting: {
176                 final String message = String.format("Connecting device %s ...", nodeId);
177                 return Futures.immediateFuture(message);
178             }
179             case Connected: {
180                 return resolveConnectedNode(node, netconfNode);
181             }
182             case UnableToConnect: {
183                 final String message = String.format("Connection status is unable to connect for node %s", nodeId);
184                 return Futures.immediateFuture(message);
185             }
186             default: {
187                 final String message = String.format("Unknown connection status for node %s", nodeId);
188                 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
189             }
190         }
191     }
192
193     private ListenableFuture<String> updateNode(final Node node) {
194         final String nodeId = node.getNodeId().getValue();
195         LOG.info("Updating node {}", nodeId);
196         final NetconfNode netconfNode = getNodeAugmentation(node);
197         if (netconfNode == null) {
198             final String message = String.format("Node %s is not an netconf node", nodeId);
199             return Futures.immediateFuture(message);
200         }
201         final NetconfNodeConnectionStatus.ConnectionStatus afterNodeStatus = netconfNode.getConnectionStatus();
202         if (Connected.equals(afterNodeStatus)) {
203             return resolveConnectedNode(node, netconfNode);
204         } else if (Connecting.equals(afterNodeStatus)) {
205             final String cause = String.format("Node %s is disconnected, removing from available nodes", nodeId);
206             return resolveDisconnectedNode(node, cause);
207         } else if (UnableToConnect.equals(afterNodeStatus)) {
208             final String cause = String.format("New node %s status is unable to connect, removing from available nodes",
209                     nodeId);
210             return resolveDisconnectedNode(node, cause);
211         } else {
212             final String cause = String.format("New node status is unknown. Node %s will be removed from available nodes",
213                     nodeId);
214             return resolveDisconnectedNode(node, cause);
215         }
216     }
217
218     private ListenableFuture<String> removeNode(final Node node) {
219         final String cause = String.format("Node %s is removed", node.getNodeId().getValue());
220         return resolveDisconnectedNode(node, cause);
221     }
222
223     private ListenableFuture<String> resolveConnectedNode(final Node node, final NetconfNode netconfNode) {
224         final String nodeId = node.getNodeId().getValue();
225         final InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
226         final RendererNode rendererNode = remapNode(mountPointIid);
227         if (!isCapableNetconfDevice(node, netconfNode)) {
228             final String message = String.format("Node %s is not connected", nodeId);
229             return Futures.immediateFuture(message);
230         }
231         final DataBroker mountpoint = getNodeMountPoint(mountPointIid);
232         if (mountpoint == null) {
233             final String message = String.format("Mountpoint not available for node %s", nodeId);
234             return Futures.immediateFuture(message);
235         }
236         final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
237         wTx.put(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodeIid(rendererNode), rendererNode, true);
238         final boolean submit = DataStoreHelper.submitToDs(wTx);
239         if (submit) {
240             final String message = String.format("Node %s is capable and ready", nodeId);
241             syncPhysicalInterfacesInLocalDs(mountpoint, mountPointIid);
242             NatUtil.resolveOutboundNatInterface(mountpoint, mountPointIid, node.getNodeId(), extInterfaces);
243             return Futures.immediateFuture(message);
244         } else {
245             final String message = String.format("Failed to resolve connected node %s", nodeId);
246             return Futures.immediateFuture(message);
247         }
248     }
249
250     private ListenableFuture<String> resolveDisconnectedNode(final Node node, final String cause) {
251         final InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
252         final RendererNode rendererNode = remapNode(mountPointIid);
253         final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
254         wTx.delete(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodeIid(rendererNode));
255         final CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = wTx.submit();
256         try {
257             checkedFuture.checkedGet();
258             return Futures.immediateFuture(cause);
259         } catch (TransactionCommitFailedException e) {
260             final String message = String.format("Failed to resolve disconnected node %s", node.getNodeId().getValue());
261             return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
262         }
263     }
264
265     @Nullable
266     private DataBroker getNodeMountPoint(final InstanceIdentifier<Node> mountPointIid) {
267         final Future<Optional<MountPoint>> futureOptionalObject = getMountpointFromSal(mountPointIid);
268         try {
269             final Optional<MountPoint> optionalObject = futureOptionalObject.get();
270             LOG.debug("Optional mountpoint object: {}", optionalObject);
271             MountPoint mountPoint;
272             if (optionalObject.isPresent()) {
273                 mountPoint = optionalObject.get();
274                 if (mountPoint != null) {
275                     Optional<DataBroker> optionalDataBroker = mountPoint.getService(DataBroker.class);
276                     if (optionalDataBroker.isPresent()) {
277                         return optionalDataBroker.get();
278                     } else {
279                         LOG.warn("Cannot obtain data broker from mountpoint {}", mountPoint);
280                     }
281                 } else {
282                     LOG.warn("Cannot obtain mountpoint with IID {}", mountPointIid);
283                 }
284             }
285             return null;
286         } catch (ExecutionException | InterruptedException e) {
287             LOG.warn("Unable to obtain mountpoint ... {}", e);
288             return null;
289         }
290     }
291
292     private RendererNode remapNode(final InstanceIdentifier<Node> path) {
293         final RendererNodeBuilder rendererNodeBuilder = new RendererNodeBuilder();
294         rendererNodeBuilder.setKey(new RendererNodeKey(path)).setNodePath(path);
295         return rendererNodeBuilder.build();
296     }
297
298     private InstanceIdentifier<Node> getMountpointIid(final Node node) {
299         return InstanceIdentifier.builder(NetworkTopology.class)
300                 .child(Topology.class, new TopologyKey(TOPOLOGY_ID))
301                 .child(Node.class, new NodeKey(node.getNodeId()))
302                 .build();
303     }
304
305     private boolean isCapableNetconfDevice(final Node node, final NetconfNode netconfAugmentation) {
306         if (netconfAugmentation.getAvailableCapabilities() == null
307                 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability() == null
308                 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability().isEmpty()) {
309             LOG.warn("Node {} does not contain any capabilities", node.getNodeId().getValue());
310             return false;
311         }
312         if (!capabilityCheck(netconfAugmentation.getAvailableCapabilities().getAvailableCapability())) {
313             LOG.warn("Node {} does not contain all capabilities required by vpp-renderer", node.getNodeId().getValue());
314             return false;
315         }
316         return true;
317     }
318
319     private boolean capabilityCheck(final List<AvailableCapability> capabilities) {
320         final List<String> availableCapabilities = capabilities.stream()
321                 .map(AvailableCapability::getCapability)
322                 .collect(Collectors.toList());
323         return requiredCapabilities.stream()
324                 .allMatch(availableCapabilities::contains);
325     }
326
327     private NetconfNode getNodeAugmentation(final Node node) {
328         final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
329         if (netconfNode == null) {
330             LOG.warn("Node {} is not a netconf device", node.getNodeId().getValue());
331             return null;
332         }
333         return netconfNode;
334     }
335
336     /**
337      * Initialize all common capabilities required by VPP renderer. Any connected node is examined
338      * whether it's
339      * an appropriate device to handle configuration created by this renderer. A device must support
340      * all capabilities
341      * in list below.
342      *
343      * @return list of string representations of required capabilities
344      */
345     private List<String> initializeRequiredCapabilities() {
346         // Required device capabilities
347         String[] capabilityEntries = {V3PO_CAPABILITY, INTERFACES_CAPABILITY};
348         return Arrays.asList(capabilityEntries);
349     }
350
351     // TODO bug 7699
352     // This works as a workaround for mountpoint registration in cluster. If application is registered on different
353     // node as netconf service, it obtains mountpoint registered by SlaveSalFacade (instead of MasterSalFacade). However
354     // this service registers mountpoint a moment later then connectionStatus is set to "Connected". If NodeManager hits
355     // state where device is connected but mountpoint is not yet available, try to get it again in a while
356     private Future<Optional<MountPoint>> getMountpointFromSal(final InstanceIdentifier<Node> iid) {
357         final ExecutorService executorService = Executors.newSingleThreadExecutor();
358         final Callable<Optional<MountPoint>> task = () -> {
359             byte attempt = 0;
360             do {
361                 try {
362                     final Optional<MountPoint> optionalMountpoint = mountService.getMountPoint(iid);
363                     if (optionalMountpoint.isPresent()) {
364                         return optionalMountpoint;
365                     }
366                     LOG.warn("Mountpoint {} is not registered yet", iid);
367                     Thread.sleep(DURATION);
368                 } catch (InterruptedException e) {
369                     LOG.warn("Thread interrupted to ", e);
370                 }
371                 attempt++;
372             } while (attempt <= 3);
373             return Optional.absent();
374         };
375         return executorService.submit(task);
376     }
377
378     private void syncPhysicalInterfacesInLocalDs(DataBroker mountPointDataBroker, InstanceIdentifier<Node> nodeIid) {
379         ReadWriteTransaction rwTx = dataBroker.newReadWriteTransaction();
380         ReadOnlyTransaction rTx = mountPointDataBroker.newReadOnlyTransaction();
381         Optional<Interfaces> readIfaces = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
382                 InstanceIdentifier.create(Interfaces.class), rTx);
383         if (readIfaces.isPresent()) {
384             InstanceIdentifier<RendererNode> rendererNodeIid = VppIidFactory.getRendererNodesIid()
385                 .builder()
386                 .child(RendererNode.class, new RendererNodeKey(nodeIid))
387                 .build();
388             Optional<RendererNode> optRendNode = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
389                     rendererNodeIid, rwTx);
390             NodeId nodeId = nodeIid.firstKeyOf(Node.class).getNodeId();
391             RendererNode rendNode = new RendererNodeBuilder(optRendNode.get())
392                 .addAugmentation(VppInterfaceAugmentation.class, resolveTerminationPoints(nodeId, readIfaces.get()))
393                 .build();
394             rwTx.put(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodeIid(optRendNode.get()), rendNode,
395                     true);
396         }
397         rTx.close();
398         DataStoreHelper.submitToDs(rwTx);
399     }
400
401     private VppInterfaceAugmentation resolveTerminationPoints(NodeId nodeId, Interfaces interfaces) {
402         List<PhysicalInterface> phIfaces = new ArrayList<>();
403         if (interfaces != null && interfaces.getInterface() != null) {
404             interfaces.getInterface()
405                 .stream()
406                 .filter(iface -> iface.getType().equals(EthernetCsmacd.class))
407                 .filter(iface -> iface.getAugmentation(Interface1.class) != null)
408                 .forEach(iface -> {
409                     PhysicalInterfaceBuilder phIface = new PhysicalInterfaceBuilder();
410                     phIface.setInterfaceName(iface.getName());
411                     phIface.setType(iface.getType());
412                     phIface.setAddress(resolveIpAddress(iface.getAugmentation(Interface1.class)));
413
414                     if (extInterfaces.get(nodeId) != null && extInterfaces.get(nodeId)
415                         .getInterfaceName()
416                         .equals(phIface.getInterfaceName())) {
417                         phIface.setExternal(true);
418                         LOG.trace("Assigning external Interface, interface name: {}, extInterface name: {}",
419                             phIface.getInterfaceName(), extInterfaces.get(nodeId).getInterfaceName());
420                     }
421                     phIfaces.add(phIface.build());
422                 });
423         }
424         return new VppInterfaceAugmentationBuilder().setPhysicalInterface(phIfaces).build();
425     }
426
427     private List<IpAddress> resolveIpAddress(Interface1 iface) {
428         if (iface.getIpv4() != null && iface.getIpv4().getAddress() != null) {
429             return iface.getIpv4().getAddress().stream().map(ipv4 ->
430                     new IpAddress(new Ipv4Address(ipv4.getIp().getValue()))).collect(Collectors.toList());
431         } else if (iface.getIpv6() != null && iface.getIpv6().getAddress() != null) {
432             return iface.getIpv6().getAddress().stream().map(ipv6 ->
433                     new IpAddress(new Ipv4Address(ipv6.getIp().getValue()))).collect(Collectors.toList());
434         }
435         return Lists.newArrayList();
436     }
437 }