2507006e872216584048eb8f976efc8b9899e406
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / manager / VppNodeManager.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.manager;
10
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
14 import java.util.Arrays;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18
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;
39
40 import com.google.common.base.Optional;
41 import com.google.common.base.Preconditions;
42
43 public class VppNodeManager {
44
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=2015-01-05)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;
55
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();
60     }
61
62     static DataBroker getDataBrokerFromCache(InstanceIdentifier<Node> iid) {
63         return netconfNodeCache.get(iid); // TODO read from DS
64     }
65
66     /**
67      * Synchronizes nodes to DataStore based on their modification state which results in
68      * create/update/remove of Node.
69      */
70     public void syncNodes(Node dataAfter, Node dataBefore) {
71         if (isControllerConfigNode(dataAfter, dataBefore)) {
72             LOG.trace("{} is ignored by VPP-renderer", CONTROLLER_CONFIG_NODE);
73             return;
74         }
75         // New node
76         if (dataBefore == null && dataAfter != null) {
77             createNode(dataAfter);
78         }
79         // Connected/disconnected node
80         if (dataBefore != null && dataAfter != null) {
81             updateNode(dataAfter);
82         }
83         // Removed node
84         if (dataBefore != null && dataAfter == null) {
85             removeNode(dataBefore);
86         }
87     }
88
89     private boolean isControllerConfigNode(Node dataAfter, Node dataBefore) {
90         if (dataAfter != null) {
91             return CONTROLLER_CONFIG_NODE.equals(dataAfter.getNodeId());
92         }
93         return CONTROLLER_CONFIG_NODE.equals(dataBefore.getNodeId());
94     }
95
96     private void createNode(Node node) {
97         LOG.info("Registering new node {}", node.getNodeId().getValue());
98         NetconfNode netconfNode = getNodeAugmentation(node);
99         if (netconfNode == null) {
100             return;
101         }
102         NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
103         switch (connectionStatus) {
104             case Connecting:
105                 LOG.info("Connecting device {} ...", node.getNodeId().getValue());
106                 break;
107             case Connected:
108                 resolveConnectedNode(node, netconfNode);
109                 LOG.info("Node {} is capable and ready", node.getNodeId().getValue());
110                 break;
111             default:
112                 break;
113         }
114     }
115
116     private void updateNode(Node node) {
117         NetconfNode netconfNode = getNodeAugmentation(node);
118         if (netconfNode == null || netconfNode.getConnectionStatus() == null) {
119             return;
120         }
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());
125         }
126         if (afterNodeStatus.equals(Connecting)) {
127             resolveDisconnectedNode(node);
128             LOG.info("Node {} has been disconnected, removing from available nodes", node.getNodeId().getValue());
129         }
130     }
131
132     private void removeNode(Node node) {
133         resolveDisconnectedNode(node);
134         LOG.info("Node {} has been removed", node.getNodeId().getValue());
135     }
136
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             return;
145         }
146         vppNodeWriter.commitToDatastore(dataBroker);
147         DataBroker mountpoint = getNodeMountPoint(mountPointIid);
148         netconfNodeCache.put(mountPointIid, mountpoint);
149     }
150
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);
158     }
159
160     private RendererNode remapNode(InstanceIdentifier<Node> path) {
161         RendererNodeBuilder rendererNodeBuilder = new RendererNodeBuilder();
162         rendererNodeBuilder.setKey(new RendererNodeKey(path)).setNodePath(path);
163         return rendererNodeBuilder.build();
164     }
165
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()))
170             .build();
171     }
172
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());
178             return false;
179         }
180         if (!capabilityCheck(netconfAugmentation.getAvailableCapabilities().getAvailableCapability())) {
181             LOG.warn("Node {} does not contain all capabilities required by vpp-renderer", node.getNodeId().getValue());
182             return false;
183         }
184         return true;
185     }
186
187     private boolean capabilityCheck(final List<String> capabilities) {
188         for (String requiredCapability : requiredCapabilities) {
189             if (!capabilities.contains(requiredCapability)) {
190                 return false;
191             }
192         }
193         return true;
194     }
195
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();
205                 } else {
206                     LOG.debug("Cannot obtain data broker from mountpoint {}", mountPoint);
207                 }
208             } else {
209                 LOG.debug("Cannot obtain mountpoint with IID {}", mountPointIid);
210             }
211         }
212         return null;
213     }
214
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());
219             return null;
220         }
221         return netconfNode;
222     }
223
224     /**
225      * Initialize all common capabilities required by VPP renderer. Any connected node is examined
226      * whether it's
227      * an appropriate device to handle configuration created by this renderer. A device must support
228      * all capabilities
229      * in list below.
230      *
231      * @return list of string representations of required capabilities
232      */
233     private List<String> initializeRequiredCapabilities() {
234         // Required device capabilities
235
236         String[] capabilityEntries = {V3PO_CAPABILITY, INTERFACES_CAPABILITY};
237         return Arrays.asList(capabilityEntries);
238     }
239
240 }