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 java.util.Arrays;
15 import java.util.List;
16 import java.util.stream.Collectors;
17 import com.google.common.base.Optional;
18 import com.google.common.base.Preconditions;
19 import com.google.common.util.concurrent.CheckedFuture;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
22 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
23 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
26 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
27 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppIidFactory;
28 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNode;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNodeBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNodeKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 public class VppNodeManager {
48 private static final TopologyId TOPOLOGY_ID = new TopologyId("topology-netconf");
49 private static final Logger LOG = LoggerFactory.getLogger(VppNodeManager.class);
50 private static final String V3PO_CAPABILITY = "(urn:opendaylight:params:xml:ns:yang:v3po?revision=2016-12-14)v3po";
51 private static final String INTERFACES_CAPABILITY = "(urn:ietf:params:xml:ns:yang:ietf-interfaces?revision=2014-05-08)ietf-interfaces";
52 private static final NodeId CONTROLLER_CONFIG_NODE = new NodeId("controller-config");
53 private final DataBroker dataBroker;
54 private final List<String> requiredCapabilities;
55 private final MountPointService mountService;
57 public VppNodeManager(final DataBroker dataBroker, final BindingAwareBroker.ProviderContext session) {
58 this.dataBroker = Preconditions.checkNotNull(dataBroker);
59 this.mountService = Preconditions.checkNotNull(session.getSALService(MountPointService.class));
60 requiredCapabilities = initializeRequiredCapabilities();
64 * Synchronizes nodes to DataStore based on their modification state which results in
65 * create/update/remove of Node.
67 public void syncNodes(Node dataAfter, Node dataBefore) {
68 if (isControllerConfigNode(dataAfter, dataBefore)) {
69 LOG.trace("{} is ignored by VPP-renderer", CONTROLLER_CONFIG_NODE);
73 if (dataBefore == null && dataAfter != null) {
74 createNode(dataAfter);
76 // Connected/disconnected node
77 if (dataBefore != null && dataAfter != null) {
78 updateNode(dataAfter);
81 if (dataBefore != null && dataAfter == null) {
82 removeNode(dataBefore);
86 private boolean isControllerConfigNode(Node dataAfter, Node dataBefore) {
87 if (dataAfter != null) {
88 return CONTROLLER_CONFIG_NODE.equals(dataAfter.getNodeId());
90 return CONTROLLER_CONFIG_NODE.equals(dataBefore.getNodeId());
93 private void createNode(Node node) {
94 LOG.info("Registering new node {}", node.getNodeId().getValue());
95 NetconfNode netconfNode = getNodeAugmentation(node);
96 if (netconfNode == null) {
99 NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
100 switch (connectionStatus) {
102 LOG.info("Connecting device {} ...", node.getNodeId().getValue());
105 resolveConnectedNode(node, netconfNode);
112 private void updateNode(Node node) {
113 LOG.info("Updating node {}", node.getNodeId());
114 NetconfNode netconfNode = getNodeAugmentation(node);
115 if (netconfNode == null || netconfNode.getConnectionStatus() == null) {
118 NetconfNodeConnectionStatus.ConnectionStatus afterNodeStatus = netconfNode.getConnectionStatus();
119 if (afterNodeStatus.equals(Connected)) {
120 resolveConnectedNode(node, netconfNode);
122 if (afterNodeStatus.equals(Connecting)) {
123 resolveDisconnectedNode(node);
124 LOG.info("Node {} is disconnected, removing from available nodes", node.getNodeId().getValue());
128 private void removeNode(Node node) {
129 resolveDisconnectedNode(node);
130 LOG.info("Node {} is removed", node.getNodeId().getValue());
133 private void resolveConnectedNode(Node node, NetconfNode netconfNode) {
134 InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
135 RendererNode rendererNode = remapNode(mountPointIid);
136 if (!isCapableNetconfDevice(node, netconfNode)) {
137 LOG.warn("Node {} is not connected.", node.getNodeId().getValue());
140 final DataBroker mountpoint = getNodeMountPoint(mountPointIid);
141 if (mountpoint == null) {
142 LOG.warn("Mountpoint not available for node {}", node.getNodeId().getValue());
145 final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
146 wTx.put(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodeIid(rendererNode), rendererNode, true);
147 DataStoreHelper.submitToDs(wTx);
148 LOG.info("Node {} is capable and ready.", node.getNodeId().getValue());
151 private void resolveDisconnectedNode(Node node) {
152 InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
153 RendererNode rendererNode = remapNode(mountPointIid);
154 final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
155 wTx.delete(LogicalDatastoreType.OPERATIONAL, VppIidFactory.getRendererNodeIid(rendererNode));
156 CheckedFuture<Void, TransactionCommitFailedException> submitFuture = wTx.submit();
158 submitFuture.checkedGet();
159 } catch (TransactionCommitFailedException e) {
160 LOG.error("Write transaction failed to {}", e.getMessage());
161 } catch (Exception e) {
162 LOG.error("Failed to .. {}", e.getMessage());
166 private DataBroker getNodeMountPoint(InstanceIdentifier<Node> mountPointIid) {
167 Optional<MountPoint> optionalObject = mountService.getMountPoint(mountPointIid);
168 LOG.debug("Optional mountpoint object: {}", optionalObject);
169 MountPoint mountPoint;
170 if (optionalObject.isPresent()) {
171 mountPoint = optionalObject.get();
172 if (mountPoint != null) {
173 Optional<DataBroker> optionalDataBroker = mountPoint.getService(DataBroker.class);
174 if (optionalDataBroker.isPresent()) {
175 return optionalDataBroker.get();
177 LOG.warn("Cannot obtain data broker from mountpoint {}", mountPoint);
180 LOG.warn("Cannot obtain mountpoint with IID {}", mountPointIid);
186 private RendererNode remapNode(InstanceIdentifier<Node> path) {
187 RendererNodeBuilder rendererNodeBuilder = new RendererNodeBuilder();
188 rendererNodeBuilder.setKey(new RendererNodeKey(path)).setNodePath(path);
189 return rendererNodeBuilder.build();
192 private InstanceIdentifier<Node> getMountpointIid(Node node) {
193 return InstanceIdentifier.builder(NetworkTopology.class)
194 .child(Topology.class, new TopologyKey(TOPOLOGY_ID))
195 .child(Node.class, new NodeKey(node.getNodeId()))
199 private boolean isCapableNetconfDevice(Node node, NetconfNode netconfAugmentation) {
200 if (netconfAugmentation.getAvailableCapabilities() == null
201 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability() == null
202 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability().isEmpty()) {
203 LOG.warn("Node {} does not contain any capabilities", node.getNodeId().getValue());
206 if (!capabilityCheck(netconfAugmentation.getAvailableCapabilities().getAvailableCapability())) {
207 LOG.warn("Node {} does not contain all capabilities required by vpp-renderer", node.getNodeId().getValue());
213 private boolean capabilityCheck(final List<AvailableCapability> capabilities) {
214 final List<String> availableCapabilities = capabilities.stream()
215 .map(AvailableCapability::getCapability)
216 .collect(Collectors.toList());
217 return requiredCapabilities.stream()
218 .allMatch(availableCapabilities::contains);
221 private NetconfNode getNodeAugmentation(Node node) {
222 NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
223 if (netconfNode == null) {
224 LOG.warn("Node {} is not a netconf device", node.getNodeId().getValue());
231 * Initialize all common capabilities required by VPP renderer. Any connected node is examined
233 * an appropriate device to handle configuration created by this renderer. A device must support
237 * @return list of string representations of required capabilities
239 private List<String> initializeRequiredCapabilities() {
240 // Required device capabilities
242 String[] capabilityEntries = {V3PO_CAPABILITY, INTERFACES_CAPABILITY};
243 return Arrays.asList(capabilityEntries);