fix Databroker deprecated warnings
[transportpce.git] / renderer / src / main / java / org / opendaylight / transportpce / renderer / ModelMappingUtils.java
1 /*
2  * Copyright © 2017 AT&T and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.transportpce.renderer;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.ArrayList;
12 import java.util.Iterator;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.TreeMap;
16
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;
41
42 public final class ModelMappingUtils {
43
44     private static final Logger LOG = LoggerFactory.getLogger(ModelMappingUtils.class);
45     private static final String TERMINATION_POINT = "TerminationPoint";
46
47     private ModelMappingUtils() {
48     }
49
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();
56     }
57
58     public static ServiceImplementationRequestOutput createServiceImplResponse(String responseCode, String message) {
59         ServiceImplementationRequestOutputBuilder outputBldr = new ServiceImplementationRequestOutputBuilder();
60         outputBldr.setConfigurationResponseCommon(createCommonResponse(responseCode, message));
61         return outputBldr.build();
62     }
63
64     public static ServiceDeleteOutput createServiceDeleteResponse(String responseCode, String message) {
65         ServiceDeleteOutputBuilder outputBldr = new ServiceDeleteOutputBuilder();
66         outputBldr.setConfigurationResponseCommon(createCommonResponse(responseCode, message));
67         return outputBldr.build();
68     }
69
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();
75     }
76
77     public static ListenableFuture<RpcResult<ServiceImplementationRequestOutput>>
78         createServiceImplementationRpcResponse(ServiceImplementationRequestOutput payload) {
79         return RpcResultBuilder.success(payload).buildFuture();
80     }
81
82     public static ListenableFuture<RpcResult<ServiceDeleteOutput>>
83         createServiceDeleteRpcResponse(ServiceDeleteOutput payload) {
84         return RpcResultBuilder.success(payload).buildFuture();
85     }
86
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);
97     }
98
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);
108     }
109
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();
116     }
117
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<>();
122         String resourceType;
123         TerminationPoint tp;
124         String tpID = "";
125         String nodeID = "";
126         String sortId = "";
127         while (iterator.hasNext()) {
128             ZToA pathDesObj = iterator.next();
129             resourceType = pathDesObj.getResource().getResource().getImplementedInterface().getSimpleName();
130             LOG.info("Inside ZtoA {}", resourceType);
131
132             try {
133                 if (TERMINATION_POINT.equals(resourceType)) {
134                     tp = (TerminationPoint) pathDesObj.getResource().getResource();
135                     LOG.info(" TP is {} {}", tp.getTpId(),
136                             tp.getTpNodeId());
137                     tpID = tp.getTpId();
138                     nodeID = tp.getTpNodeId();
139                     sortId = pathDesObj.getId();
140
141                     //TODO: do not rely on ID to be in certain format
142                     if (tpID.contains("CTP") || tpID.contains("CP")) {
143                         continue;
144                     }
145                     if (!tpID.contains("TTP") && !tpID.contains("PP") && !tpID.contains("NETWORK")
146                             && !tpID.contains("CLIENT")) {
147                         continue;
148                     }
149
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");
156                 } else {
157                     LOG.info("The type is not indentified: {}", resourceType);
158                 }
159             } catch (IllegalArgumentException | SecurityException e) {
160                 // TODO Auto-generated catch block
161                 LOG.error("Dont find the getResource method", e);
162             }
163         }
164
165         String desID = null;
166         String srcID = null;
167         for (NodeIdPair values : treeMap.values()) {
168             if (srcID == null) {
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()));
174                 nb.setDestTp(desID);
175                 nb.setSrcTp(srcID);
176                 list.add(nb.build());
177
178                 NodesBuilder olmNb = new NodesBuilder();
179                 olmNb.setNodeId(values.getNodeID());
180                 olmNb.setDestTp(desID);
181                 olmNb.setSrcTp(srcID);
182                 olmList.add(olmNb.build());
183                 srcID = null;
184                 desID = null;
185             } else {
186                 LOG.warn("both, the source and destination id are null!");
187             }
188         }
189         return new NodeLists(olmList, list);
190     }
191
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<>();
196         String resourceType;
197         TerminationPoint tp;
198         String tpID = "";
199         String nodeID = "";
200         String sortId = "";
201
202         while (iterator.hasNext()) {
203             AToZ pathDesObj = iterator.next();
204             resourceType = pathDesObj.getResource().getResource().getImplementedInterface().getSimpleName();
205             LOG.info("Inside AtoZ {}", resourceType);
206             try {
207                 if (TERMINATION_POINT.equals(resourceType)) {
208                     tp = (TerminationPoint) pathDesObj.getResource().getResource();
209                     LOG.info(" TP is {} {}", tp.getTpId(),
210                             tp.getTpNodeId());
211                     tpID = tp.getTpId();
212                     nodeID = tp.getTpNodeId();
213                     sortId = pathDesObj.getId();
214
215                     //TODO: do not rely on ID to be in certain format
216                     if (tpID.contains("CTP") || tpID.contains("CP")) {
217                         continue;
218                     }
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)) {
223                         continue;
224                     }
225
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");
232                 } else {
233                     LOG.info("The type is not indentified: {}", resourceType);
234                 }
235             } catch (IllegalArgumentException | SecurityException e) {
236                 //TODO: Auto-generated catch block
237                 LOG.error("Dont find the getResource method", e);
238             }
239         }
240
241         String desID = null;
242         String srcID = null;
243         for (NodeIdPair values : treeMap.values()) {
244             if (srcID == null) {
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()));
250                 nb.setDestTp(desID);
251                 nb.setSrcTp(srcID);
252                 list.add(nb.build());
253
254                 NodesBuilder olmNb = new NodesBuilder();
255                 olmNb.setNodeId(values.getNodeID());
256                 olmNb.setDestTp(desID);
257                 olmNb.setSrcTp(srcID);
258                 olmList.add(olmNb.build());
259                 srcID = null;
260                 desID = null;
261             } else {
262                 LOG.warn("both, the source and destination id are null!");
263             }
264         }
265         return new NodeLists(olmList, list);
266     }
267
268     public static int[] findTheLongestSubstring(String s1, String s2) {
269         if ((s1 == null) || (s2 == null)) {
270             return null;
271         }
272         int[][] dp = new int[s1.length() + 1][s2.length() + 1];
273         int maxLen = 0;
274         int endPos = 0;
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);
279                 if (ch1 == ch2) {
280                     dp[i][j] = dp[i - 1][j - 1] + 1;
281                     if (dp[i][j] >= maxLen) {
282                         maxLen = dp[i][j];
283                         endPos = i;
284                     }
285                 }
286             }
287         }
288         return new int[] { endPos - maxLen, endPos };
289     }
290
291 }