Technical debt - Fix PCE sonar issues
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / gnpy / GnpyResult.java
index 829c50df41cefbecb9b3a40dee73ebeac1f01715..ab127889f9d2834c15dcc08d379bc07343e729a8 100644 (file)
@@ -8,9 +8,7 @@
 
 package org.opendaylight.transportpce.pce.gnpy;
 
-//import com.google.common.base.Preconditions;
 import com.google.gson.stream.JsonReader;
-
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -18,28 +16,24 @@ import java.io.InputStreamReader;
 import java.math.BigDecimal;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
-import java.util.Collections;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Optional;
-
+import java.util.stream.Collectors;
 import javax.annotation.Nonnull;
-
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
-import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
-import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
-import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
-import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
-import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.Result;
-import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.explicit.route.hop.type.NumUnnumHop;
-import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.generic.path.properties.path.properties.PathMetric;
-import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.generic.path.properties.path.properties.PathRouteObjects;
-import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.result.Response;
-import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.result.response.response.type.NoPathCase;
-import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.result.response.response.type.PathCase;
+import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecServices;
+import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.Result;
+import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.explicit.route.hop.type.NumUnnumHop;
+import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.generic.path.properties.path.properties.PathMetric;
+import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.generic.path.properties.path.properties.PathRouteObjects;
+import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.result.Response;
+import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.result.response.response.type.NoPathCase;
+import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.result.response.response.type.PathCase;
 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.General;
 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.GeneralBuilder;
 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing.constraints.rev171017.constraints.sp.co.routing.or.general.general.Include;
@@ -54,8 +48,9 @@ import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.routing
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
 import org.opendaylight.yangtools.yang.binding.DataObject;
