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;
17 import org.opendaylight.transportpce.common.StringConstants;
18 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerSetupInput;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev170418.ServicePowerSetupInputBuilder;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.device.rev170228.ServicePathInput;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.device.rev170228.ServicePathInputBuilder;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceDeleteOutput;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceDeleteOutputBuilder;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceImplementationRequestInput;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceImplementationRequestOutput;
26 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceImplementationRequestOutputBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.configuration.response.common.ConfigurationResponseCommon;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.configuration.response.common.ConfigurationResponseCommonBuilder;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceDeleteInput;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.PathDescription;
31 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.atoz.direction.AToZ;
32 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.path.description.ztoa.direction.ZToA;
33 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev171017.pce.resource.resource.resource.TerminationPoint;
34 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev170907.olm.renderer.input.Nodes;
35 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev170907.olm.renderer.input.NodesBuilder;
36 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev170907.olm.renderer.input.NodesKey;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public final class ModelMappingUtils {
44 private static final Logger LOG = LoggerFactory.getLogger(ModelMappingUtils.class);
45 private static final String TERMINATION_POINT = "TerminationPoint";
47 private ModelMappingUtils() {
50 public static ServicePowerSetupInput createServicePowerSetupInput(List<Nodes> olmList,
51 ServiceImplementationRequestInput input) {
52 ServicePowerSetupInputBuilder olmSetupBldr = new ServicePowerSetupInputBuilder();
53 olmSetupBldr.setNodes(olmList);
54 olmSetupBldr.setWaveNumber(input.getPathDescription().getAToZDirection().getAToZWavelengthNumber());
55 return olmSetupBldr.build();
58 public static ServiceImplementationRequestOutput createServiceImplResponse(String responseCode, String message) {
59 ServiceImplementationRequestOutputBuilder outputBldr = new ServiceImplementationRequestOutputBuilder();
60 outputBldr.setConfigurationResponseCommon(createCommonResponse(responseCode, message));
61 return outputBldr.build();
64 public static ServiceDeleteOutput createServiceDeleteResponse(String responseCode, String message) {
65 ServiceDeleteOutputBuilder outputBldr = new ServiceDeleteOutputBuilder();
66 outputBldr.setConfigurationResponseCommon(createCommonResponse(responseCode, message));
67 return outputBldr.build();
70 public static ConfigurationResponseCommon createCommonResponse(String responseCode, String message) {
71 ConfigurationResponseCommonBuilder cmBldr = new ConfigurationResponseCommonBuilder();
72 cmBldr.setResponseMessage(message);
73 cmBldr.setResponseCode(responseCode);
74 return cmBldr.build();
77 public static ListenableFuture<RpcResult<ServiceImplementationRequestOutput>>
78 createServiceImplementationRpcResponse(ServiceImplementationRequestOutput payload) {
79 return RpcResultBuilder.success(payload).buildFuture();
82 public static ListenableFuture<RpcResult<ServiceDeleteOutput>>
83 createServiceDeleteRpcResponse(ServiceDeleteOutput payload) {
84 return RpcResultBuilder.success(payload).buildFuture();
87 public static ServicePathInputData rendererCreateServiceInputAToZ(String serviceName,
88 PathDescription pathDescription) {
89 ServicePathInputBuilder servicePathInputBuilder = new ServicePathInputBuilder();
90 servicePathInputBuilder.setServiceName(serviceName);
91 NodeLists nodeLists = getNodesListAToZ(pathDescription.getAToZDirection().getAToZ().iterator());
92 servicePathInputBuilder.setServiceName(serviceName);
93 servicePathInputBuilder.setOperation(ServicePathInput.Operation.Create);
94 servicePathInputBuilder.setWaveNumber(new Long(pathDescription.getAToZDirection().getAToZWavelengthNumber()));
95 servicePathInputBuilder.setNodes(nodeLists.getList());
96 return new ServicePathInputData(servicePathInputBuilder.build(), nodeLists);
99 public static ServicePathInputData rendererCreateServiceInputZToA(String serviceName,
100 PathDescription pathDescription) {
101 ServicePathInputBuilder servicePathInputBuilder = new ServicePathInputBuilder();
102 NodeLists nodeLists = getNodesListZtoA(pathDescription.getZToADirection().getZToA().iterator());
103 servicePathInputBuilder.setOperation(ServicePathInput.Operation.Create);
104 servicePathInputBuilder.setServiceName(serviceName);
105 servicePathInputBuilder.setWaveNumber(new Long(pathDescription.getZToADirection().getZToAWavelengthNumber()));
106 servicePathInputBuilder.setNodes(nodeLists.getList());
107 return new ServicePathInputData(servicePathInputBuilder.build(), nodeLists);
110 public static ServicePathInput rendererDeleteServiceInput(String serviceName,
111 ServiceDeleteInput serviceDeleteInput) {
112 ServicePathInputBuilder servicePathInput = new ServicePathInputBuilder();
113 servicePathInput.setServiceName(serviceName);
114 //TODO: finish model-model mapping
115 return servicePathInput.build();
118 public static NodeLists getNodesListZtoA(Iterator<ZToA> iterator) {
119 Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
120 List<Nodes> olmList = new ArrayList<>();
121 List<Nodes> list = new ArrayList<>();
127 while (iterator.hasNext()) {
128 ZToA pathDesObj = iterator.next();
129 resourceType = pathDesObj.getResource().getResource().getImplementedInterface().getSimpleName();
130 LOG.info("Inside ZtoA {}", resourceType);
133 if (TERMINATION_POINT.equals(resourceType)) {
134 tp = (TerminationPoint) pathDesObj.getResource().getResource();
135 LOG.info(" TP is {} {}", tp.getTpId(),
138 nodeID = tp.getTpNodeId();
139 sortId = pathDesObj.getId();
141 //TODO: do not rely on ID to be in certain format
142 if (tpID.contains("CTP") || tpID.contains("CP")) {
145 if (!tpID.contains("TTP") && !tpID.contains("PP") && !tpID.contains("NETWORK")
146 && !tpID.contains("CLIENT")) {
150 int[] pos = findTheLongestSubstring(nodeID, tpID);
151 //TODO: do not rely on nodeId to be integer
152 int id = Integer.parseInt(sortId);
153 treeMap.put(id, new NodeIdPair(nodeID.substring(0, pos[0] - 1), tpID));
154 } else if (resourceType.equals("Link")) {
155 LOG.info("The type is link");
157 LOG.info("The type is not indentified: {}", resourceType);
159 } catch (IllegalArgumentException | SecurityException e) {
160 // TODO Auto-generated catch block
161 LOG.error("Dont find the getResource method", e);
167 for (NodeIdPair values : treeMap.values()) {
169 srcID = values.getTpID();
170 } else if (desID == null) {
171 desID = values.getTpID();
172 NodesBuilder nb = new NodesBuilder();
173 nb.withKey(new NodesKey(values.getNodeID()));
176 list.add(nb.build());
178 NodesBuilder olmNb = new NodesBuilder();
179 olmNb.setNodeId(values.getNodeID());
180 olmNb.setDestTp(desID);
181 olmNb.setSrcTp(srcID);
182 olmList.add(olmNb.build());
186 LOG.warn("both, the source and destination id are null!");
189 return new NodeLists(olmList, list);
192 public static NodeLists getNodesListAToZ(Iterator<AToZ> iterator) {
193 Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
194 List<Nodes> list = new ArrayList<>();
195 List<Nodes> olmList = new ArrayList<>();
202 while (iterator.hasNext()) {
203 AToZ pathDesObj = iterator.next();
204 resourceType = pathDesObj.getResource().getResource().getImplementedInterface().getSimpleName();
205 LOG.info("Inside AtoZ {}", resourceType);
207 if (TERMINATION_POINT.equals(resourceType)) {
208 tp = (TerminationPoint) pathDesObj.getResource().getResource();
209 LOG.info(" TP is {} {}", tp.getTpId(),
212 nodeID = tp.getTpNodeId();
213 sortId = pathDesObj.getId();
215 //TODO: do not rely on ID to be in certain format
216 if (tpID.contains("CTP") || tpID.contains("CP")) {
219 if (!tpID.contains(StringConstants.TTP_TOKEN)
220 && !tpID.contains(StringConstants.PP_TOKEN)
221 && !tpID.contains(StringConstants.NETWORK_TOKEN)
222 && !tpID.contains(StringConstants.CLIENT_TOKEN)) {
226 int[] pos = findTheLongestSubstring(nodeID, tpID);
227 //TODO: do not rely on nodeId to be integer
228 int id = Integer.parseInt(sortId);
229 treeMap.put(id, new NodeIdPair(nodeID.substring(0, pos[0] - 1), tpID));
230 } else if (resourceType.equals("Link")) {
231 LOG.info("The type is link");
233 LOG.info("The type is not indentified: {}", resourceType);
235 } catch (IllegalArgumentException | SecurityException e) {
236 //TODO: Auto-generated catch block
237 LOG.error("Dont find the getResource method", e);
243 for (NodeIdPair values : treeMap.values()) {
245 srcID = values.getTpID();
246 } else if (desID == null) {
247 desID = values.getTpID();
248 NodesBuilder nb = new NodesBuilder();
249 nb.withKey(new NodesKey(values.getNodeID()));
252 list.add(nb.build());
254 NodesBuilder olmNb = new NodesBuilder();
255 olmNb.setNodeId(values.getNodeID());
256 olmNb.setDestTp(desID);
257 olmNb.setSrcTp(srcID);
258 olmList.add(olmNb.build());
262 LOG.warn("both, the source and destination id are null!");
265 return new NodeLists(olmList, list);
268 public static int[] findTheLongestSubstring(String s1, String s2) {
269 if ((s1 == null) || (s2 == null)) {
272 int[][] dp = new int[s1.length() + 1][s2.length() + 1];
275 for (int i = 1; i < dp.length; i++) {
276 for (int j = 1; j < dp[0].length; j++) {
277 char ch1 = s1.charAt(i - 1);
278 char ch2 = s2.charAt(j - 1);
280 dp[i][j] = dp[i - 1][j - 1] + 1;
281 if (dp[i][j] >= maxLen) {
288 return new int[] { endPos - maxLen, endPos };