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.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.rev210705.PathDescription;
39 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.AToZDirection;
40 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.ZToADirection;
41 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.atoz.direction.AToZ;
42 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.ztoa.direction.ZToA;
43 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.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;
55 public final class ModelMappingUtils {
57 private static final Logger LOG = LoggerFactory.getLogger(ModelMappingUtils.class);
58 private static final String TERMINATION_POINT = "TerminationPoint";
60 private ModelMappingUtils() {
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
73 .getLowerSpectralIndexFromFrequency(atoZDirection.getAToZMinFrequency().getValue())));
75 if (atoZDirection.getAToZMaxFrequency() != null) {
76 olmSetupBldr.setHigherSpectralSlotNumber(Uint32
78 .getHigherSpectralIndexFromFrequency(atoZDirection.getAToZMaxFrequency().getValue())));
81 return olmSetupBldr.build();
84 public static ServiceImplementationRequestOutput createServiceImplResponse(String responseCode, String message) {
85 return new ServiceImplementationRequestOutputBuilder()
86 .setConfigurationResponseCommon(createCommonResponse(responseCode, message))
90 public static ServiceDeleteOutput createServiceDeleteResponse(String responseCode, String message) {
91 return new ServiceDeleteOutputBuilder()
92 .setConfigurationResponseCommon(createCommonResponse(responseCode, message))
96 public static ConfigurationResponseCommon createCommonResponse(String responseCode, String message) {
97 return new ConfigurationResponseCommonBuilder()
98 .setResponseMessage(message)
99 .setResponseCode(responseCode)
103 public static ListenableFuture<RpcResult<ServiceImplementationRequestOutput>>
104 createServiceImplementationRpcResponse(ServiceImplementationRequestOutput payload) {
105 return RpcResultBuilder.success(payload).buildFuture();
108 public static ListenableFuture<RpcResult<ServiceDeleteOutput>>
109 createServiceDeleteRpcResponse(ServiceDeleteOutput payload) {
110 return RpcResultBuilder.success(payload).buildFuture();
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());
128 if (Uint32.valueOf(GridConstant.IRRELEVANT_WAVELENGTH_NUMBER)
129 .equals(atoZDirection.getAToZWavelengthNumber())) {
130 scale = GridConstant.FLEX_GRID_FREQUENCY_PRECISION;
132 if (atoZDirection.getAToZMinFrequency() != null) {
133 servicePathInputBuilder.setMinFreq(new FrequencyTHz(atoZDirection.getAToZMinFrequency().getValue()));
134 servicePathInputBuilder.setLowerSpectralSlotNumber(Uint32
136 .getLowerSpectralIndexFromFrequency(atoZDirection.getAToZMinFrequency().getValue())));
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())));
144 if (atoZDirection.getAToZMinFrequency() != null && atoZDirection.getAToZMaxFrequency() != null) {
145 servicePathInputBuilder.setCenterFreq(
146 GridUtils.getCentralFrequencyWithPrecision(atoZDirection.getAToZMinFrequency().getValue(),
147 atoZDirection.getAToZMaxFrequency().getValue(), scale));
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())));
161 servicePathInputBuilder.setModulationFormat(atoZDirection.getModulationFormat());
162 return new ServicePathInputData(servicePathInputBuilder.build(), nodeLists);
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());
180 if (Uint32.valueOf(GridConstant.IRRELEVANT_WAVELENGTH_NUMBER)
181 .equals(ztoADirection.getZToAWavelengthNumber())) {
182 scale = GridConstant.FLEX_GRID_FREQUENCY_PRECISION;
184 if (ztoADirection.getZToAMinFrequency() != null) {
185 servicePathInputBuilder.setMinFreq(new FrequencyTHz(ztoADirection.getZToAMinFrequency().getValue()));
186 servicePathInputBuilder.setLowerSpectralSlotNumber(Uint32
188 .getLowerSpectralIndexFromFrequency(ztoADirection.getZToAMinFrequency().getValue())));
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())));
196 if (ztoADirection.getZToAMinFrequency() != null && ztoADirection.getZToAMaxFrequency() != null) {
197 servicePathInputBuilder.setCenterFreq(
198 GridUtils.getCentralFrequencyWithPrecision(ztoADirection.getZToAMinFrequency().getValue(),
199 ztoADirection.getZToAMaxFrequency().getValue(), scale));
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())));
212 servicePathInputBuilder.setModulationFormat(ztoADirection.getModulationFormat());
213 return new ServicePathInputData(servicePathInputBuilder.build(), nodeLists);
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 =
222 NodeLists nodeLists = getNodesListAToZ(pathDescription.getAToZDirection().nonnullAToZ().values().iterator());
224 nodeLists = getNodesListZtoA(pathDescription.getZToADirection().nonnullZToA().values().iterator());
226 LOG.info("These are node-lists {}, {}", nodeLists.getList(), nodeLists.getOlmList());
227 for (Nodes node: nodeLists.getList()) {
229 new org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev201211.otn.renderer.input
231 .setNodeId(node.getNodeId())
232 .setClientTp(node.getSrcTp())
233 .setNetworkTp(node.getDestTp())
236 OtnServicePathInputBuilder otnServicePathInputBuilder = new OtnServicePathInputBuilder()
237 .setServiceName(serviceName)
238 .setServiceType(serviceType)
239 .setServiceRate(serviceRate)
242 // set the trib-slots and trib-ports for the lower oder odu
243 if (serviceRate.equals("1G") || (serviceRate.equals("10G"))) {
244 Short tribPort = Short.valueOf(pathDescription.getAToZDirection().getMinTribSlot().getValue()
246 Short minTribSlot = Short.valueOf(pathDescription.getAToZDirection().getMinTribSlot().getValue()
248 otnServicePathInputBuilder
249 .setTribPortNumber(tribPort)
250 .setTribSlot(minTribSlot);
252 return otnServicePathInputBuilder.build();
255 public static ServicePathInput rendererDeleteServiceInput(String serviceName,
256 ServiceDeleteInput serviceDeleteInput) {
257 //TODO: finish model-model mapping
258 return new ServicePathInputBuilder().setServiceName(serviceName).build();
261 private static NodeLists getNodesListZtoA(Iterator<ZToA> iterator) {
262 Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
263 List<Nodes> olmList = new ArrayList<>();
264 List<Nodes> list = new ArrayList<>();
270 while (iterator.hasNext()) {
271 ZToA pathDesObj = iterator.next();
272 resourceType = pathDesObj.getResource().getResource().implementedInterface().getSimpleName();
273 LOG.info("Inside ZtoA {}", resourceType);
276 if (TERMINATION_POINT.equals(resourceType)) {
277 tp = (TerminationPoint) pathDesObj.getResource().getResource();
278 LOG.info(" TP is {} {}", tp.getTpId(),
281 sortId = pathDesObj.getId();
283 //TODO: do not rely on ID to be in certain format
284 if (tpID.contains("CTP") || tpID.contains("CP")) {
287 if (tpID.contains(StringConstants.TTP_TOKEN)) {
288 nodeID = tp.getTpNodeId().split("-DEG")[0];
289 } else if (tpID.contains(StringConstants.PP_TOKEN)) {
290 nodeID = tp.getTpNodeId().split("-SRG")[0];
291 } else if (tpID.contains(StringConstants.NETWORK_TOKEN)
292 || tpID.contains(StringConstants.CLIENT_TOKEN) || tpID.isEmpty()) {
293 nodeID = tp.getTpNodeId().split("-XPDR")[0];
297 int id = Integer.parseInt(sortId);
298 treeMap.put(id, new NodeIdPair(nodeID, tpID));
299 } else if ("Link".equals(resourceType)) {
300 LOG.info("The type is link");
302 LOG.info("The type is not identified: {}", resourceType);
304 } catch (IllegalArgumentException | SecurityException e) {
305 LOG.error("Dont find the getResource method", e);
308 populateNodeLists(treeMap, list, olmList, false);
309 return new NodeLists(olmList, list);
312 private static NodeLists getNodesListAToZ(Iterator<AToZ> iterator) {
313 Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
314 List<Nodes> list = new ArrayList<>();
315 List<Nodes> olmList = new ArrayList<>();
322 while (iterator.hasNext()) {
323 AToZ pathDesObj = iterator.next();
324 resourceType = pathDesObj.getResource().getResource().implementedInterface().getSimpleName();
325 LOG.info("Inside AtoZ {}", resourceType);
327 if (TERMINATION_POINT.equals(resourceType)) {
328 tp = (TerminationPoint) pathDesObj.getResource().getResource();
329 LOG.info("TP is {} {}", tp.getTpId(),
332 sortId = pathDesObj.getId();
334 //TODO: do not rely on ID to be in certain format
335 if (tpID.contains("CTP") || tpID.contains("CP")) {
338 if (tpID.contains(StringConstants.TTP_TOKEN)) {
339 nodeID = tp.getTpNodeId().split("-DEG")[0];
340 } else if (tpID.contains(StringConstants.PP_TOKEN)) {
341 nodeID = tp.getTpNodeId().split("-SRG")[0];
342 } else if (tpID.contains(StringConstants.NETWORK_TOKEN)
343 || tpID.contains(StringConstants.CLIENT_TOKEN) || tpID.isEmpty()) {
344 nodeID = tp.getTpNodeId().split("-XPDR")[0];
348 int id = Integer.parseInt(sortId);
349 treeMap.put(id, new NodeIdPair(nodeID, tpID));
350 } else if ("Link".equals(resourceType)) {
351 LOG.info("The type is link");
353 LOG.info("The type is not identified: {}", resourceType);
355 } catch (IllegalArgumentException | SecurityException e) {
356 //TODO: Auto-generated catch block
357 LOG.error("Did not find the getResource method", e);
360 populateNodeLists(treeMap, list, olmList, true);
361 return new NodeLists(olmList, list);
364 @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
365 value = {"NP_LOAD_OF_KNOWN_NULL_VALUE","RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE"},
366 justification = "loop when value is not always null - "
367 + "TODO: check if something exists in Java lib")
368 private static void populateNodeLists(Map<Integer, NodeIdPair> treeMap, List<Nodes> list, List<Nodes> olmList,
372 LOG.info("treeMap values = {}", treeMap.values());
373 for (NodeIdPair values : treeMap.values()) {
375 srcID = values.getTpID();
376 } else if (desID == null) {
377 desID = values.getTpID();
378 NodesBuilder olmNb = new NodesBuilder()
379 .setNodeId(values.getNodeID())
382 olmList.add(olmNb.build());
383 if (srcID.isEmpty()) {
386 if (desID.isEmpty()) {
387 desID = new StringBuilder(srcID).toString();
391 NodesBuilder nb = new NodesBuilder()
392 .withKey(new NodesKey(values.getNodeID()))
395 if (srcID != null && desID != null && srcID.contains(StringConstants.NETWORK_TOKEN)) {
396 nb.setDestTp(srcID).setSrcTp(desID);
398 list.add(nb.build());
400 if (srcID != null && desID != null && !srcID.contains(StringConstants.NETWORK_TOKEN)
401 && !desID.contains(StringConstants.NETWORK_TOKEN)) {
402 NodesBuilder nb = new NodesBuilder()
403 .withKey(new NodesKey(values.getNodeID()))
406 list.add(nb.build());
412 LOG.warn("both, the source and destination id are null!");
418 @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
419 value = "PZLA_PREFER_ZERO_LENGTH_ARRAYS",
420 justification = "not relevant to return and zero length array"
421 + " as we need real pos")
422 public static int[] findTheLongestSubstring(String s1, String s2) {
423 if ((s1 == null) || (s2 == null)) {
426 int[][] dp = new int[s1.length() + 1][s2.length() + 1];
429 for (int i = 1; i < dp.length; i++) {
430 for (int j = 1; j < dp[0].length; j++) {
431 char ch1 = s1.charAt(i - 1);
432 char ch2 = s2.charAt(j - 1);
434 dp[i][j] = dp[i - 1][j - 1] + 1;
435 if (dp[i][j] >= maxLen) {
442 return new int[] { endPos - maxLen, endPos };