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.stream.Collectors;
23 import javax.annotation.Nonnull;
24 import javax.annotation.Nullable;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
28 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
29 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
30 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
31 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
32 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
33 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
34 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
35 import org.opendaylight.groupbasedpolicy.renderer.vpp.lisp.info.container.HostRelatedInfoContainer;
36 import org.opendaylight.groupbasedpolicy.renderer.vpp.lisp.info.container.states.PhysicalInterfaces;
37 import org.opendaylight.groupbasedpolicy.renderer.vpp.nat.NatUtil;
38 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.MountedDataBrokerProvider;
39 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppIidFactory;
40 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppRendererProcessingException;
41 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.EthernetCsmacd;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface1;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.RendererNodes;
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.base.Optional;
71 import com.google.common.base.Preconditions;
72 import com.google.common.base.Splitter;
73 import com.google.common.base.Strings;
74 import com.google.common.collect.Lists;
75 import com.google.common.collect.Sets;
76 import com.google.common.util.concurrent.CheckedFuture;
77 import com.google.common.util.concurrent.FutureCallback;
78 import com.google.common.util.concurrent.Futures;
79 import com.google.common.util.concurrent.ListenableFuture;
80 import com.google.common.util.concurrent.MoreExecutors;
82 public class VppNodeManager {
84 private static final TopologyId TOPOLOGY_ID = new TopologyId("topology-netconf");
85 private static final Logger LOG = LoggerFactory.getLogger(VppNodeManager.class);
86 private static final String V3PO_CAPABILITY = "(urn:opendaylight:params:xml:ns:yang:v3po?revision=2017-06-07)v3po";
87 private static final String INTERFACES_CAPABILITY = "(urn:ietf:params:xml:ns:yang:ietf-interfaces?revision=2014-05-08)ietf-interfaces";
88 private static final NodeId CONTROLLER_CONFIG_NODE = new NodeId("controller-config");
89 private static final String NO_PUBLIC_INT_SPECIFIED = "unspecified";
90 private static final String PUBLIC_INTERFACE = "public-interface";
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 private final HostRelatedInfoContainer hostRelatedInfoContainer = HostRelatedInfoContainer.getInstance();
96 private final MountedDataBrokerProvider mountProvider;
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 this.mountProvider = new MountedDataBrokerProvider(mountService, dataBroker);
103 requiredCapabilities = initializeRequiredCapabilities();
104 if (!Strings.isNullOrEmpty(physicalInterfaces) && !Objects.equals(physicalInterfaces, NO_PUBLIC_INT_SPECIFIED)) {
105 loadPhysicalInterfaces(physicalInterfaces);
110 * Caches list of physical interfaces.
112 private void loadPhysicalInterfaces(@Nonnull String physicalInterfaces) {
113 for (String intfOnNode : Sets.newConcurrentHashSet(Splitter.on(",").split(physicalInterfaces))) {
114 List<String> entries = Lists.newArrayList(Splitter.on(":").split(intfOnNode));
115 if (entries.size() != 2) {
116 LOG.warn("Cannot resolve {} initial configuration for physical interfaces.", intfOnNode);
119 NodeId nodeId = new NodeId(entries.get(0));
120 PhysicalInterfaceKey infaceKey = new PhysicalInterfaceKey(entries.get(1));
121 LOG.info("Interface %s on node %swill be considered as external", infaceKey, nodeId);
122 extInterfaces.put(nodeId, infaceKey);
127 * Synchronizes nodes to DataStore based on their modification state which results in
128 * create/update/remove of Node.
129 * @param dataAfter data after modification
130 * @param dataBefore data Before modification
132 public void syncNodes(final Node dataAfter, final Node dataBefore) {
133 if (isControllerConfigNode(dataAfter, dataBefore)) {
134 LOG.trace("{} is ignored by VPP-renderer", CONTROLLER_CONFIG_NODE);
137 ListenableFuture<String> syncFuture = Futures.immediateFuture(null);
139 if (dataBefore == null && dataAfter != null) {
140 syncFuture = createNode(dataAfter);
142 // Connected/disconnected node
143 else if (dataBefore != null && dataAfter != null) {
144 syncFuture = updateNode(dataAfter);
147 else if (dataBefore != null) {
148 syncFuture = removeNode(dataBefore);
150 Futures.addCallback(syncFuture, new FutureCallback<String>() {
152 public void onSuccess(@Nullable String message) {
153 LOG.info("Node synchronization completed. {} ", message);
157 public void onFailure(@Nonnull Throwable t) {
158 LOG.warn("Node synchronization failed. Data before: {} after {}", dataBefore, dataAfter);
160 }, MoreExecutors.directExecutor());
163 private boolean isControllerConfigNode(final Node dataAfter, final Node dataBefore) {
164 if (dataAfter != null) {
165 return CONTROLLER_CONFIG_NODE.equals(dataAfter.getNodeId());
167 return CONTROLLER_CONFIG_NODE.equals(dataBefore.getNodeId());
170 private ListenableFuture<String> createNode(final Node node) {
171 final String nodeId = node.getNodeId().getValue();
172 LOG.info("Registering new node {}", nodeId);
173 final NetconfNode netconfNode = getNodeAugmentation(node);
174 if (netconfNode == null) {
175 final String message = String.format("Node %s is not an netconf node", nodeId);
176 return Futures.immediateFuture(message);
178 final NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
179 switch (connectionStatus) {
181 final String message = String.format("Connecting device %s ...", nodeId);
182 return Futures.immediateFuture(message);
185 return resolveConnectedNode(node, netconfNode);
187 case UnableToConnect: {
188 final String message = String.format("Connection status is unable to connect for node %s", nodeId);
189 return Futures.immediateFuture(message);
192 final String message = String.format("Unknown connection status for node %s", nodeId);
193 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
198 private ListenableFuture<String> updateNode(final Node node) {
199 final String nodeId = node.getNodeId().getValue();
200 LOG.info("Updating node {}", nodeId);
201 final NetconfNode netconfNode = getNodeAugmentation(node);
202 if (netconfNode == null) {
203 final String message = String.format("Node %s is not an netconf node", nodeId);
204 return Futures.immediateFuture(message);
206 final NetconfNodeConnectionStatus.ConnectionStatus afterNodeStatus = netconfNode.getConnectionStatus();
207 if (Connected.equals(afterNodeStatus)) {
208 return resolveConnectedNode(node, netconfNode);
209 } else if (Connecting.equals(afterNodeStatus)) {
210 final String cause = String.format("Node %s is disconnected, removing from available nodes", nodeId);
211 return resolveDisconnectedNode(node, cause);
212 } else if (UnableToConnect.equals(afterNodeStatus)) {
213 final String cause = String.format("New node %s status is unable to connect, removing from available nodes",
215 return resolveDisconnectedNode(node, cause);
217 final String cause = String.format("New node status is unknown. Node %s will be removed from available nodes",
219 return resolveDisconnectedNode(node, cause);
223 private ListenableFuture<String> removeNode(final Node node) {
224 final String cause = String.format("Node %s is removed", node.getNodeId().getValue());
225 return resolveDisconnectedNode(node, cause);
228 private ListenableFuture<String> resolveConnectedNode(final Node node, final NetconfNode netconfNode) {
229 final String nodeId = node.getNodeId().getValue();
230 final InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
231 final RendererNode rendererNode = remapNode(mountPointIid);
232 if (!isCapableNetconfDevice(node, netconfNode)) {
233 final String message = String.format("Node %s is not connected", nodeId);
234 return Futures.immediateFuture(message);
236 final Optional<DataBroker> mountpoint = mountProvider.resolveDataBrokerForMountPoint(mountPointIid);
237 if (!mountpoint.isPresent()) {
238 final String message = String.format("Mountpoint not available for node %s", nodeId);
239 return Futures.immediateFuture(message);
241 final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
242 wTx.put(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodeIid(rendererNode), rendererNode, true);
243 final boolean submit = DataStoreHelper.submitToDs(wTx);
245 final String message = String.format("Node %s is capable and ready", nodeId);
246 syncPhysicalInterfacesInLocalDs(mountpoint.get(), mountPointIid);
247 NatUtil.resolveOutboundNatInterface(mountPointIid, node.getNodeId(), extInterfaces);
248 return Futures.immediateFuture(message);
250 final String message = String.format("Failed to resolve connected node %s", nodeId);
251 return Futures.immediateFuture(message);
255 private ListenableFuture<String> resolveDisconnectedNode(final Node node, final String cause) {
256 final InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
257 final RendererNode rendererNode = remapNode(mountPointIid);
258 final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
259 wTx.delete(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodeIid(rendererNode));
260 final CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = wTx.submit();
262 checkedFuture.checkedGet();
263 mountProvider.deleteDataBrokerForMountPoint(mountPointIid);
264 return Futures.immediateFuture(cause);
265 } catch (TransactionCommitFailedException e) {
266 final String message = String.format("Failed to resolve disconnected node %s", node.getNodeId().getValue());
267 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
271 private RendererNode remapNode(final InstanceIdentifier<Node> path) {
272 final RendererNodeBuilder rendererNodeBuilder = new RendererNodeBuilder();
273 rendererNodeBuilder.setKey(new RendererNodeKey(path)).setNodePath(path);
274 return rendererNodeBuilder.build();
277 private InstanceIdentifier<Node> getMountpointIid(final Node node) {
278 return InstanceIdentifier.builder(NetworkTopology.class)
279 .child(Topology.class, new TopologyKey(TOPOLOGY_ID))
280 .child(Node.class, new NodeKey(node.getNodeId()))
284 private boolean isCapableNetconfDevice(final Node node, final NetconfNode netconfAugmentation) {
285 if (netconfAugmentation.getAvailableCapabilities() == null
286 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability() == null
287 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability().isEmpty()) {
288 LOG.warn("Node {} does not contain any capabilities", node.getNodeId().getValue());
291 if (!capabilityCheck(netconfAugmentation.getAvailableCapabilities().getAvailableCapability())) {
292 LOG.warn("Node {} does not contain all capabilities required by vpp-renderer", node.getNodeId().getValue());
298 private boolean capabilityCheck(final List<AvailableCapability> capabilities) {
299 final List<String> availableCapabilities = capabilities.stream()
300 .map(AvailableCapability::getCapability)
301 .collect(Collectors.toList());
302 return requiredCapabilities.stream()
303 .allMatch(availableCapabilities::contains);
306 private NetconfNode getNodeAugmentation(final Node node) {
307 final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
308 if (netconfNode == null) {
309 LOG.warn("Node {} is not a netconf device", node.getNodeId().getValue());
316 * Initialize all common capabilities required by VPP renderer. Any connected node is examined
318 * an appropriate device to handle configuration created by this renderer. A device must support
322 * @return list of string representations of required capabilities
324 private List<String> initializeRequiredCapabilities() {
325 // Required device capabilities
326 String[] capabilityEntries = {V3PO_CAPABILITY, INTERFACES_CAPABILITY};
327 return Arrays.asList(capabilityEntries);
330 private void syncPhysicalInterfacesInLocalDs(DataBroker mountPointDataBroker, InstanceIdentifier<Node> nodeIid) {
331 ReadWriteTransaction rwTx = dataBroker.newReadWriteTransaction();
332 ReadOnlyTransaction rTx = mountPointDataBroker.newReadOnlyTransaction();
333 Optional<Interfaces> readIfaces = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
334 InstanceIdentifier.create(Interfaces.class), rTx);
335 if (readIfaces.isPresent()) {
336 InstanceIdentifier<RendererNode> rendererNodeIid = VppIidFactory.getRendererNodesIid()
338 .child(RendererNode.class, new RendererNodeKey(nodeIid))
340 Optional<RendererNode> optRendNode = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
341 rendererNodeIid, rwTx);
342 NodeId nodeId = nodeIid.firstKeyOf(Node.class).getNodeId();
343 RendererNode rendNode = new RendererNodeBuilder(optRendNode.get())
344 .addAugmentation(VppInterfaceAugmentation.class, resolveTerminationPoints(nodeId, readIfaces.get()))
346 rwTx.put(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodeIid(optRendNode.get()), rendNode,
350 DataStoreHelper.submitToDs(rwTx);
353 private VppInterfaceAugmentation resolveTerminationPoints(NodeId nodeId, Interfaces interfaces) {
354 List<PhysicalInterface> phIfaces = new ArrayList<>();
355 if (interfaces != null && interfaces.getInterface() != null) {
356 interfaces.getInterface()
358 .filter(iface -> iface.getType().equals(EthernetCsmacd.class))
359 .filter(iface -> iface.getAugmentation(Interface1.class) != null)
361 PhysicalInterfaceBuilder phIface = new PhysicalInterfaceBuilder();
362 phIface.setInterfaceName(iface.getName());
363 phIface.setType(iface.getType());
364 phIface.setAddress(resolveIpAddress(iface.getAugmentation(Interface1.class)));
365 PhysicalInterfaces physicalInterfaces = hostRelatedInfoContainer
366 .getPhysicalInterfaceState(nodeId.getValue());
367 if (physicalInterfaces == null) {
368 physicalInterfaces = new PhysicalInterfaces();
369 hostRelatedInfoContainer.setPhysicalInterfaceStateOfHost(nodeId.getValue(), physicalInterfaces);
373 if (extInterfaces.get(nodeId) != null
374 && extInterfaces.get(nodeId).getInterfaceName().equals(phIface.getInterfaceName())) {
375 phIface.setExternal(true);
376 extInterfaces.put(nodeId, new PhysicalInterfaceKey(iface.getName()));
378 .addPhysicalInterfaceInfo(PhysicalInterfaces.PhysicalInterfaceType.PUBLIC,
379 phIface.getInterfaceName(), phIface.getAddress().get(0));
380 LOG.info("Interface {} is marked as public interface based on bundle configuration.",
383 if (PUBLIC_INTERFACE.equals(iface.getDescription())) {
384 phIface.setExternal(true);
385 extInterfaces.put(nodeId, new PhysicalInterfaceKey(iface.getName()));
387 .addPhysicalInterfaceInfo(PhysicalInterfaces.PhysicalInterfaceType.PUBLIC,
388 phIface.getInterfaceName(), phIface.getAddress().get(0));
389 LOG.info("Interface {} is marked as public interface based on HC configuration.",
392 phIfaces.add(phIface.build());
395 return new VppInterfaceAugmentationBuilder().setPhysicalInterface(phIfaces).build();
398 private List<IpAddress> resolveIpAddress(Interface1 iface) {
399 if (iface.getIpv4() != null && iface.getIpv4().getAddress() != null) {
400 return iface.getIpv4().getAddress().stream().map(ipv4 ->
401 new IpAddress(new Ipv4Address(ipv4.getIp().getValue()))).collect(Collectors.toList());
402 } else if (iface.getIpv6() != null && iface.getIpv6().getAddress() != null) {
403 return iface.getIpv6().getAddress().stream().map(ipv6 ->
404 new IpAddress(new Ipv4Address(ipv6.getIp().getValue()))).collect(Collectors.toList());
406 return Lists.newArrayList();
409 public static Map<NodeId, String> resolvePublicInterfaces(ReadTransaction rTx) {
410 Map<NodeId, String> nodes = new HashMap<>();
411 Optional<RendererNodes> rendNodes =
412 DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodesIid(), rTx);
413 if (!rendNodes.isPresent()) {
419 .filter(rn -> rn.getAugmentation(VppInterfaceAugmentation.class) != null)
420 .filter(rn -> rn.getAugmentation(VppInterfaceAugmentation.class).getPhysicalInterface() != null)
422 java.util.Optional<PhysicalInterface> pubInt = rn.getAugmentation(VppInterfaceAugmentation.class)
423 .getPhysicalInterface()
425 .filter(PhysicalInterface::isExternal)
427 if (pubInt.isPresent()) {
428 nodes.put(rn.getNodePath().firstKeyOf(Node.class).getNodeId(), pubInt.get().getInterfaceName());