2 * Copyright © 2017 AT&T and others. All rights reserved.
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
8 package org.opendaylight.transportpce.renderer;
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.ArrayList;
12 import java.util.Iterator;
13 import java.util.List;
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.rev210618.Action;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev210618.OtnServicePathInput;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev210618.OtnServicePathInputBuilder;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev210618.ServicePathInput;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev210618.ServicePathInputBuilder;
26 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupInput;
27 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupInputBuilder;
28 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210618.ServiceDeleteOutput;
29 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210618.ServiceDeleteOutputBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210618.ServiceImplementationRequestInput;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210618.ServiceImplementationRequestOutput;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210618.ServiceImplementationRequestOutputBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.configuration.response.common.ConfigurationResponseCommon;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.configuration.response.common.ConfigurationResponseCommonBuilder;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.FrequencyGHz;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.FrequencyTHz;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev181019.ModulationFormat;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.ServiceDeleteInput;
39 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.PathDescription;
40 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.AToZDirection;
41 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.ZToADirection;
42 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.atoz.direction.AToZ;
43 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.ztoa.direction.ZToA;
44 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.pce.resource.resource.resource.TerminationPoint;
45 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210618.optical.renderer.nodes.Nodes;
46 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210618.optical.renderer.nodes.NodesBuilder;
47 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210618.optical.renderer.nodes.NodesKey;
48 import org.opendaylight.yangtools.yang.common.RpcResult;
49 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
50 import org.opendaylight.yangtools.yang.common.Uint32;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
56 public final class ModelMappingUtils {
58 private static final Logger LOG = LoggerFactory.getLogger(ModelMappingUtils.class);
59 private static final String TERMINATION_POINT = "TerminationPoint";
61 private ModelMappingUtils() {
64 public static ServicePowerSetupInput createServicePowerSetupInput(List<Nodes> olmList,
65 ServiceImplementationRequestInput input) {
66 ServicePowerSetupInputBuilder olmSetupBldr = new ServicePowerSetupInputBuilder().setNodes(olmList);
67 if (input != null && input.getPathDescription() != null
68 && input.getPathDescription().getAToZDirection() != null) {
69 AToZDirection atoZDirection = input.getPathDescription().getAToZDirection();
70 olmSetupBldr.setWaveNumber(atoZDirection.getAToZWavelengthNumber());
71 if (atoZDirection.getAToZMinFrequency() != null) {
72 olmSetupBldr.setLowerSpectralSlotNumber(Uint32
74 .getLowerSpectralIndexFromFrequency(atoZDirection.getAToZMinFrequency().getValue())));
76 if (atoZDirection.getAToZMaxFrequency() != null) {
77 olmSetupBldr.setHigherSpectralSlotNumber(Uint32
79 .getHigherSpectralIndexFromFrequency(atoZDirection.getAToZMaxFrequency().getValue())));
82 return olmSetupBldr.build();
85 public static ServiceImplementationRequestOutput createServiceImplResponse(String responseCode, String message) {
86 return new ServiceImplementationRequestOutputBuilder()
87 .setConfigurationResponseCommon(createCommonResponse(responseCode, message))
91 public static ServiceDeleteOutput createServiceDeleteResponse(String responseCode, String message) {
92 return new ServiceDeleteOutputBuilder()
93 .setConfigurationResponseCommon(createCommonResponse(responseCode, message))
97 public static ConfigurationResponseCommon createCommonResponse(String responseCode, String message) {
98 return new ConfigurationResponseCommonBuilder()
99 .setResponseMessage(message)
100 .setResponseCode(responseCode)
104 public static ListenableFuture<RpcResult<ServiceImplementationRequestOutput>>
105 createServiceImplementationRpcResponse(ServiceImplementationRequestOutput payload) {
106 return RpcResultBuilder.success(payload).buildFuture();
109 public static ListenableFuture<RpcResult<ServiceDeleteOutput>>
110 createServiceDeleteRpcResponse(ServiceDeleteOutput payload) {
111 return RpcResultBuilder.success(payload).buildFuture();
114 public static ServicePathInputData rendererCreateServiceInputAToZ(String serviceName,
115 PathDescription pathDescription, Action operation) {
116 int scale = GridConstant.FIXED_GRID_FREQUENCY_PRECISION;
117 AToZDirection atoZDirection = pathDescription.getAToZDirection();
118 LOG.info("Building ServicePathInputData for a to z direction {}", atoZDirection);
119 NodeLists nodeLists = getNodesListAToZ(atoZDirection.nonnullAToZ().values().iterator());
120 ServicePathInputBuilder servicePathInputBuilder = new ServicePathInputBuilder()
121 .setServiceName(serviceName)
122 .setOperation(operation)
123 .setNodes(nodeLists.getRendererNodeList())
124 .setWidth(new FrequencyGHz(GridConstant.WIDTH_40));
125 if (atoZDirection.getAToZWavelengthNumber() != null) {
126 servicePathInputBuilder
127 .setWaveNumber(atoZDirection.getAToZWavelengthNumber());
129 if (Uint32.valueOf(GridConstant.IRRELEVANT_WAVELENGTH_NUMBER)
130 .equals(atoZDirection.getAToZWavelengthNumber())) {
131 scale = GridConstant.FLEX_GRID_FREQUENCY_PRECISION;
133 if (atoZDirection.getAToZMinFrequency() != null) {
134 servicePathInputBuilder.setMinFreq(new FrequencyTHz(atoZDirection.getAToZMinFrequency().getValue()));
135 servicePathInputBuilder.setLowerSpectralSlotNumber(Uint32
137 .getLowerSpectralIndexFromFrequency(atoZDirection.getAToZMinFrequency().getValue())));
139 if (atoZDirection.getAToZMaxFrequency() != null) {
140 servicePathInputBuilder.setMaxFreq(new FrequencyTHz(atoZDirection.getAToZMaxFrequency().getValue()));
141 servicePathInputBuilder.setHigherSpectralSlotNumber(
142 Uint32.valueOf(GridUtils
143 .getHigherSpectralIndexFromFrequency(atoZDirection.getAToZMaxFrequency().getValue())));
145 if (atoZDirection.getAToZMinFrequency() != null && atoZDirection.getAToZMaxFrequency() != null) {
146 servicePathInputBuilder.setCenterFreq(
147 GridUtils.getCentralFrequencyWithPrecision(atoZDirection.getAToZMinFrequency().getValue(),
148 atoZDirection.getAToZMaxFrequency().getValue(), scale));
150 if (atoZDirection.getRate() != null && atoZDirection.getModulationFormat() != null) {
151 Optional<ModulationFormat> optionalModulationFormat = ModulationFormat
152 .forName(atoZDirection.getModulationFormat());
153 if (optionalModulationFormat.isPresent()
154 && GridConstant.FREQUENCY_WIDTH_TABLE
155 .contains(atoZDirection.getRate(), optionalModulationFormat.get())) {
156 servicePathInputBuilder
157 .setWidth(FrequencyGHz
158 .getDefaultInstance(GridConstant.FREQUENCY_WIDTH_TABLE.get(atoZDirection.getRate(),
159 optionalModulationFormat.get())));
162 servicePathInputBuilder.setModulationFormat(atoZDirection.getModulationFormat());
163 return new ServicePathInputData(servicePathInputBuilder.build(), nodeLists);
166 public static ServicePathInputData rendererCreateServiceInputZToA(String serviceName,
167 PathDescription pathDescription, Action operation) {
168 int scale = GridConstant.FIXED_GRID_FREQUENCY_PRECISION;
169 ZToADirection ztoADirection = pathDescription.getZToADirection();
170 LOG.info("Building ServicePathInputData for z to a direction {}", ztoADirection);
171 NodeLists nodeLists = getNodesListZtoA(pathDescription.getZToADirection().nonnullZToA().values().iterator());
172 ServicePathInputBuilder servicePathInputBuilder = new ServicePathInputBuilder()
173 .setOperation(operation)
174 .setServiceName(serviceName)
175 .setNodes(nodeLists.getRendererNodeList())
176 .setWidth(new FrequencyGHz(GridConstant.WIDTH_40));
177 if (ztoADirection.getZToAWavelengthNumber() != null) {
178 servicePathInputBuilder
179 .setWaveNumber(ztoADirection.getZToAWavelengthNumber());
181 if (Uint32.valueOf(GridConstant.IRRELEVANT_WAVELENGTH_NUMBER)
182 .equals(ztoADirection.getZToAWavelengthNumber())) {
183 scale = GridConstant.FLEX_GRID_FREQUENCY_PRECISION;
185 if (ztoADirection.getZToAMinFrequency() != null) {
186 servicePathInputBuilder.setMinFreq(new FrequencyTHz(ztoADirection.getZToAMinFrequency().getValue()));
187 servicePathInputBuilder.setLowerSpectralSlotNumber(Uint32
189 .getLowerSpectralIndexFromFrequency(ztoADirection.getZToAMinFrequency().getValue())));
191 if (ztoADirection.getZToAMaxFrequency() != null) {
192 servicePathInputBuilder.setMaxFreq(new FrequencyTHz(ztoADirection.getZToAMaxFrequency().getValue()));
193 servicePathInputBuilder.setHigherSpectralSlotNumber(
194 Uint32.valueOf(GridUtils
195 .getHigherSpectralIndexFromFrequency(ztoADirection.getZToAMaxFrequency().getValue())));
197 if (ztoADirection.getZToAMinFrequency() != null && ztoADirection.getZToAMaxFrequency() != null) {
198 servicePathInputBuilder.setCenterFreq(
199 GridUtils.getCentralFrequencyWithPrecision(ztoADirection.getZToAMinFrequency().getValue(),
200 ztoADirection.getZToAMaxFrequency().getValue(), scale));
202 if (ztoADirection.getRate() != null && ztoADirection.getModulationFormat() != null) {
203 Optional<ModulationFormat> optionalModulationFormat = ModulationFormat
204 .forName(ztoADirection.getModulationFormat());
205 if (optionalModulationFormat.isPresent()
206 && GridConstant.FREQUENCY_WIDTH_TABLE
207 .contains(ztoADirection.getRate(), optionalModulationFormat.get())) {
208 servicePathInputBuilder.setWidth(FrequencyGHz
209 .getDefaultInstance(GridConstant.FREQUENCY_WIDTH_TABLE.get(ztoADirection.getRate(),
210 optionalModulationFormat.get())));
213 servicePathInputBuilder.setModulationFormat(ztoADirection.getModulationFormat());
214 return new ServicePathInputData(servicePathInputBuilder.build(), nodeLists);
217 // Adding createOtnServiceInputpath for A-Z and Z-A directions as one method
218 public static OtnServicePathInput rendererCreateOtnServiceInput(String serviceName, Action operation,
219 String serviceFormat, Uint32 serviceRate, PathDescription pathDescription, boolean asideToZside) {
220 // If atoZ is set true use A-to-Z direction otherwise use Z-to-A
221 List<org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210618.otn.renderer.nodes.Nodes> nodes =
225 nodeLists = getNodesListAToZ(pathDescription.getAToZDirection().nonnullAToZ().values().iterator());
227 nodeLists = getNodesListZtoA(pathDescription.getZToADirection().nonnullZToA().values().iterator());
229 LOG.info("These are node-lists {}, {}", nodeLists.getRendererNodeList(), nodeLists.getOlmNodeList());
230 for (Nodes node: nodeLists.getRendererNodeList()) {
231 nodes.add(new org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210618.otn.renderer.nodes
233 .setNodeId(node.getNodeId())
234 .setClientTp(node.getSrcTp())
235 .setNetworkTp(node.getDestTp())
238 OtnServicePathInputBuilder otnServicePathInputBuilder = new OtnServicePathInputBuilder()
239 .setServiceName(serviceName)
240 .setOperation(operation)
241 .setServiceFormat(serviceFormat)
242 .setServiceRate(serviceRate)
245 // set the trib-slots and trib-ports for the lower oder odu
246 if (serviceRate.intValue() == 1 || (serviceRate.intValue() == 10)) {
247 Short tribPort = Short.valueOf(pathDescription.getAToZDirection().getMinTribSlot().getValue()
249 Short minTribSlot = Short.valueOf(pathDescription.getAToZDirection().getMinTribSlot().getValue()
251 otnServicePathInputBuilder
252 .setTribPortNumber(tribPort)
253 .setTribSlot(minTribSlot);
255 return otnServicePathInputBuilder.build();
258 public static ServicePathInput rendererDeleteServiceInput(String serviceName,
259 ServiceDeleteInput serviceDeleteInput) {
260 //TODO: finish model-model mapping
261 return new ServicePathInputBuilder().setServiceName(serviceName).build();
264 private static NodeLists getNodesListZtoA(Iterator<ZToA> iterator) {
265 Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
266 List<Nodes> olmList = new ArrayList<>();
267 List<Nodes> list = new ArrayList<>();
273 while (iterator.hasNext()) {
274 ZToA pathDesObj = iterator.next();
275 resourceType = pathDesObj.getResource().getResource().implementedInterface().getSimpleName();
276 LOG.info("Inside ZtoA {}", resourceType);
279 if (TERMINATION_POINT.equals(resourceType)) {
280 tp = (TerminationPoint) pathDesObj.getResource().getResource();
281 LOG.info(" TP is {} {}", tp.getTpId(),
284 sortId = pathDesObj.getId();
286 //TODO: do not rely on ID to be in certain format
287 if (tpID.contains("CTP") || tpID.contains("CP")) {
290 if (tpID.contains(StringConstants.TTP_TOKEN)) {
291 nodeID = tp.getTpNodeId().split("-DEG")[0];
292 } else if (tpID.contains(StringConstants.PP_TOKEN)) {
293 nodeID = tp.getTpNodeId().split("-SRG")[0];
294 } else if (tpID.contains(StringConstants.NETWORK_TOKEN)
295 || tpID.contains(StringConstants.CLIENT_TOKEN) || tpID.isEmpty()) {
296 nodeID = tp.getTpNodeId().split("-XPDR")[0];
300 int id = Integer.parseInt(sortId);
301 treeMap.put(id, new NodeIdPair(nodeID, tpID));
302 } else if ("Link".equals(resourceType)) {
303 LOG.info("The type is link");
305 LOG.info("The type is not identified: {}", resourceType);
307 } catch (IllegalArgumentException | SecurityException e) {
308 LOG.error("Dont find the getResource method", e);
311 populateNodeLists(treeMap, list, olmList, false);
312 return new NodeLists(olmList, list);
315 private static NodeLists getNodesListAToZ(Iterator<AToZ> iterator) {
316 Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
317 List<Nodes> list = new ArrayList<>();
318 List<Nodes> olmList = new ArrayList<>();
325 while (iterator.hasNext()) {
326 AToZ pathDesObj = iterator.next();
327 resourceType = pathDesObj.getResource().getResource().implementedInterface().getSimpleName();
328 LOG.info("Inside AtoZ {}", resourceType);
330 if (TERMINATION_POINT.equals(resourceType)) {
331 tp = (TerminationPoint) pathDesObj.getResource().getResource();
332 LOG.info("TP is {} {}", tp.getTpId(),
335 sortId = pathDesObj.getId();
337 //TODO: do not rely on ID to be in certain format
338 if (tpID.contains("CTP") || tpID.contains("CP")) {
341 if (tpID.contains(StringConstants.TTP_TOKEN)) {
342 nodeID = tp.getTpNodeId().split("-DEG")[0];
343 } else if (tpID.contains(StringConstants.PP_TOKEN)) {
344 nodeID = tp.getTpNodeId().split("-SRG")[0];
345 } else if (tpID.contains(StringConstants.NETWORK_TOKEN)
346 || tpID.contains(StringConstants.CLIENT_TOKEN) || tpID.isEmpty()) {
347 nodeID = tp.getTpNodeId().split("-XPDR")[0];
351 int id = Integer.parseInt(sortId);
352 treeMap.put(id, new NodeIdPair(nodeID, tpID));
353 } else if ("Link".equals(resourceType)) {
354 LOG.info("The type is link");
356 LOG.info("The type is not identified: {}", resourceType);
358 } catch (IllegalArgumentException | SecurityException e) {
359 //TODO: Auto-generated catch block
360 LOG.error("Did not find the getResource method", e);
363 populateNodeLists(treeMap, list, olmList, true);
364 return new NodeLists(olmList, list);
367 @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
368 value = {"NP_LOAD_OF_KNOWN_NULL_VALUE","RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE"},
369 justification = "loop when value is not always null - "
370 + "TODO: check if something exists in Java lib")
371 private static void populateNodeLists(Map<Integer, NodeIdPair> treeMap, List<Nodes> list, List<Nodes> olmList,
375 LOG.info("treeMap values = {}", treeMap.values());
376 for (NodeIdPair values : treeMap.values()) {
378 srcID = values.getTpID();
379 } else if (desID == null) {
380 desID = values.getTpID();
381 NodesBuilder olmNb = new NodesBuilder()
382 .setNodeId(values.getNodeID())
385 olmList.add(olmNb.build());
386 if (srcID.isEmpty()) {
389 if (desID.isEmpty()) {
390 desID = new StringBuilder(srcID).toString();
394 NodesBuilder nb = new NodesBuilder()
395 .withKey(new NodesKey(values.getNodeID()))
398 if (srcID != null && desID != null && srcID.contains(StringConstants.NETWORK_TOKEN)) {
399 nb.setDestTp(srcID).setSrcTp(desID);
401 list.add(nb.build());
403 if (srcID != null && desID != null && !srcID.contains(StringConstants.NETWORK_TOKEN)
404 && !desID.contains(StringConstants.NETWORK_TOKEN)) {
405 NodesBuilder nb = new NodesBuilder()
406 .withKey(new NodesKey(values.getNodeID()))
409 list.add(nb.build());
415 LOG.warn("both, the source and destination id are null!");
421 @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
422 value = "PZLA_PREFER_ZERO_LENGTH_ARRAYS",
423 justification = "not relevant to return and zero length array"
424 + " as we need real pos")
425 public static int[] findTheLongestSubstring(String s1, String s2) {
426 if ((s1 == null) || (s2 == null)) {
429 int[][] dp = new int[s1.length() + 1][s2.length() + 1];
432 for (int i = 1; i < dp.length; i++) {
433 for (int j = 1; j < dp[0].length; j++) {
434 char ch1 = s1.charAt(i - 1);
435 char ch2 = s2.charAt(j - 1);
437 dp[i][j] = dp[i - 1][j - 1] + 1;
438 if (dp[i][j] >= maxLen) {
445 return new int[] { endPos - maxLen, endPos };