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