JsonStringConverter in GNPy
[transportpce.git] / pce / src / test / java / org / opendaylight / transportpce / pce / gnpy / GnpyStub.java
1 /*
2  * Copyright © 2020 Orange Labs, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.transportpce.pce.gnpy;
9
10 import com.google.gson.stream.JsonReader;
11 import java.io.IOException;
12 import java.io.StringReader;
13 import java.net.URI;
14 import java.nio.file.Files;
15 import java.nio.file.Paths;
16 import java.util.ArrayList;
17 import java.util.List;
18 import javax.ws.rs.Consumes;
19 import javax.ws.rs.HEAD;
20 import javax.ws.rs.POST;
21 import javax.ws.rs.Path;
22 import javax.ws.rs.Produces;
23 import javax.ws.rs.core.Response;
24 import org.opendaylight.transportpce.pce.utils.JsonUtil;
25 import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev190103.GnpyApi;
26 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.service.PathRequest;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 @Path("/gnpy/api/v1.0/files")
32 public class GnpyStub {
33     private static final Logger LOG = LoggerFactory.getLogger(GnpyStub.class);
34
35     @HEAD
36     public Response testConnection() {
37         return Response.ok().build();
38     }
39
40     @POST
41     @Produces({ "application/json" })
42     @Consumes({ "application/json" })
43     public Response computePath(String body) {
44         LOG.info("Received path request {}", body);
45         // as type-element and explicit route usage are not in the same namespace than
46         // gnpy-api,
47         // we add to add prefix if they are not set (request generated by
48         // GnpyUtilitiesImpl does not
49         // contain prefix)
50         if (body != null && !body.contains("gnpy-network-topology")) {
51             body = body.replaceAll("\"type\":\\s*\"", "\"type\":\"gnpy-network-topology:")
52                     .replaceAll("\"length_units\":\\s*\"", "\"length_units\":\"gnpy-network-topology:");
53         }
54         if (body != null && !body.contains("gnpy-path-computation-simplified")) {
55             body = body.replaceAll("\"explicit-route-usage\":\\s*\"",
56                     "\"explicit-route-usage\":\"gnpy-path-computation-simplified:");
57         }
58         GnpyApi request = (GnpyApi) JsonUtil.getInstance().getDataObjectFromJson(new JsonReader(new StringReader(body)),
59                 QName.create("gnpy:gnpy-api", "2019-01-03", "gnpy-api"));
60         URI location = URI.create("http://127.0.0.1:8008/gnpy/api/v1.0/files");
61         // TODO: return different response based on body data
62         try {
63             String response = null;
64             List<PathRequest> pathRequest = new ArrayList<>(request.getServiceFile().nonnullPathRequest().values());
65             // this condition is totally arbitrary and could be modified
66             if (!pathRequest.isEmpty() && "127.0.0.31".contentEquals(pathRequest.get(0).getSource().stringValue())) {
67                 response = Files
68                         .readString(Paths.get("src", "test", "resources", "gnpy", "gnpy_result_with_path.json"));
69             } else {
70                 response = Files.readString(Paths.get("src", "test", "resources", "gnpy", "gnpy_result_no_path.json"));
71             }
72
73             return Response.created(location).entity(response).build();
74         } catch (IOException e) {
75             return Response.serverError().build();
76         }
77     }
78
79 }