-import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.Uint16;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
@@ -66,7 +61,7 @@ import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
 import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -82,24 +77,15 @@ public class GnpyResult {
     private static final Logger LOG = LoggerFactory.getLogger(GnpyResult.class);
     private Response response = null;
     private Map<String, IpAddress> mapNodeRefIp = new HashMap<>();
+    private EffectiveModelContext effectiveModelcontext;
 
-    public GnpyResult(String gnpyResponseString, GnpyTopoImpl gnpyTopo) throws GnpyException, Exception {
+    public GnpyResult(String gnpyResponseString, GnpyTopoImpl gnpyTopo,
+            BindingDOMCodecServices bindingDOMCodecServices) throws GnpyException {
         this.mapNodeRefIp = gnpyTopo.getMapNodeRefIp();
-        // Create the schema context
-        final ModuleInfoBackedContext moduleContext = ModuleInfoBackedContext.create();
-        Iterable<? extends YangModuleInfo> moduleInfos;
-
-        moduleInfos = Collections.singleton(BindingReflections.getModuleInfo(Result.class));
-        moduleContext.addModuleInfos(moduleInfos);
-        SchemaContext schemaContext = moduleContext.tryToCreateSchemaContext().get();
-
-        // Create the binding binding normalized node codec registry
-        BindingRuntimeContext bindingRuntimeContext = BindingRuntimeContext.create(moduleContext, schemaContext);
-        final BindingNormalizedNodeCodecRegistry codecRegistry =
-            new BindingNormalizedNodeCodecRegistry(bindingRuntimeContext);
+        effectiveModelcontext = bindingDOMCodecServices.getRuntimeContext().getEffectiveModelContext();
 
         // Create the data object
-        QName pathQname = QName.create("gnpy:path", "2020-02-02", "result");
+        QName pathQname = QName.create("gnpy:path", "2020-09-09", "result");
         LOG.debug("the Qname is {} / namesapce {} ; module {}; ", pathQname, pathQname.getNamespace(),
             pathQname.getModule());
         YangInstanceIdentifier yangId = YangInstanceIdentifier.of(pathQname);
@@ -115,15 +101,16 @@ public class GnpyResult {
             throw new GnpyException("In GnpyResult: the Normalized Node is not present");
         }
         NormalizedNode<? extends PathArgument, ?> normalizedNode = transformIntoNormalizedNode.get();
+        Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode = bindingDOMCodecServices
+                .fromNormalizedNode(yangId, normalizedNode);
 
-        if (codecRegistry.fromNormalizedNode(yangId, normalizedNode) != null) {
-            dataObject = codecRegistry.fromNormalizedNode(yangId, normalizedNode).getValue();
+        if (fromNormalizedNode != null) {
+            dataObject = fromNormalizedNode.getValue();
         } else {
             throw new GnpyException("In GnpyResult: the codec registry from the normalized node is null");
         }
-        List<Response> responses = null;
-        responses = ((Result) dataObject).getResponse();
-        if (responses == null) {
+        List<Response> responses = new ArrayList<>(((Result) dataObject).nonnullResponse().values());
+        if (responses.isEmpty()) {
             throw new GnpyException("In GnpyResult: the response from GNpy is null!");
         }
         LOG.info("The response id is {}; ", responses.get(0).getResponseId());
@@ -154,7 +141,8 @@ public class GnpyResult {
                 if (((noPathType.equals("NO_FEASIBLE_BAUDRATE_WITH_SPACING"))
                     && (noPathType.equals("NO_FEASIBLE_MODE"))) && ((noPathType.equals("MODE_NOT_FEASIBLE"))
                         && (noPathType.equals("NO_SPECTRUM")))) {
-                    List<PathMetric> pathMetricList = noPathCase.getNoPath().getPathProperties().getPathMetric();
+                    Collection<PathMetric> pathMetricList = noPathCase.getNoPath()
+                            .getPathProperties().nonnullPathMetric().values();
                     LOG.info("GNPy : path is not feasible : {}", noPathType);
                     for (PathMetric pathMetric : pathMetricList) {
                         String metricType = pathMetric.getMetricType().getSimpleName();
@@ -165,7 +153,8 @@ public class GnpyResult {
             } else if (response.getResponseType() instanceof PathCase) {
                 LOG.info("GNPy : path is feasible");
                 PathCase pathCase = (PathCase) response.getResponseType();
-                List<PathMetric> pathMetricList = pathCase.getPathProperties().getPathMetric();
+                Collection<PathMetric> pathMetricList = pathCase
+                        .getPathProperties().nonnullPathMetric().values();
                 // Path metrics
                 for (PathMetric pathMetric : pathMetricList) {
                     String metricType = pathMetric.getMetricType().getSimpleName();
@@ -188,7 +177,7 @@ public class GnpyResult {
         int counter = 0;
         for (PathRouteObjects pathRouteObjects : pathRouteObjectList) {
             if (pathRouteObjects.getPathRouteObject().getType() instanceof NumUnnumHop) {
-                NumUnnumHop numUnnumHop = (org.opendaylight.yang.gen.v1.gnpy.path.rev200202.explicit.route.hop.type
+                NumUnnumHop numUnnumHop = (org.opendaylight.yang.gen.v1.gnpy.path.rev200909.explicit.route.hop.type
                     .NumUnnumHop) pathRouteObjects.getPathRouteObject().getType();
                 String nodeIp = numUnnumHop.getNumUnnumHop().getNodeId();
                 try {
@@ -200,7 +189,8 @@ public class GnpyResult {
                             .ordered.constraints.sp.hop.type.hop.type.Node node = new NodeBuilder().setNodeId(nodeId)
                             .build();
                         HopType hopType = new HopTypeBuilder().setHopType(node).build();
-                        OrderedHops orderedHops = new OrderedHopsBuilder().setHopNumber(counter).setHopType(hopType)
+                        OrderedHops orderedHops = new OrderedHopsBuilder()
+                                .setHopNumber(Uint16.valueOf(counter)).setHopType(hopType)
                             .build();
                         orderedHopsList.add(orderedHops);
                         counter++;
@@ -210,7 +200,10 @@ public class GnpyResult {
                 }
             }
         }
-        Include include = new IncludeBuilder().setOrderedHops(orderedHopsList).build();
+        Include include = new IncludeBuilder()
+                .setOrderedHops(orderedHopsList.stream()
+                        .collect(Collectors.toMap(OrderedHops::key, orderedHops -> orderedHops)))
+                .build();
         General general = new GeneralBuilder().setInclude(include).build();
         hardConstraints = new HardConstraintsBuilder().setCoRoutingOrGeneral(general).build();
         return hardConstraints;
@@ -233,13 +226,12 @@ public class GnpyResult {
      * @throws Exception exception
      */
     private Optional<NormalizedNode<? extends YangInstanceIdentifier.PathArgument, ?>> parseInputJSON(JsonReader reader,
-        Class<? extends DataObject> objectClass) throws Exception {
+        Class<? extends DataObject> objectClass) {
         NormalizedNodeResult result = new NormalizedNodeResult();
-        SchemaContext schemaContext = getSchemaContext(objectClass);
         try (NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
             JsonParserStream jsonParser = JsonParserStream.create(streamWriter,
-                JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext),
-                schemaContext);) {
+                JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(effectiveModelcontext),
+                effectiveModelcontext);) {
             jsonParser.parse(reader);
         } catch (IOException e) {
             LOG.warn("GNPy: exception {} occured during parsing Json input stream", e.getMessage());
@@ -248,16 +240,6 @@ public class GnpyResult {
         return Optional.ofNullable(result.getResult());
     }
 
-    private SchemaContext getSchemaContext(Class<? extends DataObject> objectClass) throws GnpyException, Exception {
-
-        final ModuleInfoBackedContext moduleContext = ModuleInfoBackedContext.create();
-        Iterable<? extends YangModuleInfo> moduleInfos;
-        SchemaContext schemaContext = null;
-        moduleInfos = Collections.singleton(BindingReflections.getModuleInfo(objectClass));
-        moduleContext.addModuleInfos(moduleInfos);
-        schemaContext = moduleContext.tryToCreateSchemaContext().get();
-        return schemaContext;
-    }
 
     /**
      * Transforms the given input {@link NormalizedNode} into the given {@link DataObject}.