Bug 7241:Fix logging for VPP node
[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                 break;
111             default:
112                 break;
113         }
114     }
115
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) {
120             return;
121         }
122         NetconfNodeConnectionStatus.ConnectionStatus afterNodeStatus = netconfNode.getConnectionStatus();
123         if (afterNodeStatus.equals(Connected)) {
124             resolveConnectedNode(node, netconfNode);
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             LOG.warn("Node {} is not connected.", node.getNodeId().getValue());
145             return;
146         }
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());
151     }
152
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);
160     }
161
162     private RendererNode remapNode(InstanceIdentifier<Node> path) {
163         RendererNodeBuilder rendererNodeBuilder = new RendererNodeBuilder();
164         rendererNodeBuilder.setKey(new RendererNodeKey(path)).setNodePath(path);
165         return rendererNodeBuilder.build();
166     }
167
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()))
172             .build();
173     }
174
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());
180             return false;
181         }
182         if (!capabilityCheck(netconfAugmentation.getAvailableCapabilities().getAvailableCapability())) {
183             LOG.warn("Node {} does not contain all capabilities required by vpp-renderer", node.getNodeId().getValue());
184             return false;
185         }
186         return true;
187     }
188
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);
195     }
196
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();
206                 } else {
207                     LOG.debug("Cannot obtain data broker from mountpoint {}", mountPoint);
208                 }
209             } else {
210                 LOG.debug("Cannot obtain mountpoint with IID {}", mountPointIid);
211             }
212         }
213         return null;
214     }
215
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());
220             return null;
221         }
222         return netconfNode;
223     }
224
225     /**
226      * Initialize all common capabilities required by VPP renderer. Any connected node is examined
227      * whether it's
228      * an appropriate device to handle configuration created by this renderer. A device must support
229      * all capabilities
230      * in list below.
231      *
232      * @return list of string representations of required capabilities
233      */
234     private List<String> initializeRequiredCapabilities() {
235         // Required device capabilities
236
237         String[] capabilityEntries = {V3PO_CAPABILITY, INTERFACES_CAPABILITY};
238         return Arrays.asList(capabilityEntries);
239     }
240
241 }