Fix spotbugs issues of the renderer module
[transportpce.git] / renderer / src / main / java / org / opendaylight / transportpce / renderer / provisiondevice / OtnDeviceRendererServiceImpl.java
1 /*
2  * Copyright © 2019 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 package org.opendaylight.transportpce.renderer.provisiondevice;
9
10 import java.util.ArrayList;
11 import java.util.LinkedList;
12 import java.util.List;
13 import java.util.Optional;
14 import java.util.concurrent.ConcurrentLinkedQueue;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.ForkJoinPool;
17 import java.util.concurrent.ForkJoinTask;
18 import java.util.concurrent.atomic.AtomicBoolean;
19
20 import org.opendaylight.transportpce.common.crossconnect.CrossConnect;
21 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
22 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
23 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
24 import org.opendaylight.transportpce.renderer.openroadminterface.OpenRoadmInterfaceFactory;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.device.rev200128.OtnServicePathInput;
26 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.device.rev200128.OtnServicePathOutput;
27 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.device.rev200128.OtnServicePathOutputBuilder;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.interfaces.grp.Interface;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev200128.node.interfaces.NodeInterface;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev200128.node.interfaces.NodeInterfaceBuilder;
31 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev200128.node.interfaces.NodeInterfaceKey;
32 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev200128.otn.renderer.input.Nodes;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class OtnDeviceRendererServiceImpl implements OtnDeviceRendererService {
37     private static final String ODU2E = "-ODU2e-";
38     private static final Logger LOG = LoggerFactory.getLogger(OtnDeviceRendererServiceImpl.class);
39     private final OpenRoadmInterfaceFactory openRoadmInterfaceFactory;
40     private final CrossConnect crossConnect;
41     private final OpenRoadmInterfaces openRoadmInterfaces;
42     private final DeviceTransactionManager deviceTransactionManager;
43
44
45     public OtnDeviceRendererServiceImpl(OpenRoadmInterfaceFactory openRoadmInterfaceFactory, CrossConnect crossConnect,
46                                         OpenRoadmInterfaces openRoadmInterfaces,
47                                         DeviceTransactionManager deviceTransactionManager) {
48         this.openRoadmInterfaceFactory = openRoadmInterfaceFactory;
49         this.crossConnect = crossConnect;
50         this.openRoadmInterfaces = openRoadmInterfaces;
51         this.deviceTransactionManager = deviceTransactionManager;
52     }
53
54     @Override
55     public OtnServicePathOutput setupOtnServicePath(OtnServicePathInput input) {
56         LOG.info("Calling setup service path");
57         boolean success = true;
58         List<NodeInterface> nodeInterfaces = new ArrayList<>();
59         List<String> results = new ArrayList<>();
60         if (input.getServiceType().equals("Ethernet")) {
61             try {
62                 LOG.info("Calling Node interfaces {} {} {} {} {} {} {}",
63                         input.getServiceRate(),input.getEthernetEncoding(),
64                         input.getServiceType(),input.getOperation(),input.getTribPortNumber(),
65                         input.getTribSlot(),input.getNodes());
66                 nodeInterfaces = createInterface(input);
67                 LOG.info("Node interfaces created just fine ");
68             }
69             catch (OpenRoadmInterfaceException e) {
70                 //handle exception
71                 LOG.warn("Set up service path failed {}", e.toString());
72                 success = false;
73             }
74
75         }
76         if (success) {
77             LOG.info("Result is success");
78             for (NodeInterface nodeInterface: nodeInterfaces) {
79                 results.add("Otn Service path was set up successfully for node :" + nodeInterface.getNodeId());
80             }
81             //TODO Add otn topology links
82         }
83         OtnServicePathOutputBuilder otnServicePathOutputBuilder = new OtnServicePathOutputBuilder()
84                 .setSuccess(success)
85                 .setNodeInterface(nodeInterfaces)
86                 .setResult(String.join("\n", results));
87         return otnServicePathOutputBuilder.build();
88     }
89
90     @Override
91     public OtnServicePathOutput deleteOtnServicePath(OtnServicePathInput input) {
92         List<Nodes> nodes = input.getNodes();
93         AtomicBoolean success = new AtomicBoolean(true);
94         ConcurrentLinkedQueue<String> results = new ConcurrentLinkedQueue<>();
95         ForkJoinPool forkJoinPool = new ForkJoinPool();
96         ForkJoinTask forkJoinTask = forkJoinPool.submit(() -> nodes.parallelStream().forEach(node -> {
97             List<String> interfacesToDelete = new LinkedList<>();
98             String nodeId = node.getNodeId();
99             LOG.info("Deleting service setup on node {}", nodeId);
100             String srcTp = node.getClientTp();
101             String destTp = node.getNetworkTp();
102             if ((srcTp == null) || (destTp == null) || input.getServiceRate() == null) {
103                 LOG.error("Source ({}) or destination ({}) termination point is null.", srcTp, destTp);
104                 return;
105             }
106             // if the node is currently mounted then proceed.
107             if (this.deviceTransactionManager.isDeviceMounted(nodeId)) {
108                 String connectionNumber = "";
109                 switch (input.getServiceRate()) {
110                     case("10G"):
111                         connectionNumber = srcTp + ODU2E + input.getServiceName() + "-x-" + destTp
112                             + ODU2E + input.getServiceName();
113                         break;
114                     case("1G"):
115                         connectionNumber = srcTp + "-ODU0-" + input.getServiceName() + "-x-" + destTp
116                             + ODU2E + input.getServiceName();
117                         break;
118                     default:
119                         LOG.error("service rate {} not managed yet", input.getServiceRate());
120                         String result = input.getServiceRate() + " is not supported";
121                         results.add(result);
122                         success.set(false);
123                         return;
124                 }
125                 List<String> intToDelete = this.crossConnect.deleteCrossConnect(nodeId, connectionNumber, true);
126                 if (intToDelete != null) {
127                     for (String interf : intToDelete) {
128                         if (!this.openRoadmInterfaceFactory.isUsedByOtnXc(nodeId, interf, connectionNumber,
129                             this.deviceTransactionManager)) {
130                             interfacesToDelete.add(interf);
131                             if (!getSupportedInterface(nodeId, interf).contains("ODU4")) {
132                                 interfacesToDelete.add(getSupportedInterface(nodeId, interf));
133                             }
134                         }
135                     }
136                 }
137             } else {
138                 String result = nodeId + " is not mounted on the controller";
139                 results.add(result);
140                 success.set(false);
141                 LOG.warn(result);
142                 forkJoinPool.shutdown();
143                 return;
144                 //TODO should deletion end here?
145             }
146             for (String interfaceId : interfacesToDelete) {
147                 try {
148                     this.openRoadmInterfaces.deleteInterface(nodeId, interfaceId);
149                 } catch (OpenRoadmInterfaceException e) {
150                     String result = String.format("Failed to delete interface %s on node %s!", interfaceId, nodeId);
151                     success.set(false);
152                     LOG.error(result, e);
153                     results.add(result);
154                 }
155             }
156         }));
157         try {
158             forkJoinTask.get();
159         } catch (InterruptedException | ExecutionException e) {
160             LOG.error("Error while deleting service paths!", e);
161         }
162         forkJoinPool.shutdown();
163         OtnServicePathOutputBuilder delServBldr = new OtnServicePathOutputBuilder();
164         delServBldr.setSuccess(success.get());
165         if (results.isEmpty()) {
166             return delServBldr.setResult("Request processed").build();
167         } else {
168             return delServBldr.setResult(String.join("\n", results)).build();
169         }
170
171     }
172
173     private String getSupportedInterface(String nodeId, String interf) {
174         Optional<Interface> supInterfOpt;
175         try {
176             supInterfOpt = this.openRoadmInterfaces.getInterface(nodeId, interf);
177             if (supInterfOpt.isPresent()) {
178                 return supInterfOpt.get().getSupportingInterface();
179             } else {
180                 return null;
181             }
182         } catch (OpenRoadmInterfaceException e) {
183             LOG.error("error getting Supported Interface of {} - {}", interf, nodeId, e);
184             return null;
185         }
186     }
187
188     private List<NodeInterface> createInterface(OtnServicePathInput input) throws OpenRoadmInterfaceException {
189         List<NodeInterface> nodeInterfaces = new ArrayList<>();
190         LOG.info("Calling Create Interface entry for OTN service path");
191         if (input.getServiceRate() == null
192             || !("1G".equals(input.getServiceRate()) || "10G".equals(input.getServiceRate()))) {
193             LOG.error("Service rate {} not managed yet", input.getServiceRate());
194         } else {
195             createLowOrderInterfaces(input, nodeInterfaces);
196         }
197         return nodeInterfaces;
198     }
199
200     private Optional<String> postCrossConnect(List<String> createdOduInterfaces, Nodes node)
201             throws OpenRoadmInterfaceException {
202         return this.crossConnect.postOtnCrossConnect(createdOduInterfaces,node);
203     }
204
205     private void createLowOrderInterfaces(OtnServicePathInput input, List<NodeInterface> nodeInterfaces)
206         throws OpenRoadmInterfaceException {
207         for (Nodes node: input.getNodes()) {
208             //check if the node is mounted or not?
209             List<String> createdEthInterfaces = new ArrayList<>();
210             List<String> createdOduInterfaces = new ArrayList<>();
211             switch (input.getServiceRate()) {
212                 case("1G"):
213                     LOG.info("Input service is 1G");
214                     createdEthInterfaces.add(
215                         openRoadmInterfaceFactory.createOpenRoadmEth1GInterface(node.getNodeId(), node.getClientTp()));
216                     createdOduInterfaces.add(
217                         //suppporting interface?, payload ?
218                         openRoadmInterfaceFactory.createOpenRoadmOdu0Interface(node.getNodeId(), node.getClientTp(),
219                             input.getServiceName(), "07", false, input.getTribPortNumber(), input.getTribSlot()));
220                     createdOduInterfaces.add(
221                         openRoadmInterfaceFactory.createOpenRoadmOdu0Interface(node.getNodeId(), node.getNetworkTp(),
222                             input.getServiceName(), "07", true, input.getTribPortNumber(), input.getTribSlot()));
223                     break;
224                 case("10G"):
225                     LOG.info("Input service is 10G");
226                     createdEthInterfaces.add(
227                         openRoadmInterfaceFactory.createOpenRoadmEth10GInterface(node.getNodeId(), node.getClientTp()));
228                     createdOduInterfaces.add(
229                         //suppporting interface?, payload ?
230                         openRoadmInterfaceFactory.createOpenRoadmOdu2eInterface(node.getNodeId(), node.getClientTp(),
231                             input.getServiceName(),"03", false ,input.getTribPortNumber(),input.getTribSlot()));
232                     createdOduInterfaces.add(
233                         // supporting interface? payload ?
234                         openRoadmInterfaceFactory.createOpenRoadmOdu2eInterface(node.getNodeId(), node.getNetworkTp(),
235                             input.getServiceName(),"03" , true ,input.getTribPortNumber(),input.getTribSlot()));
236                     break;
237                 default:
238                     LOG.error("service rate {} not managed yet", input.getServiceRate());
239                     return;
240             }
241
242             //implement cross connect
243             List<String> createdConnections = new ArrayList<>();
244             if (!createdOduInterfaces.isEmpty()) {
245                 Optional<String> connectionNameOpt = postCrossConnect(createdOduInterfaces, node);
246                 createdConnections.add(connectionNameOpt.get());
247                 LOG.info("Created cross connects");
248             }
249             NodeInterfaceBuilder nodeInterfaceBuilder = new NodeInterfaceBuilder()
250                     .withKey(new NodeInterfaceKey(input.getServiceName() + "-" + node.getNodeId()))
251                     .setNodeId(input.getServiceName() + "-" + node.getNodeId())
252                     .setConnectionId(createdConnections)
253                     .setEthInterfaceId(createdEthInterfaces)
254                     .setOduInterfaceId(createdOduInterfaces);
255             nodeInterfaces.add(nodeInterfaceBuilder.build());
256         }
257     }
258 }