NetworkModelListener: handling TopologyUpdates
[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.rev201210.PathDescription;
39 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201210.path.description.AToZDirection;
40 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201210.path.description.ZToADirection;
41 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201210.path.description.atoz.direction.AToZ;
42 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201210.path.description.ztoa.direction.ZToA;
43 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev201210.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         int scale = GridConstant.FIXED_GRID_FREQUENCY_PRECISION;
116         AToZDirection atoZDirection = pathDescription.getAToZDirection();
117         LOG.info("Building ServicePathInputData for a to z direction {}", atoZDirection);
118         NodeLists nodeLists = getNodesListAToZ(atoZDirection.nonnullAToZ().values().iterator());
119         ServicePathInputBuilder servicePathInputBuilder = new ServicePathInputBuilder()
120             .setServiceName(serviceName)
121             .setOperation(ServicePathInput.Operation.Create)
122             .setNodes(nodeLists.getList())
123             .setWidth(new FrequencyGHz(GridConstant.WIDTH_40));
124         if (atoZDirection.getAToZWavelengthNumber() != null) {
125             servicePathInputBuilder
126                 .setWaveNumber(atoZDirection.getAToZWavelengthNumber());
127         }
128         if (Uint32.valueOf(GridConstant.IRRELEVANT_WAVELENGTH_NUMBER)
129                 .equals(atoZDirection.getAToZWavelengthNumber())) {
130             scale = GridConstant.FLEX_GRID_FREQUENCY_PRECISION;
131         }
132         if (atoZDirection.getAToZMinFrequency() != null) {
133             servicePathInputBuilder.setMinFreq(new FrequencyTHz(atoZDirection.getAToZMinFrequency().getValue()));
134             servicePathInputBuilder.setLowerSpectralSlotNumber(Uint32
135                     .valueOf(GridUtils
136                             .getLowerSpectralIndexFromFrequency(atoZDirection.getAToZMinFrequency().getValue())));
137         }
138         if (atoZDirection.getAToZMaxFrequency() != null) {
139             servicePathInputBuilder.setMaxFreq(new FrequencyTHz(atoZDirection.getAToZMaxFrequency().getValue()));
140             servicePathInputBuilder.setHigherSpectralSlotNumber(
141                     Uint32.valueOf(GridUtils
142                             .getHigherSpectralIndexFromFrequency(atoZDirection.getAToZMaxFrequency().getValue())));
143         }
144         if (atoZDirection.getAToZMinFrequency() != null && atoZDirection.getAToZMaxFrequency() != null) {
145             servicePathInputBuilder.setCenterFreq(
146                     GridUtils.getCentralFrequencyWithPrecision(atoZDirection.getAToZMinFrequency().getValue(),
147                             atoZDirection.getAToZMaxFrequency().getValue(), scale));
148         }
149         if (atoZDirection.getRate() != null && atoZDirection.getModulationFormat() != null) {
150             Optional<ModulationFormat> optionalModulationFormat = ModulationFormat
151                     .forName(atoZDirection.getModulationFormat());
152             if (optionalModulationFormat.isPresent()
153                     && GridConstant.FREQUENCY_WIDTH_TABLE
154                     .contains(atoZDirection.getRate(), optionalModulationFormat.get())) {
155                 servicePathInputBuilder
156                     .setWidth(FrequencyGHz
157                         .getDefaultInstance(GridConstant.FREQUENCY_WIDTH_TABLE.get(atoZDirection.getRate(),
158                         optionalModulationFormat.get())));
159             }
160         }
161         servicePathInputBuilder.setModulationFormat(atoZDirection.getModulationFormat());
162         return new ServicePathInputData(servicePathInputBuilder.build(), nodeLists);
163     }
164
165     public static ServicePathInputData rendererCreateServiceInputZToA(String serviceName,
166             PathDescription pathDescription) {
167         int scale = GridConstant.FIXED_GRID_FREQUENCY_PRECISION;
168         ZToADirection ztoADirection = pathDescription.getZToADirection();
169         LOG.info("Building ServicePathInputData for z to a direction {}", ztoADirection);
170         NodeLists nodeLists = getNodesListZtoA(pathDescription.getZToADirection().nonnullZToA().values().iterator());
171         ServicePathInputBuilder servicePathInputBuilder = new ServicePathInputBuilder()
172             .setOperation(ServicePathInput.Operation.Create)
173             .setServiceName(serviceName)
174             .setNodes(nodeLists.getList())
175             .setWidth(new FrequencyGHz(GridConstant.WIDTH_40));
176         if (ztoADirection.getZToAWavelengthNumber() != null) {
177             servicePathInputBuilder
178                 .setWaveNumber(ztoADirection.getZToAWavelengthNumber());
179         }
180         if (Uint32.valueOf(GridConstant.IRRELEVANT_WAVELENGTH_NUMBER)
181                 .equals(ztoADirection.getZToAWavelengthNumber())) {
182             scale = GridConstant.FLEX_GRID_FREQUENCY_PRECISION;
183         }
184         if (ztoADirection.getZToAMinFrequency() != null) {
185             servicePathInputBuilder.setMinFreq(new FrequencyTHz(ztoADirection.getZToAMinFrequency().getValue()));
186             servicePathInputBuilder.setLowerSpectralSlotNumber(Uint32
187                     .valueOf(GridUtils
188                             .getLowerSpectralIndexFromFrequency(ztoADirection.getZToAMinFrequency().getValue())));
189         }
190         if (ztoADirection.getZToAMaxFrequency() != null) {
191             servicePathInputBuilder.setMaxFreq(new FrequencyTHz(ztoADirection.getZToAMaxFrequency().getValue()));
192             servicePathInputBuilder.setHigherSpectralSlotNumber(
193                     Uint32.valueOf(GridUtils
194                             .getHigherSpectralIndexFromFrequency(ztoADirection.getZToAMaxFrequency().getValue())));
195         }
196         if (ztoADirection.getZToAMinFrequency() != null && ztoADirection.getZToAMaxFrequency() != null) {
197             servicePathInputBuilder.setCenterFreq(
198                     GridUtils.getCentralFrequencyWithPrecision(ztoADirection.getZToAMinFrequency().getValue(),
199                             ztoADirection.getZToAMaxFrequency().getValue(), scale));
200         }
201         if (ztoADirection.getRate() != null && ztoADirection.getModulationFormat() != null) {
202             Optional<ModulationFormat> optionalModulationFormat = ModulationFormat
203                     .forName(ztoADirection.getModulationFormat());
204             if (optionalModulationFormat.isPresent()
205                     && GridConstant.FREQUENCY_WIDTH_TABLE
206                     .contains(ztoADirection.getRate(), optionalModulationFormat.get())) {
207                 servicePathInputBuilder.setWidth(FrequencyGHz
208                         .getDefaultInstance(GridConstant.FREQUENCY_WIDTH_TABLE.get(ztoADirection.getRate(),
209                                 optionalModulationFormat.get())));
210             }
211         }
212         servicePathInputBuilder.setModulationFormat(ztoADirection.getModulationFormat());
213         return new ServicePathInputData(servicePathInputBuilder.build(), nodeLists);
214     }
215
216     // Adding createOtnServiceInputpath for A-Z and Z-A directions as one method
217     public static OtnServicePathInput rendererCreateOtnServiceInput(String serviceName, String serviceType,
218         String serviceRate, PathDescription pathDescription, boolean asideToZside) {
219         // If atoZ is set true use A-to-Z direction otherwise use Z-to-A
220         List<org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev201211.otn.renderer.input.Nodes> nodes =
221             new ArrayList<>();
222         NodeLists nodeLists = getNodesListAToZ(pathDescription.getAToZDirection().nonnullAToZ().values().iterator());
223         if (!asideToZside) {
224             nodeLists = getNodesListZtoA(pathDescription.getZToADirection().nonnullZToA().values().iterator());
225         }
226         LOG.info("These are node-lists {}, {}", nodeLists.getList(), nodeLists.getOlmList());
227         for (Nodes node: nodeLists.getList()) {
228             nodes.add(
229                     new org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev201211.otn.renderer.input
230                         .NodesBuilder()
231                             .setNodeId(node.getNodeId())
232                             .setClientTp(node.getSrcTp())
233                             .setNetworkTp(node.getDestTp())
234                             .build());
235         }
236         OtnServicePathInputBuilder otnServicePathInputBuilder = new OtnServicePathInputBuilder()
237             .setServiceName(serviceName)
238             .setServiceType(serviceType)
239             .setServiceRate(serviceRate)
240             .setNodes(nodes);
241
242         // set the trib-slots and trib-ports for the lower oder odu
243         if (serviceRate.equals("1G") || (serviceRate.equals("10G"))) {
244             otnServicePathInputBuilder
245                 .setTribPortNumber(pathDescription.getAToZDirection().getTribPortNumber().shortValue())
246                 .setTribSlot(pathDescription.getAToZDirection().getTribSlotNumber().shortValue());
247         }
248         return otnServicePathInputBuilder.build();
249     }
250
251     public static ServicePathInput rendererDeleteServiceInput(String serviceName,
252             ServiceDeleteInput serviceDeleteInput) {
253         //TODO: finish model-model mapping
254         return new ServicePathInputBuilder().setServiceName(serviceName).build();
255     }
256
257     private static NodeLists getNodesListZtoA(Iterator<ZToA> iterator) {
258         Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
259         List<Nodes> olmList = new ArrayList<>();
260         List<Nodes> list = new ArrayList<>();
261         String resourceType;
262         TerminationPoint tp;
263         String tpID = "";
264         String nodeID = "";
265         String sortId = "";
266         while (iterator.hasNext()) {
267             ZToA pathDesObj = iterator.next();
268             resourceType = pathDesObj.getResource().getResource().implementedInterface().getSimpleName();
269             LOG.info("Inside ZtoA {}", resourceType);
270
271             try {
272                 if (TERMINATION_POINT.equals(resourceType)) {
273                     tp = (TerminationPoint) pathDesObj.getResource().getResource();
274                     LOG.info(" TP is {} {}", tp.getTpId(),
275                             tp.getTpNodeId());
276                     tpID = tp.getTpId();
277                     sortId = pathDesObj.getId();
278
279                     //TODO: do not rely on ID to be in certain format
280                     if (tpID.contains("CTP") || tpID.contains("CP")) {
281                         continue;
282                     }
283                     if (tpID.contains(StringConstants.TTP_TOKEN)) {
284                         nodeID = tp.getTpNodeId().split("-DEG")[0];
285                     } else if (tpID.contains(StringConstants.PP_TOKEN)) {
286                         nodeID = tp.getTpNodeId().split("-SRG")[0];
287                     } else if (tpID.contains(StringConstants.NETWORK_TOKEN)
288                         || tpID.contains(StringConstants.CLIENT_TOKEN) || tpID.isEmpty()) {
289                         nodeID = tp.getTpNodeId().split("-XPDR")[0];
290                     } else {
291                         continue;
292                     }
293                     int id = Integer.parseInt(sortId);
294                     treeMap.put(id, new NodeIdPair(nodeID, tpID));
295                 } else if ("Link".equals(resourceType)) {
296                     LOG.info("The type is link");
297                 } else {
298                     LOG.info("The type is not identified: {}", resourceType);
299                 }
300             } catch (IllegalArgumentException | SecurityException e) {
301                 LOG.error("Dont find the getResource method", e);
302             }
303         }
304         populateNodeLists(treeMap, list, olmList, false);
305         return new NodeLists(olmList, list);
306     }
307
308     private static NodeLists getNodesListAToZ(Iterator<AToZ> iterator) {
309         Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
310         List<Nodes> list = new ArrayList<>();
311         List<Nodes> olmList = new ArrayList<>();
312         String resourceType;
313         TerminationPoint tp;
314         String tpID = "";
315         String nodeID = "";
316         String sortId = "";
317
318         while (iterator.hasNext()) {
319             AToZ pathDesObj = iterator.next();
320             resourceType = pathDesObj.getResource().getResource().implementedInterface().getSimpleName();
321             LOG.info("Inside AtoZ {}", resourceType);
322             try {
323                 if (TERMINATION_POINT.equals(resourceType)) {
324                     tp = (TerminationPoint) pathDesObj.getResource().getResource();
325                     LOG.info("TP is {} {}", tp.getTpId(),
326                             tp.getTpNodeId());
327                     tpID = tp.getTpId();
328                     sortId = pathDesObj.getId();
329
330                     //TODO: do not rely on ID to be in certain format
331                     if (tpID.contains("CTP") || tpID.contains("CP")) {
332                         continue;
333                     }
334                     if (tpID.contains(StringConstants.TTP_TOKEN)) {
335                         nodeID = tp.getTpNodeId().split("-DEG")[0];
336                     } else if (tpID.contains(StringConstants.PP_TOKEN)) {
337                         nodeID = tp.getTpNodeId().split("-SRG")[0];
338                     } else if (tpID.contains(StringConstants.NETWORK_TOKEN)
339                         || tpID.contains(StringConstants.CLIENT_TOKEN) || tpID.isEmpty()) {
340                         nodeID = tp.getTpNodeId().split("-XPDR")[0];
341                     } else {
342                         continue;
343                     }
344                     int id = Integer.parseInt(sortId);
345                     treeMap.put(id, new NodeIdPair(nodeID, tpID));
346                 } else if ("Link".equals(resourceType)) {
347                     LOG.info("The type is link");
348                 } else {
349                     LOG.info("The type is not identified: {}", resourceType);
350                 }
351             } catch (IllegalArgumentException | SecurityException e) {
352                 //TODO: Auto-generated catch block
353                 LOG.error("Did not find the getResource method", e);
354             }
355         }
356         populateNodeLists(treeMap, list, olmList, true);
357         return new NodeLists(olmList, list);
358     }
359
360     @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
361         value = {"NP_LOAD_OF_KNOWN_NULL_VALUE","RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE"},
362         justification = "loop when value is not always null - "
363                 + "TODO: check if something exists in Java lib")
364     private static void populateNodeLists(Map<Integer, NodeIdPair> treeMap, List<Nodes> list, List<Nodes> olmList,
365         boolean isAToz) {
366         String desID = null;
367         String srcID = null;
368         LOG.info("treeMap values = {}", treeMap.values());
369         for (NodeIdPair values : treeMap.values()) {
370             if (srcID == null) {
371                 srcID = values.getTpID();
372             } else if (desID == null) {
373                 desID = values.getTpID();
374                 NodesBuilder olmNb = new NodesBuilder()
375                     .setNodeId(values.getNodeID())
376                     .setDestTp(desID)
377                     .setSrcTp(srcID);
378                 olmList.add(olmNb.build());
379                 if (srcID.isEmpty()) {
380                     srcID = null;
381                 }
382                 if (desID.isEmpty()) {
383                     desID = new StringBuilder(srcID).toString();
384                     srcID = null;
385                 }
386                 if (isAToz) {
387                     NodesBuilder nb = new NodesBuilder()
388                         .withKey(new NodesKey(values.getNodeID()))
389                         .setDestTp(desID)
390                         .setSrcTp(srcID);
391                     if (srcID != null && desID != null && srcID.contains(StringConstants.NETWORK_TOKEN)) {
392                         nb.setDestTp(srcID).setSrcTp(desID);
393                     }
394                     list.add(nb.build());
395                 } else {
396                     if (srcID != null && desID != null && !srcID.contains(StringConstants.NETWORK_TOKEN)
397                         && !desID.contains(StringConstants.NETWORK_TOKEN)) {
398                         NodesBuilder nb = new NodesBuilder()
399                             .withKey(new NodesKey(values.getNodeID()))
400                             .setDestTp(desID)
401                             .setSrcTp(srcID);
402                         list.add(nb.build());
403                     }
404                 }
405                 srcID = null;
406                 desID = null;
407             } else {
408                 LOG.warn("both, the source and destination id are null!");
409             }
410         }
411     }
412
413
414     @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
415             value = "PZLA_PREFER_ZERO_LENGTH_ARRAYS",
416             justification = "not relevant to return and zero length array"
417                     + " as we need real pos")
418     public static int[] findTheLongestSubstring(String s1, String s2) {
419         if ((s1 == null) || (s2 == null)) {
420             return null;
421         }
422         int[][] dp = new int[s1.length() + 1][s2.length() + 1];
423         int maxLen = 0;
424         int endPos = 0;
425         for (int i = 1; i < dp.length; i++) {
426             for (int j = 1; j < dp[0].length; j++) {
427                 char ch1 = s1.charAt(i - 1);
428                 char ch2 = s2.charAt(j - 1);
429                 if (ch1 == ch2) {
430                     dp[i][j] = dp[i - 1][j - 1] + 1;
431                     if (dp[i][j] >= maxLen) {
432                         maxLen = dp[i][j];
433                         endPos = i;
434                     }
435                 }
436             }
437         }
438         return new int[] { endPos - maxLen, endPos };
439     }
440
441 }