Path description YANG update
[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.math.BigDecimal;
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.TreeMap;
19 import java.util.stream.Collectors;
20 import org.opendaylight.transportpce.common.NodeIdPair;
21 import org.opendaylight.transportpce.common.StringConstants;
22 import org.opendaylight.transportpce.common.fixedflex.GridConstant;
23 import org.opendaylight.transportpce.common.fixedflex.GridUtils;
24 import org.opendaylight.transportpce.common.mapping.PortMappingUtils;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.Action;
26 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.OtnServicePathInput;
27 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.OtnServicePathInputBuilder;
28 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.ServicePathInput;
29 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.ServicePathInputBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.az.api.info.AEndApiInfo;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.az.api.info.AEndApiInfoBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.az.api.info.ZEndApiInfo;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.az.api.info.ZEndApiInfoBuilder;
34 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupInput;
35 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupInputBuilder;
36 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.ServiceDeleteOutput;
37 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.ServiceDeleteOutputBuilder;
38 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.ServiceImplementationRequestInput;
39 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.ServiceImplementationRequestOutput;
40 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.ServiceImplementationRequestOutputBuilder;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev211210.configuration.response.common.ConfigurationResponseCommon;
42 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev211210.configuration.response.common.ConfigurationResponseCommonBuilder;
43 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.FrequencyGHz;
44 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.FrequencyTHz;
45 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.ModulationFormat;
46 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.common.types.rev210924.OpucnTribSlotDef;
47 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.ServiceDeleteInput;
48 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.PathDescription;
49 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.path.description.AToZDirection;
50 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.path.description.ZToADirection;
51 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.path.description.atoz.direction.AToZ;
52 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.path.description.ztoa.direction.ZToA;
53 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.pce.resource.resource.Resource;
54 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.pce.resource.resource.resource.Link;
55 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.pce.resource.resource.resource.TerminationPoint;
56 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev220926.optical.renderer.nodes.Nodes;
57 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev220926.optical.renderer.nodes.NodesBuilder;
58 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev220926.optical.renderer.nodes.NodesKey;
59 import org.opendaylight.yangtools.yang.common.Decimal64;
60 import org.opendaylight.yangtools.yang.common.RpcResult;
61 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
62 import org.opendaylight.yangtools.yang.common.Uint32;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
65
66
67 public final class ModelMappingUtils {
68
69     private static final Logger LOG = LoggerFactory.getLogger(ModelMappingUtils.class);
70     private static final String TERMINATION_POINT = "TerminationPoint";
71     private static final String LINK = "Link";
72
73     private ModelMappingUtils() {
74     }
75
76     public static ServicePowerSetupInput createServicePowerSetupInput(List<Nodes> olmList,
77             ServiceImplementationRequestInput input) {
78         ServicePowerSetupInputBuilder olmSetupBldr = new ServicePowerSetupInputBuilder().setNodes(olmList);
79         if (input != null && input.getPathDescription() != null
80                 && input.getPathDescription().getAToZDirection() != null) {
81             AToZDirection atoZDirection = input.getPathDescription().getAToZDirection();
82             olmSetupBldr.setWaveNumber(atoZDirection.getAToZWavelengthNumber());
83             if (atoZDirection.getAToZMinFrequency() != null) {
84                 olmSetupBldr.setLowerSpectralSlotNumber(Uint32
85                     .valueOf(GridUtils
86                         .getLowerSpectralIndexFromFrequency(atoZDirection.getAToZMinFrequency().getValue())));
87             }
88             if (atoZDirection.getAToZMaxFrequency() != null) {
89                 olmSetupBldr.setHigherSpectralSlotNumber(Uint32
90                     .valueOf(GridUtils
91                         .getHigherSpectralIndexFromFrequency(atoZDirection.getAToZMaxFrequency().getValue())));
92             }
93             // Set the MC-width for the OLM
94             olmSetupBldr.setMcWidth(new FrequencyGHz(Decimal64.valueOf(
95                     atoZDirection.getAToZMaxFrequency().getValue().decimalValue()
96                     .subtract(atoZDirection.getAToZMinFrequency().getValue().decimalValue())
97                     .multiply(new BigDecimal(1000)))));
98         }
99         return olmSetupBldr.build();
100     }
101
102     public static ServiceImplementationRequestOutput createServiceImplResponse(String responseCode, String message) {
103         return new ServiceImplementationRequestOutputBuilder()
104                 .setConfigurationResponseCommon(createCommonResponse(responseCode, message))
105                 .build();
106     }
107
108     public static ServiceDeleteOutput createServiceDeleteResponse(String responseCode, String message) {
109         return new ServiceDeleteOutputBuilder()
110                 .setConfigurationResponseCommon(createCommonResponse(responseCode, message))
111                 .build();
112     }
113
114     public static ConfigurationResponseCommon createCommonResponse(String responseCode, String message) {
115         return new ConfigurationResponseCommonBuilder()
116                 .setResponseMessage(message)
117                 .setResponseCode(responseCode)
118                 .build();
119     }
120
121     public static ListenableFuture<RpcResult<ServiceImplementationRequestOutput>>
122             createServiceImplementationRpcResponse(ServiceImplementationRequestOutput payload) {
123         return RpcResultBuilder.success(payload).buildFuture();
124     }
125
126     public static ListenableFuture<RpcResult<ServiceDeleteOutput>>
127             createServiceDeleteRpcResponse(ServiceDeleteOutput payload) {
128         return RpcResultBuilder.success(payload).buildFuture();
129     }
130
131     public static ServicePathInputData rendererCreateServiceInputAToZ(String serviceName,
132             PathDescription pathDescription, Action operation) {
133         int scale = GridConstant.FIXED_GRID_FREQUENCY_PRECISION;
134         AToZDirection atoZDirection = pathDescription.getAToZDirection();
135         LOG.info("Building ServicePathInputData for a to z direction {}", atoZDirection);
136         NodeLists nodeLists = getNodesListAToZ(atoZDirection.nonnullAToZ().values().iterator());
137         ServicePathInputBuilder servicePathInputBuilder = new ServicePathInputBuilder()
138             .setServiceName(serviceName)
139             .setOperation(operation)
140             .setNodes(nodeLists.getRendererNodeList())
141             .setNmcWidth(new FrequencyGHz(Decimal64.valueOf(GridConstant.WIDTH_40)))
142             .setMcWidth(new FrequencyGHz(Decimal64.valueOf(
143                     pathDescription.getAToZDirection().getAToZMaxFrequency().getValue().decimalValue()
144                     .subtract(pathDescription.getAToZDirection().getAToZMinFrequency().getValue().decimalValue()))));
145         if (atoZDirection.getAToZWavelengthNumber() != null) {
146             servicePathInputBuilder
147                 .setWaveNumber(atoZDirection.getAToZWavelengthNumber());
148         }
149         if (Uint32.valueOf(GridConstant.IRRELEVANT_WAVELENGTH_NUMBER)
150                 .equals(atoZDirection.getAToZWavelengthNumber())) {
151             scale = GridConstant.FLEX_GRID_FREQUENCY_PRECISION;
152         }
153         if (atoZDirection.getAToZMinFrequency() != null) {
154             servicePathInputBuilder.setMinFreq(new FrequencyTHz(atoZDirection.getAToZMinFrequency().getValue()));
155             servicePathInputBuilder.setLowerSpectralSlotNumber(Uint32
156                     .valueOf(GridUtils
157                             .getLowerSpectralIndexFromFrequency(atoZDirection.getAToZMinFrequency().getValue())));
158         }
159         if (atoZDirection.getAToZMaxFrequency() != null) {
160             servicePathInputBuilder.setMaxFreq(new FrequencyTHz(atoZDirection.getAToZMaxFrequency().getValue()));
161             servicePathInputBuilder.setHigherSpectralSlotNumber(
162                     Uint32.valueOf(GridUtils
163                             .getHigherSpectralIndexFromFrequency(atoZDirection.getAToZMaxFrequency().getValue())));
164         }
165         if (atoZDirection.getAToZMinFrequency() != null && atoZDirection.getAToZMaxFrequency() != null) {
166             servicePathInputBuilder.setCenterFreq(GridUtils.getCentralFrequencyWithPrecision(
167                     atoZDirection.getAToZMinFrequency().getValue().decimalValue(),
168                     atoZDirection.getAToZMaxFrequency().getValue().decimalValue(),
169                     scale));
170         }
171         if (atoZDirection.getRate() != null && atoZDirection.getModulationFormat() != null) {
172             ModulationFormat modulationFormat = ModulationFormat.forName(atoZDirection.getModulationFormat());
173             if (modulationFormat != null
174                     && GridConstant.FREQUENCY_WIDTH_TABLE
175                     .contains(atoZDirection.getRate(), modulationFormat)) {
176                 servicePathInputBuilder
177                     .setNmcWidth(FrequencyGHz
178                         .getDefaultInstance(GridConstant.FREQUENCY_WIDTH_TABLE.get(atoZDirection.getRate(),
179                         modulationFormat)));
180             }
181         }
182         servicePathInputBuilder.setModulationFormat(atoZDirection.getModulationFormat())
183             .setAEndApiInfo(createAendApiInfo(pathDescription, false))
184             .setZEndApiInfo(createZendApiInfo(pathDescription, false));
185         return new ServicePathInputData(servicePathInputBuilder.build(), nodeLists);
186     }
187
188     public static ServicePathInputData rendererCreateServiceInputZToA(String serviceName,
189             PathDescription pathDescription, Action operation) {
190         int scale = GridConstant.FIXED_GRID_FREQUENCY_PRECISION;
191         ZToADirection ztoADirection = pathDescription.getZToADirection();
192         LOG.info("Building ServicePathInputData for z to a direction {}", ztoADirection);
193         NodeLists nodeLists = getNodesListZtoA(pathDescription.getZToADirection().nonnullZToA().values().iterator());
194         ServicePathInputBuilder servicePathInputBuilder = new ServicePathInputBuilder()
195             .setOperation(operation)
196             .setServiceName(serviceName)
197             .setNodes(nodeLists.getRendererNodeList())
198             .setNmcWidth(new FrequencyGHz(Decimal64.valueOf(GridConstant.WIDTH_40)))
199             .setMcWidth(new FrequencyGHz(Decimal64.valueOf(
200                     pathDescription.getAToZDirection().getAToZMaxFrequency().getValue().decimalValue()
201                     .subtract(pathDescription.getAToZDirection().getAToZMinFrequency().getValue().decimalValue()))));
202         if (ztoADirection.getZToAWavelengthNumber() != null) {
203             servicePathInputBuilder
204                 .setWaveNumber(ztoADirection.getZToAWavelengthNumber());
205         }
206         if (Uint32.valueOf(GridConstant.IRRELEVANT_WAVELENGTH_NUMBER)
207                 .equals(ztoADirection.getZToAWavelengthNumber())) {
208             scale = GridConstant.FLEX_GRID_FREQUENCY_PRECISION;
209         }
210         if (ztoADirection.getZToAMinFrequency() != null) {
211             servicePathInputBuilder.setMinFreq(new FrequencyTHz(ztoADirection.getZToAMinFrequency().getValue()));
212             servicePathInputBuilder.setLowerSpectralSlotNumber(Uint32
213                     .valueOf(GridUtils
214                             .getLowerSpectralIndexFromFrequency(ztoADirection.getZToAMinFrequency().getValue())));
215         }
216         if (ztoADirection.getZToAMaxFrequency() != null) {
217             servicePathInputBuilder.setMaxFreq(new FrequencyTHz(ztoADirection.getZToAMaxFrequency().getValue()));
218             servicePathInputBuilder.setHigherSpectralSlotNumber(
219                     Uint32.valueOf(GridUtils
220                             .getHigherSpectralIndexFromFrequency(ztoADirection.getZToAMaxFrequency().getValue())));
221         }
222         if (ztoADirection.getZToAMinFrequency() != null && ztoADirection.getZToAMaxFrequency() != null) {
223             servicePathInputBuilder.setCenterFreq(GridUtils.getCentralFrequencyWithPrecision(
224                     ztoADirection.getZToAMinFrequency().getValue().decimalValue(),
225                     ztoADirection.getZToAMaxFrequency().getValue().decimalValue(),
226                     scale));
227         }
228         if (ztoADirection.getRate() != null && ztoADirection.getModulationFormat() != null) {
229             ModulationFormat modulationFormat = ModulationFormat.forName(ztoADirection.getModulationFormat());
230             if (modulationFormat != null
231                     && GridConstant.FREQUENCY_WIDTH_TABLE
232                     .contains(ztoADirection.getRate(), modulationFormat)) {
233                 servicePathInputBuilder.setNmcWidth(FrequencyGHz
234                         .getDefaultInstance(GridConstant.FREQUENCY_WIDTH_TABLE.get(ztoADirection.getRate(),
235                                 modulationFormat)));
236             }
237         }
238         servicePathInputBuilder.setModulationFormat(ztoADirection.getModulationFormat())
239             .setAEndApiInfo(createAendApiInfo(pathDescription, false))
240             .setZEndApiInfo(createZendApiInfo(pathDescription, false));
241         return new ServicePathInputData(servicePathInputBuilder.build(), nodeLists);
242     }
243
244     // Adding createOtnServiceInputpath for A-Z and Z-A directions as one method
245     public static OtnServicePathInput rendererCreateOtnServiceInput(String serviceName, Action operation,
246         String serviceFormat, Uint32 serviceRate, PathDescription pathDescription, boolean asideToZside) {
247         // If atoZ is set true use A-to-Z direction otherwise use Z-to-A
248         List<org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev220926.otn.renderer.nodes.Nodes> nodes =
249             new ArrayList<>();
250         NodeLists nodeLists =
251             (asideToZside)
252             ? getNodesListAToZ(pathDescription.getAToZDirection().nonnullAToZ().values().iterator())
253             : getNodesListZtoA(pathDescription.getZToADirection().nonnullZToA().values().iterator());
254         LOG.info("These are node-lists {}, {}", nodeLists.getRendererNodeList(), nodeLists.getOlmNodeList());
255         for (Nodes node: nodeLists.getRendererNodeList()) {
256             org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev220926.otn.renderer.nodes.NodesBuilder nb
257                 = new org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev220926.otn.renderer.nodes
258                     .NodesBuilder().setNodeId(node.getNodeId()).setNetworkTp(node.getDestTp());
259             if (node.getSrcTp() != null && node.getSrcTp().contains("NETWORK")) {
260                 nb.setNetwork2Tp(node.getSrcTp());
261             } else {
262                 nb.setClientTp(node.getSrcTp());
263             }
264             nodes.add(nb.build());
265         }
266         OtnServicePathInputBuilder otnServicePathInputBuilder = new OtnServicePathInputBuilder()
267             .setServiceName(serviceName)
268             .setOperation(operation)
269             .setServiceFormat(serviceFormat)
270             .setServiceRate(serviceRate)
271             .setNodes(nodes)
272             .setAEndApiInfo(createAendApiInfo(pathDescription, true))
273             .setZEndApiInfo(createZendApiInfo(pathDescription, true));
274
275         // set the trib-slots and trib-ports for the lower oder odu
276         if (serviceRate.intValue() == 1 || (serviceRate.intValue() == 10)) {
277             Short tribPort = Short.valueOf(pathDescription.getAToZDirection().getMinTribSlot().getValue()
278                 .split("\\.")[0]);
279             Short minTribSlot = Short.valueOf(pathDescription.getAToZDirection().getMinTribSlot().getValue()
280                 .split("\\.")[1]);
281             otnServicePathInputBuilder
282                 .setTribPortNumber(tribPort)
283                 .setTribSlot(minTribSlot);
284         }
285         if (serviceRate.intValue() == 100 && pathDescription.getAToZDirection().getMinTribSlot() != null
286                 && pathDescription.getAToZDirection().getMaxTribSlot() != null) {
287             Set<OpucnTribSlotDef> opucnTribSlotDefList = new HashSet<>();
288             opucnTribSlotDefList.add(
289                 new OpucnTribSlotDef(pathDescription.getAToZDirection().getMinTribSlot().getValue()));
290             opucnTribSlotDefList.add(
291                 new OpucnTribSlotDef(pathDescription.getAToZDirection().getMaxTribSlot().getValue()));
292             otnServicePathInputBuilder.setOpucnTribSlots(opucnTribSlotDefList);
293         }
294         return otnServicePathInputBuilder.build();
295     }
296
297     public static ServicePathInput rendererDeleteServiceInput(String serviceName,
298             ServiceDeleteInput serviceDeleteInput) {
299         //TODO: finish model-model mapping
300         return new ServicePathInputBuilder().setServiceName(serviceName).build();
301     }
302
303     public static Set<String> getLinksFromServicePathDescription(PathDescription pathDescription) {
304         Set<String> linkidList = new HashSet<>();
305         pathDescription.getAToZDirection().getAToZ().values().stream()
306             .filter(lk -> "Link".equals(lk.getResource().getResource().implementedInterface().getSimpleName()))
307             .forEach(rsc -> {
308                 Link link = (Link) rsc.getResource().getResource();
309                 linkidList.add(link.getLinkId());
310             });
311         pathDescription.getZToADirection().getZToA().values().stream()
312             .filter(lk -> "Link".equals(lk.getResource().getResource().implementedInterface().getSimpleName()))
313             .forEach(rsc -> {
314                 Link link = (Link) rsc.getResource().getResource();
315                 linkidList.add(link.getLinkId());
316             });
317         return linkidList;
318     }
319
320     private static NodeLists getNodesListZtoA(Iterator<ZToA> iterator) {
321         Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
322         List<Nodes> olmList = new ArrayList<>();
323         List<Nodes> list = new ArrayList<>();
324
325         while (iterator.hasNext()) {
326             ZToA pathDesObj = iterator.next();
327             try {
328                 populateTreeMap(treeMap, pathDesObj.getResource().getResource(), pathDesObj.getId(), "ZtoA");
329             } catch (IllegalArgumentException | SecurityException e) {
330                 //TODO: Auto-generated catch block
331                 LOG.error("Did not find the getResource method", e);
332             }
333         }
334         populateNodeLists(treeMap, list, olmList, false);
335         return new NodeLists(olmList, list);
336     }
337
338     private static NodeLists getNodesListAToZ(Iterator<AToZ> iterator) {
339         Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
340         List<Nodes> list = new ArrayList<>();
341         List<Nodes> olmList = new ArrayList<>();
342
343         while (iterator.hasNext()) {
344             AToZ pathDesObj = iterator.next();
345             try {
346                 populateTreeMap(treeMap, pathDesObj.getResource().getResource(), pathDesObj.getId(), "AtoZ");
347             } catch (IllegalArgumentException | SecurityException e) {
348                 //TODO: Auto-generated catch block
349                 LOG.error("Did not find the getResource method", e);
350             }
351         }
352         populateNodeLists(treeMap, list, olmList, true);
353         return new NodeLists(olmList, list);
354     }
355
356     private static void populateTreeMap(Map<Integer, NodeIdPair> treeMap, Resource rsrc, String sortId,
357             String direction) {
358         String resourceType = rsrc.implementedInterface().getSimpleName();
359         LOG.info("Inside {} {}", direction, resourceType);
360         switch (resourceType) {
361             case TERMINATION_POINT:
362                 TerminationPoint tp = (TerminationPoint) rsrc;
363                 LOG.info(" TP is {} {}", tp.getTpId(), tp.getTpNodeId());
364                 String tpID = tp.getTpId();
365
366                 //TODO: do not rely on ID to be in certain format
367                 if (tpID.contains("CTP") || tpID.contains("CP")) {
368                     return;
369                 }
370                 String nodeID = "";
371                 if (tpID.contains(StringConstants.TTP_TOKEN)) {
372                     nodeID = tp.getTpNodeId().split("-DEG")[0];
373                 } else if (tpID.contains(StringConstants.PP_TOKEN)) {
374                     nodeID = tp.getTpNodeId().split("-SRG")[0];
375                 } else if (tpID.contains(StringConstants.NETWORK_TOKEN)
376                         || tpID.contains(StringConstants.CLIENT_TOKEN) || tpID.isEmpty()) {
377                     nodeID = tp.getTpNodeId().split("-XPDR")[0];
378                 } else {
379                     return;
380                 }
381                 int id = Integer.parseInt(sortId);
382                 treeMap.put(id, new NodeIdPair(nodeID, tpID));
383                 return;
384             case LINK:
385                 LOG.info("The type is link");
386                 return;
387             default:
388                 LOG.info("The type is not identified: {}", resourceType);
389                 return;
390         }
391     }
392
393     @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
394         value = {"NP_LOAD_OF_KNOWN_NULL_VALUE","RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE"},
395         justification = "loop when value is not always null - "
396                 + "TODO: check if something exists in Java lib")
397     private static void populateNodeLists(Map<Integer, NodeIdPair> treeMap, List<Nodes> list, List<Nodes> olmList,
398             boolean isAToz) {
399         String desID = null;
400         String srcID = null;
401         LOG.info("treeMap values = {}", treeMap.values());
402         for (NodeIdPair values : treeMap.values()) {
403             if (srcID == null) {
404                 srcID = values.getTpID();
405             } else if (desID == null) {
406                 desID = values.getTpID();
407                 NodesBuilder olmNb = new NodesBuilder()
408                     .setNodeId(values.getNodeID())
409                     .setDestTp(desID)
410                     .setSrcTp(srcID);
411                 olmList.add(olmNb.build());
412                 if (srcID.isEmpty()) {
413                     srcID = null;
414                 }
415                 if (desID.isEmpty()) {
416                     desID = new StringBuilder(srcID).toString();
417                     srcID = null;
418                 }
419                 if (isAToz) {
420                     NodesBuilder nb = new NodesBuilder()
421                         .withKey(new NodesKey(values.getNodeID()))
422                         .setDestTp(desID)
423                         .setSrcTp(srcID);
424                     if (srcID != null && desID != null && srcID.contains(StringConstants.NETWORK_TOKEN)) {
425                         nb.setDestTp(srcID).setSrcTp(desID);
426                     }
427                     list.add(nb.build());
428                 } else {
429                     if (srcID != null && desID != null && !srcID.contains(StringConstants.NETWORK_TOKEN)
430                         && !desID.contains(StringConstants.NETWORK_TOKEN)) {
431                         NodesBuilder nb = new NodesBuilder()
432                             .withKey(new NodesKey(values.getNodeID()))
433                             .setDestTp(desID)
434                             .setSrcTp(srcID);
435                         list.add(nb.build());
436                     }
437                 }
438                 srcID = null;
439                 desID = null;
440             } else {
441                 LOG.warn("both, the source and destination id are not null!");
442             }
443         }
444     }
445
446     private static AEndApiInfo createAendApiInfo(PathDescription pathDescription, boolean isForOtn) {
447         String anodeId = null;
448         String sapi = null;
449         String dapi = null;
450         if (isForOtn) {
451             anodeId = extractAendFromPathDescriptionForOtn(pathDescription).get("nodeId");
452             sapi = PortMappingUtils.fnv1size64(
453                 String.join("-", anodeId, extractAendFromPathDescriptionForOtn(pathDescription).get("tpid")));
454             dapi = PortMappingUtils.fnv1size64(
455                 String.join("-", extractZendFromPathDescriptionForOtn(pathDescription).get("nodeId"),
456                     extractZendFromPathDescriptionForOtn(pathDescription).get("tpid")));
457         } else {
458             anodeId = extractAendFromPathDescriptionForOptical(pathDescription).get("nodeId");
459             sapi = PortMappingUtils.fnv1size64(
460                 String.join("-", anodeId, extractAendFromPathDescriptionForOptical(pathDescription).get("tpid")));
461             dapi = PortMappingUtils.fnv1size64(
462                 String.join("-", extractZendFromPathDescriptionForOptical(pathDescription).get("nodeId"),
463                     extractZendFromPathDescriptionForOptical(pathDescription).get("tpid")));
464         }
465         return new AEndApiInfoBuilder()
466             .setSapi(sapi)
467             .setExpectedDapi(sapi)
468             .setDapi(dapi)
469             .setExpectedSapi(dapi)
470             .setNodeId(anodeId)
471             .build();
472     }
473
474     private static ZEndApiInfo createZendApiInfo(PathDescription pathDescription, boolean isForOtn) {
475         String znodeId = null;
476         String sapi = null;
477         String dapi = null;
478         if (isForOtn) {
479             znodeId = extractZendFromPathDescriptionForOtn(pathDescription).get("nodeId");
480             sapi = PortMappingUtils.fnv1size64(
481                 String.join("-", znodeId, extractZendFromPathDescriptionForOtn(pathDescription).get("tpid")));
482             dapi = PortMappingUtils.fnv1size64(
483                 String.join("-", extractAendFromPathDescriptionForOtn(pathDescription).get("nodeId"),
484                     extractAendFromPathDescriptionForOtn(pathDescription).get("tpid")));
485         } else {
486             znodeId = extractZendFromPathDescriptionForOptical(pathDescription).get("nodeId");
487             sapi = PortMappingUtils.fnv1size64(
488                 String.join("-", znodeId, extractZendFromPathDescriptionForOptical(pathDescription).get("tpid")));
489             dapi = PortMappingUtils.fnv1size64(
490                 String.join("-", extractAendFromPathDescriptionForOptical(pathDescription).get("nodeId"),
491                     extractAendFromPathDescriptionForOptical(pathDescription).get("tpid")));
492         }
493         return new ZEndApiInfoBuilder()
494             .setSapi(sapi)
495             .setExpectedDapi(sapi)
496             .setDapi(dapi)
497             .setExpectedSapi(dapi)
498             .setNodeId(znodeId)
499             .build();
500     }
501
502     private static Map<String, String> extractAendFromPathDescriptionForOtn(PathDescription pathDescription) {
503         List<AToZ> tpList = pathDescription.getAToZDirection().getAToZ().values().stream()
504             .sorted((az1, az2) -> Integer.compare(Integer.parseInt(az1.getId()), Integer.parseInt(az2.getId())))
505             .filter(az -> TERMINATION_POINT.equals(az.getResource().getResource().implementedInterface()
506                 .getSimpleName()))
507             .collect(Collectors.toList());
508         for (AToZ atoZ : tpList) {
509             TerminationPoint tp = (TerminationPoint) atoZ.getResource().getResource();
510             if (!tp.getTpId().isEmpty() && !tp.getTpNodeId().isEmpty()) {
511                 String nodeId = tp.getTpNodeId();
512                 String lcp = tp.getTpId();
513                 return Map.of("nodeId", nodeId, "tpid", lcp);
514             }
515         }
516         return Map.of();
517     }
518
519     private static Map<String, String> extractZendFromPathDescriptionForOtn(PathDescription pathDescription) {
520         List<ZToA> tpList = pathDescription.getZToADirection().getZToA().values().stream()
521             .sorted((az1, az2) -> Integer.compare(Integer.parseInt(az1.getId()), Integer.parseInt(az2.getId())))
522             .filter(az -> TERMINATION_POINT.equals(az.getResource().getResource().implementedInterface()
523                 .getSimpleName()))
524             .collect(Collectors.toList());
525         for (ZToA ztoA : tpList) {
526             TerminationPoint tp = (TerminationPoint) ztoA.getResource().getResource();
527             if (!tp.getTpId().isEmpty() && !tp.getTpNodeId().isEmpty()) {
528                 String nodeId = tp.getTpNodeId();
529                 String lcp = tp.getTpId();
530                 return Map.of("nodeId", nodeId, "tpid", lcp);
531             }
532         }
533         return Map.of();
534     }
535
536     private static Map<String, String> extractAendFromPathDescriptionForOptical(PathDescription pathDescription) {
537         List<AToZ> tpList = pathDescription.getAToZDirection().getAToZ().values().stream()
538             .sorted((az1, az2) -> Integer.compare(Integer.parseInt(az1.getId()), Integer.parseInt(az2.getId())))
539             .filter(az -> TERMINATION_POINT.equals(az.getResource().getResource().implementedInterface()
540                 .getSimpleName()))
541             .collect(Collectors.toList());
542         for (AToZ atoZ : tpList) {
543             TerminationPoint tp = (TerminationPoint) atoZ.getResource().getResource();
544             if (!tp.getTpId().contains("CLIENT") && !tp.getTpId().isEmpty() && !tp.getTpNodeId().isEmpty()) {
545 //                String nodeId = tp.getTpNodeId();
546 //                String lcp = tp.getTpId();
547                 return Map.of("nodeId", tp.getTpNodeId(), "tpid", tp.getTpId());
548             }
549         }
550         return Map.of();
551     }
552
553     private static Map<String, String> extractZendFromPathDescriptionForOptical(PathDescription pathDescription) {
554         List<ZToA> tpList = pathDescription.getZToADirection().getZToA().values().stream()
555             .sorted((az1, az2) -> Integer.compare(Integer.parseInt(az1.getId()), Integer.parseInt(az2.getId())))
556             .filter(az -> TERMINATION_POINT.equals(az.getResource().getResource().implementedInterface()
557                 .getSimpleName()))
558             .collect(Collectors.toList());
559         for (ZToA ztoA : tpList) {
560             TerminationPoint tp = (TerminationPoint) ztoA.getResource().getResource();
561             if (!tp.getTpId().contains("CLIENT")  && !tp.getTpId().isEmpty() && !tp.getTpNodeId().isEmpty()) {
562 //                String nodeId = tp.getTpNodeId();
563 //                String lcp = tp.getTpId();
564                 return Map.of("nodeId", tp.getTpNodeId(), "tpid", tp.getTpId());
565             }
566         }
567         return Map.of();
568     }
569 }