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.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;
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 otnServicePathInputBuilder
245 .setTribPortNumber(pathDescription.getAToZDirection().getTribPortNumber().shortValue())
246 .setTribSlot(pathDescription.getAToZDirection().getTribSlotNumber().shortValue());
248 return otnServicePathInputBuilder.build();
251 public static ServicePathInput rendererDeleteServiceInput(String serviceName,
252 ServiceDeleteInput serviceDeleteInput) {
253 //TODO: finish model-model mapping
254 return new ServicePathInputBuilder().setServiceName(serviceName).build();
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<>();
266 while (iterator.hasNext()) {
267 ZToA pathDesObj = iterator.next();
268 resourceType = pathDesObj.getResource().getResource().implementedInterface().getSimpleName();
269 LOG.info("Inside ZtoA {}", resourceType);
272 if (TERMINATION_POINT.equals(resourceType)) {
273 tp = (TerminationPoint) pathDesObj.getResource().getResource();
274 LOG.info(" TP is {} {}", tp.getTpId(),
277 sortId = pathDesObj.getId();
279 //TODO: do not rely on ID to be in certain format
280 if (tpID.contains("CTP") || tpID.contains("CP")) {
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];
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");
298 LOG.info("The type is not identified: {}", resourceType);
300 } catch (IllegalArgumentException | SecurityException e) {
301 LOG.error("Dont find the getResource method", e);
304 populateNodeLists(treeMap, list, olmList, false);
305 return new NodeLists(olmList, list);
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<>();
318 while (iterator.hasNext()) {
319 AToZ pathDesObj = iterator.next();
320 resourceType = pathDesObj.getResource().getResource().implementedInterface().getSimpleName();
321 LOG.info("Inside AtoZ {}", resourceType);
323 if (TERMINATION_POINT.equals(resourceType)) {
324 tp = (TerminationPoint) pathDesObj.getResource().getResource();
325 LOG.info("TP is {} {}", tp.getTpId(),
328 sortId = pathDesObj.getId();
330 //TODO: do not rely on ID to be in certain format
331 if (tpID.contains("CTP") || tpID.contains("CP")) {
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];
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");
349 LOG.info("The type is not identified: {}", resourceType);
351 } catch (IllegalArgumentException | SecurityException e) {
352 //TODO: Auto-generated catch block
353 LOG.error("Did not find the getResource method", e);
356 populateNodeLists(treeMap, list, olmList, true);
357 return new NodeLists(olmList, list);
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,
368 LOG.info("treeMap values = {}", treeMap.values());
369 for (NodeIdPair values : treeMap.values()) {
371 srcID = values.getTpID();
372 } else if (desID == null) {
373 desID = values.getTpID();
374 NodesBuilder olmNb = new NodesBuilder()
375 .setNodeId(values.getNodeID())
378 olmList.add(olmNb.build());
379 if (srcID.isEmpty()) {
382 if (desID.isEmpty()) {
383 desID = new StringBuilder(srcID).toString();
387 NodesBuilder nb = new NodesBuilder()
388 .withKey(new NodesKey(values.getNodeID()))
391 if (srcID != null && desID != null && srcID.contains(StringConstants.NETWORK_TOKEN)) {
392 nb.setDestTp(srcID).setSrcTp(desID);
394 list.add(nb.build());
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()))
402 list.add(nb.build());
408 LOG.warn("both, the source and destination id are null!");
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)) {
422 int[][] dp = new int[s1.length() + 1][s2.length() + 1];
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);
430 dp[i][j] = dp[i - 1][j - 1] + 1;
431 if (dp[i][j] >= maxLen) {
438 return new int[] { endPos - maxLen, endPos };