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;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
21 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
23 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppNodeWriter;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNode;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNodeBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNodeKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import com.google.common.base.Optional;
41 import com.google.common.base.Preconditions;
43 public class VppNodeManager {
45 private static final TopologyId TOPOLOGY_ID = new TopologyId("topology-netconf");
46 private static final Logger LOG = LoggerFactory.getLogger(VppNodeManager.class);
47 private static final Map<InstanceIdentifier<Node>, DataBroker> netconfNodeCache = new HashMap<>();
48 private static final String V3PO_CAPABILITY = "(urn:opendaylight:params:xml:ns:yang:v3po?revision=2016-12-14)v3po";
49 private static final String INTERFACES_CAPABILITY =
50 "(urn:ietf:params:xml:ns:yang:ietf-interfaces?revision=2014-05-08)ietf-interfaces";
51 private static final NodeId CONTROLLER_CONFIG_NODE = new NodeId("controller-config");
52 private final DataBroker dataBroker;
53 private final MountPointService mountService;
54 private final List<String> requiredCapabilities;
56 public VppNodeManager(DataBroker dataBroker, BindingAwareBroker.ProviderContext session) {
57 this.dataBroker = Preconditions.checkNotNull(dataBroker);
58 mountService = Preconditions.checkNotNull(session.getSALService(MountPointService.class));
59 requiredCapabilities = initializeRequiredCapabilities();
62 static DataBroker getDataBrokerFromCache(InstanceIdentifier<Node> iid) {
63 return netconfNodeCache.get(iid); // TODO read from DS
67 * Synchronizes nodes to DataStore based on their modification state which results in
68 * create/update/remove of Node.
70 public void syncNodes(Node dataAfter, Node dataBefore) {
71 if (isControllerConfigNode(dataAfter, dataBefore)) {
72 LOG.trace("{} is ignored by VPP-renderer", CONTROLLER_CONFIG_NODE);
76 if (dataBefore == null && dataAfter != null) {
77 createNode(dataAfter);
79 // Connected/disconnected node
80 if (dataBefore != null && dataAfter != null) {
81 updateNode(dataAfter);
84 if (dataBefore != null && dataAfter == null) {
85 removeNode(dataBefore);
89 private boolean isControllerConfigNode(Node dataAfter, Node dataBefore) {
90 if (dataAfter != null) {
91 return CONTROLLER_CONFIG_NODE.equals(dataAfter.getNodeId());
93 return CONTROLLER_CONFIG_NODE.equals(dataBefore.getNodeId());
96 private void createNode(Node node) {
97 LOG.info("Registering new node {}", node.getNodeId().getValue());
98 NetconfNode netconfNode = getNodeAugmentation(node);
99 if (netconfNode == null) {
102 NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
103 switch (connectionStatus) {
105 LOG.info("Connecting device {} ...", node.getNodeId().getValue());
108 resolveConnectedNode(node, netconfNode);
109 LOG.info("Node {} is capable and ready", node.getNodeId().getValue());
116 private void updateNode(Node node) {
117 NetconfNode netconfNode = getNodeAugmentation(node);
118 if (netconfNode == null || netconfNode.getConnectionStatus() == null) {
121 NetconfNodeConnectionStatus.ConnectionStatus afterNodeStatus = netconfNode.getConnectionStatus();
122 if (afterNodeStatus.equals(Connected)) {
123 resolveConnectedNode(node, netconfNode);
124 LOG.info("Node {} is capable and ready", node.getNodeId().getValue());
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)) {
146 vppNodeWriter.commitToDatastore(dataBroker);
147 DataBroker mountpoint = getNodeMountPoint(mountPointIid);
148 netconfNodeCache.put(mountPointIid, mountpoint);
151 private void resolveDisconnectedNode(Node node) {
152 InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
153 RendererNode rendererNode = remapNode(mountPointIid);
154 VppNodeWriter vppNodeWriter = new VppNodeWriter();
155 vppNodeWriter.cache(rendererNode);
156 vppNodeWriter.removeFromDatastore(dataBroker);
157 netconfNodeCache.remove(mountPointIid);
160 private RendererNode remapNode(InstanceIdentifier<Node> path) {
161 RendererNodeBuilder rendererNodeBuilder = new RendererNodeBuilder();
162 rendererNodeBuilder.setKey(new RendererNodeKey(path)).setNodePath(path);
163 return rendererNodeBuilder.build();
166 private InstanceIdentifier<Node> getMountpointIid(Node node) {
167 return InstanceIdentifier.builder(NetworkTopology.class)
168 .child(Topology.class, new TopologyKey(TOPOLOGY_ID))
169 .child(Node.class, new NodeKey(node.getNodeId()))
173 private boolean isCapableNetconfDevice(Node node, NetconfNode netconfAugmentation) {
174 if (netconfAugmentation.getAvailableCapabilities() == null
175 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability() == null
176 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability().isEmpty()) {
177 LOG.warn("Node {} does not contain any capabilities", node.getNodeId().getValue());
180 if (!capabilityCheck(netconfAugmentation.getAvailableCapabilities().getAvailableCapability())) {
181 LOG.warn("Node {} does not contain all capabilities required by vpp-renderer", node.getNodeId().getValue());
187 private boolean capabilityCheck(final List<String> capabilities) {
188 for (String requiredCapability : requiredCapabilities) {
189 if (!capabilities.contains(requiredCapability)) {
196 private DataBroker getNodeMountPoint(InstanceIdentifier<Node> mountPointIid) {
197 Optional<MountPoint> optionalObject = mountService.getMountPoint(mountPointIid);
198 MountPoint mountPoint;
199 if (optionalObject.isPresent()) {
200 mountPoint = optionalObject.get();
201 if (mountPoint != null) {
202 Optional<DataBroker> optionalDataBroker = mountPoint.getService(DataBroker.class);
203 if (optionalDataBroker.isPresent()) {
204 return optionalDataBroker.get();
206 LOG.debug("Cannot obtain data broker from mountpoint {}", mountPoint);
209 LOG.debug("Cannot obtain mountpoint with IID {}", mountPointIid);
215 private NetconfNode getNodeAugmentation(Node node) {
216 NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
217 if (netconfNode == null) {
218 LOG.warn("Node {} is not a netconf device", node.getNodeId().getValue());
225 * Initialize all common capabilities required by VPP renderer. Any connected node is examined
227 * an appropriate device to handle configuration created by this renderer. A device must support
231 * @return list of string representations of required capabilities
233 private List<String> initializeRequiredCapabilities() {
234 // Required device capabilities
236 String[] capabilityEntries = {V3PO_CAPABILITY, INTERFACES_CAPABILITY};
237 return Arrays.asList(capabilityEntries);