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