get rid of mdsal.binding.dom.codec.impl
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / gnpy / GnpyResult.java
index 50e2b7c8d9e517d932a29ac64fb3e8185f617285..e0775608113b20291ad534e8a621db20eebcafed 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,27 @@ import java.io.InputStreamReader;
 import java.math.BigDecimal;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 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.Set;
-
 import javax.annotation.Nonnull;
-
+import org.opendaylight.binding.runtime.spi.BindingRuntimeHelpers;
+import org.opendaylight.mdsal.binding.dom.adapter.AdapterContext;
+import org.opendaylight.mdsal.binding.dom.adapter.ConstantAdapterContext;
 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.rev190502.Result;
-import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.explicit.route.hop.type.NumUnnumHop;
-import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.generic.path.properties.path.properties.PathMetric;
-import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.generic.path.properties.path.properties.PathRouteObjects;
-import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.result.Response;
-import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.result.response.response.type.NoPathCase;
-import org.opendaylight.yang.gen.v1.gnpy.path.rev190502.result.response.response.type.PathCase;
+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.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;
@@ -56,6 +53,7 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
 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;
@@ -67,7 +65,6 @@ 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.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -82,50 +79,47 @@ public class GnpyResult {
 
     private static final Logger LOG = LoggerFactory.getLogger(GnpyResult.class);
     private Response response = null;
-    private Map<String, IpAddress> mapNodeRefIp = new HashMap<String, IpAddress>();
-    //private Map<String, IpAddress> mapFiberIp = new HashMap<String, IpAddress>();
-
-    public GnpyResult(String gnpyResponseString, GnpyTopoImpl gnpyTopo) throws Exception {
+    private Map<String, IpAddress> mapNodeRefIp = new HashMap<>();
+    private final AdapterContext adapterContext = new ConstantAdapterContext();
 
+    public GnpyResult(String gnpyResponseString, GnpyTopoImpl gnpyTopo) throws GnpyException, Exception {
         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();
+        Collection<? extends YangModuleInfo> moduleInfos = Collections.singleton(BindingReflections
+                .getModuleInfo(Result.class));
+        BindingRuntimeHelpers.createEffectiveModel(moduleInfos);
 
         // Create the binding binding normalized node codec registry
-        BindingRuntimeContext bindingRuntimeContext = BindingRuntimeContext.create(moduleContext, schemaContext);
-        final BindingNormalizedNodeCodecRegistry codecRegistry =
-            new BindingNormalizedNodeCodecRegistry(bindingRuntimeContext);
+        final BindingNormalizedNodeSerializer codecRegistry = adapterContext.currentSerializer();
 
         // Create the data object
-        QName pathQname = QName.create("gnpy:path", "2019-05-02", "result");
-        LOG.debug("the Qname is {} / namesapce {} ; module {}; ", pathQname.toString(), pathQname.getNamespace(),
+        QName pathQname = QName.create("gnpy:path", "2020-02-02", "result");
+        LOG.debug("the Qname is {} / namesapce {} ; module {}; ", pathQname, pathQname.getNamespace(),
             pathQname.getModule());
         YangInstanceIdentifier yangId = YangInstanceIdentifier.of(pathQname);
         DataObject dataObject = null;
+
         // Create the object response
-        // Create JsonReader from String
         InputStream streamGnpyRespnse = new ByteArrayInputStream(gnpyResponseString.getBytes(StandardCharsets.UTF_8));
-        InputStreamReader gnpyResultReader = new InputStreamReader(streamGnpyRespnse);
+        InputStreamReader gnpyResultReader = new InputStreamReader(streamGnpyRespnse,StandardCharsets.UTF_8);
         JsonReader jsonReader = new JsonReader(gnpyResultReader);
         Optional<NormalizedNode<? extends PathArgument, ?>> transformIntoNormalizedNode = parseInputJSON(jsonReader,
             Result.class);
+        if (!transformIntoNormalizedNode.isPresent()) {
+            throw new GnpyException("In GnpyResult: the Normalized Node is not present");
+        }
         NormalizedNode<? extends PathArgument, ?> normalizedNode = transformIntoNormalizedNode.get();
+
         if (codecRegistry.fromNormalizedNode(yangId, normalizedNode) != null) {
             dataObject = codecRegistry.fromNormalizedNode(yangId, normalizedNode).getValue();
         } else {
-            LOG.warn("The codec registry from the normalized node is null!");
+            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) {
-            LOG.info("The response id is {}; ", responses.get(0).getResponseId());
-        } else {
-            LOG.warn("The response is 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());
         this.response = responses.get(0);
         analyzeResult();
     }
@@ -134,11 +128,10 @@ public class GnpyResult {
         boolean isFeasible = false;
         if (response != null) {
             if (response.getResponseType() instanceof NoPathCase) {
-                isFeasible = false;
-                LOG.info("The path is not feasible ");
+                LOG.info("In GnpyResult: The path is not feasible ");
             } else if (response.getResponseType() instanceof PathCase) {
                 isFeasible = true;
-                LOG.info("The path is feasible ");
+                LOG.info("In GnpyResult: The path is feasible ");
             }
         }
         return isFeasible;
@@ -154,7 +147,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 +159,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 +183,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.rev190502.explicit.route.hop.type
+                NumUnnumHop numUnnumHop = (org.opendaylight.yang.gen.v1.gnpy.path.rev200202.explicit.route.hop.type
                     .NumUnnumHop) pathRouteObjects.getPathRouteObject().getType();
                 String nodeIp = numUnnumHop.getNumUnnumHop().getNodeId();
                 try {
@@ -200,7 +195,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++;
@@ -217,13 +213,11 @@ public class GnpyResult {
     }
 
     private String findOrdNetworkNodeId(IpAddress nodeIpAddress) {
-        String nodeId;
-        Set<String> keySet = this.mapNodeRefIp.keySet();
-        Iterator<String> it = keySet.iterator();
+        Iterator<Map.Entry<String,IpAddress>> it = this.mapNodeRefIp.entrySet().iterator();
         while (it.hasNext()) {
-            nodeId = it.next();
-            if (this.mapNodeRefIp.get(nodeId).equals(nodeIpAddress)) {
-                return nodeId;
+            Entry<String, IpAddress> entry = it.next();
+            if (entry.getValue().equals(nodeIpAddress)) {
+                return entry.getKey();
             }
         }
         return null;
@@ -250,14 +244,10 @@ public class GnpyResult {
         return Optional.ofNullable(result.getResult());
     }
 
-    private SchemaContext getSchemaContext(Class<? extends DataObject> objectClass) throws 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;
+    private SchemaContext getSchemaContext(Class<? extends DataObject> objectClass) throws GnpyException, Exception {
+        Collection<? extends YangModuleInfo> moduleInfos = Collections.singleton(BindingReflections
+                .getModuleInfo(objectClass));
+        return BindingRuntimeHelpers.createEffectiveModel(moduleInfos);
     }
 
     /**
@@ -273,11 +263,11 @@ public class GnpyResult {
     public <T extends DataObject> Optional<T> getDataObject(@Nonnull NormalizedNode<?, ?> normalizedNode,
         @Nonnull QName rootNode, BindingNormalizedNodeSerializer codecRegistry) {
         if (normalizedNode != null) {
-            LOG.debug("GNPy: The codecRegistry is ", codecRegistry.toString());
+            LOG.debug("GNPy: The codecRegistry is {}", codecRegistry);
         } else {
             LOG.warn("GNPy: The codecRegistry is null");
         }
-        Preconditions.checkNotNull(normalizedNode);
+        //Preconditions.checkNotNull(normalizedNode);
         if (normalizedNode instanceof ContainerNode) {
             YangInstanceIdentifier.PathArgument directChildIdentifier = YangInstanceIdentifier.of(rootNode)
                 .getLastPathArgument();
@@ -287,10 +277,10 @@ public class GnpyResult {
                 throw new IllegalStateException(String.format("Could not get the direct child of %s", rootNode));
             }
             normalizedNode = directChild.get();
-            LOG.debug("GNPy: the normalized node is ", normalizedNode.getNodeType());
+            LOG.debug("GNPy: the normalized node is {}", normalizedNode.getNodeType());
         }
         YangInstanceIdentifier rootNodeYangInstanceIdentifier = YangInstanceIdentifier.of(rootNode);
-        LOG.debug("GNPy: the root Node Yang Instance Identifier is ", rootNodeYangInstanceIdentifier.toString());
+        LOG.debug("GNPy: the root Node Yang Instance Identifier is {}", rootNodeYangInstanceIdentifier);
         Map.Entry<?, ?> bindingNodeEntry = codecRegistry.fromNormalizedNode(rootNodeYangInstanceIdentifier,
             normalizedNode);
         if (bindingNodeEntry == null) {