Update connection and interface name
[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.Optional;
16 import java.util.TreeMap;
17 import org.opendaylight.transportpce.common.NodeIdPair;
18 import org.opendaylight.transportpce.common.StringConstants;
19 import org.opendaylight.transportpce.common.fixedflex.GridConstant;
20 import org.opendaylight.transportpce.common.fixedflex.GridUtils;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev200128.OtnServicePathInput;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev200128.OtnServicePathInputBuilder;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev200128.ServicePathInput;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev200128.ServicePathInputBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerSetupInput;
26 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerSetupInputBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev201125.ServiceDeleteOutput;
28 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev201125.ServiceDeleteOutputBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev201125.ServiceImplementationRequestInput;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev201125.ServiceImplementationRequestOutput;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev201125.ServiceImplementationRequestOutputBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.configuration.response.common.ConfigurationResponseCommon;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.configuration.response.common.ConfigurationResponseCommonBuilder;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.FrequencyGHz;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.FrequencyTHz;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.ModulationFormat;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.ServiceDeleteInput;
38 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.PathDescription;
39 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.path.description.AToZDirection;
40 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.path.description.ZToADirection;
41 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.path.description.atoz.direction.AToZ;
42 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.path.description.ztoa.direction.ZToA;
43 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201126.pce.resource.resource.resource.TerminationPoint;
44 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev201211.olm.renderer.input.Nodes;
45 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev201211.olm.renderer.input.NodesBuilder;
46 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev201211.olm.renderer.input.NodesKey;
47 import org.opendaylight.yangtools.yang.common.RpcResult;
48 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
49 import org.opendaylight.yangtools.yang.common.Uint32;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53
54
55 public final class ModelMappingUtils {
56
57     private static final Logger LOG = LoggerFactory.getLogger(ModelMappingUtils.class);
58     private static final String TERMINATION_POINT = "TerminationPoint";
59
60     private ModelMappingUtils() {
61     }
62
63     public static ServicePowerSetupInput createServicePowerSetupInput(List<Nodes> olmList,
64             ServiceImplementationRequestInput input) {
65         ServicePowerSetupInputBuilder olmSetupBldr = new ServicePowerSetupInputBuilder().setNodes(olmList);
66         if (input != null && input.getPathDescription() != null
67                 && input.getPathDescription().getAToZDirection() != null) {
68             AToZDirection atoZDirection = input.getPathDescription().getAToZDirection();
69             olmSetupBldr.setWaveNumber(atoZDirection.getAToZWavelengthNumber());
70             if (atoZDirection.getAToZMinFrequency() != null) {
71                 olmSetupBldr.setLowerSpectralSlotNumber(Uint32
72                         .valueOf(GridUtils
73                                 .getLowerSpectralIndexFromFrequency(atoZDirection.getAToZMinFrequency().getValue())));
74             }
75             if (atoZDirection.getAToZMaxFrequency() != null) {
76                 olmSetupBldr.setHigherSpectralSlotNumber(Uint32
77                         .valueOf(GridUtils
78                                 .getHigherSpectralIndexFromFrequency(atoZDirection.getAToZMaxFrequency().getValue())));
79             }
80         }
81         return olmSetupBldr.build();
82     }
83
84     public static ServiceImplementationRequestOutput createServiceImplResponse(String responseCode, String message) {
85         return new ServiceImplementationRequestOutputBuilder()
86                 .setConfigurationResponseCommon(createCommonResponse(responseCode, message))
87                 .build();
88     }
89
90     public static ServiceDeleteOutput createServiceDeleteResponse(String responseCode, String message) {
91         return new ServiceDeleteOutputBuilder()
92                 .setConfigurationResponseCommon(createCommonResponse(responseCode, message))
93                 .build();
94     }
95
96     public static ConfigurationResponseCommon createCommonResponse(String responseCode, String message) {
97         return new ConfigurationResponseCommonBuilder()
98                 .setResponseMessage(message)
99                 .setResponseCode(responseCode)
100                 .build();
101     }
102
103     public static ListenableFuture<RpcResult<ServiceImplementationRequestOutput>>
104             createServiceImplementationRpcResponse(ServiceImplementationRequestOutput payload) {
105         return RpcResultBuilder.success(payload).buildFuture();
106     }
107
108     public static ListenableFuture<RpcResult<ServiceDeleteOutput>>
109             createServiceDeleteRpcResponse(ServiceDeleteOutput payload) {
110         return RpcResultBuilder.success(payload).buildFuture();
111     }
112
113     public static ServicePathInputData rendererCreateServiceInputAToZ(String serviceName,
114             PathDescription pathDescription) {
115         AToZDirection atoZDirection = pathDescription.getAToZDirection();
116         LOG.info("Building ServicePathInputData for a to z direction {}", atoZDirection);
117         NodeLists nodeLists = getNodesListAToZ(atoZDirection.nonnullAToZ().values().iterator());
118         ServicePathInputBuilder servicePathInputBuilder = new ServicePathInputBuilder()
119             .setServiceName(serviceName)
120             .setOperation(ServicePathInput.Operation.Create)
121             .setNodes(nodeLists.getList())
122             .setWidth(new FrequencyGHz(GridConstant.WIDTH_40));
123         if (atoZDirection.getAToZWavelengthNumber() != null) {
124             servicePathInputBuilder
125                 .setWaveNumber(atoZDirection.getAToZWavelengthNumber());
126         }
127         if (atoZDirection.getAToZMinFrequency() != null) {
128             servicePathInputBuilder.setMinFreq(new FrequencyTHz(atoZDirection.getAToZMinFrequency().getValue()));
129             servicePathInputBuilder.setLowerSpectralSlotNumber(Uint32
130                     .valueOf(GridUtils
131                             .getLowerSpectralIndexFromFrequency(atoZDirection.getAToZMinFrequency().getValue())));
132         }
133         if (atoZDirection.getAToZMaxFrequency() != null) {
134             servicePathInputBuilder.setMaxFreq(new FrequencyTHz(atoZDirection.getAToZMaxFrequency().getValue()));
135             servicePathInputBuilder.setHigherSpectralSlotNumber(
136                     Uint32.valueOf(GridUtils
137                             .getHigherSpectralIndexFromFrequency(atoZDirection.getAToZMaxFrequency().getValue())));
138         }
139         if (atoZDirection.getAToZMinFrequency() != null && atoZDirection.getAToZMaxFrequency() != null) {
140             servicePathInputBuilder.setCenterFreq(
141                     GridUtils.getCentralFrequencyWithPrecision(atoZDirection.getAToZMinFrequency().getValue(),
142                             atoZDirection.getAToZMaxFrequency().getValue(), GridConstant.FREQUENCY_PRECISION));
143         }
144         if (atoZDirection.getRate() != null && atoZDirection.getModulationFormat() != null) {
145             Optional<ModulationFormat> optionalModulationFormat = ModulationFormat
146                     .forName(atoZDirection.getModulationFormat());
147             if (optionalModulationFormat.isPresent()
148                     && GridConstant.FREQUENCY_WIDTH_TABLE
149                     .contains(atoZDirection.getRate(), optionalModulationFormat.get())) {
150                 servicePathInputBuilder
151                     .setWidth(FrequencyGHz
152                         .getDefaultInstance(GridConstant.FREQUENCY_WIDTH_TABLE.get(atoZDirection.getRate(),
153                         optionalModulationFormat.get())));
154             }
155         }
156         servicePathInputBuilder.setModulationFormat(atoZDirection.getModulationFormat());
157         return new ServicePathInputData(servicePathInputBuilder.build(), nodeLists);
158     }
159
160     public static ServicePathInputData rendererCreateServiceInputZToA(String serviceName,
161             PathDescription pathDescription) {
162         ZToADirection ztoADirection = pathDescription.getZToADirection();
163         LOG.info("Building ServicePathInputData for z to a direction {}", ztoADirection);
164         NodeLists nodeLists = getNodesListZtoA(pathDescription.getZToADirection().nonnullZToA().values().iterator());
165         ServicePathInputBuilder servicePathInputBuilder = new ServicePathInputBuilder()
166             .setOperation(ServicePathInput.Operation.Create)
167             .setServiceName(serviceName)
168             .setNodes(nodeLists.getList())
169             .setWidth(new FrequencyGHz(GridConstant.WIDTH_40));
170         if (ztoADirection.getZToAWavelengthNumber() != null) {
171             servicePathInputBuilder
172                 .setWaveNumber(ztoADirection.getZToAWavelengthNumber());
173         }
174         if (ztoADirection.getZToAMinFrequency() != null) {
175             servicePathInputBuilder.setMinFreq(new FrequencyTHz(ztoADirection.getZToAMinFrequency().getValue()));
176             servicePathInputBuilder.setLowerSpectralSlotNumber(Uint32
177                     .valueOf(GridUtils
178                             .getLowerSpectralIndexFromFrequency(ztoADirection.getZToAMinFrequency().getValue())));
179         }
180         if (ztoADirection.getZToAMaxFrequency() != null) {
181             servicePathInputBuilder.setMaxFreq(new FrequencyTHz(ztoADirection.getZToAMaxFrequency().getValue()));
182             servicePathInputBuilder.setHigherSpectralSlotNumber(
183                     Uint32.valueOf(GridUtils
184                             .getHigherSpectralIndexFromFrequency(ztoADirection.getZToAMaxFrequency().getValue())));
185         }
186         if (ztoADirection.getZToAMinFrequency() != null && ztoADirection.getZToAMaxFrequency() != null) {
187             servicePathInputBuilder.setCenterFreq(
188                     GridUtils.getCentralFrequencyWithPrecision(ztoADirection.getZToAMinFrequency().getValue(),
189                             ztoADirection.getZToAMaxFrequency().getValue(), GridConstant.FREQUENCY_PRECISION));
190         }
191         if (ztoADirection.getRate() != null && ztoADirection.getModulationFormat() != null) {
192             Optional<ModulationFormat> optionalModulationFormat = ModulationFormat
193                     .forName(ztoADirection.getModulationFormat());
194             if (optionalModulationFormat.isPresent()
195                     && GridConstant.FREQUENCY_WIDTH_TABLE
196                     .contains(ztoADirection.getRate(), optionalModulationFormat.get())) {
197                 servicePathInputBuilder.setWidth(FrequencyGHz
198                         .getDefaultInstance(GridConstant.FREQUENCY_WIDTH_TABLE.get(ztoADirection.getRate(),
199                                 optionalModulationFormat.get())));
200             }
201         }
202         servicePathInputBuilder.setModulationFormat(ztoADirection.getModulationFormat());
203         return new ServicePathInputData(servicePathInputBuilder.build(), nodeLists);
204     }
205
206     // Adding createOtnServiceInputpath for A-Z and Z-A directions as one method
207     public static OtnServicePathInput rendererCreateOtnServiceInput(String serviceName, String serviceType,
208         String serviceRate, PathDescription pathDescription, boolean asideToZside) {
209         // If atoZ is set true use A-to-Z direction otherwise use Z-to-A
210         List<org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev201211.otn.renderer.input.Nodes> nodes =
211             new ArrayList<>();
212         NodeLists nodeLists = getNodesListAToZ(pathDescription.getAToZDirection().nonnullAToZ().values().iterator());
213         if (!asideToZside) {
214             nodeLists = getNodesListZtoA(pathDescription.getZToADirection().nonnullZToA().values().iterator());
215         }
216         LOG.info("These are node-lists {}, {}", nodeLists.getList(), nodeLists.getOlmList());
217         for (Nodes node: nodeLists.getList()) {
218             nodes.add(
219                     new org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev201211.otn.renderer.input
220                         .NodesBuilder()
221                             .setNodeId(node.getNodeId())
222                             .setClientTp(node.getSrcTp())
223                             .setNetworkTp(node.getDestTp())
224                             .build());
225         }
226         OtnServicePathInputBuilder otnServicePathInputBuilder = new OtnServicePathInputBuilder()
227             .setServiceName(serviceName)
228             .setServiceType(serviceType)
229             .setServiceRate(serviceRate)
230             .setNodes(nodes);
231
232         // set the trib-slots and trib-ports for the lower oder odu
233         if (serviceRate.equals("1G") || (serviceRate.equals("10G"))) {
234             otnServicePathInputBuilder
235                 .setTribPortNumber(pathDescription.getAToZDirection().getTribPortNumber().shortValue())
236                 .setTribSlot(pathDescription.getAToZDirection().getTribSlotNumber().shortValue());
237         }
238         return otnServicePathInputBuilder.build();
239     }
240
241     public static ServicePathInput rendererDeleteServiceInput(String serviceName,
242             ServiceDeleteInput serviceDeleteInput) {
243         //TODO: finish model-model mapping
244         return new ServicePathInputBuilder().setServiceName(serviceName).build();
245     }
246
247     private static NodeLists getNodesListZtoA(Iterator<ZToA> iterator) {
248         Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
249         List<Nodes> olmList = new ArrayList<>();
250         List<Nodes> list = new ArrayList<>();
251         String resourceType;
252         TerminationPoint tp;
253         String tpID = "";
254         String nodeID = "";
255         String sortId = "";
256         while (iterator.hasNext()) {
257             ZToA pathDesObj = iterator.next();
258             resourceType = pathDesObj.getResource().getResource().implementedInterface().getSimpleName();
259             LOG.info("Inside ZtoA {}", resourceType);
260
261             try {
262                 if (TERMINATION_POINT.equals(resourceType)) {
263                     tp = (TerminationPoint) pathDesObj.getResource().getResource();
264                     LOG.info(" TP is {} {}", tp.getTpId(),
265                             tp.getTpNodeId());
266                     tpID = tp.getTpId();
267                     sortId = pathDesObj.getId();
268
269                     //TODO: do not rely on ID to be in certain format
270                     if (tpID.contains("CTP") || tpID.contains("CP")) {
271                         continue;
272                     }
273                     if (tpID.contains(StringConstants.TTP_TOKEN)) {
274                         nodeID = tp.getTpNodeId().split("-DEG")[0];
275                     } else if (tpID.contains(StringConstants.PP_TOKEN)) {
276                         nodeID = tp.getTpNodeId().split("-SRG")[0];
277                     } else if (tpID.contains(StringConstants.NETWORK_TOKEN)
278                         || tpID.contains(StringConstants.CLIENT_TOKEN) || tpID.isEmpty()) {
279                         nodeID = tp.getTpNodeId().split("-XPDR")[0];
280                     } else {
281                         continue;
282                     }
283                     int id = Integer.parseInt(sortId);
284                     treeMap.put(id, new NodeIdPair(nodeID, tpID));
285                 } else if ("Link".equals(resourceType)) {
286                     LOG.info("The type is link");
287                 } else {
288                     LOG.info("The type is not identified: {}", resourceType);
289                 }
290             } catch (IllegalArgumentException | SecurityException e) {
291                 LOG.error("Dont find the getResource method", e);
292             }
293         }
294         populateNodeLists(treeMap, list, olmList, false);
295         return new NodeLists(olmList, list);
296     }
297
298     private static NodeLists getNodesListAToZ(Iterator<AToZ> iterator) {
299         Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
300         List<Nodes> list = new ArrayList<>();
301         List<Nodes> olmList = new ArrayList<>();
302         String resourceType;
303         TerminationPoint tp;
304         String tpID = "";
305         String nodeID = "";
306         String sortId = "";
307
308         while (iterator.hasNext()) {
309             AToZ pathDesObj = iterator.next();
310             resourceType = pathDesObj.getResource().getResource().implementedInterface().getSimpleName();
311             LOG.info("Inside AtoZ {}", resourceType);
312             try {
313                 if (TERMINATION_POINT.equals(resourceType)) {
314                     tp = (TerminationPoint) pathDesObj.getResource().getResource();
315                     LOG.info("TP is {} {}", tp.getTpId(),
316                             tp.getTpNodeId());
317                     tpID = tp.getTpId();
318                     sortId = pathDesObj.getId();
319
320                     //TODO: do not rely on ID to be in certain format
321                     if (tpID.contains("CTP") || tpID.contains("CP")) {
322                         continue;
323                     }
324                     if (tpID.contains(StringConstants.TTP_TOKEN)) {
325                         nodeID = tp.getTpNodeId().split("-DEG")[0];
326                     } else if (tpID.contains(StringConstants.PP_TOKEN)) {
327                         nodeID = tp.getTpNodeId().split("-SRG")[0];
328                     } else if (tpID.contains(StringConstants.NETWORK_TOKEN)
329                         || tpID.contains(StringConstants.CLIENT_TOKEN) || tpID.isEmpty()) {
330                         nodeID = tp.getTpNodeId().split("-XPDR")[0];
331                     } else {
332                         continue;
333                     }
334                     int id = Integer.parseInt(sortId);
335                     treeMap.put(id, new NodeIdPair(nodeID, tpID));
336                 } else if ("Link".equals(resourceType)) {
337                     LOG.info("The type is link");
338                 } else {
339                     LOG.info("The type is not identified: {}", resourceType);
340                 }
341             } catch (IllegalArgumentException | SecurityException e) {
342                 //TODO: Auto-generated catch block
343                 LOG.error("Did not find the getResource method", e);
344             }
345         }
346         populateNodeLists(treeMap, list, olmList, true);
347         return new NodeLists(olmList, list);
348     }
349
350     @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
351         value = {"NP_LOAD_OF_KNOWN_NULL_VALUE","RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE"},
352         justification = "loop when value is not always null - "
353                 + "TODO: check if something exists in Java lib")
354     private static void populateNodeLists(Map<Integer, NodeIdPair> treeMap, List<Nodes> list, List<Nodes> olmList,
355         boolean isAToz) {
356         String desID = null;
357         String srcID = null;
358         LOG.info("treeMap values = {}", treeMap.values());
359         for (NodeIdPair values : treeMap.values()) {
360             if (srcID == null) {
361                 srcID = values.getTpID();
362             } else if (desID == null) {
363                 desID = values.getTpID();
364                 NodesBuilder olmNb = new NodesBuilder()
365                     .setNodeId(values.getNodeID())
366                     .setDestTp(desID)
367                     .setSrcTp(srcID);
368                 olmList.add(olmNb.build());
369                 if (srcID.isEmpty()) {
370                     srcID = null;
371                 }
372                 if (desID.isEmpty()) {
373                     desID = new StringBuilder(srcID).toString();
374                     srcID = null;
375                 }
376                 if (isAToz) {
377                     NodesBuilder nb = new NodesBuilder()
378                         .withKey(new NodesKey(values.getNodeID()))
379                         .setDestTp(desID)
380                         .setSrcTp(srcID);
381                     if (srcID != null && desID != null && srcID.contains(StringConstants.NETWORK_TOKEN)) {
382                         nb.setDestTp(srcID).setSrcTp(desID);
383                     }
384                     list.add(nb.build());
385                 } else {
386                     if (srcID != null && desID != null && !srcID.contains(StringConstants.NETWORK_TOKEN)
387                         && !desID.contains(StringConstants.NETWORK_TOKEN)) {
388                         NodesBuilder nb = new NodesBuilder()
389                             .withKey(new NodesKey(values.getNodeID()))
390                             .setDestTp(desID)
391                             .setSrcTp(srcID);
392                         list.add(nb.build());
393                     }
394                 }
395                 srcID = null;
396                 desID = null;
397             } else {
398                 LOG.warn("both, the source and destination id are null!");
399             }
400         }
401     }
402
403
404     @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
405             value = "PZLA_PREFER_ZERO_LENGTH_ARRAYS",
406             justification = "not relevant to return and zero length array"
407                     + " as we need real pos")
408     public static int[] findTheLongestSubstring(String s1, String s2) {
409         if ((s1 == null) || (s2 == null)) {
410             return null;
411         }
412         int[][] dp = new int[s1.length() + 1][s2.length() + 1];
413         int maxLen = 0;
414         int endPos = 0;
415         for (int i = 1; i < dp.length; i++) {
416             for (int j = 1; j < dp[0].length; j++) {
417                 char ch1 = s1.charAt(i - 1);
418                 char ch2 = s2.charAt(j - 1);
419                 if (ch1 == ch2) {
420                     dp[i][j] = dp[i - 1][j - 1] + 1;
421                     if (dp[i][j] >= maxLen) {
422                         maxLen = dp[i][j];
423                         endPos = i;
424                     }
425                 }
426             }
427         }
428         return new int[] { endPos - maxLen, endPos };
429     }
430
431 }