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;
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();
@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;
}
}
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;
.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();
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;
.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();
+++ /dev/null
-/*
- * 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<Request> requestConverter, JsonStringConverter<Result> resultConverter) {
- super(PackageVersion.VERSION);
- addSerializer(Request.class, new RequestSerializer(requestConverter));
- addDeserializer(Result.class, new ResultDeserializer(resultConverter));
- }
-}
public class GnpyConsumerImpl implements GnpyConsumer {
private static final Logger LOG = LoggerFactory.getLogger(GnpyConsumerImpl.class);
- private GnpyResource api;
- JsonStringConverter<Request> gnpyRequestConverter;
- JsonStringConverter<Result> resultConverter;
+ private final GnpyResource api;
public GnpyConsumerImpl(String baseUrl, String username, String password,
BindingDOMCodecServices bindingDOMCodecServices) {
- gnpyRequestConverter = new JsonStringConverter<>(bindingDOMCodecServices);
- resultConverter = new JsonStringConverter<>(bindingDOMCodecServices);
+ JsonStringConverter<Request> gnpyRequestConverter = new JsonStringConverter<>(bindingDOMCodecServices);
+ JsonStringConverter<Result> 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));
}
+++ /dev/null
-/*
- * 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<ObjectMapper> {
-
- private final ObjectMapper mapper;
-
- public JsonConfigurator(JsonStringConverter<Request> requestConverter,
- JsonStringConverter<Result> 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;
- }
-
-}
--- /dev/null
+/*
+ * 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<Request> {
+ private static final Logger LOG = LoggerFactory.getLogger(RequestMessageBodyWriter.class);
+ private final InstanceIdentifier<Request> idRequest = InstanceIdentifier.builder(Request.class).build();
+ private final JsonStringConverter<Request> converter;
+
+ public RequestMessageBodyWriter(JsonStringConverter<Request> 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<String, Object> 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));
+ }
+}
+++ /dev/null
-/*
- * 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<Request> {
- private static final long serialVersionUID = 1L;
- private static final Logger LOG = LoggerFactory.getLogger(RequestSerializer.class);
- private JsonStringConverter<Request> converter;
- private InstanceIdentifier<Request> idRequest = InstanceIdentifier.builder(Request.class).build();
-
- public RequestSerializer(JsonStringConverter<Request> 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);
- }
-}
+++ /dev/null
-/*
- * 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<Result> {
-
- private static final Logger LOG = LoggerFactory.getLogger(ResultDeserializer.class);
- private static final long serialVersionUID = 1L;
- private JsonStringConverter<Result> converter;
- private YangInstanceIdentifier yangId;
-
- public ResultDeserializer(JsonStringConverter<Result> 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);
- }
-
-}
--- /dev/null
+/*
+ * 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<Result> {
+ private final JsonStringConverter<Result> converter;
+ private final YangInstanceIdentifier yangId;
+
+ public ResultMessageBodyReader(JsonStringConverter<Result> 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<Result> type, Type genericType, Annotation[] annotations,
+ MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
+ throws IOException, WebApplicationException {
+ return converter.createDataObjectFromInputStream(yangId, entityStream,
+ JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02);
+ }
+}
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;
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<String> listNodeToInclude = listToInclude
+ .stream().filter(rp -> PceConstraints.ResourceType.NODE.equals(rp.getType()))
+ .map(ResourcePair::getName).collect(Collectors.toList());
+ List<String> listSrlgToInclude = listToInclude
+ .stream().filter(rp -> PceConstraints.ResourceType.SRLG.equals(rp.getType()))
+ .map(ResourcePair::getName).collect(Collectors.toList());
+ List<String> 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<String> listOfElementsBuild(List<PceGraphEdge> pathEdges, PceConstraints.ResourceType type,
.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());
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;
}
- @Ignore
@Test
public void askNewPathFromGnpyTest() throws Exception {
gnpyUtilitiesImpl = new GnpyUtilitiesImpl(networkTransaction,
}
],
"spacing": 50000000000,
- "path_bandwidth": 100
+ "path_bandwidth": 1000000000.0
}
}
}
]
}
}
-}
\ No newline at end of file
+}
},
{
"metric-type": "path_bandwidth",
- "accumulative-value": 100
+ "accumulative-value": 1000000000.0
}
],
"path-route-objects": [
}
]
}
-}
\ No newline at end of file
+}
},
{
"metric-type": "path_bandwidth",
- "accumulative-value": 100
+ "accumulative-value": 1000000000.0
}
],
"path-route-objects": [