Add submodules and scripts to run netconf-testtool
[transportpce.git] / renderer / src / main / java / org / opendaylight / transportpce / renderer / provisiondevice / DeviceRenderer.java
1 /*
2  * Copyright © 2017 AT&T 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.transportpce.renderer.provisiondevice;
10
11 import java.util.HashSet;
12 import java.util.List;
13 import java.util.Set;
14 import java.util.concurrent.Future;
15
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
18 import org.opendaylight.transportpce.renderer.mapping.PortMapping;
19 import org.opendaylight.transportpce.renderer.openroadminterface.OpenRoadmInterfaces;
20 import org.opendaylight.transportpce.renderer.openroadminterface.OpenRoadmOchInterface;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.renderer.rev170228.RendererService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.renderer.rev170228.ServicePathInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.renderer.rev170228.ServicePathOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.renderer.rev170228.ServicePathOutputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.renderer.rev170228.service.path.input.Nodes;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class DeviceRenderer implements RendererService {
32
33     private final DataBroker db;
34     private final MountPointService mps;
35     private static final Logger LOG = LoggerFactory.getLogger(RendererService.class);
36     private final Set<String> currentMountedDevice;
37     private final Set<String> nodesProvisioned;
38
39     public DeviceRenderer(DataBroker db, MountPointService mps, Set<String> currentMountedDevice) {
40         this.db = db;
41         this.mps = mps;
42         this.currentMountedDevice = currentMountedDevice;
43         this.nodesProvisioned = new HashSet<>();
44     }
45
46     /**
47      * This method is the implementation of the 'service-path' RESTCONF service,
48      * which is one of the external APIs into the renderer application. The
49      * service provides two functions:
50      *
51      * <p>
52      * 1. Create This operation results in provisioning the device for a given
53      * wavelength and a list of nodes with each node listing its termination
54      * points.
55      *
56      * <p>
57      * 2. Delete This operation results in de-provisioning the device for a
58      * given wavelength and a list of nodes with each node listing its
59      * termination points.
60      *
61      * <p>
62      * The signature for this method was generated by yang tools from the
63      * renderer API model.
64      *
65      * @param input
66      *            Input parameter from the service-path yang model
67      *
68      * @return Result of the request
69      */
70     @Override
71     public Future<RpcResult<ServicePathOutput>> servicePath(ServicePathInput input) {
72
73         if (input.getOperation().getIntValue() == 1) {
74             LOG.info("Create operation request received");
75             return RpcResultBuilder.success(setupServicePath(input)).buildFuture();
76         } else if (input.getOperation().getIntValue() == 2) {
77             LOG.info("Delete operation request received");
78             return RpcResultBuilder.success(deleteServicePath(input)).buildFuture();
79         }
80         return RpcResultBuilder.success(new ServicePathOutputBuilder().setResult("Invalid operation")).buildFuture();
81     }
82
83     /**
84      * This method set's wavelength path based on following steps: For each
85      * node:
86      *
87      * <p>
88      * 1. Create Och interface on source termination point. 2. Create Och
89      * interface on destination termination point. 3. Create cross connect
90      * between source and destination tps created in step 1 and 2.
91      *
92      * <p>
93      * Naming convention used for OCH interfaces name : tp-wavenumber Naming
94      * convention used for cross connect name : src-dest-wavenumber
95      *
96      * @param input
97      *            Input parameter from the service-path yang model
98      *
99      * @return Result list of all nodes if request successful otherwise specific
100      *         reason of failure.
101      */
102     public ServicePathOutputBuilder setupServicePath(ServicePathInput input) {
103
104         List<Nodes> nodes = input.getNodes();
105         ServicePathOutputBuilder setServBldr = new ServicePathOutputBuilder();
106         LOG.info(currentMountedDevice.toString());
107         for (Nodes n : nodes) {
108             LOG.info("Starting provisioning for node : " + n.getNodeId());
109             String nodeId = n.getNodeId();
110             // if the node is currently mounted then proceed
111             if (currentMountedDevice.contains(n.getNodeId())) {
112                 String srcTp = n.getSrcTp();
113                 String destTp = n.getDestTp();
114
115                 Long waveNumber = input.getWaveNumber();
116                 String srcIf = new OpenRoadmOchInterface(db, mps, nodeId, srcTp).createInterface(waveNumber);
117                 // if source interface creation was successful then proceed
118                 // otherwise return.
119                 if (srcIf == null) {
120                     LOG.warn("Unable to create OCH interface on " + nodeId + " at " + srcTp);
121                     return setServBldr.setResult("Unable to create OCH interface on " + nodeId + " at " + srcTp);
122                 }
123                 // if destination interface creation was then proceed otherwise
124                 // return.
125                 String dstIf = new OpenRoadmOchInterface(db, mps, nodeId, destTp).createInterface(waveNumber);
126                 if (dstIf == null) {
127                     LOG.warn("Unable to create OCH interface on " + nodeId + " at " + destTp);
128                     return setServBldr.setResult("Unable to create OCH interface on " + nodeId + " at " + destTp);
129                 }
130                 LOG.info("Creating cross connect between source :" + srcTp + " destination " + destTp + " for node " + n
131                     .getNodeId());
132                 DataBroker netconfNodeDataBroker = PortMapping.getDeviceDataBroker(nodeId, mps);
133                 CrossConnect roadmConnections = new CrossConnect(netconfNodeDataBroker);
134                 if (roadmConnections.postCrossConnect(waveNumber, srcTp, destTp) == true) {
135                     nodesProvisioned.add(nodeId);
136                     roadmConnections.getConnectionPortTrail(nodeId, mps, waveNumber, srcTp, destTp);
137
138                 } else {
139                     return setServBldr.setResult("Unable to post Roadm-connection for node " + nodeId);
140                 }
141             } else {
142                 LOG.warn(nodeId + " is not mounted on the controller");
143                 return setServBldr.setResult(nodeId + " is not mounted on the controller");
144             }
145         }
146         return setServBldr.setResult("Roadm-connection successfully created for nodes " + nodesProvisioned.toString());
147     }
148
149     /**
150      * This method removes wavelength path based on following steps: For each
151      * node:
152      *
153      * <p>
154      * 1. Delete Cross connect between source and destination tps. 2. Delete Och
155      * interface on source termination point. 3. Delete Och interface on
156      * destination termination point.
157      *
158      * <p>
159      * Naming convention used for OCH interfaces name : tp-wavenumber Naming
160      * convention used for cross connect name : src-dest-wavenumber
161      *
162      * @param input
163      *            Input parameter from the service-path yang model
164      *
165      * @return Result result of the request.
166      */
167     public ServicePathOutputBuilder deleteServicePath(ServicePathInput input) {
168         List<Nodes> nodes = input.getNodes();
169         ServicePathOutputBuilder delServBldr = new ServicePathOutputBuilder();
170         LOG.info(currentMountedDevice.toString());
171         for (Nodes n : nodes) {
172
173             String nodeId = n.getNodeId();
174             LOG.info("Deleting service setup on node " + nodeId);
175             String srcTp = n.getSrcTp();
176             String destTp = n.getDestTp();
177             Long waveNumber = input.getWaveNumber();
178
179             // if the node is currently mounted then proceed.
180             if (currentMountedDevice.contains(nodeId)) {
181                 String connectionNumber = srcTp + "-" + destTp + "-" + waveNumber;
182                 CrossConnect roadmConnection = new CrossConnect(PortMapping.getDeviceDataBroker(nodeId, mps),
183                     connectionNumber);
184                 if (!roadmConnection.deleteCrossConnect()) {
185                     LOG.error("Failed to delete {} ", srcTp + "-" + destTp + "-" + waveNumber);
186                 }
187                 // Deleting interface on source termination point
188                 if (new OpenRoadmInterfaces(db, mps, nodeId, srcTp).deleteInterface(srcTp + "-" + waveNumber
189                     .toString()) == false) {
190                     LOG.error("Failed to delete interface " + srcTp + "-" + waveNumber.toString());
191                 }
192
193                 // Deleting interface on destination termination point
194                 if (new OpenRoadmInterfaces(db, mps, nodeId, srcTp).deleteInterface(destTp + "-" + waveNumber
195                     .toString()) == false) {
196                     LOG.error("Failed to delete interface " + srcTp + "-" + waveNumber.toString());
197                 }
198             } else {
199                 LOG.warn(nodeId + " is not mounted on the controller");
200                 return delServBldr.setResult(nodeId + " is not mounted on the controller");
201             }
202         }
203         return delServBldr.setResult("Request processed");
204     }
205 }