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