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.HashMap;
16 import java.util.List;
18 import java.util.stream.Collectors;
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.sal.binding.api.BindingAwareBroker;
24 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppNodeWriter;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNode;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNodeBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNodeKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import com.google.common.base.Optional;
43 import com.google.common.base.Preconditions;
45 public class VppNodeManager {
47 private static final TopologyId TOPOLOGY_ID = new TopologyId("topology-netconf");
48 private static final Logger LOG = LoggerFactory.getLogger(VppNodeManager.class);
49 private static final Map<InstanceIdentifier<Node>, DataBroker> netconfNodeCache = new HashMap<>();
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 MountPointService mountService;
55 private final List<String> requiredCapabilities;
57 public VppNodeManager(DataBroker dataBroker, BindingAwareBroker.ProviderContext session) {
58 this.dataBroker = Preconditions.checkNotNull(dataBroker);
59 mountService = Preconditions.checkNotNull(session.getSALService(MountPointService.class));
60 requiredCapabilities = initializeRequiredCapabilities();
63 static DataBroker getDataBrokerFromCache(InstanceIdentifier<Node> iid) {
64 return netconfNodeCache.get(iid); // TODO read from DS
68 * Synchronizes nodes to DataStore based on their modification state which results in
69 * create/update/remove of Node.
71 public void syncNodes(Node dataAfter, Node dataBefore) {
72 if (isControllerConfigNode(dataAfter, dataBefore)) {
73 LOG.trace("{} is ignored by VPP-renderer", CONTROLLER_CONFIG_NODE);
77 if (dataBefore == null && dataAfter != null) {
78 createNode(dataAfter);
80 // Connected/disconnected node
81 if (dataBefore != null && dataAfter != null) {
82 updateNode(dataAfter);
85 if (dataBefore != null && dataAfter == null) {
86 removeNode(dataBefore);
90 private boolean isControllerConfigNode(Node dataAfter, Node dataBefore) {
91 if (dataAfter != null) {
92 return CONTROLLER_CONFIG_NODE.equals(dataAfter.getNodeId());
94 return CONTROLLER_CONFIG_NODE.equals(dataBefore.getNodeId());
97 private void createNode(Node node) {
98 LOG.info("Registering new node {}", node.getNodeId().getValue());
99 NetconfNode netconfNode = getNodeAugmentation(node);
100 if (netconfNode == null) {
103 NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
104 switch (connectionStatus) {
106 LOG.info("Connecting device {} ...", node.getNodeId().getValue());
109 resolveConnectedNode(node, netconfNode);
116 private void updateNode(Node node) {
117 LOG.info("Updating node {}", node.getNodeId());
118 NetconfNode netconfNode = getNodeAugmentation(node);
119 if (netconfNode == null || netconfNode.getConnectionStatus() == null) {
122 NetconfNodeConnectionStatus.ConnectionStatus afterNodeStatus = netconfNode.getConnectionStatus();
123 if (afterNodeStatus.equals(Connected)) {
124 resolveConnectedNode(node, netconfNode);
126 if (afterNodeStatus.equals(Connecting)) {
127 resolveDisconnectedNode(node);
128 LOG.info("Node {} has been disconnected, removing from available nodes", node.getNodeId().getValue());
132 private void removeNode(Node node) {
133 resolveDisconnectedNode(node);
134 LOG.info("Node {} has been removed", node.getNodeId().getValue());
137 private void resolveConnectedNode(Node node, NetconfNode netconfNode) {
138 InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
139 // Mountpoint iid == path in renderer-node
140 RendererNode rendererNode = remapNode(mountPointIid);
141 VppNodeWriter vppNodeWriter = new VppNodeWriter();
142 vppNodeWriter.cache(rendererNode);
143 if (!isCapableNetconfDevice(node, netconfNode)) {
144 LOG.warn("Node {} is not connected.", node.getNodeId().getValue());
147 vppNodeWriter.commitToDatastore(dataBroker);
148 DataBroker mountpoint = getNodeMountPoint(mountPointIid);
149 netconfNodeCache.put(mountPointIid, mountpoint);
150 LOG.info("Node {} is capable and ready.", node.getNodeId().getValue());
153 private void resolveDisconnectedNode(Node node) {
154 InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
155 RendererNode rendererNode = remapNode(mountPointIid);
156 VppNodeWriter vppNodeWriter = new VppNodeWriter();
157 vppNodeWriter.cache(rendererNode);
158 vppNodeWriter.removeFromDatastore(dataBroker);
159 netconfNodeCache.remove(mountPointIid);
162 private RendererNode remapNode(InstanceIdentifier<Node> path) {
163 RendererNodeBuilder rendererNodeBuilder = new RendererNodeBuilder();
164 rendererNodeBuilder.setKey(new RendererNodeKey(path)).setNodePath(path);
165 return rendererNodeBuilder.build();
168 private InstanceIdentifier<Node> getMountpointIid(Node node) {
169 return InstanceIdentifier.builder(NetworkTopology.class)
170 .child(Topology.class, new TopologyKey(TOPOLOGY_ID))
171 .child(Node.class, new NodeKey(node.getNodeId()))
175 private boolean isCapableNetconfDevice(Node node, NetconfNode netconfAugmentation) {
176 if (netconfAugmentation.getAvailableCapabilities() == null
177 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability() == null
178 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability().isEmpty()) {
179 LOG.warn("Node {} does not contain any capabilities", node.getNodeId().getValue());
182 if (!capabilityCheck(netconfAugmentation.getAvailableCapabilities().getAvailableCapability())) {
183 LOG.warn("Node {} does not contain all capabilities required by vpp-renderer", node.getNodeId().getValue());
189 private boolean capabilityCheck(final List<AvailableCapability> capabilities) {
190 final List<String> availableCapabilities = capabilities.stream()
191 .map(AvailableCapability::getCapability)
192 .collect(Collectors.toList());
193 return requiredCapabilities.stream()
194 .allMatch(availableCapabilities::contains);
197 private DataBroker getNodeMountPoint(InstanceIdentifier<Node> mountPointIid) {
198 Optional<MountPoint> optionalObject = mountService.getMountPoint(mountPointIid);
199 MountPoint mountPoint;
200 if (optionalObject.isPresent()) {
201 mountPoint = optionalObject.get();
202 if (mountPoint != null) {
203 Optional<DataBroker> optionalDataBroker = mountPoint.getService(DataBroker.class);
204 if (optionalDataBroker.isPresent()) {
205 return optionalDataBroker.get();
207 LOG.debug("Cannot obtain data broker from mountpoint {}", mountPoint);
210 LOG.debug("Cannot obtain mountpoint with IID {}", mountPointIid);
216 private NetconfNode getNodeAugmentation(Node node) {
217 NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
218 if (netconfNode == null) {
219 LOG.warn("Node {} is not a netconf device", node.getNodeId().getValue());
226 * Initialize all common capabilities required by VPP renderer. Any connected node is examined
228 * an appropriate device to handle configuration created by this renderer. A device must support
232 * @return list of string representations of required capabilities
234 private List<String> initializeRequiredCapabilities() {
235 // Required device capabilities
237 String[] capabilityEntries = {V3PO_CAPABILITY, INTERFACES_CAPABILITY};
238 return Arrays.asList(capabilityEntries);