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.RendererName;
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.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
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 public static final RendererName vppRenderer = new RendererName("vpp-renderer");
46 private static final TopologyId TOPOLOGY_ID = new TopologyId("topology-netconf");
47 private static final Logger LOG = LoggerFactory.getLogger(VppNodeManager.class);
48 private static final Map<InstanceIdentifier<Node>, DataBroker> netconfNodeCache = new HashMap<>();
49 private static final String V3PO_CAPABILITY = "(urn:opendaylight:params:xml:ns:yang:v3po?revision=2015-01-05)v3po";
50 private static final String INTERFACES_CAPABILITY =
51 "(urn:ietf:params:xml:ns:yang:ietf-interfaces?revision=2014-05-08)ietf-interfaces";
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) {
72 if (dataBefore == null && dataAfter != null) {
73 createNode(dataAfter);
75 // Connected/disconnected node
76 if (dataBefore != null && dataAfter != null) {
77 updateNode(dataAfter);
80 if (dataBefore != null && dataAfter == null) {
81 removeNode(dataBefore);
85 private void createNode(Node node) {
86 LOG.info("Registering new node {}", node.getNodeId().getValue());
87 NetconfNode netconfNode = getNodeAugmentation(node);
88 if (netconfNode == null) {
91 NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
92 switch (connectionStatus) {
94 LOG.info("Connecting device {} ...", node.getNodeId().getValue());
97 resolveConnectedNode(node, netconfNode);
98 LOG.info("Node {} is capable and ready", node.getNodeId().getValue());
105 private void updateNode(Node node) {
106 NetconfNode netconfNode = getNodeAugmentation(node);
107 if (netconfNode == null || netconfNode.getConnectionStatus() == null) {
110 NetconfNodeConnectionStatus.ConnectionStatus afterNodeStatus = netconfNode.getConnectionStatus();
111 if (afterNodeStatus.equals(Connected)) {
112 resolveConnectedNode(node, netconfNode);
113 LOG.info("Node {} is capable and ready", node.getNodeId().getValue());
115 if (afterNodeStatus.equals(Connecting)) {
116 resolveDisconnectedNode(node);
117 LOG.info("Node {} has been disconnected, removing from available nodes", node.getNodeId().getValue());
121 private void removeNode(Node node) {
122 resolveDisconnectedNode(node);
123 LOG.info("Node {} has been removed", node.getNodeId().getValue());
126 private void resolveConnectedNode(Node node, NetconfNode netconfNode) {
127 InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
128 // Mountpoint iid == path in renderer-node
129 RendererNode rendererNode = remapNode(mountPointIid);
130 VppNodeWriter vppNodeWriter = new VppNodeWriter();
131 vppNodeWriter.cache(rendererNode);
132 if (!isCapableNetconfDevice(node, netconfNode)) {
135 vppNodeWriter.commitToDatastore(dataBroker);
136 DataBroker mountpoint = getNodeMountPoint(mountPointIid);
137 netconfNodeCache.put(mountPointIid, mountpoint);
140 private void resolveDisconnectedNode(Node node) {
141 InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
142 RendererNode rendererNode = remapNode(mountPointIid);
143 VppNodeWriter vppNodeWriter = new VppNodeWriter();
144 vppNodeWriter.cache(rendererNode);
145 vppNodeWriter.removeFromDatastore(dataBroker);
146 netconfNodeCache.remove(mountPointIid);
149 private RendererNode remapNode(InstanceIdentifier<Node> path) {
150 RendererNodeBuilder rendererNodeBuilder = new RendererNodeBuilder();
151 rendererNodeBuilder.setKey(new RendererNodeKey(path)).setNodePath(path);
152 return rendererNodeBuilder.build();
155 private InstanceIdentifier<Node> getMountpointIid(Node node) {
156 return InstanceIdentifier.builder(NetworkTopology.class)
157 .child(Topology.class, new TopologyKey(TOPOLOGY_ID))
158 .child(Node.class, new NodeKey(node.getNodeId()))
162 private boolean isCapableNetconfDevice(Node node, NetconfNode netconfAugmentation) {
163 if (netconfAugmentation.getAvailableCapabilities() == null
164 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability() == null
165 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability().isEmpty()) {
166 LOG.warn("Node {} does not contain any capabilities", node.getNodeId().getValue());
169 if (!capabilityCheck(netconfAugmentation.getAvailableCapabilities().getAvailableCapability())) {
170 LOG.warn("Node {} does not contain all capabilities required by vpp-renderer", node.getNodeId().getValue());
176 private boolean capabilityCheck(final List<String> capabilities) {
177 for (String requiredCapability : requiredCapabilities) {
178 if (!capabilities.contains(requiredCapability)) {
185 private DataBroker getNodeMountPoint(InstanceIdentifier<Node> mountPointIid) {
186 Optional<MountPoint> optionalObject = mountService.getMountPoint(mountPointIid);
187 MountPoint mountPoint;
188 if (optionalObject.isPresent()) {
189 mountPoint = optionalObject.get();
190 if (mountPoint != null) {
191 Optional<DataBroker> optionalDataBroker = mountPoint.getService(DataBroker.class);
192 if (optionalDataBroker.isPresent()) {
193 return optionalDataBroker.get();
195 LOG.debug("Cannot obtain data broker from mountpoint {}", mountPoint);
198 LOG.debug("Cannot obtain mountpoint with IID {}", mountPointIid);
204 private NetconfNode getNodeAugmentation(Node node) {
205 NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
206 if (netconfNode == null) {
207 LOG.warn("Node {} is not a netconf device", node.getNodeId().getValue());
214 * Initialize all common capabilities required by VPP renderer. Any connected node is examined
216 * an appropriate device to handle configuration created by this renderer. A device must support
220 * @return list of string representations of required capabilities
222 private List<String> initializeRequiredCapabilities() {
223 // Required device capabilities
225 String[] capabilityEntries = {V3PO_CAPABILITY, INTERFACES_CAPABILITY};
226 return Arrays.asList(capabilityEntries);