JsonStringConverter in GNPy
[transportpce.git] / pce / src / test / java / org / opendaylight / transportpce / pce / gnpy / consumer / 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.consumer;
9
10 import java.io.IOException;
11 import java.net.URI;
12 import java.nio.file.Files;
13 import java.nio.file.Paths;
14 import java.util.ArrayList;
15 import java.util.List;
16 import javax.ws.rs.Consumes;
17 import javax.ws.rs.HEAD;
18 import javax.ws.rs.POST;
19 import javax.ws.rs.Path;
20 import javax.ws.rs.Produces;
21 import javax.ws.rs.core.Response;
22 import org.opendaylight.transportpce.common.converter.JsonStringConverter;
23 import org.opendaylight.transportpce.test.AbstractTest;
24 import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev190103.GnpyApi;
25 import org.opendaylight.yang.gen.v1.gnpy.path.rev200909.service.PathRequest;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 @Path("/gnpy/api/v1.0/files")
33 public class GnpyStub {
34     private static final Logger LOG = LoggerFactory.getLogger(GnpyStub.class);
35
36     @HEAD
37     public Response testConnection() {
38         return Response.ok().build();
39     }
40
41     @POST
42     @Produces({ "application/json" })
43     @Consumes({ "application/json" })
44     public Response computePath(String request) {
45         LOG.info("Received path request {}", request);
46         URI location = URI.create("http://127.0.0.1:9998/gnpy/api/v1.0/files");
47         // TODO: return different response based on body data
48         QName pathQname = QName.create("gnpy:gnpy-api", "2019-01-03", "gnpy-api");
49         YangInstanceIdentifier yangId = YangInstanceIdentifier.of(pathQname);
50         JsonStringConverter<GnpyApi> converter = new JsonStringConverter<>(
51                 AbstractTest.getDataStoreContextUtil().getBindingDOMCodecServices());
52         try {
53             String response = null;
54             request = request.replace("Transceiver", "gnpy-network-topology:Transceiver")
55                     .replace("Roadm", "gnpy-network-topology:Roadm")
56                     .replace("Fiber", "gnpy-network-topology:Fiber")
57                     .replace("km", "gnpy-network-topology:km")
58                     .replace("route-include-ero", "gnpy-path-computation-simplified:route-include-ero");
59             GnpyApi data = converter.createDataObjectFromJsonString(yangId,
60                     request, JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02);
61             LOG.info("Converted request {}", data);
62             List<PathRequest> pathRequest = new ArrayList<>(data.getServiceFile().nonnullPathRequest().values());
63             // this condition is totally arbitrary and could be modified
64             if (!pathRequest.isEmpty() && "127.0.0.31".contentEquals(pathRequest.get(0).getSource().stringValue())) {
65                 response = Files
66                         .readString(Paths.get("src", "test", "resources", "gnpy", "gnpy_result_with_path.json"));
67             } else {
68                 response = Files.readString(Paths.get("src", "test", "resources", "gnpy", "gnpy_result_no_path.json"));
69             }
70
71             return Response.created(location).entity(response).build();
72         } catch (IOException e) {
73             LOG.error("Cannot manage request", e);
74             return Response.serverError().build();
75         }
76     }
77
78 }