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;
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;
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;
70 import com.google.common.collect.Lists;
71 import com.google.common.collect.Sets;
72 public class VppNodeManager {
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;
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);
97 * Caches list of physical interfaces.
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);
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);
114 * Synchronizes nodes to DataStore based on their modification state which results in
115 * create/update/remove of Node.
117 public void syncNodes(Node dataAfter, Node dataBefore) {
118 if (isControllerConfigNode(dataAfter, dataBefore)) {
119 LOG.trace("{} is ignored by VPP-renderer", CONTROLLER_CONFIG_NODE);
123 if (dataBefore == null && dataAfter != null) {
124 createNode(dataAfter);
126 // Connected/disconnected node
127 if (dataBefore != null && dataAfter != null) {
128 updateNode(dataAfter);
131 if (dataBefore != null && dataAfter == null) {
132 removeNode(dataBefore);
136 private boolean isControllerConfigNode(Node dataAfter, Node dataBefore) {
137 if (dataAfter != null) {
138 return CONTROLLER_CONFIG_NODE.equals(dataAfter.getNodeId());
140 return CONTROLLER_CONFIG_NODE.equals(dataBefore.getNodeId());
143 private void createNode(Node node) {
144 LOG.info("Registering new node {}", node.getNodeId().getValue());
145 NetconfNode netconfNode = getNodeAugmentation(node);
146 if (netconfNode == null) {
149 NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
150 switch (connectionStatus) {
152 LOG.info("Connecting device {} ...", node.getNodeId().getValue());
155 resolveConnectedNode(node, netconfNode);
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) {
168 NetconfNodeConnectionStatus.ConnectionStatus afterNodeStatus = netconfNode.getConnectionStatus();
169 if (afterNodeStatus.equals(Connected)) {
170 resolveConnectedNode(node, netconfNode);
172 if (afterNodeStatus.equals(Connecting)) {
173 resolveDisconnectedNode(node);
174 LOG.info("Node {} is disconnected, removing from available nodes", node.getNodeId().getValue());
178 private void removeNode(Node node) {
179 resolveDisconnectedNode(node);
180 LOG.info("Node {} is removed", node.getNodeId().getValue());
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());
190 final DataBroker mountpoint = getNodeMountPoint(mountPointIid);
191 if (mountpoint == null) {
192 LOG.warn("Mountpoint not available for node {}", node.getNodeId().getValue());
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);
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();
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());
218 private DataBroker getNodeMountPoint(InstanceIdentifier<Node> mountPointIid) {
219 final Future<Optional<MountPoint>> futureOptionalObject = getMountpointFromSal(mountPointIid);
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();
231 LOG.warn("Cannot obtain data broker from mountpoint {}", mountPoint);
234 LOG.warn("Cannot obtain mountpoint with IID {}", mountPointIid);
238 } catch (ExecutionException | InterruptedException e) {
239 LOG.warn("Unable to obtain mountpoint ... {}", e);
244 private RendererNode remapNode(InstanceIdentifier<Node> path) {
245 RendererNodeBuilder rendererNodeBuilder = new RendererNodeBuilder();
246 rendererNodeBuilder.setKey(new RendererNodeKey(path)).setNodePath(path);
247 return rendererNodeBuilder.build();
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()))
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());
264 if (!capabilityCheck(netconfAugmentation.getAvailableCapabilities().getAvailableCapability())) {
265 LOG.warn("Node {} does not contain all capabilities required by vpp-renderer", node.getNodeId().getValue());
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);
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());
289 * Initialize all common capabilities required by VPP renderer. Any connected node is examined
291 * an appropriate device to handle configuration created by this renderer. A device must support
295 * @return list of string representations of required capabilities
297 private List<String> initializeRequiredCapabilities() {
298 // Required device capabilities
300 String[] capabilityEntries = {V3PO_CAPABILITY, INTERFACES_CAPABILITY};
301 return Arrays.asList(capabilityEntries);
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 = () -> {
315 final Optional<MountPoint> optionalMountpoint = mountService.getMountPoint(iid);
316 if (optionalMountpoint.isPresent()) {
317 return optionalMountpoint;
319 LOG.warn("Mountpoint {} is not registered yet", iid);
320 Thread.sleep(DURATION);
321 } catch (InterruptedException e) {
322 LOG.warn("Thread interrupted to ", e);
325 } while (attempt <= 3);
326 return Optional.absent();
328 return executorService.submit(task);
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()
339 .child(RendererNode.class, new RendererNodeKey(nodeIid))
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()))
347 rwTx.put(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodeIid(optRendNode.get()), rendNode,
351 DataStoreHelper.submitToDs(rwTx);
354 private VppInterfaceAugmentation resolveTerminationPoints(NodeId nodeId, Interfaces interfaces) {
355 List<PhysicalInterface> phIfaces = new ArrayList<>();
356 if (interfaces != null && interfaces.getInterface() != null) {
357 interfaces.getInterface()
359 .filter(iface -> iface.getType().equals(EthernetCsmacd.class))
360 .filter(iface -> iface.getAugmentation(Interface1.class) != null)
362 PhysicalInterfaceBuilder phIface = new PhysicalInterfaceBuilder();
363 phIface.setInterfaceName(iface.getName());
364 phIface.setType(iface.getType());
365 phIface.setAddress(resolveIpAddress(iface.getAugmentation(Interface1.class)));
367 if (extInterfaces.get(nodeId) != null && extInterfaces.get(nodeId)
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());
374 phIfaces.add(phIface.build());
377 return new VppInterfaceAugmentationBuilder().setPhysicalInterface(phIfaces).build();
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());
390 return Lists.newArrayList();