Functional test for OTN Renderer
[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                 }
120                 List<String> intToDelete = this.crossConnect.deleteCrossConnect(nodeId, connectionNumber, true);
121                 if (intToDelete != null) {
122                     for (String interf : intToDelete) {
123                         if (!this.openRoadmInterfaceFactory.isUsedbyOtnXc(nodeId, interf, connectionNumber,
124                             this.deviceTransactionManager)) {
125                             interfacesToDelete.add(interf);
126                             if (!getSupportedInterface(nodeId, interf).contains("ODU4")) {
127                                 interfacesToDelete.add(getSupportedInterface(nodeId, interf));
128                             }
129                         }
130                     }
131                 }
132             } else {
133                 String result = nodeId + " is not mounted on the controller";
134                 results.add(result);
135                 success.set(false);
136                 LOG.warn(result);
137                 forkJoinPool.shutdown();
138                 return;
139                 //TODO should deletion end here?
140             }
141             for (String interfaceId : interfacesToDelete) {
142                 try {
143                     this.openRoadmInterfaces.deleteInterface(nodeId, interfaceId);
144                 } catch (OpenRoadmInterfaceException e) {
145                     String result = String.format("Failed to delete interface %s on node %s!", interfaceId, nodeId);
146                     success.set(false);
147                     LOG.error(result, e);
148                     results.add(result);
149                 }
150             }
151         }));
152         try {
153             forkJoinTask.get();
154         } catch (InterruptedException | ExecutionException e) {
155             LOG.error("Error while deleting service paths!", e);
156         }
157         forkJoinPool.shutdown();
158         OtnServicePathOutputBuilder delServBldr = new OtnServicePathOutputBuilder();
159         delServBldr.setSuccess(success.get());
160         if (results.isEmpty()) {
161             return delServBldr.setResult("Request processed").build();
162         } else {
163             return delServBldr.setResult(String.join("\n", results)).build();
164         }
165
166     }
167
168     private String getSupportedInterface(String nodeId, String interf) {
169         Optional<Interface> supInterfOpt;
170         try {
171             supInterfOpt = this.openRoadmInterfaces.getInterface(nodeId, interf);
172             if (supInterfOpt.isPresent()) {
173                 return supInterfOpt.get().getSupportingInterface();
174             } else {
175                 return null;
176             }
177         } catch (OpenRoadmInterfaceException e) {
178             LOG.error("error getting Supported Interface of {} - {}", interf, nodeId, e);
179             return null;
180         }
181     }
182
183     private List<NodeInterface> createInterface(OtnServicePathInput input) throws OpenRoadmInterfaceException {
184         List<NodeInterface> nodeInterfaces = new ArrayList<>();
185         LOG.info("Calling Create Interface entry for OTN service path");
186         if (input.getServiceRate() == null
187             || !("1G".equals(input.getServiceRate()) || "10G".equals(input.getServiceRate()))) {
188             LOG.error("Service rate {} not managed yet", input.getServiceRate());
189         } else {
190             createLowOrderInterfaces(input, nodeInterfaces);
191         }
192         return nodeInterfaces;
193     }
194
195     private Optional<String> postCrossConnect(List<String> createdOduInterfaces, Nodes node)
196             throws OpenRoadmInterfaceException {
197         return this.crossConnect.postOtnCrossConnect(createdOduInterfaces,node);
198     }
199
200     private void createLowOrderInterfaces(OtnServicePathInput input, List<NodeInterface> nodeInterfaces)
201         throws OpenRoadmInterfaceException {
202         for (Nodes node: input.getNodes()) {
203             //check if the node is mounted or not?
204             List<String> createdEthInterfaces = new ArrayList<>();
205             List<String> createdOduInterfaces = new ArrayList<>();
206             if ("1G".equals(input.getServiceRate())) {
207                 LOG.info("Input service is 1G");
208                 createdEthInterfaces.add(
209                     openRoadmInterfaceFactory.createOpenRoadmEth1GInterface(node.getNodeId(), node.getClientTp()));
210                 createdOduInterfaces.add(
211                     //suppporting interface?, payload ?
212                     openRoadmInterfaceFactory.createOpenRoadmOdu0Interface(node.getNodeId(), node.getClientTp(),
213                         input.getServiceName(), "07", false, input.getTribPortNumber(), input.getTribSlot()));
214                 createdOduInterfaces.add(
215                     openRoadmInterfaceFactory.createOpenRoadmOdu0Interface(node.getNodeId(), node.getNetworkTp(),
216                         input.getServiceName(), "07", true, input.getTribPortNumber(), input.getTribSlot()));
217             } else if ("10G".equals(input.getServiceRate())) {
218                 LOG.info("Input service is 10G");
219                 createdEthInterfaces.add(
220                     openRoadmInterfaceFactory.createOpenRoadmEth10GInterface(node.getNodeId(), node.getClientTp()));
221                 createdOduInterfaces.add(
222                     //suppporting interface?, payload ?
223                     openRoadmInterfaceFactory.createOpenRoadmOdu2eInterface(node.getNodeId(), node.getClientTp(),
224                         input.getServiceName(),"03", false ,input.getTribPortNumber(),input.getTribSlot()));
225                 createdOduInterfaces.add(
226                     // supporting interface? payload ?
227                     openRoadmInterfaceFactory.createOpenRoadmOdu2eInterface(node.getNodeId(), node.getNetworkTp(),
228                         input.getServiceName(),"03" , true ,input.getTribPortNumber(),input.getTribSlot()));
229             }
230             //implement cross connect
231             List<String> createdConnections = new ArrayList<>();
232             if (!createdOduInterfaces.isEmpty()) {
233                 Optional<String> connectionNameOpt = postCrossConnect(createdOduInterfaces, node);
234                 createdConnections.add(connectionNameOpt.get());
235                 LOG.info("Created cross connects");
236             }
237             NodeInterfaceBuilder nodeInterfaceBuilder = new NodeInterfaceBuilder()
238                     .withKey(new NodeInterfaceKey(input.getServiceName() + "-" + node.getNodeId()))
239                     .setNodeId(input.getServiceName() + "-" + node.getNodeId())
240                     .setConnectionId(createdConnections)
241                     .setEthInterfaceId(createdEthInterfaces)
242                     .setOduInterfaceId(createdOduInterfaces);
243             nodeInterfaces.add(nodeInterfaceBuilder.build());
244         }
245     }
246 }