GNPy stub for PCE
[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.List;
17 import javax.ws.rs.Consumes;
18 import javax.ws.rs.HEAD;
19 import javax.ws.rs.POST;
20 import javax.ws.rs.Path;
21 import javax.ws.rs.Produces;
22 import javax.ws.rs.core.Response;
23 import org.opendaylight.transportpce.pce.utils.JsonUtil;
24 import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev190103.GnpyApi;
25 import org.opendaylight.yang.gen.v1.gnpy.path.rev200202.service.PathRequest;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 @Path("/gnpy/api/v1.0/files")
31 public class GnpyStub {
32     private static final Logger LOG = LoggerFactory.getLogger(GnpyStub.class);
33
34     @HEAD
35     public Response testConnection() {
36         return Response.ok().build();
37     }
38
39     @POST
40     @Produces({ "application/json" })
41     @Consumes({ "application/json" })
42     public Response computePath(String body) {
43         LOG.info("Received path request {}", body);
44         // as type-element and explicit route usage are not in the same namespace than
45         // gnpy-api,
46         // we add to add prefix if they are not set (request generated by
47         // GnpyUtilitiesImpl does not
48         // contain prefix)
49         if (body != null && !body.contains("gnpy-network-topology")) {
50             body = body.replaceAll("\"type\":\\s*\"", "\"type\":\"gnpy-network-topology:")
51                     .replaceAll("\"length_units\":\\s*\"", "\"length_units\":\"gnpy-network-topology:");
52         }
53         if (body != null && !body.contains("gnpy-path-computation-simplified")) {
54             body = body.replaceAll("\"explicit-route-usage\":\\s*\"",
55                     "\"explicit-route-usage\":\"gnpy-path-computation-simplified:");
56         }
57         GnpyApi request = (GnpyApi) JsonUtil.getInstance().getDataObjectFromJson(new JsonReader(new StringReader(body)),
58                 QName.create("gnpy:gnpy-api", "2019-01-03", "gnpy-api"));
59         URI location = URI.create("http://127.0.0.1:8008/gnpy/api/v1.0/files");
60         // TODO: return different response based on body data
61         try {
62             String response = null;
63             List<PathRequest> pathRequest = request.getServiceFile().getPathRequest();
64             // this condition is totally arbitrary and could be modified
65             if (!pathRequest.isEmpty() && "127.0.0.31".contentEquals(pathRequest.get(0).getSource().stringValue())) {
66                 response = Files
67                         .readString(Paths.get("src", "test", "resources", "gnpy", "gnpy_result_with_path.json"));
68             } else {
69                 response = Files.readString(Paths.get("src", "test", "resources", "gnpy", "gnpy_result_no_path.json"));
70             }
71
72             return Response.created(location).entity(response).build();
73         } catch (IOException e) {
74             return Response.serverError().build();
75         }
76     }
77
78 }