fix for netconf model 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
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.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapabilityBuilder;
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 AvailableCapability V3PO_CAPABILITY = new AvailableCapabilityBuilder()
51             .setCapability("(urn:opendaylight:params:xml:ns:yang:v3po?revision=2016-12-14)v3po").build();
52     private static final AvailableCapability INTERFACES_CAPABILITY = new AvailableCapabilityBuilder()
53             .setCapability("(urn:ietf:params:xml:ns:yang:ietf-interfaces?revision=2014-05-08)ietf-interfaces").build();
54     private static final NodeId CONTROLLER_CONFIG_NODE = new NodeId("controller-config");
55     private final DataBroker dataBroker;
56     private final MountPointService mountService;
57     private final List<AvailableCapability> requiredCapabilities;
58
59     public VppNodeManager(DataBroker dataBroker, BindingAwareBroker.ProviderContext session) {
60         this.dataBroker = Preconditions.checkNotNull(dataBroker);
61         mountService = Preconditions.checkNotNull(session.getSALService(MountPointService.class));
62         requiredCapabilities = initializeRequiredCapabilities();
63     }
64
65     static DataBroker getDataBrokerFromCache(InstanceIdentifier<Node> iid) {
66         return netconfNodeCache.get(iid); // TODO read from DS
67     }
68
69     /**
70      * Synchronizes nodes to DataStore based on their modification state which results in
71      * create/update/remove of Node.
72      */
73     public void syncNodes(Node dataAfter, Node dataBefore) {
74         if (isControllerConfigNode(dataAfter, dataBefore)) {
75             LOG.trace("{} is ignored by VPP-renderer", CONTROLLER_CONFIG_NODE);
76             return;
77         }
78         // New node
79         if (dataBefore == null && dataAfter != null) {
80             createNode(dataAfter);
81         }
82         // Connected/disconnected node
83         if (dataBefore != null && dataAfter != null) {
84             updateNode(dataAfter);
85         }
86         // Removed node
87         if (dataBefore != null && dataAfter == null) {
88             removeNode(dataBefore);
89         }
90     }
91
92     private boolean isControllerConfigNode(Node dataAfter, Node dataBefore) {
93         if (dataAfter != null) {
94             return CONTROLLER_CONFIG_NODE.equals(dataAfter.getNodeId());
95         }
96         return CONTROLLER_CONFIG_NODE.equals(dataBefore.getNodeId());
97     }
98
99     private void createNode(Node node) {
100         LOG.info("Registering new node {}", node.getNodeId().getValue());
101         NetconfNode netconfNode = getNodeAugmentation(node);
102         if (netconfNode == null) {
103             return;
104         }
105         NetconfNodeConnectionStatus.ConnectionStatus connectionStatus = netconfNode.getConnectionStatus();
106         switch (connectionStatus) {
107             case Connecting:
108                 LOG.info("Connecting device {} ...", node.getNodeId().getValue());
109                 break;
110             case Connected:
111                 resolveConnectedNode(node, netconfNode);
112                 LOG.info("Node {} is capable and ready", node.getNodeId().getValue());
113                 break;
114             default:
115                 break;
116         }
117     }
118
119     private void updateNode(Node node) {
120         NetconfNode netconfNode = getNodeAugmentation(node);
121         if (netconfNode == null || netconfNode.getConnectionStatus() == null) {
122             return;
123         }
124         NetconfNodeConnectionStatus.ConnectionStatus afterNodeStatus = netconfNode.getConnectionStatus();
125         if (afterNodeStatus.equals(Connected)) {
126             resolveConnectedNode(node, netconfNode);
127             LOG.info("Node {} is capable and ready", node.getNodeId().getValue());
128         }
129         if (afterNodeStatus.equals(Connecting)) {
130             resolveDisconnectedNode(node);
131             LOG.info("Node {} has been disconnected, removing from available nodes", node.getNodeId().getValue());
132         }
133     }
134
135     private void removeNode(Node node) {
136         resolveDisconnectedNode(node);
137         LOG.info("Node {} has been removed", node.getNodeId().getValue());
138     }
139
140     private void resolveConnectedNode(Node node, NetconfNode netconfNode) {
141         InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
142         // Mountpoint iid == path in renderer-node
143         RendererNode rendererNode = remapNode(mountPointIid);
144         VppNodeWriter vppNodeWriter = new VppNodeWriter();
145         vppNodeWriter.cache(rendererNode);
146         if (!isCapableNetconfDevice(node, netconfNode)) {
147             return;
148         }
149         vppNodeWriter.commitToDatastore(dataBroker);
150         DataBroker mountpoint = getNodeMountPoint(mountPointIid);
151         netconfNodeCache.put(mountPointIid, mountpoint);
152     }
153
154     private void resolveDisconnectedNode(Node node) {
155         InstanceIdentifier<Node> mountPointIid = getMountpointIid(node);
156         RendererNode rendererNode = remapNode(mountPointIid);
157         VppNodeWriter vppNodeWriter = new VppNodeWriter();
158         vppNodeWriter.cache(rendererNode);
159         vppNodeWriter.removeFromDatastore(dataBroker);
160         netconfNodeCache.remove(mountPointIid);
161     }
162
163     private RendererNode remapNode(InstanceIdentifier<Node> path) {
164         RendererNodeBuilder rendererNodeBuilder = new RendererNodeBuilder();
165         rendererNodeBuilder.setKey(new RendererNodeKey(path)).setNodePath(path);
166         return rendererNodeBuilder.build();
167     }
168
169     private InstanceIdentifier<Node> getMountpointIid(Node node) {
170         return InstanceIdentifier.builder(NetworkTopology.class)
171             .child(Topology.class, new TopologyKey(TOPOLOGY_ID))
172             .child(Node.class, new NodeKey(node.getNodeId()))
173             .build();
174     }
175
176     private boolean isCapableNetconfDevice(Node node, NetconfNode netconfAugmentation) {
177         if (netconfAugmentation.getAvailableCapabilities() == null
178                 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability() == null
179                 || netconfAugmentation.getAvailableCapabilities().getAvailableCapability().isEmpty()) {
180             LOG.warn("Node {} does not contain any capabilities", node.getNodeId().getValue());
181             return false;
182         }
183         if (!capabilityCheck(netconfAugmentation.getAvailableCapabilities().getAvailableCapability())) {
184             LOG.warn("Node {} does not contain all capabilities required by vpp-renderer", node.getNodeId().getValue());
185             return false;
186         }
187         return true;
188     }
189
190     private boolean capabilityCheck(final List<AvailableCapability> capabilities) {
191         for (AvailableCapability requiredCapability : requiredCapabilities) {
192             if (!capabilities.contains(requiredCapability)) {
193                 return false;
194             }
195         }
196         return true;
197     }
198
199     private DataBroker getNodeMountPoint(InstanceIdentifier<Node> mountPointIid) {
200         Optional<MountPoint> optionalObject = mountService.getMountPoint(mountPointIid);
201         MountPoint mountPoint;
202         if (optionalObject.isPresent()) {
203             mountPoint = optionalObject.get();
204             if (mountPoint != null) {
205                 Optional<DataBroker> optionalDataBroker = mountPoint.getService(DataBroker.class);
206                 if (optionalDataBroker.isPresent()) {
207                     return optionalDataBroker.get();
208                 } else {
209                     LOG.debug("Cannot obtain data broker from mountpoint {}", mountPoint);
210                 }
211             } else {
212                 LOG.debug("Cannot obtain mountpoint with IID {}", mountPointIid);
213             }
214         }
215         return null;
216     }
217
218     private NetconfNode getNodeAugmentation(Node node) {
219         NetconfNode netconfNode = node.getAugmentation(NetconfNode.class);
220         if (netconfNode == null) {
221             LOG.warn("Node {} is not a netconf device", node.getNodeId().getValue());
222             return null;
223         }
224         return netconfNode;
225     }
226
227     /**
228      * Initialize all common capabilities required by VPP renderer. Any connected node is examined
229      * whether it's
230      * an appropriate device to handle configuration created by this renderer. A device must support
231      * all capabilities
232      * in list below.
233      *
234      * @return list of string representations of required capabilities
235      */
236     private List<AvailableCapability> initializeRequiredCapabilities() {
237         // Required device capabilities
238
239         AvailableCapability[] capabilityEntries = {V3PO_CAPABILITY, INTERFACES_CAPABILITY};
240         return Arrays.asList(capabilityEntries);
241     }
242
243 }