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 = getNodesListAToZ(pathDescription.getAToZDirection().getAToZ().iterator());
124 nodeLists = getNodesListZtoA(pathDescription.getZToADirection().getZToA().iterator());
126 for (Nodes node: nodeLists.getList()) {
127 if (serviceRate.equals("100G")) {
129 new org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev200615.otn.renderer.input
131 .setNodeId(node.getNodeId())
132 .setNetworkTp(node.getDestTp())
135 else { // For any other service rate (1G or 10G)
137 new org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev200615.otn.renderer.input
139 .setNodeId(node.getNodeId())
140 .setClientTp(node.getSrcTp())
141 .setNetworkTp(node.getDestTp())
145 OtnServicePathInputBuilder otnServicePathInputBuilder = new OtnServicePathInputBuilder()
146 .setServiceName(serviceName)
147 .setServiceType(serviceType)
148 .setServiceRate(serviceRate)
151 //TODO: set the trib-slots and trib-ports for the lower oder odu, this should from SH rather than setting here
152 if (serviceRate.equals("1G") || (serviceRate.equals("10G"))) {
153 otnServicePathInputBuilder.setTribPortNumber((short) 1).setTribSlot((short) 1);
156 return otnServicePathInputBuilder.build();
159 public static ServicePathInput rendererDeleteServiceInput(String serviceName,
160 ServiceDeleteInput serviceDeleteInput) {
161 //TODO: finish model-model mapping
162 return new ServicePathInputBuilder().setServiceName(serviceName).build();
165 public static NodeLists getNodesListZtoA(Iterator<ZToA> iterator) {
166 Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
167 List<Nodes> olmList = new ArrayList<>();
168 List<Nodes> list = new ArrayList<>();
174 while (iterator.hasNext()) {
175 ZToA pathDesObj = iterator.next();
176 resourceType = pathDesObj.getResource().getResource().implementedInterface().getSimpleName();
177 LOG.info("Inside ZtoA {}", resourceType);
180 if (TERMINATION_POINT.equals(resourceType)) {
181 tp = (TerminationPoint) pathDesObj.getResource().getResource();
182 LOG.info(" TP is {} {}", tp.getTpId(),
185 nodeID = tp.getTpNodeId();
186 sortId = pathDesObj.getId();
188 //TODO: do not rely on ID to be in certain format
189 if (tpID.contains("CTP") || tpID.contains("CP")) {
192 if (!tpID.contains("TTP") && !tpID.contains("PP") && !tpID.contains("NETWORK")
193 && !tpID.contains("CLIENT")) {
197 int[] pos = findTheLongestSubstring(nodeID, tpID);
199 //TODO: do not rely on nodeId to be integer
200 int id = Integer.parseInt(sortId);
201 treeMap.put(id, new NodeIdPair(nodeID.substring(0, pos[0] - 1), tpID));
203 } else if ("Link".equals(resourceType)) {
204 LOG.info("The type is link");
206 LOG.info("The type is not indentified: {}", resourceType);
208 } catch (IllegalArgumentException | SecurityException e) {
209 LOG.error("Dont find the getResource method", e);
212 populateNodeLists(treeMap, list, olmList);
213 return new NodeLists(olmList, list);
216 public static NodeLists getNodesListAToZ(Iterator<AToZ> iterator) {
217 Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
218 List<Nodes> list = new ArrayList<>();
219 List<Nodes> olmList = new ArrayList<>();
226 while (iterator.hasNext()) {
227 AToZ pathDesObj = iterator.next();
228 resourceType = pathDesObj.getResource().getResource().implementedInterface().getSimpleName();
229 LOG.info("Inside AtoZ {}", resourceType);
231 if (TERMINATION_POINT.equals(resourceType)) {
232 tp = (TerminationPoint) pathDesObj.getResource().getResource();
233 LOG.info(" TP is {} {}", tp.getTpId(),
236 nodeID = tp.getTpNodeId();
237 sortId = pathDesObj.getId();
239 //TODO: do not rely on ID to be in certain format
240 if (tpID.contains("CTP") || tpID.contains("CP")) {
243 if (!tpID.contains(StringConstants.TTP_TOKEN)
244 && !tpID.contains(StringConstants.PP_TOKEN)
245 && !tpID.contains(StringConstants.NETWORK_TOKEN)
246 && !tpID.contains(StringConstants.CLIENT_TOKEN)) {
250 int[] pos = findTheLongestSubstring(nodeID, tpID);
252 //TODO: do not rely on nodeId to be integer
253 int id = Integer.parseInt(sortId);
254 treeMap.put(id, new NodeIdPair(nodeID.substring(0, pos[0] - 1), tpID));
256 } else if ("Link".equals(resourceType)) {
257 LOG.info("The type is link");
259 LOG.info("The type is not indentified: {}", resourceType);
261 } catch (IllegalArgumentException | SecurityException e) {
262 //TODO: Auto-generated catch block
263 LOG.error("Dont find the getResource method", e);
266 populateNodeLists(treeMap, list, olmList);
267 return new NodeLists(olmList, list);
270 @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
271 value = {"NP_LOAD_OF_KNOWN_NULL_VALUE","RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE"},
272 justification = "loop when value is not always null - "
273 + "TODO: check if something exists in Java lib")
274 private static void populateNodeLists(Map<Integer, NodeIdPair> treeMap,
275 List<Nodes> list, List<Nodes> olmList) {
278 int cnt = 0; // This is a counter to check for NETWORK_TOKEN
279 for (NodeIdPair values : treeMap.values()) {
282 srcID = values.getTpID();
283 // This code is added in support of OTU4 service
284 if ((srcID.contains(StringConstants.NETWORK_TOKEN)) && cnt == 1) {
285 NodesBuilder nb = new NodesBuilder()
286 .withKey(new NodesKey(values.getNodeID()))
287 .setDestTp(srcID) // desTp is assigned to the srcID
288 .setSrcTp(null); // srcTp is set to null to create OTU service
289 list.add(nb.build());
291 NodesBuilder olmNb = new NodesBuilder()
292 .setNodeId(values.getNodeID())
293 .setDestTp(srcID) // desTp is assigned to the srcID
294 .setSrcTp(null); // srcTp is set to null to create OTU service
295 olmList.add(olmNb.build());
298 continue; // Continue to the next element in the for loop
300 if ((srcID.contains(StringConstants.NETWORK_TOKEN)) && cnt == treeMap.size()) {
302 NodesBuilder nb = new NodesBuilder()
303 .withKey(new NodesKey(values.getNodeID()))
304 .setDestTp(srcID) //TODO: check this assignment
306 list.add(nb.build());
308 NodesBuilder olmNb = new NodesBuilder()
309 .setNodeId(values.getNodeID())
310 .setDestTp(null) // In the case of final node, destTp is set to null
311 .setSrcTp(srcID); // srcTp is srcTp
312 olmList.add(olmNb.build());
316 // End of code for support of ODT4 service
317 } else if (desID == null) {
318 desID = values.getTpID();
319 NodesBuilder nb = new NodesBuilder()
320 .withKey(new NodesKey(values.getNodeID()))
323 list.add(nb.build());
325 NodesBuilder olmNb = new NodesBuilder()
326 .setNodeId(values.getNodeID())
329 olmList.add(olmNb.build());
333 LOG.warn("both, the source and destination id are null!");
338 @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
339 value = "PZLA_PREFER_ZERO_LENGTH_ARRAYS",
340 justification = "not relevant to return and zero length array"
341 + " as we need real pos")
342 public static int[] findTheLongestSubstring(String s1, String s2) {
343 if ((s1 == null) || (s2 == null)) {
346 int[][] dp = new int[s1.length() + 1][s2.length() + 1];
349 for (int i = 1; i < dp.length; i++) {
350 for (int j = 1; j < dp[0].length; j++) {
351 char ch1 = s1.charAt(i - 1);
352 char ch2 = s2.charAt(j - 1);
354 dp[i][j] = dp[i - 1][j - 1] + 1;
355 if (dp[i][j] >= maxLen) {
362 return new int[] { endPos - maxLen, endPos };