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.otn.common.types.rev181130.OpucnTribSlotDef;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.ServiceDeleteInput;
40 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.PathDescription;
41 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.AToZDirection;
42 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.ZToADirection;
43 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.atoz.direction.AToZ;
44 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.ztoa.direction.ZToA;
45 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.pce.resource.resource.Resource;
46 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.pce.resource.resource.resource.TerminationPoint;
47 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210618.optical.renderer.nodes.Nodes;
48 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210618.optical.renderer.nodes.NodesBuilder;
49 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210618.optical.renderer.nodes.NodesKey;
50 import org.opendaylight.yangtools.yang.common.RpcResult;
51 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
52 import org.opendaylight.yangtools.yang.common.Uint32;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
58 public final class ModelMappingUtils {
60 private static final Logger LOG = LoggerFactory.getLogger(ModelMappingUtils.class);
61 private static final String TERMINATION_POINT = "TerminationPoint";
62 private static final String LINK = "Link";
64 private ModelMappingUtils() {
67 public static ServicePowerSetupInput createServicePowerSetupInput(List<Nodes> olmList,
68 ServiceImplementationRequestInput input) {
69 ServicePowerSetupInputBuilder olmSetupBldr = new ServicePowerSetupInputBuilder().setNodes(olmList);
70 if (input != null && input.getPathDescription() != null
71 && input.getPathDescription().getAToZDirection() != null) {
72 AToZDirection atoZDirection = input.getPathDescription().getAToZDirection();
73 olmSetupBldr.setWaveNumber(atoZDirection.getAToZWavelengthNumber());
74 if (atoZDirection.getAToZMinFrequency() != null) {
75 olmSetupBldr.setLowerSpectralSlotNumber(Uint32
77 .getLowerSpectralIndexFromFrequency(atoZDirection.getAToZMinFrequency().getValue())));
79 if (atoZDirection.getAToZMaxFrequency() != null) {
80 olmSetupBldr.setHigherSpectralSlotNumber(Uint32
82 .getHigherSpectralIndexFromFrequency(atoZDirection.getAToZMaxFrequency().getValue())));
85 return olmSetupBldr.build();
88 public static ServiceImplementationRequestOutput createServiceImplResponse(String responseCode, String message) {
89 return new ServiceImplementationRequestOutputBuilder()
90 .setConfigurationResponseCommon(createCommonResponse(responseCode, message))
94 public static ServiceDeleteOutput createServiceDeleteResponse(String responseCode, String message) {
95 return new ServiceDeleteOutputBuilder()
96 .setConfigurationResponseCommon(createCommonResponse(responseCode, message))
100 public static ConfigurationResponseCommon createCommonResponse(String responseCode, String message) {
101 return new ConfigurationResponseCommonBuilder()
102 .setResponseMessage(message)
103 .setResponseCode(responseCode)
107 public static ListenableFuture<RpcResult<ServiceImplementationRequestOutput>>
108 createServiceImplementationRpcResponse(ServiceImplementationRequestOutput payload) {
109 return RpcResultBuilder.success(payload).buildFuture();
112 public static ListenableFuture<RpcResult<ServiceDeleteOutput>>
113 createServiceDeleteRpcResponse(ServiceDeleteOutput payload) {
114 return RpcResultBuilder.success(payload).buildFuture();
117 public static ServicePathInputData rendererCreateServiceInputAToZ(String serviceName,
118 PathDescription pathDescription, Action operation) {
119 int scale = GridConstant.FIXED_GRID_FREQUENCY_PRECISION;
120 AToZDirection atoZDirection = pathDescription.getAToZDirection();
121 LOG.info("Building ServicePathInputData for a to z direction {}", atoZDirection);
122 NodeLists nodeLists = getNodesListAToZ(atoZDirection.nonnullAToZ().values().iterator());
123 ServicePathInputBuilder servicePathInputBuilder = new ServicePathInputBuilder()
124 .setServiceName(serviceName)
125 .setOperation(operation)
126 .setNodes(nodeLists.getRendererNodeList())
127 .setWidth(new FrequencyGHz(GridConstant.WIDTH_40));
128 if (atoZDirection.getAToZWavelengthNumber() != null) {
129 servicePathInputBuilder
130 .setWaveNumber(atoZDirection.getAToZWavelengthNumber());
132 if (Uint32.valueOf(GridConstant.IRRELEVANT_WAVELENGTH_NUMBER)
133 .equals(atoZDirection.getAToZWavelengthNumber())) {
134 scale = GridConstant.FLEX_GRID_FREQUENCY_PRECISION;
136 if (atoZDirection.getAToZMinFrequency() != null) {
137 servicePathInputBuilder.setMinFreq(new FrequencyTHz(atoZDirection.getAToZMinFrequency().getValue()));
138 servicePathInputBuilder.setLowerSpectralSlotNumber(Uint32
140 .getLowerSpectralIndexFromFrequency(atoZDirection.getAToZMinFrequency().getValue())));
142 if (atoZDirection.getAToZMaxFrequency() != null) {
143 servicePathInputBuilder.setMaxFreq(new FrequencyTHz(atoZDirection.getAToZMaxFrequency().getValue()));
144 servicePathInputBuilder.setHigherSpectralSlotNumber(
145 Uint32.valueOf(GridUtils
146 .getHigherSpectralIndexFromFrequency(atoZDirection.getAToZMaxFrequency().getValue())));
148 if (atoZDirection.getAToZMinFrequency() != null && atoZDirection.getAToZMaxFrequency() != null) {
149 servicePathInputBuilder.setCenterFreq(
150 GridUtils.getCentralFrequencyWithPrecision(atoZDirection.getAToZMinFrequency().getValue(),
151 atoZDirection.getAToZMaxFrequency().getValue(), scale));
153 if (atoZDirection.getRate() != null && atoZDirection.getModulationFormat() != null) {
154 Optional<ModulationFormat> optionalModulationFormat = ModulationFormat
155 .forName(atoZDirection.getModulationFormat());
156 if (optionalModulationFormat.isPresent()
157 && GridConstant.FREQUENCY_WIDTH_TABLE
158 .contains(atoZDirection.getRate(), optionalModulationFormat.get())) {
159 servicePathInputBuilder
160 .setWidth(FrequencyGHz
161 .getDefaultInstance(GridConstant.FREQUENCY_WIDTH_TABLE.get(atoZDirection.getRate(),
162 optionalModulationFormat.get())));
165 servicePathInputBuilder.setModulationFormat(atoZDirection.getModulationFormat());
166 return new ServicePathInputData(servicePathInputBuilder.build(), nodeLists);
169 public static ServicePathInputData rendererCreateServiceInputZToA(String serviceName,
170 PathDescription pathDescription, Action operation) {
171 int scale = GridConstant.FIXED_GRID_FREQUENCY_PRECISION;
172 ZToADirection ztoADirection = pathDescription.getZToADirection();
173 LOG.info("Building ServicePathInputData for z to a direction {}", ztoADirection);
174 NodeLists nodeLists = getNodesListZtoA(pathDescription.getZToADirection().nonnullZToA().values().iterator());
175 ServicePathInputBuilder servicePathInputBuilder = new ServicePathInputBuilder()
176 .setOperation(operation)
177 .setServiceName(serviceName)
178 .setNodes(nodeLists.getRendererNodeList())
179 .setWidth(new FrequencyGHz(GridConstant.WIDTH_40));
180 if (ztoADirection.getZToAWavelengthNumber() != null) {
181 servicePathInputBuilder
182 .setWaveNumber(ztoADirection.getZToAWavelengthNumber());
184 if (Uint32.valueOf(GridConstant.IRRELEVANT_WAVELENGTH_NUMBER)
185 .equals(ztoADirection.getZToAWavelengthNumber())) {
186 scale = GridConstant.FLEX_GRID_FREQUENCY_PRECISION;
188 if (ztoADirection.getZToAMinFrequency() != null) {
189 servicePathInputBuilder.setMinFreq(new FrequencyTHz(ztoADirection.getZToAMinFrequency().getValue()));
190 servicePathInputBuilder.setLowerSpectralSlotNumber(Uint32
192 .getLowerSpectralIndexFromFrequency(ztoADirection.getZToAMinFrequency().getValue())));
194 if (ztoADirection.getZToAMaxFrequency() != null) {
195 servicePathInputBuilder.setMaxFreq(new FrequencyTHz(ztoADirection.getZToAMaxFrequency().getValue()));
196 servicePathInputBuilder.setHigherSpectralSlotNumber(
197 Uint32.valueOf(GridUtils
198 .getHigherSpectralIndexFromFrequency(ztoADirection.getZToAMaxFrequency().getValue())));
200 if (ztoADirection.getZToAMinFrequency() != null && ztoADirection.getZToAMaxFrequency() != null) {
201 servicePathInputBuilder.setCenterFreq(
202 GridUtils.getCentralFrequencyWithPrecision(ztoADirection.getZToAMinFrequency().getValue(),
203 ztoADirection.getZToAMaxFrequency().getValue(), scale));
205 if (ztoADirection.getRate() != null && ztoADirection.getModulationFormat() != null) {
206 Optional<ModulationFormat> optionalModulationFormat = ModulationFormat
207 .forName(ztoADirection.getModulationFormat());
208 if (optionalModulationFormat.isPresent()
209 && GridConstant.FREQUENCY_WIDTH_TABLE
210 .contains(ztoADirection.getRate(), optionalModulationFormat.get())) {
211 servicePathInputBuilder.setWidth(FrequencyGHz
212 .getDefaultInstance(GridConstant.FREQUENCY_WIDTH_TABLE.get(ztoADirection.getRate(),
213 optionalModulationFormat.get())));
216 servicePathInputBuilder.setModulationFormat(ztoADirection.getModulationFormat());
217 return new ServicePathInputData(servicePathInputBuilder.build(), nodeLists);
220 // Adding createOtnServiceInputpath for A-Z and Z-A directions as one method
221 public static OtnServicePathInput rendererCreateOtnServiceInput(String serviceName, Action operation,
222 String serviceFormat, Uint32 serviceRate, PathDescription pathDescription, boolean asideToZside) {
223 // If atoZ is set true use A-to-Z direction otherwise use Z-to-A
224 List<org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210618.otn.renderer.nodes.Nodes> nodes =
226 NodeLists nodeLists =
228 ? getNodesListAToZ(pathDescription.getAToZDirection().nonnullAToZ().values().iterator())
229 : getNodesListZtoA(pathDescription.getZToADirection().nonnullZToA().values().iterator());
230 LOG.info("These are node-lists {}, {}", nodeLists.getRendererNodeList(), nodeLists.getOlmNodeList());
231 for (Nodes node: nodeLists.getRendererNodeList()) {
232 org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210618.otn.renderer.nodes.NodesBuilder nb
233 = new org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210618.otn.renderer.nodes
234 .NodesBuilder().setNodeId(node.getNodeId()).setNetworkTp(node.getDestTp());
235 if (node.getSrcTp() != null && node.getSrcTp().contains("NETWORK")) {
236 nb.setNetwork2Tp(node.getSrcTp());
238 nb.setClientTp(node.getSrcTp());
240 nodes.add(nb.build());
242 OtnServicePathInputBuilder otnServicePathInputBuilder = new OtnServicePathInputBuilder()
243 .setServiceName(serviceName)
244 .setOperation(operation)
245 .setServiceFormat(serviceFormat)
246 .setServiceRate(serviceRate)
249 // set the trib-slots and trib-ports for the lower oder odu
250 if (serviceRate.intValue() == 1 || (serviceRate.intValue() == 10)) {
251 Short tribPort = Short.valueOf(pathDescription.getAToZDirection().getMinTribSlot().getValue()
253 Short minTribSlot = Short.valueOf(pathDescription.getAToZDirection().getMinTribSlot().getValue()
255 otnServicePathInputBuilder
256 .setTribPortNumber(tribPort)
257 .setTribSlot(minTribSlot);
259 if (serviceRate.intValue() == 100) {
260 List<OpucnTribSlotDef> opucnTribSlotDefList = new ArrayList<>();
261 opucnTribSlotDefList.add(pathDescription.getAToZDirection().getMinTribSlot());
262 opucnTribSlotDefList.add(pathDescription.getAToZDirection().getMaxTribSlot());
263 otnServicePathInputBuilder.setOpucnTribSlots(opucnTribSlotDefList);
265 return otnServicePathInputBuilder.build();
268 public static ServicePathInput rendererDeleteServiceInput(String serviceName,
269 ServiceDeleteInput serviceDeleteInput) {
270 //TODO: finish model-model mapping
271 return new ServicePathInputBuilder().setServiceName(serviceName).build();
274 private static NodeLists getNodesListZtoA(Iterator<ZToA> iterator) {
275 Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
276 List<Nodes> olmList = new ArrayList<>();
277 List<Nodes> list = new ArrayList<>();
279 while (iterator.hasNext()) {
280 ZToA pathDesObj = iterator.next();
282 populateTreeMap(treeMap, pathDesObj.getResource().getResource(), pathDesObj.getId(), "ZtoA");
283 } catch (IllegalArgumentException | SecurityException e) {
284 //TODO: Auto-generated catch block
285 LOG.error("Did not find the getResource method", e);
288 populateNodeLists(treeMap, list, olmList, false);
289 return new NodeLists(olmList, list);
292 private static NodeLists getNodesListAToZ(Iterator<AToZ> iterator) {
293 Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
294 List<Nodes> list = new ArrayList<>();
295 List<Nodes> olmList = new ArrayList<>();
297 while (iterator.hasNext()) {
298 AToZ pathDesObj = iterator.next();
300 populateTreeMap(treeMap, pathDesObj.getResource().getResource(), pathDesObj.getId(), "AtoZ");
301 } catch (IllegalArgumentException | SecurityException e) {
302 //TODO: Auto-generated catch block
303 LOG.error("Did not find the getResource method", e);
306 populateNodeLists(treeMap, list, olmList, true);
307 return new NodeLists(olmList, list);
310 private static void populateTreeMap(Map<Integer, NodeIdPair> treeMap, Resource rsrc, String sortId,
312 String resourceType = rsrc.implementedInterface().getSimpleName();
313 LOG.info("Inside {} {}", direction, resourceType);
314 switch (resourceType) {
315 case TERMINATION_POINT:
316 TerminationPoint tp = (TerminationPoint) rsrc;
317 LOG.info(" TP is {} {}", tp.getTpId(), tp.getTpNodeId());
318 String tpID = tp.getTpId();
320 //TODO: do not rely on ID to be in certain format
321 if (tpID.contains("CTP") || tpID.contains("CP")) {
325 if (tpID.contains(StringConstants.TTP_TOKEN)) {
326 nodeID = tp.getTpNodeId().split("-DEG")[0];
327 } else if (tpID.contains(StringConstants.PP_TOKEN)) {
328 nodeID = tp.getTpNodeId().split("-SRG")[0];
329 } else if (tpID.contains(StringConstants.NETWORK_TOKEN)
330 || tpID.contains(StringConstants.CLIENT_TOKEN) || tpID.isEmpty()) {
331 nodeID = tp.getTpNodeId().split("-XPDR")[0];
335 int id = Integer.parseInt(sortId);
336 treeMap.put(id, new NodeIdPair(nodeID, tpID));
339 LOG.info("The type is link");
342 LOG.info("The type is not identified: {}", resourceType);
347 @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
348 value = {"NP_LOAD_OF_KNOWN_NULL_VALUE","RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE"},
349 justification = "loop when value is not always null - "
350 + "TODO: check if something exists in Java lib")
351 private static void populateNodeLists(Map<Integer, NodeIdPair> treeMap, List<Nodes> list, List<Nodes> olmList,
355 LOG.info("treeMap values = {}", treeMap.values());
356 for (NodeIdPair values : treeMap.values()) {
358 srcID = values.getTpID();
359 } else if (desID == null) {
360 desID = values.getTpID();
361 NodesBuilder olmNb = new NodesBuilder()
362 .setNodeId(values.getNodeID())
365 olmList.add(olmNb.build());
366 if (srcID.isEmpty()) {
369 if (desID.isEmpty()) {
370 desID = new StringBuilder(srcID).toString();
374 NodesBuilder nb = new NodesBuilder()
375 .withKey(new NodesKey(values.getNodeID()))
378 if (srcID != null && desID != null && srcID.contains(StringConstants.NETWORK_TOKEN)) {
379 nb.setDestTp(srcID).setSrcTp(desID);
381 list.add(nb.build());
383 if (srcID != null && desID != null && !srcID.contains(StringConstants.NETWORK_TOKEN)
384 && !desID.contains(StringConstants.NETWORK_TOKEN)) {
385 NodesBuilder nb = new NodesBuilder()
386 .withKey(new NodesKey(values.getNodeID()))
389 list.add(nb.build());
395 LOG.warn("both, the source and destination id are not null!");
401 @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
402 value = "PZLA_PREFER_ZERO_LENGTH_ARRAYS",
403 justification = "not relevant to return a zero length array"
404 + " as we need real pos")
405 public static int[] findTheLongestSubstring(String s1, String s2) {
406 if ((s1 == null) || (s2 == null)) {
409 int[][] dp = new int[s1.length() + 1][s2.length() + 1];
412 for (int i = 1; i < dp.length; i++) {
413 for (int j = 1; j < dp[0].length; j++) {
414 char ch1 = s1.charAt(i - 1);
415 char ch2 = s2.charAt(j - 1);
417 dp[i][j] = dp[i - 1][j - 1] + 1;
418 if (dp[i][j] >= maxLen) {
425 return new int[] { endPos - maxLen, endPos };