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.vpp.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.ArrayList;
16 import java.util.Arrays;
17 import java.util.HashMap;
18 import java.util.List;
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;
28 import javax.annotation.Nonnull;
29 import javax.annotation.Nullable;
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.RendererNodes;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNode;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNodeBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNodeKey;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.VppInterfaceAugmentation;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.VppInterfaceAugmentationBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.renderers.renderer.renderer.nodes.renderer.node.PhysicalInterface;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.renderers.renderer.renderer.nodes.renderer.node.PhysicalInterfaceBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.renderers.renderer.renderer.nodes.renderer.node.PhysicalInterfaceKey;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
69 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
74 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
75 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
76 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
77 import org.slf4j.Logger;
78 import org.slf4j.LoggerFactory;
80 import com.google.common.collect.Lists;
81 import com.google.common.collect.Sets;
83 public class VppNodeManager {
85 private static final short DURATION = 3000;
86 private static final TopologyId TOPOLOGY_ID = new TopologyId("topology-netconf");
87 private static final Logger LOG = LoggerFactory.getLogger(VppNodeManager.class);
88 private static final String V3PO_CAPABILITY = "(urn:opendaylight:params:xml:ns:yang:v3po?revision=2017-03-15)v3po";
89 private static final String INTERFACES_CAPABILITY = "(urn:ietf:params:xml:ns:yang:ietf-interfaces?revision=2014-05-08)ietf-interfaces";
90 private static final NodeId CONTROLLER_CONFIG_NODE = new NodeId("controller-config");
91 private static final String NO_PUBLIC_INT_SPECIFIED = "unspecified";
92 private static final String PUBLIC_INTERFACE = "public-interface";
93 private final Map<NodeId, PhysicalInterfaceKey> extInterfaces = new HashMap<>();
94 private final DataBroker dataBroker;
95 private final List<String> requiredCapabilities;
96 private final MountPointService mountService;
98 public VppNodeManager(@Nonnull final DataBroker dataBroker,
99 @Nonnull final BindingAwareBroker.ProviderContext session, @Nullable String physicalInterfaces) {
100 this.dataBroker = Preconditions.checkNotNull(dataBroker);
101 this.mountService = Preconditions.checkNotNull(session.getSALService(MountPointService.class));
102 requiredCapabilities = initializeRequiredCapabilities();
103 if (!Strings.isNullOrEmpty(physicalInterfaces) && !Objects.equals(physicalInterfaces, NO_PUBLIC_INT_SPECIFIED)) {
104 loadPhysicalInterfaces(physicalInterfaces);
109 * Caches list of physical interfaces.
111 private void loadPhysicalInterfaces(@Nonnull String physicalInterfaces) {
112 for (String intfOnNode : Sets.newConcurrentHashSet(Splitter.on(",").split(physicalInterfaces))) {
113 List<String> entries = Lists.newArrayList(Splitter.on(":").split(intfOnNode));
114 if (entries.size() != 2) {
115 LOG.warn("Cannot resolve {} initial configuration for physical interfaces.", intfOnNode);
118 NodeId nodeId = new NodeId(entries.get(0));
119 PhysicalInterfaceKey infaceKey = new PhysicalInterfaceKey(entries.get(1));
120 LOG.info("Interface " + infaceKey + " on node " + nodeId + "will be considered as external");
121 extInterfaces.put(nodeId, infaceKey);
126 * Synchronizes nodes to DataStore based on their modification state which results in
127 * create/update/remove of Node.
129 public void syncNodes(final Node dataAfter, final Node dataBefore) {
130 if (isControllerConfigNode(dataAfter, dataBefore)) {
131 LOG.trace("{} is ignored by VPP-renderer", CONTROLLER_CONFIG_NODE);
134 ListenableFuture<String> syncFuture = Futures.immediateFuture(null);
136 if (dataBefore == null && dataAfter != null) {
137 syncFuture = createNode(dataAfter);
139 // Connected/disconnected node
140 else if (dataBefore != null && dataAfter != null) {
141 syncFuture = updateNode(dataAfter);
144 else if (dataBefore != null) {
145 syncFuture = removeNode(dataBefore);
147 Futures.addCallback(syncFuture, new FutureCallback<String>() {
149 public void onSuccess(@Nullable String message) {
150 LOG.info("Node synchronization completed. {} ", message);
154 public void onFailure(@Nonnull Throwable t) {
155 LOG.warn("Node synchronization failed. Data before: {} after {}", dataBefore, dataAfter);
160 private boolean isControllerConfigNode(final Node dataAfter, final Node dataBefore) {
161 if (dataAfter != null) {
162 return CONTROLLER_CONFIG_NODE.equals(dataAfter.getNodeId());
164 return CONTROLLER_CONFIG_NODE.equals(dataBefore.getNodeId());
167 private ListenableFuture<String> createNode(final Node node) {
168 final String nodeId = node.getNodeId().getValue();
169 LOG.info("Registering new node {}", nodeId);
170 final NetconfNode netconfNode = getNodeAugmentation(node);
171 if (netconfNode == null) {
172 final String message = String.format("Node %s is not an netconf node", nodeId);
173 return Futures.immediateFuture(message);
175 final NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
176 switch (connectionStatus) {
178 final String message = String.format("Connecting device %s ...", nodeId);
179 return Futures.immediateFuture(message);
182 return resolveConnectedNode(node, netconfNode);
184 case UnableToConnect: {
185 final String message = String.format("Connection status is unable to connect for node %s", nodeId);
186 return Futures.immediateFuture(message);
189 final String message = String.format("Unknown connection status for node %s", nodeId);
190 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
195 private ListenableFuture<String> updateNode(final Node node) {
196 final String nodeId = node.getNodeId().getValue();
197 LOG.info("Updating node {}", nodeId);
198 final NetconfNode netconfNode = getNodeAugmentation(node);
199 if (netconfNode == null) {
200 final String message = String.format("Node %s is not an netconf node", nodeId);
201 return Futures.immediateFuture(message);
203 final NetconfNodeConnectionStatus.ConnectionStatus afterNodeStatus = netconfNode.getConnectionStatus();
204 if (Connected.equals(afterNodeStatus)) {
205 return resolveConnectedNode(node, netconfNode);
206 } else if (Connecting.equals(afterNodeStatus)) {
207 final String cause = String.format("Node %s is disconnected, removing from available nodes", nodeId);
208 return resolveDisconnectedNode(node, cause);
209 } else if (UnableToConnect.equals(afterNodeStatus)) {
210 final String cause = String.format("New node %s status is unable to connect, removing from available nodes",
212 return resolveDisconnectedNode(node, cause);
214 final String cause = String.format("New node status is unknown. Node %s will be removed from available nodes",
216 return resolveDisconnectedNode(node, cause);
220 private ListenableFuture<String> removeNode(final Node node) {
221 final String cause = String.format("Node %s is removed", node.getNodeId().getValue());
222 return resolveDisconnectedNode(node, cause);
225 private ListenableFuture<String> resolveConnectedNode(final Node node, final NetconfNode netconfNode) {
226 final String nodeId = node.getNodeId().getValue();
227 final InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
228 final RendererNode rendererNode = remapNode(mountPointIid);
229 if (!isCapableNetconfDevice(node, netconfNode)) {
230 final String message = String.format("Node %s is not connected", nodeId);
231 return Futures.immediateFuture(message);
233 final DataBroker mountpoint = getNodeMountPoint(mountPointIid);
234 if (mountpoint == null) {
235 final String message = String.format("Mountpoint not available for node %s", nodeId);
236 return Futures.immediateFuture(message);
238 final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
239 wTx.put(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodeIid(rendererNode), rendererNode, true);
240 final boolean submit = DataStoreHelper.submitToDs(wTx);
242 final String message = String.format("Node %s is capable and ready", nodeId);
243 syncPhysicalInterfacesInLocalDs(mountpoint, mountPointIid);
244 NatUtil.resolveOutboundNatInterface(mountpoint, mountPointIid, node.getNodeId(), extInterfaces);
245 return Futures.immediateFuture(message);
247 final String message = String.format("Failed to resolve connected node %s", nodeId);
248 return Futures.immediateFuture(message);
252 private ListenableFuture<String> resolveDisconnectedNode(final Node node, final String cause) {
253 final InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
254 final RendererNode rendererNode = remapNode(mountPointIid);
255 final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
256 wTx.delete(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodeIid(rendererNode));
257 extInterfaces.remove(node.getNodeId());
258 final CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = wTx.submit();
260 checkedFuture.checkedGet();
261 return Futures.immediateFuture(cause);
262 } catch (TransactionCommitFailedException e) {
263 final String message = String.format("Failed to resolve disconnected node %s", node.getNodeId().getValue());
264 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
269 private DataBroker getNodeMountPoint(final InstanceIdentifier<Node> mountPointIid) {
270 final Future<Optional<MountPoint>> futureOptionalObject = getMountpointFromSal(mountPointIid);
272 final Optional<MountPoint> optionalObject = futureOptionalObject.get();
273 LOG.debug("Optional mountpoint object: {}", optionalObject);
274 MountPoint mountPoint;
275 if (optionalObject.isPresent()) {
276 mountPoint = optionalObject.get();
277 if (mountPoint != null) {
278 Optional<DataBroker> optionalDataBroker = mountPoint.getService(DataBroker.class);
279 if (optionalDataBroker.isPresent()) {
280 return optionalDataBroker.get();
282 LOG.warn("Cannot obtain data broker from mountpoint {}", mountPoint);
285 LOG.warn("Cannot obtain mountpoint with IID {}", mountPointIid);
289 } catch (ExecutionException | InterruptedException e) {
290 LOG.warn("Unable to obtain mountpoint ... {}", e);
295 private RendererNode remapNode(final InstanceIdentifier<Node> path) {
296 final RendererNodeBuilder rendererNodeBuilder = new RendererNodeBuilder();
297 rendererNodeBuilder.setKey(new RendererNodeKey(path)).setNodePath(path);
298 return rendererNodeBuilder.build();
301 private InstanceIdentifier<Node> getMountpointIid(final Node node) {
302 return InstanceIdentifier.builder(NetworkTopology.class)
303 .child(Topology.class, new TopologyKey(TOPOLOGY_ID))
304 .child(Node.class, new NodeKey(node.getNodeId()))
308 private boolean isCapableNetconfDevice(final Node node, final NetconfNode netconfAugmentation) {
309 if (netconfAugmentation.getAvailableCapabilities() == null
310 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability() == null
311 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability().isEmpty()) {
312 LOG.warn("Node {} does not contain any capabilities", node.getNodeId().getValue());
315 if (!capabilityCheck(netconfAugmentation.getAvailableCapabilities().getAvailableCapability())) {
316 LOG.warn("Node {} does not contain all capabilities required by vpp-renderer", node.getNodeId().getValue());
322 private boolean capabilityCheck(final List<AvailableCapability> capabilities) {
323 final List<String> availableCapabilities = capabilities.stream()
324 .map(AvailableCapability::getCapability)
325 .collect(Collectors.toList());
326 return requiredCapabilities.stream()
327 .allMatch(availableCapabilities::contains);
330 private NetconfNode getNodeAugmentation(final Node node) {
331 final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
332 if (netconfNode == null) {
333 LOG.warn("Node {} is not a netconf device", node.getNodeId().getValue());
340 * Initialize all common capabilities required by VPP renderer. Any connected node is examined
342 * an appropriate device to handle configuration created by this renderer. A device must support
346 * @return list of string representations of required capabilities
348 private List<String> initializeRequiredCapabilities() {
349 // Required device capabilities
350 String[] capabilityEntries = {V3PO_CAPABILITY, INTERFACES_CAPABILITY};
351 return Arrays.asList(capabilityEntries);
355 // This works as a workaround for mountpoint registration in cluster. If application is registered on different
356 // node as netconf service, it obtains mountpoint registered by SlaveSalFacade (instead of MasterSalFacade). However
357 // this service registers mountpoint a moment later then connectionStatus is set to "Connected". If NodeManager hits
358 // state where device is connected but mountpoint is not yet available, try to get it again in a while
359 private Future<Optional<MountPoint>> getMountpointFromSal(final InstanceIdentifier<Node> iid) {
360 final ExecutorService executorService = Executors.newSingleThreadExecutor();
361 final Callable<Optional<MountPoint>> task = () -> {
365 final Optional<MountPoint> optionalMountpoint = mountService.getMountPoint(iid);
366 if (optionalMountpoint.isPresent()) {
367 return optionalMountpoint;
369 LOG.warn("Mountpoint {} is not registered yet", iid);
370 Thread.sleep(DURATION);
371 } catch (InterruptedException e) {
372 LOG.warn("Thread interrupted to ", e);
375 } while (attempt <= 3);
376 return Optional.absent();
378 return executorService.submit(task);
381 private void syncPhysicalInterfacesInLocalDs(DataBroker mountPointDataBroker, InstanceIdentifier<Node> nodeIid) {
382 ReadWriteTransaction rwTx = dataBroker.newReadWriteTransaction();
383 ReadOnlyTransaction rTx = mountPointDataBroker.newReadOnlyTransaction();
384 Optional<Interfaces> readIfaces = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
385 InstanceIdentifier.create(Interfaces.class), rTx);
386 if (readIfaces.isPresent()) {
387 InstanceIdentifier<RendererNode> rendererNodeIid = VppIidFactory.getRendererNodesIid()
389 .child(RendererNode.class, new RendererNodeKey(nodeIid))
391 Optional<RendererNode> optRendNode = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
392 rendererNodeIid, rwTx);
393 NodeId nodeId = nodeIid.firstKeyOf(Node.class).getNodeId();
394 RendererNode rendNode = new RendererNodeBuilder(optRendNode.get())
395 .addAugmentation(VppInterfaceAugmentation.class, resolveTerminationPoints(nodeId, readIfaces.get()))
397 rwTx.put(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodeIid(optRendNode.get()), rendNode,
401 DataStoreHelper.submitToDs(rwTx);
404 private VppInterfaceAugmentation resolveTerminationPoints(NodeId nodeId, Interfaces interfaces) {
405 List<PhysicalInterface> phIfaces = new ArrayList<>();
406 if (interfaces != null && interfaces.getInterface() != null) {
407 interfaces.getInterface()
409 .filter(iface -> iface.getType().equals(EthernetCsmacd.class))
410 .filter(iface -> iface.getAugmentation(Interface1.class) != null)
412 PhysicalInterfaceBuilder phIface = new PhysicalInterfaceBuilder();
413 phIface.setInterfaceName(iface.getName());
414 phIface.setType(iface.getType());
415 phIface.setAddress(resolveIpAddress(iface.getAugmentation(Interface1.class)));
416 if (extInterfaces.get(nodeId) != null
417 && extInterfaces.get(nodeId).getInterfaceName().equals(phIface.getInterfaceName())) {
418 phIface.setExternal(true);
419 extInterfaces.put(nodeId, new PhysicalInterfaceKey(iface.getName()));
420 LOG.info("Interface {} is marked as public interface based on bundle configuration.",
423 if (PUBLIC_INTERFACE.equals(iface.getDescription())) {
424 phIface.setExternal(true);
425 extInterfaces.put(nodeId, new PhysicalInterfaceKey(iface.getName()));
426 LOG.info("Interface {} is marked as public interface based on HC configuration.",
429 phIfaces.add(phIface.build());
432 return new VppInterfaceAugmentationBuilder().setPhysicalInterface(phIfaces).build();
435 private List<IpAddress> resolveIpAddress(Interface1 iface) {
436 if (iface.getIpv4() != null && iface.getIpv4().getAddress() != null) {
437 return iface.getIpv4().getAddress().stream().map(ipv4 ->
438 new IpAddress(new Ipv4Address(ipv4.getIp().getValue()))).collect(Collectors.toList());
439 } else if (iface.getIpv6() != null && iface.getIpv6().getAddress() != null) {
440 return iface.getIpv6().getAddress().stream().map(ipv6 ->
441 new IpAddress(new Ipv4Address(ipv6.getIp().getValue()))).collect(Collectors.toList());
443 return Lists.newArrayList();
446 public static Map<NodeId, String> resolvePublicInterfaces(DataBroker dataProvider) {
447 Map<NodeId, String> nodes = new HashMap<>();
448 ReadOnlyTransaction rTx = dataProvider.newReadOnlyTransaction();
449 Optional<RendererNodes> rendNodes =
450 DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodesIid(), rTx);
452 if (!rendNodes.isPresent()) {
458 .filter(rn -> rn.getAugmentation(VppInterfaceAugmentation.class) != null)
459 .filter(rn -> rn.getAugmentation(VppInterfaceAugmentation.class).getPhysicalInterface() != null)
461 java.util.Optional<PhysicalInterface> pubInt = rn.getAugmentation(VppInterfaceAugmentation.class)
462 .getPhysicalInterface()
464 .filter(phInt -> phInt.isExternal())
466 if (pubInt.isPresent()) {
467 nodes.put(rn.getNodePath().firstKeyOf(Node.class).getNodeId(), pubInt.get().getInterfaceName());