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.TreeMap;
16 import org.opendaylight.transportpce.common.StringConstants;
17 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerSetupInput;
18 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerSetupInputBuilder;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.device.rev200128.OtnServicePathInput;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.device.rev200128.OtnServicePathInputBuilder;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.device.rev200128.ServicePathInput;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.device.rev200128.ServicePathInputBuilder;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev200520.ServiceDeleteOutput;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev200520.ServiceDeleteOutputBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev200520.ServiceImplementationRequestInput;
26 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev200520.ServiceImplementationRequestOutput;
27 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev200520.ServiceImplementationRequestOutputBuilder;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.configuration.response.common.ConfigurationResponseCommon;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev190531.configuration.response.common.ConfigurationResponseCommonBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.ServiceDeleteInput;
31 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.PathDescription;
32 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.atoz.direction.AToZ;
33 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.ztoa.direction.ZToA;
34 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce.resource.resource.resource.TerminationPoint;
35 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev200615.olm.renderer.input.Nodes;
36 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev200615.olm.renderer.input.NodesBuilder;
37 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev200615.olm.renderer.input.NodesKey;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
45 public final class ModelMappingUtils {
47 private static final Logger LOG = LoggerFactory.getLogger(ModelMappingUtils.class);
48 private static final String TERMINATION_POINT = "TerminationPoint";
50 private ModelMappingUtils() {
53 public static ServicePowerSetupInput createServicePowerSetupInput(List<Nodes> olmList,
54 ServiceImplementationRequestInput input) {
55 ServicePowerSetupInputBuilder olmSetupBldr = new ServicePowerSetupInputBuilder()
57 if (input != null && input.getPathDescription() != null
58 && input.getPathDescription().getAToZDirection() != null) {
59 olmSetupBldr.setWaveNumber(
60 input.getPathDescription().getAToZDirection().getAToZWavelengthNumber());
62 return olmSetupBldr.build();
65 public static ServiceImplementationRequestOutput createServiceImplResponse(String responseCode, String message) {
66 return new ServiceImplementationRequestOutputBuilder()
67 .setConfigurationResponseCommon(createCommonResponse(responseCode, message))
71 public static ServiceDeleteOutput createServiceDeleteResponse(String responseCode, String message) {
72 return new ServiceDeleteOutputBuilder()
73 .setConfigurationResponseCommon(createCommonResponse(responseCode, message))
77 public static ConfigurationResponseCommon createCommonResponse(String responseCode, String message) {
78 return new ConfigurationResponseCommonBuilder()
79 .setResponseMessage(message)
80 .setResponseCode(responseCode)
84 public static ListenableFuture<RpcResult<ServiceImplementationRequestOutput>>
85 createServiceImplementationRpcResponse(ServiceImplementationRequestOutput payload) {
86 return RpcResultBuilder.success(payload).buildFuture();
89 public static ListenableFuture<RpcResult<ServiceDeleteOutput>>
90 createServiceDeleteRpcResponse(ServiceDeleteOutput payload) {
91 return RpcResultBuilder.success(payload).buildFuture();
94 public static ServicePathInputData rendererCreateServiceInputAToZ(String serviceName,
95 PathDescription pathDescription) {
96 NodeLists nodeLists = getNodesListAToZ(pathDescription.getAToZDirection().getAToZ().iterator());
97 ServicePathInputBuilder servicePathInputBuilder = new ServicePathInputBuilder()
98 .setServiceName(serviceName)
99 .setOperation(ServicePathInput.Operation.Create)
100 .setWaveNumber(Long.valueOf(pathDescription.getAToZDirection().getAToZWavelengthNumber().toJava()))
101 .setNodes(nodeLists.getList());
102 return new ServicePathInputData(servicePathInputBuilder.build(), nodeLists);
105 public static ServicePathInputData rendererCreateServiceInputZToA(String serviceName,
106 PathDescription pathDescription) {
107 NodeLists nodeLists = getNodesListZtoA(pathDescription.getZToADirection().getZToA().iterator());
108 ServicePathInputBuilder servicePathInputBuilder = new ServicePathInputBuilder()
109 .setOperation(ServicePathInput.Operation.Create)
110 .setServiceName(serviceName)
111 .setWaveNumber(Long.valueOf(pathDescription.getZToADirection().getZToAWavelengthNumber().toJava()))
112 .setNodes(nodeLists.getList());
113 return new ServicePathInputData(servicePathInputBuilder.build(), nodeLists);
116 // Adding createOtnServiceInputpath for A-Z and Z-A directions as one method
117 public static OtnServicePathInput rendererCreateOtnServiceInput(String serviceName, String serviceType,
118 String serviceRate, PathDescription pathDescription, boolean asideToZside) {
119 // If atoZ is set true use A-to-Z direction otherwise use Z-to-A
120 List<org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev200615.otn.renderer.input.Nodes> nodes =
122 NodeLists nodeLists = null;
124 nodeLists = getNodesListAToZ(pathDescription.getAToZDirection().getAToZ().iterator());
126 nodeLists = getNodesListZtoA(pathDescription.getZToADirection().getZToA().iterator());
128 LOG.info("These are node-lists {}, {}", nodeLists.getList(), nodeLists.getOlmList());
129 for (int i = 0; i < nodeLists.getList().size(); i++) {
130 Nodes node = nodeLists.getList().get(i);
131 if (serviceRate.equals("100G")) {
133 new org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev200615.otn.renderer.input
135 .setNodeId(node.getNodeId())
136 .setNetworkTp(node.getDestTp())
139 else { // For any other service rate (1G or 10G)
140 // For the last node in the list, clientTp and NetworkTp has to be reversed
141 if (i == nodeLists.getList().size() - 1) {
143 new org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev200615.otn.renderer.input
145 .setNodeId(node.getNodeId())
146 .setClientTp(node.getDestTp())
147 .setNetworkTp(node.getSrcTp())
152 new org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev200615.otn.renderer.input
154 .setNodeId(node.getNodeId())
155 .setClientTp(node.getSrcTp())
156 .setNetworkTp(node.getDestTp())
161 OtnServicePathInputBuilder otnServicePathInputBuilder = new OtnServicePathInputBuilder()
162 .setServiceName(serviceName)
163 .setServiceType(serviceType)
164 .setServiceRate(serviceRate)
167 //TODO: set the trib-slots and trib-ports for the lower oder odu, this should from SH rather than setting here
168 if (serviceRate.equals("1G") || (serviceRate.equals("10G"))) {
169 otnServicePathInputBuilder.setTribPortNumber((short) 1).setTribSlot((short) 1);
172 return otnServicePathInputBuilder.build();
175 public static ServicePathInput rendererDeleteServiceInput(String serviceName,
176 ServiceDeleteInput serviceDeleteInput) {
177 //TODO: finish model-model mapping
178 return new ServicePathInputBuilder().setServiceName(serviceName).build();
181 public static NodeLists getNodesListZtoA(Iterator<ZToA> iterator) {
182 Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
183 List<Nodes> olmList = new ArrayList<>();
184 List<Nodes> list = new ArrayList<>();
190 while (iterator.hasNext()) {
191 ZToA pathDesObj = iterator.next();
192 resourceType = pathDesObj.getResource().getResource().implementedInterface().getSimpleName();
193 LOG.info("Inside ZtoA {}", resourceType);
196 if (TERMINATION_POINT.equals(resourceType)) {
197 tp = (TerminationPoint) pathDesObj.getResource().getResource();
198 LOG.info(" TP is {} {}", tp.getTpId(),
201 nodeID = tp.getTpNodeId();
202 sortId = pathDesObj.getId();
204 //TODO: do not rely on ID to be in certain format
205 if (tpID.contains("CTP") || tpID.contains("CP")) {
208 if (!tpID.contains("TTP") && !tpID.contains("PP") && !tpID.contains("NETWORK")
209 && !tpID.contains("CLIENT")) {
213 int[] pos = findTheLongestSubstring(nodeID, tpID);
215 //TODO: do not rely on nodeId to be integer
216 int id = Integer.parseInt(sortId);
217 treeMap.put(id, new NodeIdPair(nodeID.substring(0, pos[0] - 1), tpID));
219 } else if ("Link".equals(resourceType)) {
220 LOG.info("The type is link");
222 LOG.info("The type is not identified: {}", resourceType);
224 } catch (IllegalArgumentException | SecurityException e) {
225 LOG.error("Dont find the getResource method", e);
228 populateNodeLists(treeMap, list, olmList);
229 return new NodeLists(olmList, list);
232 public static NodeLists getNodesListAToZ(Iterator<AToZ> iterator) {
233 Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
234 List<Nodes> list = new ArrayList<>();
235 List<Nodes> olmList = new ArrayList<>();
242 while (iterator.hasNext()) {
243 AToZ pathDesObj = iterator.next();
244 resourceType = pathDesObj.getResource().getResource().implementedInterface().getSimpleName();
245 LOG.info("Inside AtoZ {}", resourceType);
247 if (TERMINATION_POINT.equals(resourceType)) {
248 tp = (TerminationPoint) pathDesObj.getResource().getResource();
249 LOG.info("TP is {} {}", tp.getTpId(),
252 nodeID = tp.getTpNodeId();
253 sortId = pathDesObj.getId();
255 //TODO: do not rely on ID to be in certain format
256 if (tpID.contains("CTP") || tpID.contains("CP")) {
259 if (!tpID.contains(StringConstants.TTP_TOKEN)
260 && !tpID.contains(StringConstants.PP_TOKEN)
261 && !tpID.contains(StringConstants.NETWORK_TOKEN)
262 && !tpID.contains(StringConstants.CLIENT_TOKEN)) {
266 int[] pos = findTheLongestSubstring(nodeID, tpID);
268 //TODO: do not rely on nodeId to be integer
269 int id = Integer.parseInt(sortId);
270 treeMap.put(id, new NodeIdPair(nodeID.substring(0, pos[0] - 1), tpID));
272 } else if ("Link".equals(resourceType)) {
273 LOG.info("The type is link");
275 LOG.info("The type is not identified: {}", resourceType);
277 } catch (IllegalArgumentException | SecurityException e) {
278 //TODO: Auto-generated catch block
279 LOG.error("Did not find the getResource method", e);
282 populateNodeLists(treeMap, list, olmList);
283 return new NodeLists(olmList, list);
286 @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
287 value = {"NP_LOAD_OF_KNOWN_NULL_VALUE","RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE"},
288 justification = "loop when value is not always null - "
289 + "TODO: check if something exists in Java lib")
290 private static void populateNodeLists(Map<Integer, NodeIdPair> treeMap,
291 List<Nodes> list, List<Nodes> olmList) {
294 int cnt = 0; // This is a counter to check for NETWORK_TOKEN
295 for (NodeIdPair values : treeMap.values()) {
298 srcID = values.getTpID();
299 // This code is added in support of OTU4 service
300 if ((srcID.contains(StringConstants.NETWORK_TOKEN)) && cnt == 1) {
301 NodesBuilder nb = new NodesBuilder()
302 .withKey(new NodesKey(values.getNodeID()))
303 .setDestTp(srcID) // desTp is assigned to the srcID
304 .setSrcTp(null); // srcTp is set to null to create OTU service
305 list.add(nb.build());
307 NodesBuilder olmNb = new NodesBuilder()
308 .setNodeId(values.getNodeID())
309 .setDestTp(srcID) // desTp is assigned to the srcID
310 .setSrcTp(null); // srcTp is set to null to create OTU service
311 olmList.add(olmNb.build());
314 continue; // Continue to the next element in the for loop
316 if ((srcID.contains(StringConstants.NETWORK_TOKEN)) && cnt == treeMap.size()) {
318 NodesBuilder nb = new NodesBuilder()
319 .withKey(new NodesKey(values.getNodeID()))
320 .setDestTp(srcID) //TODO: check this assignment
322 list.add(nb.build());
324 NodesBuilder olmNb = new NodesBuilder()
325 .setNodeId(values.getNodeID())
326 .setDestTp(null) // In the case of final node, destTp is set to null
327 .setSrcTp(srcID); // srcTp is srcTp
328 olmList.add(olmNb.build());
332 // End of code for support of ODT4 service
333 } else if (desID == null) {
334 desID = values.getTpID();
335 NodesBuilder nb = new NodesBuilder()
336 .withKey(new NodesKey(values.getNodeID()))
339 list.add(nb.build());
341 NodesBuilder olmNb = new NodesBuilder()
342 .setNodeId(values.getNodeID())
345 olmList.add(olmNb.build());
349 LOG.warn("both, the source and destination id are null!");
354 @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
355 value = "PZLA_PREFER_ZERO_LENGTH_ARRAYS",
356 justification = "not relevant to return and zero length array"
357 + " as we need real pos")
358 public static int[] findTheLongestSubstring(String s1, String s2) {
359 if ((s1 == null) || (s2 == null)) {
362 int[][] dp = new int[s1.length() + 1][s2.length() + 1];
365 for (int i = 1; i < dp.length; i++) {
366 for (int j = 1; j < dp[0].length; j++) {
367 char ch1 = s1.charAt(i - 1);
368 char ch2 = s2.charAt(j - 1);
370 dp[i][j] = dp[i - 1][j - 1] + 1;
371 if (dp[i][j] >= maxLen) {
378 return new int[] { endPos - maxLen, endPos };