additional fix for netconf change
[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 import java.util.stream.Collectors;
19
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;
41
42 import com.google.common.base.Optional;
43 import com.google.common.base.Preconditions;
44
45 public class VppNodeManager {
46
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;
56
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();
61     }
62
63     static DataBroker getDataBrokerFromCache(InstanceIdentifier<Node> iid) {
64         return netconfNodeCache.get(iid); // TODO read from DS
65     }
66
67     /**
68      * Synchronizes nodes to DataStore based on their modification state which results in
69      * create/update/remove of Node.
70      */
71     public void syncNodes(Node dataAfter, Node dataBefore) {
72         if (isControllerConfigNode(dataAfter, dataBefore)) {
73             LOG.trace("{} is ignored by VPP-renderer", CONTROLLER_CONFIG_NODE);
74             return;
75         }
76         // New node
77         if (dataBefore == null && dataAfter != null) {
78             createNode(dataAfter);
79         }
80         // Connected/disconnected node
81         if (dataBefore != null && dataAfter != null) {
82             updateNode(dataAfter);
83         }
84         // Removed node
85         if (dataBefore != null && dataAfter == null) {
86             removeNode(dataBefore);
87         }
88     }
89
90     private boolean isControllerConfigNode(Node dataAfter, Node dataBefore) {
91         if (dataAfter != null) {
92             return CONTROLLER_CONFIG_NODE.equals(dataAfter.getNodeId());
93         }
94         return CONTROLLER_CONFIG_NODE.equals(dataBefore.getNodeId());
95     }
96
97     private void createNode(Node node) {
98         LOG.info("Registering new node {}", node.getNodeId().getValue());
99         NetconfNode netconfNode = getNodeAugmentation(node);
100         if (netconfNode == null) {
101             return;
102         }
103         NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
104         switch (connectionStatus) {
105             case Connecting:
106                 LOG.info("Connecting device {} ...", node.getNodeId().getValue());
107                 break;
108             case Connected:
109                 resolveConnectedNode(node, netconfNode);
110                 LOG.info("Node {} is capable and ready", node.getNodeId().getValue());
111                 break;
112             default:
113                 break;
114         }
115     }
116
117     private void updateNode(Node node) {
118         NetconfNode netconfNode = getNodeAugmentation(node);
119         if (netconfNode == null || netconfNode.getConnectionStatus() == null) {
120             return;
121         }
122         NetconfNodeConnectionStatus.ConnectionStatus afterNodeStatus = netconfNode.getConnectionStatus();
123         if (afterNodeStatus.equals(Connected)) {
124             resolveConnectedNode(node, netconfNode);
125             LOG.info("Node {} is capable and ready", node.getNodeId().getValue());
126         }
127         if (afterNodeStatus.equals(Connecting)) {
128             resolveDisconnectedNode(node);
129             LOG.info("Node {} has been disconnected, removing from available nodes", node.getNodeId().getValue());
130         }
131     }
132
133     private void removeNode(Node node) {
134         resolveDisconnectedNode(node);
135         LOG.info("Node {} has been removed", node.getNodeId().getValue());
136     }
137
138     private void resolveConnectedNode(Node node, NetconfNode netconfNode) {
139         InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
140         // Mountpoint iid == path in renderer-node
141         RendererNode rendererNode = remapNode(mountPointIid);
142         VppNodeWriter vppNodeWriter = new VppNodeWriter();
143         vppNodeWriter.cache(rendererNode);
144         if (!isCapableNetconfDevice(node, netconfNode)) {
145             return;
146         }
147         vppNodeWriter.commitToDatastore(dataBroker);
148         DataBroker mountpoint = getNodeMountPoint(mountPointIid);
149         netconfNodeCache.put(mountPointIid, mountpoint);
150     }
151
152     private void resolveDisconnectedNode(Node node) {
153         InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
154         RendererNode rendererNode = remapNode(mountPointIid);
155         VppNodeWriter vppNodeWriter = new VppNodeWriter();
156         vppNodeWriter.cache(rendererNode);
157         vppNodeWriter.removeFromDatastore(dataBroker);
158         netconfNodeCache.remove(mountPointIid);
159     }
160
161     private RendererNode remapNode(InstanceIdentifier<Node> path) {
162         RendererNodeBuilder rendererNodeBuilder = new RendererNodeBuilder();
163         rendererNodeBuilder.setKey(new RendererNodeKey(path)).setNodePath(path);
164         return rendererNodeBuilder.build();
165     }
166
167     private InstanceIdentifier<Node> getMountpointIid(Node node) {
168         return InstanceIdentifier.builder(NetworkTopology.class)
169             .child(Topology.class, new TopologyKey(TOPOLOGY_ID))
170             .child(Node.class, new NodeKey(node.getNodeId()))
171             .build();
172     }
173
174     private boolean isCapableNetconfDevice(Node node, NetconfNode netconfAugmentation) {
175         if (netconfAugmentation.getAvailableCapabilities() == null
176                 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability() == null
177                 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability().isEmpty()) {
178             LOG.warn("Node {} does not contain any capabilities", node.getNodeId().getValue());
179             return false;
180         }
181         if (!capabilityCheck(netconfAugmentation.getAvailableCapabilities().getAvailableCapability())) {
182             LOG.warn("Node {} does not contain all capabilities required by vpp-renderer", node.getNodeId().getValue());
183             return false;
184         }
185         return true;
186     }
187
188     private boolean capabilityCheck(final List<AvailableCapability> capabilities) {
189         final List<String> availableCapabilities = capabilities.stream()
190                 .map(AvailableCapability::getCapability)
191                 .collect(Collectors.toList());
192         return requiredCapabilities.stream()
193                 .allMatch(availableCapabilities::contains);
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 }