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;
81 public class VppNodeManager {
83 private static final TopologyId TOPOLOGY_ID = new TopologyId("topology-netconf");
84 private static final Logger LOG = LoggerFactory.getLogger(VppNodeManager.class);
85 private static final String V3PO_CAPABILITY = "(urn:opendaylight:params:xml:ns:yang:v3po?revision=2017-06-07)v3po";
86 private static final String INTERFACES_CAPABILITY = "(urn:ietf:params:xml:ns:yang:ietf-interfaces?revision=2014-05-08)ietf-interfaces";
87 private static final NodeId CONTROLLER_CONFIG_NODE = new NodeId("controller-config");
88 private static final String NO_PUBLIC_INT_SPECIFIED = "unspecified";
89 private static final String PUBLIC_INTERFACE = "public-interface";
90 private final Map<NodeId, PhysicalInterfaceKey> extInterfaces = new HashMap<>();
91 private final DataBroker dataBroker;
92 private final List<String> requiredCapabilities;
93 private final MountPointService mountService;
94 private final HostRelatedInfoContainer hostRelatedInfoContainer = HostRelatedInfoContainer.getInstance();
95 private final MountedDataBrokerProvider mountProvider;
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 this.mountProvider = new MountedDataBrokerProvider(mountService, dataBroker);
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 %s on node %swill be considered as external", infaceKey, nodeId);
121 extInterfaces.put(nodeId, infaceKey);
126 * Synchronizes nodes to DataStore based on their modification state which results in
127 * create/update/remove of Node.
128 * @param dataAfter data after modification
129 * @param dataBefore data Before modification
131 public void syncNodes(final Node dataAfter, final Node dataBefore) {
132 if (isControllerConfigNode(dataAfter, dataBefore)) {
133 LOG.trace("{} is ignored by VPP-renderer", CONTROLLER_CONFIG_NODE);
136 ListenableFuture<String> syncFuture = Futures.immediateFuture(null);
138 if (dataBefore == null && dataAfter != null) {
139 syncFuture = createNode(dataAfter);
141 // Connected/disconnected node
142 else if (dataBefore != null && dataAfter != null) {
143 syncFuture = updateNode(dataAfter);
146 else if (dataBefore != null) {
147 syncFuture = removeNode(dataBefore);
149 Futures.addCallback(syncFuture, new FutureCallback<String>() {
151 public void onSuccess(@Nullable String message) {
152 LOG.info("Node synchronization completed. {} ", message);
156 public void onFailure(@Nonnull Throwable t) {
157 LOG.warn("Node synchronization failed. Data before: {} after {}", dataBefore, dataAfter);
162 private boolean isControllerConfigNode(final Node dataAfter, final Node dataBefore) {
163 if (dataAfter != null) {
164 return CONTROLLER_CONFIG_NODE.equals(dataAfter.getNodeId());
166 return CONTROLLER_CONFIG_NODE.equals(dataBefore.getNodeId());
169 private ListenableFuture<String> createNode(final Node node) {
170 final String nodeId = node.getNodeId().getValue();
171 LOG.info("Registering new node {}", nodeId);
172 final NetconfNode netconfNode = getNodeAugmentation(node);
173 if (netconfNode == null) {
174 final String message = String.format("Node %s is not an netconf node", nodeId);
175 return Futures.immediateFuture(message);
177 final NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
178 switch (connectionStatus) {
180 final String message = String.format("Connecting device %s ...", nodeId);
181 return Futures.immediateFuture(message);
184 return resolveConnectedNode(node, netconfNode);
186 case UnableToConnect: {
187 final String message = String.format("Connection status is unable to connect for node %s", nodeId);
188 return Futures.immediateFuture(message);
191 final String message = String.format("Unknown connection status for node %s", nodeId);
192 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
197 private ListenableFuture<String> updateNode(final Node node) {
198 final String nodeId = node.getNodeId().getValue();
199 LOG.info("Updating node {}", nodeId);
200 final NetconfNode netconfNode = getNodeAugmentation(node);
201 if (netconfNode == null) {
202 final String message = String.format("Node %s is not an netconf node", nodeId);
203 return Futures.immediateFuture(message);
205 final NetconfNodeConnectionStatus.ConnectionStatus afterNodeStatus = netconfNode.getConnectionStatus();
206 if (Connected.equals(afterNodeStatus)) {
207 return resolveConnectedNode(node, netconfNode);
208 } else if (Connecting.equals(afterNodeStatus)) {
209 final String cause = String.format("Node %s is disconnected, removing from available nodes", nodeId);
210 return resolveDisconnectedNode(node, cause);
211 } else if (UnableToConnect.equals(afterNodeStatus)) {
212 final String cause = String.format("New node %s status is unable to connect, removing from available nodes",
214 return resolveDisconnectedNode(node, cause);
216 final String cause = String.format("New node status is unknown. Node %s will be removed from available nodes",
218 return resolveDisconnectedNode(node, cause);
222 private ListenableFuture<String> removeNode(final Node node) {
223 final String cause = String.format("Node %s is removed", node.getNodeId().getValue());
224 return resolveDisconnectedNode(node, cause);
227 private ListenableFuture<String> resolveConnectedNode(final Node node, final NetconfNode netconfNode) {
228 final String nodeId = node.getNodeId().getValue();
229 final InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
230 final RendererNode rendererNode = remapNode(mountPointIid);
231 if (!isCapableNetconfDevice(node, netconfNode)) {
232 final String message = String.format("Node %s is not connected", nodeId);
233 return Futures.immediateFuture(message);
235 final Optional<DataBroker> mountpoint = mountProvider.resolveDataBrokerForMountPoint(mountPointIid);
236 if (mountpoint.isPresent()) {
237 final String message = String.format("Mountpoint not available for node %s", nodeId);
238 return Futures.immediateFuture(message);
240 final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
241 wTx.put(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodeIid(rendererNode), rendererNode, true);
242 final boolean submit = DataStoreHelper.submitToDs(wTx);
244 final String message = String.format("Node %s is capable and ready", nodeId);
245 syncPhysicalInterfacesInLocalDs(mountpoint.get(), mountPointIid);
246 NatUtil.resolveOutboundNatInterface(mountPointIid, node.getNodeId(), extInterfaces);
247 return Futures.immediateFuture(message);
249 final String message = String.format("Failed to resolve connected node %s", nodeId);
250 return Futures.immediateFuture(message);
254 private ListenableFuture<String> resolveDisconnectedNode(final Node node, final String cause) {
255 final InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
256 final RendererNode rendererNode = remapNode(mountPointIid);
257 final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
258 wTx.delete(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodeIid(rendererNode));
259 final CheckedFuture<Void, TransactionCommitFailedException> checkedFuture = wTx.submit();
261 checkedFuture.checkedGet();
262 return Futures.immediateFuture(cause);
263 } catch (TransactionCommitFailedException e) {
264 final String message = String.format("Failed to resolve disconnected node %s", node.getNodeId().getValue());
265 return Futures.immediateFailedFuture(new VppRendererProcessingException(message));
269 private RendererNode remapNode(final InstanceIdentifier<Node> path) {
270 final RendererNodeBuilder rendererNodeBuilder = new RendererNodeBuilder();
271 rendererNodeBuilder.setKey(new RendererNodeKey(path)).setNodePath(path);
272 return rendererNodeBuilder.build();
275 private InstanceIdentifier<Node> getMountpointIid(final Node node) {
276 return InstanceIdentifier.builder(NetworkTopology.class)
277 .child(Topology.class, new TopologyKey(TOPOLOGY_ID))
278 .child(Node.class, new NodeKey(node.getNodeId()))
282 private boolean isCapableNetconfDevice(final Node node, final NetconfNode netconfAugmentation) {
283 if (netconfAugmentation.getAvailableCapabilities() == null
284 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability() == null
285 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability().isEmpty()) {
286 LOG.warn("Node {} does not contain any capabilities", node.getNodeId().getValue());
289 if (!capabilityCheck(netconfAugmentation.getAvailableCapabilities().getAvailableCapability())) {
290 LOG.warn("Node {} does not contain all capabilities required by vpp-renderer", node.getNodeId().getValue());
296 private boolean capabilityCheck(final List<AvailableCapability> capabilities) {
297 final List<String> availableCapabilities = capabilities.stream()
298 .map(AvailableCapability::getCapability)
299 .collect(Collectors.toList());
300 return requiredCapabilities.stream()
301 .allMatch(availableCapabilities::contains);
304 private NetconfNode getNodeAugmentation(final Node node) {
305 final NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
306 if (netconfNode == null) {
307 LOG.warn("Node {} is not a netconf device", node.getNodeId().getValue());
314 * Initialize all common capabilities required by VPP renderer. Any connected node is examined
316 * an appropriate device to handle configuration created by this renderer. A device must support
320 * @return list of string representations of required capabilities
322 private List<String> initializeRequiredCapabilities() {
323 // Required device capabilities
324 String[] capabilityEntries = {V3PO_CAPABILITY, INTERFACES_CAPABILITY};
325 return Arrays.asList(capabilityEntries);
328 private void syncPhysicalInterfacesInLocalDs(DataBroker mountPointDataBroker, InstanceIdentifier<Node> nodeIid) {
329 ReadWriteTransaction rwTx = dataBroker.newReadWriteTransaction();
330 ReadOnlyTransaction rTx = mountPointDataBroker.newReadOnlyTransaction();
331 Optional<Interfaces> readIfaces = DataStoreHelper.readFromDs(LogicalDatastoreType.CONFIGURATION,
332 InstanceIdentifier.create(Interfaces.class), rTx);
333 if (readIfaces.isPresent()) {
334 InstanceIdentifier<RendererNode> rendererNodeIid = VppIidFactory.getRendererNodesIid()
336 .child(RendererNode.class, new RendererNodeKey(nodeIid))
338 Optional<RendererNode> optRendNode = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
339 rendererNodeIid, rwTx);
340 NodeId nodeId = nodeIid.firstKeyOf(Node.class).getNodeId();
341 RendererNode rendNode = new RendererNodeBuilder(optRendNode.get())
342 .addAugmentation(VppInterfaceAugmentation.class, resolveTerminationPoints(nodeId, readIfaces.get()))
344 rwTx.put(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodeIid(optRendNode.get()), rendNode,
348 DataStoreHelper.submitToDs(rwTx);
351 private VppInterfaceAugmentation resolveTerminationPoints(NodeId nodeId, Interfaces interfaces) {
352 List<PhysicalInterface> phIfaces = new ArrayList<>();
353 if (interfaces != null && interfaces.getInterface() != null) {
354 interfaces.getInterface()
356 .filter(iface -> iface.getType().equals(EthernetCsmacd.class))
357 .filter(iface -> iface.getAugmentation(Interface1.class) != null)
359 PhysicalInterfaceBuilder phIface = new PhysicalInterfaceBuilder();
360 phIface.setInterfaceName(iface.getName());
361 phIface.setType(iface.getType());
362 phIface.setAddress(resolveIpAddress(iface.getAugmentation(Interface1.class)));
363 PhysicalInterfaces physicalInterfaces = hostRelatedInfoContainer
364 .getPhysicalInterfaceState(nodeId.getValue());
365 if (physicalInterfaces == null) {
366 physicalInterfaces = new PhysicalInterfaces();
367 hostRelatedInfoContainer.setPhysicalInterfaceStateOfHost(nodeId.getValue(), physicalInterfaces);
371 if (extInterfaces.get(nodeId) != null
372 && extInterfaces.get(nodeId).getInterfaceName().equals(phIface.getInterfaceName())) {
373 phIface.setExternal(true);
374 extInterfaces.put(nodeId, new PhysicalInterfaceKey(iface.getName()));
376 .addPhysicalInterfaceInfo(PhysicalInterfaces.PhysicalInterfaceType.PUBLIC,
377 phIface.getInterfaceName(), phIface.getAddress().get(0));
378 LOG.info("Interface {} is marked as public interface based on bundle configuration.",
381 if (PUBLIC_INTERFACE.equals(iface.getDescription())) {
382 phIface.setExternal(true);
383 extInterfaces.put(nodeId, new PhysicalInterfaceKey(iface.getName()));
385 .addPhysicalInterfaceInfo(PhysicalInterfaces.PhysicalInterfaceType.PUBLIC,
386 phIface.getInterfaceName(), phIface.getAddress().get(0));
387 LOG.info("Interface {} is marked as public interface based on HC configuration.",
390 phIfaces.add(phIface.build());
393 return new VppInterfaceAugmentationBuilder().setPhysicalInterface(phIfaces).build();
396 private List<IpAddress> resolveIpAddress(Interface1 iface) {
397 if (iface.getIpv4() != null && iface.getIpv4().getAddress() != null) {
398 return iface.getIpv4().getAddress().stream().map(ipv4 ->
399 new IpAddress(new Ipv4Address(ipv4.getIp().getValue()))).collect(Collectors.toList());
400 } else if (iface.getIpv6() != null && iface.getIpv6().getAddress() != null) {
401 return iface.getIpv6().getAddress().stream().map(ipv6 ->
402 new IpAddress(new Ipv4Address(ipv6.getIp().getValue()))).collect(Collectors.toList());
404 return Lists.newArrayList();
407 public static Map<NodeId, String> resolvePublicInterfaces(ReadTransaction rTx) {
408 Map<NodeId, String> nodes = new HashMap<>();
409 Optional<RendererNodes> rendNodes =
410 DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodesIid(), rTx);
411 if (!rendNodes.isPresent()) {
417 .filter(rn -> rn.getAugmentation(VppInterfaceAugmentation.class) != null)
418 .filter(rn -> rn.getAugmentation(VppInterfaceAugmentation.class).getPhysicalInterface() != null)
420 java.util.Optional<PhysicalInterface> pubInt = rn.getAugmentation(VppInterfaceAugmentation.class)
421 .getPhysicalInterface()
423 .filter(phInt -> phInt.isExternal())
425 if (pubInt.isPresent()) {
426 nodes.put(rn.getNodePath().firstKeyOf(Node.class).getNodeId(), pubInt.get().getInterfaceName());