09c6f3391c79a8778f6a92ff94f5b94596f01a34
[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.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.service.rev190531.ServiceDeleteInput;
39 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.PathDescription;
40 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.AToZDirection;
41 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.ZToADirection;
42 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.atoz.direction.AToZ;
43 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.path.description.ztoa.direction.ZToA;
44 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev210705.pce.resource.resource.resource.TerminationPoint;
45 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210618.optical.renderer.nodes.Nodes;
46 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210618.optical.renderer.nodes.NodesBuilder;
47 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210618.optical.renderer.nodes.NodesKey;
48 import org.opendaylight.yangtools.yang.common.RpcResult;
49 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
50 import org.opendaylight.yangtools.yang.common.Uint32;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54
55
56 public final class ModelMappingUtils {
57
58     private static final Logger LOG = LoggerFactory.getLogger(ModelMappingUtils.class);
59     private static final String TERMINATION_POINT = "TerminationPoint";
60
61     private ModelMappingUtils() {
62     }
63
64     public static ServicePowerSetupInput createServicePowerSetupInput(List<Nodes> olmList,
65             ServiceImplementationRequestInput input) {
66         ServicePowerSetupInputBuilder olmSetupBldr = new ServicePowerSetupInputBuilder().setNodes(olmList);
67         if (input != null && input.getPathDescription() != null
68                 && input.getPathDescription().getAToZDirection() != null) {
69             AToZDirection atoZDirection = input.getPathDescription().getAToZDirection();
70             olmSetupBldr.setWaveNumber(atoZDirection.getAToZWavelengthNumber());
71             if (atoZDirection.getAToZMinFrequency() != null) {
72                 olmSetupBldr.setLowerSpectralSlotNumber(Uint32
73                         .valueOf(GridUtils
74                                 .getLowerSpectralIndexFromFrequency(atoZDirection.getAToZMinFrequency().getValue())));
75             }
76             if (atoZDirection.getAToZMaxFrequency() != null) {
77                 olmSetupBldr.setHigherSpectralSlotNumber(Uint32
78                         .valueOf(GridUtils
79                                 .getHigherSpectralIndexFromFrequency(atoZDirection.getAToZMaxFrequency().getValue())));
80             }
81         }
82         return olmSetupBldr.build();
83     }
84
85     public static ServiceImplementationRequestOutput createServiceImplResponse(String responseCode, String message) {
86         return new ServiceImplementationRequestOutputBuilder()
87                 .setConfigurationResponseCommon(createCommonResponse(responseCode, message))
88                 .build();
89     }
90
91     public static ServiceDeleteOutput createServiceDeleteResponse(String responseCode, String message) {
92         return new ServiceDeleteOutputBuilder()
93                 .setConfigurationResponseCommon(createCommonResponse(responseCode, message))
94                 .build();
95     }
96
97     public static ConfigurationResponseCommon createCommonResponse(String responseCode, String message) {
98         return new ConfigurationResponseCommonBuilder()
99                 .setResponseMessage(message)
100                 .setResponseCode(responseCode)
101                 .build();
102     }
103
104     public static ListenableFuture<RpcResult<ServiceImplementationRequestOutput>>
105             createServiceImplementationRpcResponse(ServiceImplementationRequestOutput payload) {
106         return RpcResultBuilder.success(payload).buildFuture();
107     }
108
109     public static ListenableFuture<RpcResult<ServiceDeleteOutput>>
110             createServiceDeleteRpcResponse(ServiceDeleteOutput payload) {
111         return RpcResultBuilder.success(payload).buildFuture();
112     }
113
114     public static ServicePathInputData rendererCreateServiceInputAToZ(String serviceName,
115             PathDescription pathDescription) {
116         int scale = GridConstant.FIXED_GRID_FREQUENCY_PRECISION;
117         AToZDirection atoZDirection = pathDescription.getAToZDirection();
118         LOG.info("Building ServicePathInputData for a to z direction {}", atoZDirection);
119         NodeLists nodeLists = getNodesListAToZ(atoZDirection.nonnullAToZ().values().iterator());
120         ServicePathInputBuilder servicePathInputBuilder = new ServicePathInputBuilder()
121             .setServiceName(serviceName)
122             .setOperation(Action.Create)
123             .setNodes(nodeLists.getRendererNodeList())
124             .setWidth(new FrequencyGHz(GridConstant.WIDTH_40));
125         if (atoZDirection.getAToZWavelengthNumber() != null) {
126             servicePathInputBuilder
127                 .setWaveNumber(atoZDirection.getAToZWavelengthNumber());
128         }
129         if (Uint32.valueOf(GridConstant.IRRELEVANT_WAVELENGTH_NUMBER)
130                 .equals(atoZDirection.getAToZWavelengthNumber())) {
131             scale = GridConstant.FLEX_GRID_FREQUENCY_PRECISION;
132         }
133         if (atoZDirection.getAToZMinFrequency() != null) {
134             servicePathInputBuilder.setMinFreq(new FrequencyTHz(atoZDirection.getAToZMinFrequency().getValue()));
135             servicePathInputBuilder.setLowerSpectralSlotNumber(Uint32
136                     .valueOf(GridUtils
137                             .getLowerSpectralIndexFromFrequency(atoZDirection.getAToZMinFrequency().getValue())));
138         }
139         if (atoZDirection.getAToZMaxFrequency() != null) {
140             servicePathInputBuilder.setMaxFreq(new FrequencyTHz(atoZDirection.getAToZMaxFrequency().getValue()));
141             servicePathInputBuilder.setHigherSpectralSlotNumber(
142                     Uint32.valueOf(GridUtils
143                             .getHigherSpectralIndexFromFrequency(atoZDirection.getAToZMaxFrequency().getValue())));
144         }
145         if (atoZDirection.getAToZMinFrequency() != null && atoZDirection.getAToZMaxFrequency() != null) {
146             servicePathInputBuilder.setCenterFreq(
147                     GridUtils.getCentralFrequencyWithPrecision(atoZDirection.getAToZMinFrequency().getValue(),
148                             atoZDirection.getAToZMaxFrequency().getValue(), scale));
149         }
150         if (atoZDirection.getRate() != null && atoZDirection.getModulationFormat() != null) {
151             Optional<ModulationFormat> optionalModulationFormat = ModulationFormat
152                     .forName(atoZDirection.getModulationFormat());
153             if (optionalModulationFormat.isPresent()
154                     && GridConstant.FREQUENCY_WIDTH_TABLE
155                     .contains(atoZDirection.getRate(), optionalModulationFormat.get())) {
156                 servicePathInputBuilder
157                     .setWidth(FrequencyGHz
158                         .getDefaultInstance(GridConstant.FREQUENCY_WIDTH_TABLE.get(atoZDirection.getRate(),
159                         optionalModulationFormat.get())));
160             }
161         }
162         servicePathInputBuilder.setModulationFormat(atoZDirection.getModulationFormat());
163         return new ServicePathInputData(servicePathInputBuilder.build(), nodeLists);
164     }
165
166     public static ServicePathInputData rendererCreateServiceInputZToA(String serviceName,
167             PathDescription pathDescription) {
168         int scale = GridConstant.FIXED_GRID_FREQUENCY_PRECISION;
169         ZToADirection ztoADirection = pathDescription.getZToADirection();
170         LOG.info("Building ServicePathInputData for z to a direction {}", ztoADirection);
171         NodeLists nodeLists = getNodesListZtoA(pathDescription.getZToADirection().nonnullZToA().values().iterator());
172         ServicePathInputBuilder servicePathInputBuilder = new ServicePathInputBuilder()
173             .setOperation(Action.Create)
174             .setServiceName(serviceName)
175             .setNodes(nodeLists.getRendererNodeList())
176             .setWidth(new FrequencyGHz(GridConstant.WIDTH_40));
177         if (ztoADirection.getZToAWavelengthNumber() != null) {
178             servicePathInputBuilder
179                 .setWaveNumber(ztoADirection.getZToAWavelengthNumber());
180         }
181         if (Uint32.valueOf(GridConstant.IRRELEVANT_WAVELENGTH_NUMBER)
182                 .equals(ztoADirection.getZToAWavelengthNumber())) {
183             scale = GridConstant.FLEX_GRID_FREQUENCY_PRECISION;
184         }
185         if (ztoADirection.getZToAMinFrequency() != null) {
186             servicePathInputBuilder.setMinFreq(new FrequencyTHz(ztoADirection.getZToAMinFrequency().getValue()));
187             servicePathInputBuilder.setLowerSpectralSlotNumber(Uint32
188                     .valueOf(GridUtils
189                             .getLowerSpectralIndexFromFrequency(ztoADirection.getZToAMinFrequency().getValue())));
190         }
191         if (ztoADirection.getZToAMaxFrequency() != null) {
192             servicePathInputBuilder.setMaxFreq(new FrequencyTHz(ztoADirection.getZToAMaxFrequency().getValue()));
193             servicePathInputBuilder.setHigherSpectralSlotNumber(
194                     Uint32.valueOf(GridUtils
195                             .getHigherSpectralIndexFromFrequency(ztoADirection.getZToAMaxFrequency().getValue())));
196         }
197         if (ztoADirection.getZToAMinFrequency() != null && ztoADirection.getZToAMaxFrequency() != null) {
198             servicePathInputBuilder.setCenterFreq(
199                     GridUtils.getCentralFrequencyWithPrecision(ztoADirection.getZToAMinFrequency().getValue(),
200                             ztoADirection.getZToAMaxFrequency().getValue(), scale));
201         }
202         if (ztoADirection.getRate() != null && ztoADirection.getModulationFormat() != null) {
203             Optional<ModulationFormat> optionalModulationFormat = ModulationFormat
204                     .forName(ztoADirection.getModulationFormat());
205             if (optionalModulationFormat.isPresent()
206                     && GridConstant.FREQUENCY_WIDTH_TABLE
207                     .contains(ztoADirection.getRate(), optionalModulationFormat.get())) {
208                 servicePathInputBuilder.setWidth(FrequencyGHz
209                         .getDefaultInstance(GridConstant.FREQUENCY_WIDTH_TABLE.get(ztoADirection.getRate(),
210                                 optionalModulationFormat.get())));
211             }
212         }
213         servicePathInputBuilder.setModulationFormat(ztoADirection.getModulationFormat());
214         return new ServicePathInputData(servicePathInputBuilder.build(), nodeLists);
215     }
216
217     // Adding createOtnServiceInputpath for A-Z and Z-A directions as one method
218     public static OtnServicePathInput rendererCreateOtnServiceInput(String serviceName, String serviceFormat,
219         Uint32 serviceRate, PathDescription pathDescription, boolean asideToZside) {
220         // If atoZ is set true use A-to-Z direction otherwise use Z-to-A
221         List<org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210618.otn.renderer.nodes.Nodes> nodes =
222             new ArrayList<>();
223         NodeLists nodeLists = getNodesListAToZ(pathDescription.getAToZDirection().nonnullAToZ().values().iterator());
224         if (!asideToZside) {
225             nodeLists = getNodesListZtoA(pathDescription.getZToADirection().nonnullZToA().values().iterator());
226         }
227         LOG.info("These are node-lists {}, {}", nodeLists.getRendererNodeList(), nodeLists.getOlmNodeList());
228         for (Nodes node: nodeLists.getRendererNodeList()) {
229             nodes.add(new org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210618.otn.renderer.nodes
230                 .NodesBuilder()
231                             .setNodeId(node.getNodeId())
232                             .setClientTp(node.getSrcTp())
233                             .setNetworkTp(node.getDestTp())
234                             .build());
235         }
236         OtnServicePathInputBuilder otnServicePathInputBuilder = new OtnServicePathInputBuilder()
237             .setServiceName(serviceName)
238             .setServiceFormat(serviceFormat)
239             .setServiceRate(serviceRate)
240             .setNodes(nodes);
241
242         // set the trib-slots and trib-ports for the lower oder odu
243         if (serviceRate.intValue() == 1 || (serviceRate.intValue() == 10)) {
244             Short tribPort = Short.valueOf(pathDescription.getAToZDirection().getMinTribSlot().getValue()
245                 .split("\\.")[0]);
246             Short minTribSlot = Short.valueOf(pathDescription.getAToZDirection().getMinTribSlot().getValue()
247                 .split("\\.")[1]);
248             otnServicePathInputBuilder
249                 .setTribPortNumber(tribPort)
250                 .setTribSlot(minTribSlot);
251         }
252         return otnServicePathInputBuilder.build();
253     }
254
255     public static ServicePathInput rendererDeleteServiceInput(String serviceName,
256             ServiceDeleteInput serviceDeleteInput) {
257         //TODO: finish model-model mapping
258         return new ServicePathInputBuilder().setServiceName(serviceName).build();
259     }
260
261     private static NodeLists getNodesListZtoA(Iterator<ZToA> iterator) {
262         Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
263         List<Nodes> olmList = new ArrayList<>();
264         List<Nodes> list = new ArrayList<>();
265         String resourceType;
266         TerminationPoint tp;
267         String tpID = "";
268         String nodeID = "";
269         String sortId = "";
270         while (iterator.hasNext()) {
271             ZToA pathDesObj = iterator.next();
272             resourceType = pathDesObj.getResource().getResource().implementedInterface().getSimpleName();
273             LOG.info("Inside ZtoA {}", resourceType);
274
275             try {
276                 if (TERMINATION_POINT.equals(resourceType)) {
277                     tp = (TerminationPoint) pathDesObj.getResource().getResource();
278                     LOG.info(" TP is {} {}", tp.getTpId(),
279                             tp.getTpNodeId());
280                     tpID = tp.getTpId();
281                     sortId = pathDesObj.getId();
282
283                     //TODO: do not rely on ID to be in certain format
284                     if (tpID.contains("CTP") || tpID.contains("CP")) {
285                         continue;
286                     }
287                     if (tpID.contains(StringConstants.TTP_TOKEN)) {
288                         nodeID = tp.getTpNodeId().split("-DEG")[0];
289                     } else if (tpID.contains(StringConstants.PP_TOKEN)) {
290                         nodeID = tp.getTpNodeId().split("-SRG")[0];
291                     } else if (tpID.contains(StringConstants.NETWORK_TOKEN)
292                         || tpID.contains(StringConstants.CLIENT_TOKEN) || tpID.isEmpty()) {
293                         nodeID = tp.getTpNodeId().split("-XPDR")[0];
294                     } else {
295                         continue;
296                     }
297                     int id = Integer.parseInt(sortId);
298                     treeMap.put(id, new NodeIdPair(nodeID, tpID));
299                 } else if ("Link".equals(resourceType)) {
300                     LOG.info("The type is link");
301                 } else {
302                     LOG.info("The type is not identified: {}", resourceType);
303                 }
304             } catch (IllegalArgumentException | SecurityException e) {
305                 LOG.error("Dont find the getResource method", e);
306             }
307         }
308         populateNodeLists(treeMap, list, olmList, false);
309         return new NodeLists(olmList, list);
310     }
311
312     private static NodeLists getNodesListAToZ(Iterator<AToZ> iterator) {
313         Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
314         List<Nodes> list = new ArrayList<>();
315         List<Nodes> olmList = new ArrayList<>();
316         String resourceType;
317         TerminationPoint tp;
318         String tpID = "";
319         String nodeID = "";
320         String sortId = "";
321
322         while (iterator.hasNext()) {
323             AToZ pathDesObj = iterator.next();
324             resourceType = pathDesObj.getResource().getResource().implementedInterface().getSimpleName();
325             LOG.info("Inside AtoZ {}", resourceType);
326             try {
327                 if (TERMINATION_POINT.equals(resourceType)) {
328                     tp = (TerminationPoint) pathDesObj.getResource().getResource();
329                     LOG.info("TP is {} {}", tp.getTpId(),
330                             tp.getTpNodeId());
331                     tpID = tp.getTpId();
332                     sortId = pathDesObj.getId();
333
334                     //TODO: do not rely on ID to be in certain format
335                     if (tpID.contains("CTP") || tpID.contains("CP")) {
336                         continue;
337                     }
338                     if (tpID.contains(StringConstants.TTP_TOKEN)) {
339                         nodeID = tp.getTpNodeId().split("-DEG")[0];
340                     } else if (tpID.contains(StringConstants.PP_TOKEN)) {
341                         nodeID = tp.getTpNodeId().split("-SRG")[0];
342                     } else if (tpID.contains(StringConstants.NETWORK_TOKEN)
343                         || tpID.contains(StringConstants.CLIENT_TOKEN) || tpID.isEmpty()) {
344                         nodeID = tp.getTpNodeId().split("-XPDR")[0];
345                     } else {
346                         continue;
347                     }
348                     int id = Integer.parseInt(sortId);
349                     treeMap.put(id, new NodeIdPair(nodeID, tpID));
350                 } else if ("Link".equals(resourceType)) {
351                     LOG.info("The type is link");
352                 } else {
353                     LOG.info("The type is not identified: {}", resourceType);
354                 }
355             } catch (IllegalArgumentException | SecurityException e) {
356                 //TODO: Auto-generated catch block
357                 LOG.error("Did not find the getResource method", e);
358             }
359         }
360         populateNodeLists(treeMap, list, olmList, true);
361         return new NodeLists(olmList, list);
362     }
363
364     @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
365         value = {"NP_LOAD_OF_KNOWN_NULL_VALUE","RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE"},
366         justification = "loop when value is not always null - "
367                 + "TODO: check if something exists in Java lib")
368     private static void populateNodeLists(Map<Integer, NodeIdPair> treeMap, List<Nodes> list, List<Nodes> olmList,
369         boolean isAToz) {
370         String desID = null;
371         String srcID = null;
372         LOG.info("treeMap values = {}", treeMap.values());
373         for (NodeIdPair values : treeMap.values()) {
374             if (srcID == null) {
375                 srcID = values.getTpID();
376             } else if (desID == null) {
377                 desID = values.getTpID();
378                 NodesBuilder olmNb = new NodesBuilder()
379                     .setNodeId(values.getNodeID())
380                     .setDestTp(desID)
381                     .setSrcTp(srcID);
382                 olmList.add(olmNb.build());
383                 if (srcID.isEmpty()) {
384                     srcID = null;
385                 }
386                 if (desID.isEmpty()) {
387                     desID = new StringBuilder(srcID).toString();
388                     srcID = null;
389                 }
390                 if (isAToz) {
391                     NodesBuilder nb = new NodesBuilder()
392                         .withKey(new NodesKey(values.getNodeID()))
393                         .setDestTp(desID)
394                         .setSrcTp(srcID);
395                     if (srcID != null && desID != null && srcID.contains(StringConstants.NETWORK_TOKEN)) {
396                         nb.setDestTp(srcID).setSrcTp(desID);
397                     }
398                     list.add(nb.build());
399                 } else {
400                     if (srcID != null && desID != null && !srcID.contains(StringConstants.NETWORK_TOKEN)
401                         && !desID.contains(StringConstants.NETWORK_TOKEN)) {
402                         NodesBuilder nb = new NodesBuilder()
403                             .withKey(new NodesKey(values.getNodeID()))
404                             .setDestTp(desID)
405                             .setSrcTp(srcID);
406                         list.add(nb.build());
407                     }
408                 }
409                 srcID = null;
410                 desID = null;
411             } else {
412                 LOG.warn("both, the source and destination id are null!");
413             }
414         }
415     }
416
417
418     @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
419             value = "PZLA_PREFER_ZERO_LENGTH_ARRAYS",
420             justification = "not relevant to return and zero length array"
421                     + " as we need real pos")
422     public static int[] findTheLongestSubstring(String s1, String s2) {
423         if ((s1 == null) || (s2 == null)) {
424             return null;
425         }
426         int[][] dp = new int[s1.length() + 1][s2.length() + 1];
427         int maxLen = 0;
428         int endPos = 0;
429         for (int i = 1; i < dp.length; i++) {
430             for (int j = 1; j < dp[0].length; j++) {
431                 char ch1 = s1.charAt(i - 1);
432                 char ch2 = s2.charAt(j - 1);
433                 if (ch1 == ch2) {
434                     dp[i][j] = dp[i - 1][j - 1] + 1;
435                     if (dp[i][j] >= maxLen) {
436                         maxLen = dp[i][j];
437                         endPos = i;
438                     }
439                 }
440             }
441         }
442         return new int[] { endPos - maxLen, endPos };
443     }
444
445 }