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.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;
79 import com.google.common.collect.Lists;
80 import com.google.common.collect.Sets;
82 public class VppNodeManager {
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=2017-03-15)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 static final String PUBLIC_INTERFACE = "public-interface";
92 private final Map<NodeId, PhysicalInterfaceKey> extInterfaces = new HashMap<>();
93 private final DataBroker dataBroker;
94 private final List<String> requiredCapabilities;
95 private final MountPointService mountService;
97 public VppNodeManager(@Nonnull final DataBroker dataBroker,
98 @Nonnull final BindingAwareBroker.ProviderContext session, @Nullable String physicalInterfaces) {
99 this.dataBroker = Preconditions.checkNotNull(dataBroker);
100 this.mountService = Preconditions.checkNotNull(session.getSALService(MountPointService.class));
101 requiredCapabilities = initializeRequiredCapabilities();
102 if (!Strings.isNullOrEmpty(physicalInterfaces) && !Objects.equals(physicalInterfaces, NO_PUBLIC_INT_SPECIFIED)) {
103 loadPhysicalInterfaces(physicalInterfaces);
108 * Caches list of physical interfaces.
110 private void loadPhysicalInterfaces(@Nonnull String physicalInterfaces) {
111 for (String intfOnNode : Sets.newConcurrentHashSet(Splitter.on(",").split(physicalInterfaces))) {
112 List<String> entries = Lists.newArrayList(Splitter.on(":").split(intfOnNode));
113 if (entries.size() != 2) {
114 LOG.warn("Cannot resolve {} initial configuration for physical interfaces.", intfOnNode);
117 NodeId nodeId = new NodeId(entries.get(0));
118 PhysicalInterfaceKey infaceKey = new PhysicalInterfaceKey(entries.get(1));
119 LOG.info("Interface " + infaceKey + " on node " + nodeId + "will be considered as external");
120 extInterfaces.put(nodeId, infaceKey);
125 * Synchronizes nodes to DataStore based on their modification state which results in
126 * create/update/remove of Node.
128 public void syncNodes(final Node dataAfter, final Node dataBefore) {
129 if (isControllerConfigNode(dataAfter, dataBefore)) {
130 LOG.trace("{} is ignored by VPP-renderer", CONTROLLER_CONFIG_NODE);
133 ListenableFuture<String> syncFuture = Futures.immediateFuture(null);
135 if (dataBefore == null && dataAfter != null) {
136 syncFuture = createNode(dataAfter);
138 // Connected/disconnected node
139 else if (dataBefore != null && dataAfter != null) {
140 syncFuture = updateNode(dataAfter);
143 else if (dataBefore != null) {
144 syncFuture = removeNode(dataBefore);
146 Futures.addCallback(syncFuture, new FutureCallback<String>() {
148 public void onSuccess(@Nullable String message) {
149 LOG.info("Node synchronization completed. {} ", message);
153 public void onFailure(@Nonnull Throwable t) {
154 LOG.warn("Node synchronization failed. Data before: {} after {}", dataBefore, dataAfter);
159 private boolean isControllerConfigNode(final Node dataAfter, final Node dataBefore) {
160 if (dataAfter != null) {
161 return CONTROLLER_CONFIG_NODE.equals(dataAfter.getNodeId());
163 return CONTROLLER_CONFIG_NODE.equals(dataBefore.getNodeId());
166 private ListenableFuture<String> createNode(final Node node) {
167 final String nodeId = node.getNodeId().getValue();
168 LOG.info("Registering new node {}", nodeId);
169 final NetconfNode netconfNode = getNodeAugmentation(node);
170 if (netconfNode == null) {
171 final String message = String.format("Node %s is not an netconf node", nodeId);
172 return Futures.immediateFuture(message);
174 final NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
175 switch (connectionStatus) {
177 final String message = String.format("Connecting device %s ...", nodeId);
178 return Futures.immediateFuture(message);
181 return resolveConnectedNode(node, netconfNode);
183 case UnableToConnect: {
184 final String message = String.format("Connection status is unable to connect for node %s", nodeId);
185 return Futures.immediateFuture(message);
188 final String message = String.format("Unknown connection status for node %s", nodeId);
189 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
194 private ListenableFuture<String> updateNode(final Node node) {
195 final String nodeId = node.getNodeId().getValue();
196 LOG.info("Updating node {}", nodeId);
197 final NetconfNode netconfNode = getNodeAugmentation(node);
198 if (netconfNode == null) {
199 final String message = String.format("Node %s is not an netconf node", nodeId);
200 return Futures.immediateFuture(message);
202 final NetconfNodeConnectionStatus.ConnectionStatus afterNodeStatus = netconfNode.getConnectionStatus();
203 if (Connected.equals(afterNodeStatus)) {
204 return resolveConnectedNode(node, netconfNode);
205 } else if (Connecting.equals(afterNodeStatus)) {
206 final String cause = String.format("Node %s is disconnected, removing from available nodes", nodeId);
207 return resolveDisconnectedNode(node, cause);
208 } else if (UnableToConnect.equals(afterNodeStatus)) {
209 final String cause = String.format("New node %s status is unable to connect, removing from available nodes",
211 return resolveDisconnectedNode(node, cause);
213 final String cause = String.format("New node status is unknown. Node %s will be removed from available nodes",
215 return resolveDisconnectedNode(node, cause);
219 private ListenableFuture<String> removeNode(final Node node) {
220 final String cause = String.format("Node %s is removed", node.getNodeId().getValue());
221 return resolveDisconnectedNode(node, cause);
224 private ListenableFuture<String> resolveConnectedNode(final Node node, final NetconfNode netconfNode) {
225 final String nodeId = node.getNodeId().getValue();
226 final InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
227 final RendererNode rendererNode = remapNode(mountPointIid);
228 if (!isCapableNetconfDevice(node, netconfNode)) {
229 final String message = String.format("Node %s is not connected", nodeId);
230 return Futures.immediateFuture(message);
232 final DataBroker mountpoint = getNodeMountPoint(mountPointIid);
233 if (mountpoint == null) {
234 final String message = String.format("Mountpoint not available for node %s", nodeId);
235 return Futures.immediateFuture(message);
237 final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
238 wTx.put(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodeIid(rendererNode), rendererNode, true);
239 final boolean submit = DataStoreHelper.submitToDs(wTx);
241 final String message = String.format("Node %s is capable and ready", nodeId);
242 syncPhysicalInterfacesInLocalDs(mountpoint, mountPointIid);
243 NatUtil.resolveOutboundNatInterface(mountpoint, mountPointIid, node.getNodeId(), extInterfaces);
244 return Futures.immediateFuture(message);
246 final String message = String.format("Failed to resolve connected node %s", nodeId);
247 return Futures.immediateFuture(message);
251 private ListenableFuture<String> resolveDisconnectedNode(final Node node, final String cause) {
252 final InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
253 final RendererNode rendererNode = remapNode(mountPointIid);
254 final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
255 wTx.delete(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodeIid(rendererNode));
256 extInterfaces.remove(node.getNodeId());
257 final CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = wTx.submit();
259 checkedFuture.checkedGet();
260 return Futures.immediateFuture(cause);
261 } catch (TransactionCommitFailedException e) {
262 final String message = String.format("Failed to resolve disconnected node %s", node.getNodeId().getValue());
263 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
268 private DataBroker getNodeMountPoint(final InstanceIdentifier<Node> mountPointIid) {
269 final Future<Optional<MountPoint>> futureOptionalObject = getMountpointFromSal(mountPointIid);
271 final Optional<MountPoint> optionalObject = futureOptionalObject.get();
272 LOG.debug("Optional mountpoint object: {}", optionalObject);
273 MountPoint mountPoint;
274 if (optionalObject.isPresent()) {
275 mountPoint = optionalObject.get();
276 if (mountPoint != null) {
277 Optional<DataBroker> optionalDataBroker = mountPoint.getService(DataBroker.class);
278 if (optionalDataBroker.isPresent()) {
279 return optionalDataBroker.get();
281 LOG.warn("Cannot obtain data broker from mountpoint {}", mountPoint);
284 LOG.warn("Cannot obtain mountpoint with IID {}", mountPointIid);
288 } catch (ExecutionException | InterruptedException e) {
289 LOG.warn("Unable to obtain mountpoint ... {}", e);
294 private RendererNode remapNode(final InstanceIdentifier<Node> path) {
295 final RendererNodeBuilder rendererNodeBuilder = new RendererNodeBuilder();
296 rendererNodeBuilder.setKey(new RendererNodeKey(path)).setNodePath(path);
297 return rendererNodeBuilder.build();
300 private InstanceIdentifier<Node> getMountpointIid(final Node node) {
301 return InstanceIdentifier.builder(NetworkTopology.class)
302 .child(Topology.class, new TopologyKey(TOPOLOGY_ID))
303 .child(Node.class, new NodeKey(node.getNodeId()))
307 private boolean isCapableNetconfDevice(final Node node, final NetconfNode netconfAugmentation) {
308 if (netconfAugmentation.getAvailableCapabilities() == null
309 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability() == null
310 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability().isEmpty()) {
311 LOG.warn("Node {} does not contain any capabilities", node.getNodeId().getValue());
314 if (!capabilityCheck(netconfAugmentation.getAvailableCapabilities().getAvailableCapability())) {
315 LOG.warn("Node {} does not contain all capabilities required by vpp-renderer", node.getNodeId().getValue());
321 private boolean capabilityCheck(final List<AvailableCapability> capabilities) {
322 final List<String> availableCapabilities = capabilities.stream()
323 .map(AvailableCapability::getCapability)
324 .collect(Collectors.toList());
325 return requiredCapabilities.stream()
326 .allMatch(availableCapabilities::contains);
329 private NetconfNode getNodeAugmentation(final Node node) {
330 final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
331 if (netconfNode == null) {
332 LOG.warn("Node {} is not a netconf device", node.getNodeId().getValue());
339 * Initialize all common capabilities required by VPP renderer. Any connected node is examined
341 * an appropriate device to handle configuration created by this renderer. A device must support
345 * @return list of string representations of required capabilities
347 private List<String> initializeRequiredCapabilities() {
348 // Required device capabilities
349 String[] capabilityEntries = {V3PO_CAPABILITY, INTERFACES_CAPABILITY};
350 return Arrays.asList(capabilityEntries);
354 // This works as a workaround for mountpoint registration in cluster. If application is registered on different
355 // node as netconf service, it obtains mountpoint registered by SlaveSalFacade (instead of MasterSalFacade). However
356 // this service registers mountpoint a moment later then connectionStatus is set to "Connected". If NodeManager hits
357 // state where device is connected but mountpoint is not yet available, try to get it again in a while
358 private Future<Optional<MountPoint>> getMountpointFromSal(final InstanceIdentifier<Node> iid) {
359 final ExecutorService executorService = Executors.newSingleThreadExecutor();
360 final Callable<Optional<MountPoint>> task = () -> {
364 final Optional<MountPoint> optionalMountpoint = mountService.getMountPoint(iid);
365 if (optionalMountpoint.isPresent()) {
366 return optionalMountpoint;
368 LOG.warn("Mountpoint {} is not registered yet", iid);
369 Thread.sleep(DURATION);
370 } catch (InterruptedException e) {
371 LOG.warn("Thread interrupted to ", e);
374 } while (attempt <= 3);
375 return Optional.absent();
377 return executorService.submit(task);
380 private void syncPhysicalInterfacesInLocalDs(DataBroker mountPointDataBroker, InstanceIdentifier<Node> nodeIid) {
381 ReadWriteTransaction rwTx = dataBroker.newReadWriteTransaction();
382 ReadOnlyTransaction rTx = mountPointDataBroker.newReadOnlyTransaction();
383 Optional<Interfaces> readIfaces = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
384 InstanceIdentifier.create(Interfaces.class), rTx);
385 if (readIfaces.isPresent()) {
386 InstanceIdentifier<RendererNode> rendererNodeIid = VppIidFactory.getRendererNodesIid()
388 .child(RendererNode.class, new RendererNodeKey(nodeIid))
390 Optional<RendererNode> optRendNode = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
391 rendererNodeIid, rwTx);
392 NodeId nodeId = nodeIid.firstKeyOf(Node.class).getNodeId();
393 RendererNode rendNode = new RendererNodeBuilder(optRendNode.get())
394 .addAugmentation(VppInterfaceAugmentation.class, resolveTerminationPoints(nodeId, readIfaces.get()))
396 rwTx.put(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodeIid(optRendNode.get()), rendNode,
400 DataStoreHelper.submitToDs(rwTx);
403 private VppInterfaceAugmentation resolveTerminationPoints(NodeId nodeId, Interfaces interfaces) {
404 List<PhysicalInterface> phIfaces = new ArrayList<>();
405 if (interfaces != null && interfaces.getInterface() != null) {
406 interfaces.getInterface()
408 .filter(iface -> iface.getType().equals(EthernetCsmacd.class))
409 .filter(iface -> iface.getAugmentation(Interface1.class) != null)
411 PhysicalInterfaceBuilder phIface = new PhysicalInterfaceBuilder();
412 phIface.setInterfaceName(iface.getName());
413 phIface.setType(iface.getType());
414 phIface.setAddress(resolveIpAddress(iface.getAugmentation(Interface1.class)));
415 if (extInterfaces.get(nodeId) != null
416 && extInterfaces.get(nodeId).getInterfaceName().equals(phIface.getInterfaceName())) {
417 phIface.setExternal(true);
418 extInterfaces.put(nodeId, new PhysicalInterfaceKey(iface.getName()));
419 LOG.info("Interface {} is marked as public interface based on bundle configuration.",
422 if (PUBLIC_INTERFACE.equals(iface.getDescription())) {
423 phIface.setExternal(true);
424 extInterfaces.put(nodeId, new PhysicalInterfaceKey(iface.getName()));
425 LOG.info("Interface {} is marked as public interface based on HC configuration.",
428 phIfaces.add(phIface.build());
431 return new VppInterfaceAugmentationBuilder().setPhysicalInterface(phIfaces).build();
434 private List<IpAddress> resolveIpAddress(Interface1 iface) {
435 if (iface.getIpv4() != null && iface.getIpv4().getAddress() != null) {
436 return iface.getIpv4().getAddress().stream().map(ipv4 ->
437 new IpAddress(new Ipv4Address(ipv4.getIp().getValue()))).collect(Collectors.toList());
438 } else if (iface.getIpv6() != null && iface.getIpv6().getAddress() != null) {
439 return iface.getIpv6().getAddress().stream().map(ipv6 ->
440 new IpAddress(new Ipv4Address(ipv6.getIp().getValue()))).collect(Collectors.toList());
442 return Lists.newArrayList();