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