SH-Renderer API code to handle OTN Step 2
[transportpce.git] / renderer / src / main / java / org / opendaylight / transportpce / renderer / ModelMappingUtils.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 package org.opendaylight.transportpce.renderer;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.ArrayList;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.TreeMap;
16 import org.opendaylight.transportpce.common.StringConstants;
17 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerSetupInput;
18 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerSetupInputBuilder;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.device.rev200128.OtnServicePathInput;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.device.rev200128.OtnServicePathInputBuilder;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.device.rev200128.ServicePathInput;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.device.rev200128.ServicePathInputBuilder;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev200520.ServiceDeleteOutput;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev200520.ServiceDeleteOutputBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev200520.ServiceImplementationRequestInput;
26 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev200520.ServiceImplementationRequestOutput;
27 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev200520.ServiceImplementationRequestOutputBuilder;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.configuration.response.common.ConfigurationResponseCommon;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.configuration.response.common.ConfigurationResponseCommonBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.ServiceDeleteInput;
31 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.PathDescription;
32 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.atoz.direction.AToZ;
33 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.ztoa.direction.ZToA;
34 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce.resource.resource.resource.TerminationPoint;
35 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev200615.olm.renderer.input.Nodes;
36 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev200615.olm.renderer.input.NodesBuilder;
37 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev200615.olm.renderer.input.NodesKey;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43
44
45 public final class ModelMappingUtils {
46
47     private static final Logger LOG = LoggerFactory.getLogger(ModelMappingUtils.class);
48     private static final String TERMINATION_POINT = "TerminationPoint";
49
50     private ModelMappingUtils() {
51     }
52
53     public static ServicePowerSetupInput createServicePowerSetupInput(List<Nodes> olmList,
54         ServiceImplementationRequestInput input) {
55         ServicePowerSetupInputBuilder olmSetupBldr = new ServicePowerSetupInputBuilder()
56             .setNodes(olmList);
57         if (input != null && input.getPathDescription() != null
58                 && input.getPathDescription().getAToZDirection() != null) {
59             olmSetupBldr.setWaveNumber(
60                     input.getPathDescription().getAToZDirection().getAToZWavelengthNumber());
61         }
62         return olmSetupBldr.build();
63     }
64
65     public static ServiceImplementationRequestOutput createServiceImplResponse(String responseCode, String message) {
66         return new ServiceImplementationRequestOutputBuilder()
67                 .setConfigurationResponseCommon(createCommonResponse(responseCode, message))
68                 .build();
69     }
70
71     public static ServiceDeleteOutput createServiceDeleteResponse(String responseCode, String message) {
72         return new ServiceDeleteOutputBuilder()
73                 .setConfigurationResponseCommon(createCommonResponse(responseCode, message))
74                 .build();
75     }
76
77     public static ConfigurationResponseCommon createCommonResponse(String responseCode, String message) {
78         return new ConfigurationResponseCommonBuilder()
79                 .setResponseMessage(message)
80                 .setResponseCode(responseCode)
81                 .build();
82     }
83
84     public static ListenableFuture<RpcResult<ServiceImplementationRequestOutput>>
85             createServiceImplementationRpcResponse(ServiceImplementationRequestOutput payload) {
86         return RpcResultBuilder.success(payload).buildFuture();
87     }
88
89     public static ListenableFuture<RpcResult<ServiceDeleteOutput>>
90             createServiceDeleteRpcResponse(ServiceDeleteOutput payload) {
91         return RpcResultBuilder.success(payload).buildFuture();
92     }
93
94     public static ServicePathInputData rendererCreateServiceInputAToZ(String serviceName,
95             PathDescription pathDescription) {
96         NodeLists nodeLists = getNodesListAToZ(pathDescription.getAToZDirection().getAToZ().iterator());
97         ServicePathInputBuilder servicePathInputBuilder = new ServicePathInputBuilder()
98             .setServiceName(serviceName)
99             .setOperation(ServicePathInput.Operation.Create)
100             .setWaveNumber(Long.valueOf(pathDescription.getAToZDirection().getAToZWavelengthNumber().toJava()))
101             .setNodes(nodeLists.getList());
102         return new ServicePathInputData(servicePathInputBuilder.build(), nodeLists);
103     }
104
105     public static ServicePathInputData rendererCreateServiceInputZToA(String serviceName,
106             PathDescription pathDescription) {
107         NodeLists nodeLists = getNodesListZtoA(pathDescription.getZToADirection().getZToA().iterator());
108         ServicePathInputBuilder servicePathInputBuilder = new ServicePathInputBuilder()
109             .setOperation(ServicePathInput.Operation.Create)
110             .setServiceName(serviceName)
111             .setWaveNumber(Long.valueOf(pathDescription.getZToADirection().getZToAWavelengthNumber().toJava()))
112             .setNodes(nodeLists.getList());
113         return new ServicePathInputData(servicePathInputBuilder.build(), nodeLists);
114     }
115
116     // Adding createOtnServiceInputpath for A-Z and Z-A directions as one method
117     public static OtnServicePathInput rendererCreateOtnServiceInput(String serviceName, String serviceType,
118         String serviceRate, PathDescription pathDescription, boolean asideToZside) {
119         // If atoZ is set true use A-to-Z direction otherwise use Z-to-A
120         List<org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev200615.otn.renderer.input.Nodes> nodes =
121             new ArrayList<>();
122         NodeLists nodeLists = null;
123         if (asideToZside) {
124             nodeLists = getNodesListAToZ(pathDescription.getAToZDirection().getAToZ().iterator());
125         } else {
126             nodeLists = getNodesListZtoA(pathDescription.getZToADirection().getZToA().iterator());
127         }
128         LOG.info("These are node-lists {}, {}", nodeLists.getList(), nodeLists.getOlmList());
129         for (int i = 0; i < nodeLists.getList().size(); i++) {
130             Nodes node = nodeLists.getList().get(i);
131             if (serviceRate.equals("100G")) {
132                 nodes.add(
133                     new org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev200615.otn.renderer.input
134                         .NodesBuilder()
135                         .setNodeId(node.getNodeId())
136                         .setNetworkTp(node.getDestTp())
137                         .build());
138             }
139             else { // For any other service rate (1G or 10G)
140                 // For the last node in the list, clientTp and NetworkTp has to be reversed
141                 if (i == nodeLists.getList().size() - 1) {
142                     nodes.add(
143                         new org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev200615.otn.renderer.input
144                             .NodesBuilder()
145                             .setNodeId(node.getNodeId())
146                             .setClientTp(node.getDestTp())
147                             .setNetworkTp(node.getSrcTp())
148                             .build());
149
150                 } else {
151                     nodes.add(
152                         new org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev200615.otn.renderer.input
153                             .NodesBuilder()
154                             .setNodeId(node.getNodeId())
155                             .setClientTp(node.getSrcTp())
156                             .setNetworkTp(node.getDestTp())
157                             .build());
158                 }
159             }
160         }
161         OtnServicePathInputBuilder otnServicePathInputBuilder = new OtnServicePathInputBuilder()
162             .setServiceName(serviceName)
163             .setServiceType(serviceType)
164             .setServiceRate(serviceRate)
165             .setNodes(nodes);
166
167         //TODO: set the trib-slots and trib-ports for the lower oder odu, this should from SH rather than setting here
168         if (serviceRate.equals("1G") || (serviceRate.equals("10G"))) {
169             otnServicePathInputBuilder.setTribPortNumber((short) 1).setTribSlot((short) 1);
170
171         }
172         return otnServicePathInputBuilder.build();
173     }
174
175     public static ServicePathInput rendererDeleteServiceInput(String serviceName,
176             ServiceDeleteInput serviceDeleteInput) {
177         //TODO: finish model-model mapping
178         return new ServicePathInputBuilder().setServiceName(serviceName).build();
179     }
180
181     public static NodeLists getNodesListZtoA(Iterator<ZToA> iterator) {
182         Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
183         List<Nodes> olmList = new ArrayList<>();
184         List<Nodes> list = new ArrayList<>();
185         String resourceType;
186         TerminationPoint tp;
187         String tpID = "";
188         String nodeID = "";
189         String sortId = "";
190         while (iterator.hasNext()) {
191             ZToA pathDesObj = iterator.next();
192             resourceType = pathDesObj.getResource().getResource().implementedInterface().getSimpleName();
193             LOG.info("Inside ZtoA {}", resourceType);
194
195             try {
196                 if (TERMINATION_POINT.equals(resourceType)) {
197                     tp = (TerminationPoint) pathDesObj.getResource().getResource();
198                     LOG.info(" TP is {} {}", tp.getTpId(),
199                             tp.getTpNodeId());
200                     tpID = tp.getTpId();
201                     nodeID = tp.getTpNodeId();
202                     sortId = pathDesObj.getId();
203
204                     //TODO: do not rely on ID to be in certain format
205                     if (tpID.contains("CTP") || tpID.contains("CP")) {
206                         continue;
207                     }
208                     if (!tpID.contains("TTP") && !tpID.contains("PP") && !tpID.contains("NETWORK")
209                             && !tpID.contains("CLIENT")) {
210                         continue;
211                     }
212
213                     int[] pos = findTheLongestSubstring(nodeID, tpID);
214                     if (pos != null) {
215                         //TODO: do not rely on nodeId to be integer
216                         int id = Integer.parseInt(sortId);
217                         treeMap.put(id, new NodeIdPair(nodeID.substring(0, pos[0] - 1), tpID));
218                     }
219                 } else if ("Link".equals(resourceType)) {
220                     LOG.info("The type is link");
221                 } else {
222                     LOG.info("The type is not identified: {}", resourceType);
223                 }
224             } catch (IllegalArgumentException | SecurityException e) {
225                 LOG.error("Dont find the getResource method", e);
226             }
227         }
228         populateNodeLists(treeMap, list, olmList);
229         return new NodeLists(olmList, list);
230     }
231
232     public static NodeLists getNodesListAToZ(Iterator<AToZ> iterator) {
233         Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
234         List<Nodes> list = new ArrayList<>();
235         List<Nodes> olmList = new ArrayList<>();
236         String resourceType;
237         TerminationPoint tp;
238         String tpID = "";
239         String nodeID = "";
240         String sortId = "";
241
242         while (iterator.hasNext()) {
243             AToZ pathDesObj = iterator.next();
244             resourceType = pathDesObj.getResource().getResource().implementedInterface().getSimpleName();
245             LOG.info("Inside AtoZ {}", resourceType);
246             try {
247                 if (TERMINATION_POINT.equals(resourceType)) {
248                     tp = (TerminationPoint) pathDesObj.getResource().getResource();
249                     LOG.info("TP is {} {}", tp.getTpId(),
250                             tp.getTpNodeId());
251                     tpID = tp.getTpId();
252                     nodeID = tp.getTpNodeId();
253                     sortId = pathDesObj.getId();
254
255                     //TODO: do not rely on ID to be in certain format
256                     if (tpID.contains("CTP") || tpID.contains("CP")) {
257                         continue;
258                     }
259                     if (!tpID.contains(StringConstants.TTP_TOKEN)
260                         && !tpID.contains(StringConstants.PP_TOKEN)
261                         && !tpID.contains(StringConstants.NETWORK_TOKEN)
262                         && !tpID.contains(StringConstants.CLIENT_TOKEN)) {
263                         continue;
264                     }
265
266                     int[] pos = findTheLongestSubstring(nodeID, tpID);
267                     if (pos != null) {
268                         //TODO: do not rely on nodeId to be integer
269                         int id = Integer.parseInt(sortId);
270                         treeMap.put(id, new NodeIdPair(nodeID.substring(0, pos[0] - 1), tpID));
271                     }
272                 } else if ("Link".equals(resourceType)) {
273                     LOG.info("The type is link");
274                 } else {
275                     LOG.info("The type is not identified: {}", resourceType);
276                 }
277             } catch (IllegalArgumentException | SecurityException e) {
278                 //TODO: Auto-generated catch block
279                 LOG.error("Did not find the getResource method", e);
280             }
281         }
282         populateNodeLists(treeMap, list, olmList);
283         return new NodeLists(olmList, list);
284     }
285
286     @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
287             value = {"NP_LOAD_OF_KNOWN_NULL_VALUE","RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE"},
288             justification = "loop when value is not always null - "
289                     + "TODO: check if something exists in Java lib")
290     private static void populateNodeLists(Map<Integer, NodeIdPair> treeMap,
291             List<Nodes> list, List<Nodes> olmList) {
292         String desID = null;
293         String srcID = null;
294         int cnt = 0; // This is a counter to check for NETWORK_TOKEN
295         for (NodeIdPair values : treeMap.values()) {
296             cnt++;
297             if (srcID == null) {
298                 srcID = values.getTpID();
299                 // This code is added in support of OTU4 service
300                 if ((srcID.contains(StringConstants.NETWORK_TOKEN)) && cnt == 1) {
301                     NodesBuilder nb = new NodesBuilder()
302                         .withKey(new NodesKey(values.getNodeID()))
303                         .setDestTp(srcID) // desTp is assigned to the srcID
304                         .setSrcTp(null); // srcTp is set to null to create OTU service
305                     list.add(nb.build());
306
307                     NodesBuilder olmNb = new NodesBuilder()
308                         .setNodeId(values.getNodeID())
309                         .setDestTp(srcID) // desTp is assigned to the srcID
310                         .setSrcTp(null); // srcTp is set to null to create OTU service
311                     olmList.add(olmNb.build());
312                     srcID = null;
313                     desID = null;
314                     continue; // Continue to the next element in the for loop
315                 }
316                 if ((srcID.contains(StringConstants.NETWORK_TOKEN)) && cnt == treeMap.size()) {
317                     // For last node
318                     NodesBuilder nb = new NodesBuilder()
319                         .withKey(new NodesKey(values.getNodeID()))
320                         .setDestTp(srcID) //TODO: check this assignment
321                         .setSrcTp(null); //
322                     list.add(nb.build());
323
324                     NodesBuilder olmNb = new NodesBuilder()
325                         .setNodeId(values.getNodeID())
326                         .setDestTp(null) // In the case of final node, destTp is set to null
327                         .setSrcTp(srcID); // srcTp is srcTp
328                     olmList.add(olmNb.build());
329                     srcID = null;
330                     desID = null;
331                 }
332             // End of code for support of ODT4 service
333             } else if (desID == null) {
334                 desID = values.getTpID();
335                 NodesBuilder nb = new NodesBuilder()
336                     .withKey(new NodesKey(values.getNodeID()))
337                     .setDestTp(desID)
338                     .setSrcTp(srcID);
339                 list.add(nb.build());
340
341                 NodesBuilder olmNb = new NodesBuilder()
342                     .setNodeId(values.getNodeID())
343                     .setDestTp(desID)
344                     .setSrcTp(srcID);
345                 olmList.add(olmNb.build());
346                 srcID = null;
347                 desID = null;
348             } else {
349                 LOG.warn("both, the source and destination id are null!");
350             }
351         }
352     }
353
354     @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
355             value = "PZLA_PREFER_ZERO_LENGTH_ARRAYS",
356             justification = "not relevant to return and zero length array"
357                     + " as we need real pos")
358     public static int[] findTheLongestSubstring(String s1, String s2) {
359         if ((s1 == null) || (s2 == null)) {
360             return null;
361         }
362         int[][] dp = new int[s1.length() + 1][s2.length() + 1];
363         int maxLen = 0;
364         int endPos = 0;
365         for (int i = 1; i < dp.length; i++) {
366             for (int j = 1; j < dp[0].length; j++) {
367                 char ch1 = s1.charAt(i - 1);
368                 char ch2 = s2.charAt(j - 1);
369                 if (ch1 == ch2) {
370                     dp[i][j] = dp[i - 1][j - 1] + 1;
371                     if (dp[i][j] >= maxLen) {
372                         maxLen = dp[i][j];
373                         endPos = i;
374                     }
375                 }
376             }
377         }
378         return new int[] { endPos - maxLen, endPos };
379     }
380
381 }