From 66f6af8141d231619ad9d6e6133910e14f3a32e2 Mon Sep 17 00:00:00 2001 From: wede7391 Date: Wed, 25 May 2022 22:26:53 +0200 Subject: [PATCH] pce(gnpy) for Sulfur - replace jackson module with MessageBodyWriter and MessageBodyReader to avoid intermediate String (fix scientific notation for decimal) - add createDataObjectFromInputStream to JsonConverter - put decimal value in gnpy test data JIRA: TRNSPRTPCE-639 Signed-off-by: wede7391 Change-Id: I4399bf1004709860796884e3c990c8466cf57ca8 --- .../common/converter/JsonStringConverter.java | 39 ++++++++++--- .../pce/gnpy/GnpyServiceImpl.java | 4 +- .../transportpce/pce/gnpy/GnpyTopoImpl.java | 3 +- .../pce/gnpy/consumer/GnpyApiModule.java | 29 ---------- .../pce/gnpy/consumer/GnpyConsumerImpl.java | 13 ++--- .../pce/gnpy/consumer/JsonConfigurator.java | 38 ------------- .../consumer/RequestMessageBodyWriter.java | 57 +++++++++++++++++++ .../pce/gnpy/consumer/RequestSerializer.java | 45 --------------- .../pce/gnpy/consumer/ResultDeserializer.java | 47 --------------- .../consumer/ResultMessageBodyReader.java | 48 ++++++++++++++++ .../pce/graph/PostAlgoPathValidator.java | 52 +++++------------ .../pce/gnpy/GnpyUtilitiesImplTest.java | 2 - pce/src/test/resources/gnpy/gnpy_request.json | 4 +- .../resources/gnpy/gnpy_result_no_path.json | 4 +- .../resources/gnpy/gnpy_result_with_path.json | 2 +- 15 files changed, 167 insertions(+), 220 deletions(-) mode change 100644 => 100755 common/src/main/java/org/opendaylight/transportpce/common/converter/JsonStringConverter.java delete mode 100644 pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/GnpyApiModule.java delete mode 100644 pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/JsonConfigurator.java create mode 100644 pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/RequestMessageBodyWriter.java delete mode 100644 pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/RequestSerializer.java delete mode 100644 pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/ResultDeserializer.java create mode 100644 pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/ResultMessageBodyReader.java diff --git a/common/src/main/java/org/opendaylight/transportpce/common/converter/JsonStringConverter.java b/common/src/main/java/org/opendaylight/transportpce/common/converter/JsonStringConverter.java old mode 100644 new mode 100755 index f29e66056..52f8e4a19 --- a/common/src/main/java/org/opendaylight/transportpce/common/converter/JsonStringConverter.java +++ b/common/src/main/java/org/opendaylight/transportpce/common/converter/JsonStringConverter.java @@ -13,9 +13,13 @@ import com.google.gson.JsonParser; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; +import java.nio.charset.StandardCharsets; import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecServices; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; @@ -63,7 +67,7 @@ public class JsonStringConverter { JSONCodecFactory codecFactory = supplier .getShared(bindingDOMCodecServices.getRuntimeContext().getEffectiveModelContext()); try (Writer writer = new StringWriter(); - JsonWriter jsonWriter = JsonWriterFactory.createJsonWriter(writer, 4);) { + JsonWriter jsonWriter = JsonWriterFactory.createJsonWriter(writer, 4)) { EffectiveStatementInference rootNode = SchemaInferenceStack .of(bindingDOMCodecServices.getRuntimeContext().getEffectiveModelContext()) .toInference(); @@ -93,17 +97,36 @@ public class JsonStringConverter { @SuppressWarnings("unchecked") public T createDataObjectFromJsonString(YangInstanceIdentifier path, String jsonString, JSONCodecFactorySupplier supplier) { - JsonReader reader = new JsonReader(new StringReader(jsonString)); + return createDataObjectFromReader(path, new StringReader(jsonString), supplier); + } + + public T createDataObjectFromInputStream(YangInstanceIdentifier path, InputStream jsonStream, + JSONCodecFactorySupplier supplier) { + return createDataObjectFromReader(path, new InputStreamReader(jsonStream, StandardCharsets.UTF_8), supplier); + } + + /** + * Create a dataObject of T type from Reader. + * @param path YangInstanceIdentifier + * @param inputReader Reader (could be all class implementing Reader) containing Json data. + * @param supplier RFC7951 or DRAFT_LHOTKA_NETMOD_YANG_JSON_02 + * @return the created object. + */ + private T createDataObjectFromReader(YangInstanceIdentifier path, Reader inputReader, + JSONCodecFactorySupplier supplier) { + NormalizedNodeResult result = new NormalizedNodeResult(); - try (NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result); - JsonParserStream jsonParser = JsonParserStream - .create( - streamWriter, - supplier.getShared(bindingDOMCodecServices.getRuntimeContext().getEffectiveModelContext()))) { + try (JsonReader reader = new JsonReader(inputReader); + NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result); + JsonParserStream jsonParser = JsonParserStream + .create( + streamWriter, + supplier.getShared(bindingDOMCodecServices + .getRuntimeContext().getEffectiveModelContext()))) { jsonParser.parse(reader); return (T) bindingDOMCodecServices.fromNormalizedNode(path, result.getResult()).getValue(); } catch (IOException e) { - LOG.warn("An error occured during parsing Json input stream", e); + LOG.warn("An error occured during parsing input reader", e); return null; } } diff --git a/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/GnpyServiceImpl.java b/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/GnpyServiceImpl.java index d8d7bd4dd..02eade3f5 100644 --- a/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/GnpyServiceImpl.java +++ b/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/GnpyServiceImpl.java @@ -11,6 +11,7 @@ package org.opendaylight.transportpce.pce.gnpy; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Table; import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -373,7 +374,8 @@ public class GnpyServiceImpl { .setTechnology("flexi-grid").setTrxType("OpenROADM MSA ver. 5.0") .setTrxMode(TRX_MODE_TABLE.get(Uint32.valueOf(rate), spacing)) .setOutputPower(Decimal64.valueOf(GridUtils.convertDbmW(GridConstant.OUTPUT_POWER_100GB_DBM - + 10 * Math.log10(mvalue / (double)GridConstant.NB_SLOTS_100G)))) + + 10 * Math.log10(mvalue / (double)GridConstant.NB_SLOTS_100G)) + .setScale(6, RoundingMode.CEILING))) .setEffectiveFreqSlot(Map.of(effectiveFreqSlot.key(), effectiveFreqSlot)) .setSpacing(Decimal64.valueOf(spacing.multiply(BigDecimal.valueOf(1e9)))) .build(); diff --git a/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/GnpyTopoImpl.java b/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/GnpyTopoImpl.java index 07528bfab..5687e4b73 100644 --- a/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/GnpyTopoImpl.java +++ b/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/GnpyTopoImpl.java @@ -9,6 +9,7 @@ package org.opendaylight.transportpce.pce.gnpy; import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -347,7 +348,7 @@ public class GnpyTopoImpl { .setLength(Decimal64.valueOf(String.valueOf(length))) .setLengthUnits(Km.class) .setAttIn(Decimal64.valueOf(String.valueOf(attIn))) - .setLossCoef(Decimal64.valueOf(String.valueOf(lossCoef))) + .setLossCoef(Decimal64.valueOf(String.valueOf(lossCoef)).scaleTo(5, RoundingMode.CEILING)) .setConIn(Decimal64.valueOf(String.valueOf(connIn))) .setConOut(Decimal64.valueOf(String.valueOf(connOut))) .build(); diff --git a/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/GnpyApiModule.java b/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/GnpyApiModule.java deleted file mode 100644 index f3f7664d5..000000000 --- a/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/GnpyApiModule.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright © 2021 Orange, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.transportpce.pce.gnpy.consumer; - -import com.fasterxml.jackson.databind.module.SimpleModule; -import com.fasterxml.jackson.datatype.jsr310.PackageVersion; -import org.opendaylight.transportpce.common.converter.JsonStringConverter; -import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev220221.Request; -import org.opendaylight.yang.gen.v1.gnpy.path.rev220615.Result; - - - -//This class is a temporary workaround while waiting jackson -//support in yang tools https://git.opendaylight.org/gerrit/c/yangtools/+/94852 -public class GnpyApiModule extends SimpleModule { - - private static final long serialVersionUID = 1L; - - public GnpyApiModule(JsonStringConverter requestConverter, JsonStringConverter resultConverter) { - super(PackageVersion.VERSION); - addSerializer(Request.class, new RequestSerializer(requestConverter)); - addDeserializer(Result.class, new ResultDeserializer(resultConverter)); - } -} diff --git a/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/GnpyConsumerImpl.java b/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/GnpyConsumerImpl.java index bb3f53b83..b0854e02b 100644 --- a/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/GnpyConsumerImpl.java +++ b/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/GnpyConsumerImpl.java @@ -25,21 +25,20 @@ import org.slf4j.LoggerFactory; public class GnpyConsumerImpl implements GnpyConsumer { private static final Logger LOG = LoggerFactory.getLogger(GnpyConsumerImpl.class); - private GnpyResource api; - JsonStringConverter gnpyRequestConverter; - JsonStringConverter resultConverter; + private final GnpyResource api; public GnpyConsumerImpl(String baseUrl, String username, String password, BindingDOMCodecServices bindingDOMCodecServices) { - gnpyRequestConverter = new JsonStringConverter<>(bindingDOMCodecServices); - resultConverter = new JsonStringConverter<>(bindingDOMCodecServices); + JsonStringConverter gnpyRequestConverter = new JsonStringConverter<>(bindingDOMCodecServices); + JsonStringConverter resultConverter = new JsonStringConverter<>(bindingDOMCodecServices); - JsonConfigurator jsonConfigurator = new JsonConfigurator(gnpyRequestConverter, resultConverter); Client client = ClientBuilder.newClient(); HttpAuthenticationFeature authFeature = HttpAuthenticationFeature.basic(username, password); client.register(authFeature); client.register(new LoggingFeature(java.util.logging.Logger.getLogger(this.getClass().getName()))) - .register(JacksonFeature.class).register(jsonConfigurator); + .register(JacksonFeature.class) + .register(new ResultMessageBodyReader(resultConverter)) + .register(new RequestMessageBodyWriter(gnpyRequestConverter)); api = WebResourceFactory.newResource(GnpyResource.class, client.target(baseUrl)); } diff --git a/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/JsonConfigurator.java b/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/JsonConfigurator.java deleted file mode 100644 index 0f9c63d39..000000000 --- a/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/JsonConfigurator.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright © 2021 Orange, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.transportpce.pce.gnpy.consumer; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; -import javax.ws.rs.ext.ContextResolver; -import org.opendaylight.transportpce.common.converter.JsonStringConverter; -import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev220221.Request; -import org.opendaylight.yang.gen.v1.gnpy.path.rev220615.Result; - -public class JsonConfigurator implements ContextResolver { - - private final ObjectMapper mapper; - - public JsonConfigurator(JsonStringConverter requestConverter, - JsonStringConverter resultConverter) { - mapper = new ObjectMapper(); - mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); - mapper.enable(SerializationFeature.INDENT_OUTPUT); - mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); - mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - mapper.registerModule(new GnpyApiModule(requestConverter, resultConverter)); - } - - @Override - public ObjectMapper getContext(Class type) { - return mapper; - } - -} diff --git a/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/RequestMessageBodyWriter.java b/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/RequestMessageBodyWriter.java new file mode 100644 index 000000000..619744849 --- /dev/null +++ b/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/RequestMessageBodyWriter.java @@ -0,0 +1,57 @@ +/* + * Copyright © 2022 Orange, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.transportpce.pce.gnpy.consumer; + +import java.io.IOException; +import java.io.OutputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import java.nio.charset.StandardCharsets; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyWriter; +import org.opendaylight.transportpce.common.converter.JsonStringConverter; +import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev220221.Request; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RequestMessageBodyWriter implements MessageBodyWriter { + private static final Logger LOG = LoggerFactory.getLogger(RequestMessageBodyWriter.class); + private final InstanceIdentifier idRequest = InstanceIdentifier.builder(Request.class).build(); + private final JsonStringConverter converter; + + public RequestMessageBodyWriter(JsonStringConverter converter) { + this.converter = converter; + } + + @Override + @SuppressWarnings("java:S1872") + public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return "org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev220221.Request$$$codecImpl" + .equals(type.getName()) + || "org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev220221.RequestBuilder$RequestImpl" + .equals(type.getName()); + } + + @Override + public void writeTo(Request request, Class type, Type genericType, Annotation[] annotations, + MediaType mediaType, MultivaluedMap httpHeaders, OutputStream entityStream) + throws IOException, WebApplicationException { + // we have to use a string because GNPy does not support prefix + String jsonStringFromDataObject = converter + .createJsonStringFromDataObject(idRequest, request, + JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02) + .replace("gnpy-network-topology:", ""); + LOG.debug("Serialized request {}", jsonStringFromDataObject); + entityStream.write(jsonStringFromDataObject + .getBytes(StandardCharsets.UTF_8)); + } +} diff --git a/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/RequestSerializer.java b/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/RequestSerializer.java deleted file mode 100644 index 4dd687cc4..000000000 --- a/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/RequestSerializer.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright © 2021 Orange, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.transportpce.pce.gnpy.consumer; - -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.databind.SerializerProvider; -import com.fasterxml.jackson.databind.ser.std.StdSerializer; -import java.io.IOException; -import org.opendaylight.transportpce.common.converter.JsonStringConverter; -import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev220221.Request; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; -import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -//This class is a temporary workaround while waiting jackson -//support in yang tools https://git.opendaylight.org/gerrit/c/yangtools/+/94852 -@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "temporary class") - -public class RequestSerializer extends StdSerializer { - private static final long serialVersionUID = 1L; - private static final Logger LOG = LoggerFactory.getLogger(RequestSerializer.class); - private JsonStringConverter converter; - private InstanceIdentifier idRequest = InstanceIdentifier.builder(Request.class).build(); - - public RequestSerializer(JsonStringConverter converter) { - super(Request.class); - this.converter = converter; - } - - @Override - public void serialize(Request value, JsonGenerator gen, SerializerProvider provider) throws IOException { - String requestStr = this.converter - .createJsonStringFromDataObject(this.idRequest, value, - JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02); - requestStr = requestStr.replace("gnpy-network-topology:", ""); - LOG.info("Serialized request {}", requestStr); - gen.writeRaw(requestStr); - } -} diff --git a/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/ResultDeserializer.java b/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/ResultDeserializer.java deleted file mode 100644 index bdf46a435..000000000 --- a/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/ResultDeserializer.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright © 2021 Orange, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.transportpce.pce.gnpy.consumer; - -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.deser.std.StdDeserializer; -import java.io.IOException; -import org.opendaylight.transportpce.common.converter.JsonStringConverter; -import org.opendaylight.yang.gen.v1.gnpy.path.rev220615.Result; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; -import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -//This class is a temporary workaround while waiting jackson -//support in yang tools https://git.opendaylight.org/gerrit/c/yangtools/+/94852 -@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "temporary class") -public class ResultDeserializer extends StdDeserializer { - - private static final Logger LOG = LoggerFactory.getLogger(ResultDeserializer.class); - private static final long serialVersionUID = 1L; - private JsonStringConverter converter; - private YangInstanceIdentifier yangId; - - public ResultDeserializer(JsonStringConverter converter) { - super(Result.class); - this.converter = converter; - QName pathQname = Result.QNAME; - LOG.info("QName of the first result class {}", pathQname); - yangId = YangInstanceIdentifier.of(pathQname); - } - - @Override - public Result deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException { - return converter.createDataObjectFromJsonString(yangId,parser.readValueAsTree().toString(), - JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02); - } - -} diff --git a/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/ResultMessageBodyReader.java b/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/ResultMessageBodyReader.java new file mode 100644 index 000000000..e1bcb18bd --- /dev/null +++ b/pce/src/main/java/org/opendaylight/transportpce/pce/gnpy/consumer/ResultMessageBodyReader.java @@ -0,0 +1,48 @@ +/* + * Copyright © 2022 Orange, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.transportpce.pce.gnpy.consumer; + +import java.io.IOException; +import java.io.InputStream; +import java.lang.annotation.Annotation; +import java.lang.reflect.Type; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.ext.MessageBodyReader; +import org.opendaylight.transportpce.common.converter.JsonStringConverter; +import org.opendaylight.yang.gen.v1.gnpy.path.rev220615.Result; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier; + +public class ResultMessageBodyReader implements MessageBodyReader { + private final JsonStringConverter converter; + private final YangInstanceIdentifier yangId; + + public ResultMessageBodyReader(JsonStringConverter converter) { + QName pathQname = Result.QNAME; + yangId = YangInstanceIdentifier.of(pathQname); + this.converter = converter; + } + + @Override + @SuppressWarnings("java:S1872") + public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) { + return "org.opendaylight.yang.gen.v1.gnpy.path.rev220615.Result" + .equals(type.getName()); + } + + @Override + public Result readFrom(Class type, Type genericType, Annotation[] annotations, + MediaType mediaType, MultivaluedMap httpHeaders, InputStream entityStream) + throws IOException, WebApplicationException { + return converter.createDataObjectFromInputStream(yangId, entityStream, + JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02); + } +} diff --git a/pce/src/main/java/org/opendaylight/transportpce/pce/graph/PostAlgoPathValidator.java b/pce/src/main/java/org/opendaylight/transportpce/pce/graph/PostAlgoPathValidator.java index ba4eee7d6..960ae3bc1 100644 --- a/pce/src/main/java/org/opendaylight/transportpce/pce/graph/PostAlgoPathValidator.java +++ b/pce/src/main/java/org/opendaylight/transportpce/pce/graph/PostAlgoPathValidator.java @@ -17,6 +17,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import org.jgrapht.GraphPath; import org.opendaylight.transportpce.common.ResponseCodes; import org.opendaylight.transportpce.common.StringConstants; @@ -202,42 +203,19 @@ public class PostAlgoPathValidator { pceHardConstraintsInput)); // validation: check each type for each element - for (ResourcePair next : listToInclude) { - int indx = -1; - switch (next.getType()) { - case NODE: - if (listOfElementsSubNode.contains(next.getName())) { - indx = listOfElementsSubNode.indexOf(next.getName()); - } - break; - case SRLG: - if (listOfElementsSRLG.contains(next.getName())) { - indx = listOfElementsSRLG.indexOf(next.getName()); - } - break; - case CLLI: - if (listOfElementsCLLI.contains(next.getName())) { - indx = listOfElementsCLLI.indexOf(next.getName()); - } - break; - default: - LOG.warn(" in checkInclude vertex list unsupported resource type: [{}]", next.getType()); - } - - if (indx < 0) { - LOG.debug(" in checkInclude stopped : {} ", next.getName()); - return false; - } - - LOG.debug(" in checkInclude next found {} in {}", next.getName(), path.getVertexList()); - - listOfElementsSubNode.subList(0, indx).clear(); - listOfElementsCLLI.subList(0, indx).clear(); - listOfElementsSRLG.subList(0, indx).clear(); - } - - LOG.info(" in checkInclude passed : {} ", path.getVertexList()); - return true; + List listNodeToInclude = listToInclude + .stream().filter(rp -> PceConstraints.ResourceType.NODE.equals(rp.getType())) + .map(ResourcePair::getName).collect(Collectors.toList()); + List listSrlgToInclude = listToInclude + .stream().filter(rp -> PceConstraints.ResourceType.SRLG.equals(rp.getType())) + .map(ResourcePair::getName).collect(Collectors.toList()); + List listClliToInclude = listToInclude + .stream().filter(rp -> PceConstraints.ResourceType.CLLI.equals(rp.getType())) + .map(ResourcePair::getName).collect(Collectors.toList()); + + return listOfElementsSubNode.containsAll(listNodeToInclude) + && listOfElementsSRLG.containsAll(listSrlgToInclude) + && listOfElementsCLLI.containsAll(listClliToInclude); } private List listOfElementsBuild(List pathEdges, PceConstraints.ResourceType type, @@ -452,7 +430,7 @@ public class PostAlgoPathValidator { .equals(GridConstant.SLOT_WIDTH_50)) && (pceNode.getCentralFreqGranularity().setScale(0, RoundingMode.CEILING) .equals(GridConstant.SLOT_WIDTH_50))) { - LOG.info("Node {}: version is {} with slot width granularity {} and central " + LOG.debug("Node {}: version is {} with slot width granularity {} and central " + "frequency granularity is {} -> fixed grid mode", pceNode.getNodeId(), pceNode.getVersion(), pceNode.getSlotWidthGranularity(), pceNode.getCentralFreqGranularity()); diff --git a/pce/src/test/java/org/opendaylight/transportpce/pce/gnpy/GnpyUtilitiesImplTest.java b/pce/src/test/java/org/opendaylight/transportpce/pce/gnpy/GnpyUtilitiesImplTest.java index 063addcbf..4c94f0628 100644 --- a/pce/src/test/java/org/opendaylight/transportpce/pce/gnpy/GnpyUtilitiesImplTest.java +++ b/pce/src/test/java/org/opendaylight/transportpce/pce/gnpy/GnpyUtilitiesImplTest.java @@ -27,7 +27,6 @@ import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; import org.opendaylight.mdsal.binding.api.WriteTransaction; import org.opendaylight.mdsal.common.api.LogicalDatastoreType; @@ -148,7 +147,6 @@ public class GnpyUtilitiesImplTest extends AbstractTest { } - @Ignore @Test public void askNewPathFromGnpyTest() throws Exception { gnpyUtilitiesImpl = new GnpyUtilitiesImpl(networkTransaction, diff --git a/pce/src/test/resources/gnpy/gnpy_request.json b/pce/src/test/resources/gnpy/gnpy_request.json index c303ba282..64a6ebdef 100644 --- a/pce/src/test/resources/gnpy/gnpy_request.json +++ b/pce/src/test/resources/gnpy/gnpy_request.json @@ -620,11 +620,11 @@ } ], "spacing": 50000000000, - "path_bandwidth": 100 + "path_bandwidth": 1000000000.0 } } } ] } } -} \ No newline at end of file +} diff --git a/pce/src/test/resources/gnpy/gnpy_result_no_path.json b/pce/src/test/resources/gnpy/gnpy_result_no_path.json index 21983b2de..f490424db 100644 --- a/pce/src/test/resources/gnpy/gnpy_result_no_path.json +++ b/pce/src/test/resources/gnpy/gnpy_result_no_path.json @@ -29,7 +29,7 @@ }, { "metric-type": "path_bandwidth", - "accumulative-value": 100 + "accumulative-value": 1000000000.0 } ], "path-route-objects": [ @@ -156,4 +156,4 @@ } ] } -} \ No newline at end of file +} diff --git a/pce/src/test/resources/gnpy/gnpy_result_with_path.json b/pce/src/test/resources/gnpy/gnpy_result_with_path.json index 0b0918216..1c9564608 100644 --- a/pce/src/test/resources/gnpy/gnpy_result_with_path.json +++ b/pce/src/test/resources/gnpy/gnpy_result_with_path.json @@ -27,7 +27,7 @@ }, { "metric-type": "path_bandwidth", - "accumulative-value": 100 + "accumulative-value": 1000000000.0 } ], "path-route-objects": [ -- 2.36.6