Move otn link update from renderer to SH
[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, Action operation) {
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(operation)
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, Action operation) {
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(operation)
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, Action operation,
219         String serviceFormat, 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;
224         if (asideToZside) {
225             nodeLists = getNodesListAToZ(pathDescription.getAToZDirection().nonnullAToZ().values().iterator());
226         } else {
227             nodeLists = getNodesListZtoA(pathDescription.getZToADirection().nonnullZToA().values().iterator());
228         }
229         LOG.info("These are node-lists {}, {}", nodeLists.getRendererNodeList(), nodeLists.getOlmNodeList());
230         for (Nodes node: nodeLists.getRendererNodeList()) {
231             nodes.add(new org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev210618.otn.renderer.nodes
232                 .NodesBuilder()
233                             .setNodeId(node.getNodeId())
234                             .setClientTp(node.getSrcTp())
235                             .setNetworkTp(node.getDestTp())
236                             .build());
237         }
238         OtnServicePathInputBuilder otnServicePathInputBuilder = new OtnServicePathInputBuilder()
239             .setServiceName(serviceName)
240             .setOperation(operation)
241             .setServiceFormat(serviceFormat)
242             .setServiceRate(serviceRate)
243             .setNodes(nodes);
244
245         // set the trib-slots and trib-ports for the lower oder odu
246         if (serviceRate.intValue() == 1 || (serviceRate.intValue() == 10)) {
247             Short tribPort = Short.valueOf(pathDescription.getAToZDirection().getMinTribSlot().getValue()
248                 .split("\\.")[0]);
249             Short minTribSlot = Short.valueOf(pathDescription.getAToZDirection().getMinTribSlot().getValue()
250                 .split("\\.")[1]);
251             otnServicePathInputBuilder
252                 .setTribPortNumber(tribPort)
253                 .setTribSlot(minTribSlot);
254         }
255         return otnServicePathInputBuilder.build();
256     }
257
258     public static ServicePathInput rendererDeleteServiceInput(String serviceName,
259             ServiceDeleteInput serviceDeleteInput) {
260         //TODO: finish model-model mapping
261         return new ServicePathInputBuilder().setServiceName(serviceName).build();
262     }
263
264     private static NodeLists getNodesListZtoA(Iterator<ZToA> iterator) {
265         Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
266         List<Nodes> olmList = new ArrayList<>();
267         List<Nodes> list = new ArrayList<>();
268         String resourceType;
269         TerminationPoint tp;
270         String tpID = "";
271         String nodeID = "";
272         String sortId = "";
273         while (iterator.hasNext()) {
274             ZToA pathDesObj = iterator.next();
275             resourceType = pathDesObj.getResource().getResource().implementedInterface().getSimpleName();
276             LOG.info("Inside ZtoA {}", resourceType);
277
278             try {
279                 if (TERMINATION_POINT.equals(resourceType)) {
280                     tp = (TerminationPoint) pathDesObj.getResource().getResource();
281                     LOG.info(" TP is {} {}", tp.getTpId(),
282                             tp.getTpNodeId());
283                     tpID = tp.getTpId();
284                     sortId = pathDesObj.getId();
285
286                     //TODO: do not rely on ID to be in certain format
287                     if (tpID.contains("CTP") || tpID.contains("CP")) {
288                         continue;
289                     }
290                     if (tpID.contains(StringConstants.TTP_TOKEN)) {
291                         nodeID = tp.getTpNodeId().split("-DEG")[0];
292                     } else if (tpID.contains(StringConstants.PP_TOKEN)) {
293                         nodeID = tp.getTpNodeId().split("-SRG")[0];
294                     } else if (tpID.contains(StringConstants.NETWORK_TOKEN)
295                         || tpID.contains(StringConstants.CLIENT_TOKEN) || tpID.isEmpty()) {
296                         nodeID = tp.getTpNodeId().split("-XPDR")[0];
297                     } else {
298                         continue;
299                     }
300                     int id = Integer.parseInt(sortId);
301                     treeMap.put(id, new NodeIdPair(nodeID, tpID));
302                 } else if ("Link".equals(resourceType)) {
303                     LOG.info("The type is link");
304                 } else {
305                     LOG.info("The type is not identified: {}", resourceType);
306                 }
307             } catch (IllegalArgumentException | SecurityException e) {
308                 LOG.error("Dont find the getResource method", e);
309             }
310         }
311         populateNodeLists(treeMap, list, olmList, false);
312         return new NodeLists(olmList, list);
313     }
314
315     private static NodeLists getNodesListAToZ(Iterator<AToZ> iterator) {
316         Map<Integer, NodeIdPair> treeMap = new TreeMap<>();
317         List<Nodes> list = new ArrayList<>();
318         List<Nodes> olmList = new ArrayList<>();
319         String resourceType;
320         TerminationPoint tp;
321         String tpID = "";
322         String nodeID = "";
323         String sortId = "";
324
325         while (iterator.hasNext()) {
326             AToZ pathDesObj = iterator.next();
327             resourceType = pathDesObj.getResource().getResource().implementedInterface().getSimpleName();
328             LOG.info("Inside AtoZ {}", resourceType);
329             try {
330                 if (TERMINATION_POINT.equals(resourceType)) {
331                     tp = (TerminationPoint) pathDesObj.getResource().getResource();
332                     LOG.info("TP is {} {}", tp.getTpId(),
333                             tp.getTpNodeId());
334                     tpID = tp.getTpId();
335                     sortId = pathDesObj.getId();
336
337                     //TODO: do not rely on ID to be in certain format
338                     if (tpID.contains("CTP") || tpID.contains("CP")) {
339                         continue;
340                     }
341                     if (tpID.contains(StringConstants.TTP_TOKEN)) {
342                         nodeID = tp.getTpNodeId().split("-DEG")[0];
343                     } else if (tpID.contains(StringConstants.PP_TOKEN)) {
344                         nodeID = tp.getTpNodeId().split("-SRG")[0];
345                     } else if (tpID.contains(StringConstants.NETWORK_TOKEN)
346                         || tpID.contains(StringConstants.CLIENT_TOKEN) || tpID.isEmpty()) {
347                         nodeID = tp.getTpNodeId().split("-XPDR")[0];
348                     } else {
349                         continue;
350                     }
351                     int id = Integer.parseInt(sortId);
352                     treeMap.put(id, new NodeIdPair(nodeID, tpID));
353                 } else if ("Link".equals(resourceType)) {
354                     LOG.info("The type is link");
355                 } else {
356                     LOG.info("The type is not identified: {}", resourceType);
357                 }
358             } catch (IllegalArgumentException | SecurityException e) {
359                 //TODO: Auto-generated catch block
360                 LOG.error("Did not find the getResource method", e);
361             }
362         }
363         populateNodeLists(treeMap, list, olmList, true);
364         return new NodeLists(olmList, list);
365     }
366
367     @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
368         value = {"NP_LOAD_OF_KNOWN_NULL_VALUE","RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE"},
369         justification = "loop when value is not always null - "
370                 + "TODO: check if something exists in Java lib")
371     private static void populateNodeLists(Map<Integer, NodeIdPair> treeMap, List<Nodes> list, List<Nodes> olmList,
372         boolean isAToz) {
373         String desID = null;
374         String srcID = null;
375         LOG.info("treeMap values = {}", treeMap.values());
376         for (NodeIdPair values : treeMap.values()) {
377             if (srcID == null) {
378                 srcID = values.getTpID();
379             } else if (desID == null) {
380                 desID = values.getTpID();
381                 NodesBuilder olmNb = new NodesBuilder()
382                     .setNodeId(values.getNodeID())
383                     .setDestTp(desID)
384                     .setSrcTp(srcID);
385                 olmList.add(olmNb.build());
386                 if (srcID.isEmpty()) {
387                     srcID = null;
388                 }
389                 if (desID.isEmpty()) {
390                     desID = new StringBuilder(srcID).toString();
391                     srcID = null;
392                 }
393                 if (isAToz) {
394                     NodesBuilder nb = new NodesBuilder()
395                         .withKey(new NodesKey(values.getNodeID()))
396                         .setDestTp(desID)
397                         .setSrcTp(srcID);
398                     if (srcID != null && desID != null && srcID.contains(StringConstants.NETWORK_TOKEN)) {
399                         nb.setDestTp(srcID).setSrcTp(desID);
400                     }
401                     list.add(nb.build());
402                 } else {
403                     if (srcID != null && desID != null && !srcID.contains(StringConstants.NETWORK_TOKEN)
404                         && !desID.contains(StringConstants.NETWORK_TOKEN)) {
405                         NodesBuilder nb = new NodesBuilder()
406                             .withKey(new NodesKey(values.getNodeID()))
407                             .setDestTp(desID)
408                             .setSrcTp(srcID);
409                         list.add(nb.build());
410                     }
411                 }
412                 srcID = null;
413                 desID = null;
414             } else {
415                 LOG.warn("both, the source and destination id are null!");
416             }
417         }
418     }
419
420
421     @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
422             value = "PZLA_PREFER_ZERO_LENGTH_ARRAYS",
423             justification = "not relevant to return and zero length array"
424                     + " as we need real pos")
425     public static int[] findTheLongestSubstring(String s1, String s2) {
426         if ((s1 == null) || (s2 == null)) {
427             return null;
428         }
429         int[][] dp = new int[s1.length() + 1][s2.length() + 1];
430         int maxLen = 0;
431         int endPos = 0;
432         for (int i = 1; i < dp.length; i++) {
433             for (int j = 1; j < dp[0].length; j++) {
434                 char ch1 = s1.charAt(i - 1);
435                 char ch2 = s2.charAt(j - 1);
436                 if (ch1 == ch2) {
437                     dp[i][j] = dp[i - 1][j - 1] + 1;
438                     if (dp[i][j] >= maxLen) {
439                         maxLen = dp[i][j];
440                         endPos = i;
441                     }
442                 }
443             }
444         }
445         return new int[] { endPos - maxLen, endPos };
446     }
447
448 }