GNPy client refactor
[transportpce.git] / pce / src / test / java / org / opendaylight / transportpce / pce / gnpy / consumer / GnpyStub.java
similarity index 55%
rename from pce/src/test/java/org/opendaylight/transportpce/pce/gnpy/GnpyStub.java
rename to pce/src/test/java/org/opendaylight/transportpce/pce/gnpy/consumer/GnpyStub.java
index ff6762ad0c6686ce209728ac2e289ecc61028313..43d6db88ac1cfceecb7b41eae88701f418b7da83 100644 (file)
@@ -5,11 +5,9 @@
  * 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;
+package org.opendaylight.transportpce.pce.gnpy.consumer;
 
-import com.google.gson.stream.JsonReader;
 import java.io.IOException;
-import java.io.StringReader;
 import java.net.URI;
 import java.nio.file.Files;
 import java.nio.file.Paths;
@@ -21,10 +19,13 @@ import javax.ws.rs.POST;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 import javax.ws.rs.core.Response;
-import org.opendaylight.transportpce.pce.utils.JsonUtil;
+import org.opendaylight.transportpce.common.converter.JsonStringConverter;
+import org.opendaylight.transportpce.test.AbstractTest;
 import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev190103.GnpyApi;
 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.service.PathRequest;
 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;
 
@@ -40,28 +41,25 @@ public class GnpyStub {
     @POST
     @Produces({ "application/json" })
     @Consumes({ "application/json" })
-    public Response computePath(String body) {
-        LOG.info("Received path request {}", body);
-        // as type-element and explicit route usage are not in the same namespace than
-        // gnpy-api,
-        // we add to add prefix if they are not set (request generated by
-        // GnpyUtilitiesImpl does not
-        // contain prefix)
-        if (body != null && !body.contains("gnpy-network-topology")) {
-            body = body.replaceAll("\"type\":\\s*\"", "\"type\":\"gnpy-network-topology:")
-                    .replaceAll("\"length_units\":\\s*\"", "\"length_units\":\"gnpy-network-topology:");
-        }
-        if (body != null && !body.contains("gnpy-path-computation-simplified")) {
-            body = body.replaceAll("\"explicit-route-usage\":\\s*\"",
-                    "\"explicit-route-usage\":\"gnpy-path-computation-simplified:");
-        }
-        GnpyApi request = (GnpyApi) JsonUtil.getInstance().getDataObjectFromJson(new JsonReader(new StringReader(body)),
-                QName.create("gnpy:gnpy-api", "2019-01-03", "gnpy-api"));
-        URI location = URI.create("http://127.0.0.1:8008/gnpy/api/v1.0/files");
+    public Response computePath(String request) {
+        LOG.info("Received path request {}", request);
+        URI location = URI.create("http://127.0.0.1:9998/gnpy/api/v1.0/files");
         // TODO: return different response based on body data
+        QName pathQname = QName.create("gnpy:gnpy-api", "2019-01-03", "gnpy-api");
+        YangInstanceIdentifier yangId = YangInstanceIdentifier.of(pathQname);
+        JsonStringConverter<GnpyApi> converter = new JsonStringConverter<>(
+                AbstractTest.getDataStoreContextUtil().getBindingDOMCodecServices());
         try {
             String response = null;
-            List<PathRequest> pathRequest = new ArrayList<>(request.getServiceFile().nonnullPathRequest().values());
+            request = request.replace("Transceiver", "gnpy-network-topology:Transceiver")
+                    .replace("Roadm", "gnpy-network-topology:Roadm")
+                    .replace("Fiber", "gnpy-network-topology:Fiber")
+                    .replace("km", "gnpy-network-topology:km")
+                    .replace("route-include-ero", "gnpy-path-computation-simplified:route-include-ero");
+            GnpyApi data = converter.createDataObjectFromJsonString(yangId,
+                    request, JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02);
+            LOG.info("Converted request {}", data);
+            List<PathRequest> pathRequest = new ArrayList<>(data.getServiceFile().nonnullPathRequest().values());
             // this condition is totally arbitrary and could be modified
             if (!pathRequest.isEmpty() && "127.0.0.31".contentEquals(pathRequest.get(0).getSource().stringValue())) {
                 response = Files
@@ -72,6 +70,7 @@ public class GnpyStub {
 
             return Response.created(location).entity(response).build();
         } catch (IOException e) {
+            LOG.error("Cannot manage request", e);
             return Response.serverError().build();
         }
     }