2 * Copyright © 2020 Orange Labs, Inc. and others. All rights reserved.
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
8 package org.opendaylight.transportpce.pce.gnpy.consumer;
10 import java.io.IOException;
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.GET;
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.MediaType;
23 import javax.ws.rs.core.Response;
24 import org.opendaylight.transportpce.common.converter.JsonStringConverter;
25 import org.opendaylight.transportpce.test.AbstractTest;
26 import org.opendaylight.yang.gen.v1.gnpy.gnpy.api.rev220221.Request;
27 import org.opendaylight.yang.gen.v1.gnpy.path.rev220615.service.PathRequest;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 public class GnpyStub {
36 private static final Logger LOG = LoggerFactory.getLogger(GnpyStub.class);
39 public Response testConnection() {
40 return Response.ok().build();
45 @Produces(MediaType.APPLICATION_JSON)
46 public Response getStatus() {
48 String response = Files.readString(Paths.get("src", "test", "resources", "gnpy", "gnpy_status.json"));
49 return Response.ok(response).build();
50 } catch (IOException e) {
51 LOG.error("Cannot manage request", e);
52 return Response.serverError().build();
57 @Produces({ "application/json" })
58 @Consumes({ "application/json" })
59 @Path("/path-computation")
60 public Response computePath(String request) {
61 LOG.info("Received path request {}", request);
62 URI location = URI.create("http://127.0.0.1:9998/api/v1/path-computation");
63 // TODO: return different response based on body data
64 QName pathQname = Request.QNAME;
65 YangInstanceIdentifier yangId = YangInstanceIdentifier.of(pathQname);
66 JsonStringConverter<Request> converter = new JsonStringConverter<>(
67 AbstractTest.getDataStoreContextUtil().getBindingDOMCodecServices());
69 String response = null;
70 request = request.replace("Transceiver", "gnpy-network-topology:Transceiver")
71 .replace("Roadm", "gnpy-network-topology:Roadm")
72 .replace("Fiber", "gnpy-network-topology:Fiber")
73 .replace("km", "gnpy-network-topology:km");
74 Request data = converter.createDataObjectFromJsonString(yangId,
75 request, JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02);
76 LOG.info("Converted request {}", data);
77 List<PathRequest> pathRequest = new ArrayList<>(data.getService().nonnullPathRequest().values());
78 // this condition is totally arbitrary and could be modified
79 if (!pathRequest.isEmpty() && "XPONDER-3".contentEquals(pathRequest.get(0).getSource())) {
81 .readString(Paths.get("src", "test", "resources", "gnpy", "gnpy_result_with_path.json"));
83 response = Files.readString(Paths.get("src", "test", "resources", "gnpy", "gnpy_result_no_path.json"));
86 return Response.created(location).entity(response).build();
87 } catch (IOException e) {
88 LOG.error("Cannot manage request", e);
89 return Response.serverError().build();