Improve AAI simulator by configuring responses
[policy/models.git] / models-interactions / model-simulators / src / main / java / org / onap / policy / simulators / AaiSimulatorJaxRs.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * simulators
4  * ================================================================================
5  * Copyright (C) 2017-2018, 2020-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
7  * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.simulators;
24
25 import java.io.IOException;
26 import javax.ws.rs.Consumes;
27 import javax.ws.rs.GET;
28 import javax.ws.rs.PUT;
29 import javax.ws.rs.Path;
30 import javax.ws.rs.PathParam;
31 import javax.ws.rs.Produces;
32 import javax.ws.rs.QueryParam;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
35 import org.onap.policy.common.utils.resources.ResourceUtils;
36 import org.onap.policy.common.utils.services.Registry;
37
38 @Path("/aai")
39 public class AaiSimulatorJaxRs {
40
41     private static final String DOT_JSON = ".json";
42     private static final String DEFAULT_RESOURCE_LOCATION = "org/onap/policy/simulators/aai/";
43     private static final String INVALID_VNF_FILE_NAME = "invalid-vnf";
44     private static final String INVALID_PNF_FILE_NAME = "invalid-pnf";
45
46     /**
47      * A&AI get query.
48      *
49      * @return the result
50      */
51     @GET
52     @Path("/{version:v16|v21}/search/nodes-query")
53     @Consumes(MediaType.APPLICATION_JSON)
54     @Produces("application/json")
55     public String aaiGetVserverQuery(@QueryParam("filter") final String filter) {
56         if (filter.equals("vserver-name:EQUALS:f953c499-4b1e-426b-8c6d-e9e9f1fc730f")
57             || filter.equals("vserver-name:EQUALS:Ete_vFWCLvFWSNK_7ba1fbde_0")
58             || filter.equals("vserver-name:EQUALS:OzVServer")
59             || filter.equals("vserver-name:EQUALS:testVserverName")) {
60             return "{\"result-data\":[{\"resource-type\": \"vserver\",\"resource-link\":\"/aai/v15/"
61                 + "cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/RegionOne/tenants"
62                 + "/tenant/3f2aaef74ecb4b19b35e26d0849fe9a2/vservers/vserver/"
63                 + "6c3b3714-e36c-45af-9f16-7d3a73d99497\"}]}";
64         } else {
65             return null;
66         }
67     }
68
69     /**
70      * A&AI put query.
71      *
72      * @param req the request
73      * @return the response
74      * @throws IOException if a response file cannot be read
75      */
76     @PUT
77     @Path("/{version:v16|v21}/query")
78     @Consumes(MediaType.APPLICATION_JSON)
79     @Produces("application/json")
80     public Response aaiPutQuery(final String req) throws IOException {
81         return getResponse("AaiCqResponse", "invalid-cq");
82     }
83
84     /**
85      * A&AI get PNF query using pnfName.
86      *
87      * @return the result
88      * @throws IOException if a response file cannot be read
89      */
90     @GET
91     @Path("/{version:v16|v21}/network/pnfs/pnf/{pnfName}")
92     @Consumes(MediaType.APPLICATION_JSON)
93     @Produces("application/json")
94     public Response aaiGetPnfUsingPnfName(@PathParam("pnfName") final String pnfName) throws IOException {
95         return getResponse(pnfName, INVALID_PNF_FILE_NAME);
96     }
97
98     /**
99      * A&AI get PNF query using pnf-id.
100      *
101      * @return the result
102      * @throws IOException if a response file cannot be read
103      */
104     @GET
105     @Path("/{version:v16|v21}/network/pnfs/pnf")
106     @Consumes(MediaType.APPLICATION_JSON)
107     @Produces("application/json")
108     public Response aaiGetPnfUsingPnfId(@QueryParam("pnf-id") final String pnfId) throws IOException {
109         return getResponse(pnfId, INVALID_PNF_FILE_NAME);
110     }
111
112     /**
113      * A&AI get VNF query using vnf-id.
114      *
115      * @return the result
116      * @throws IOException if a response file cannot be read
117      */
118     @GET
119     @Path("/{version:v16|v21}/network/generic-vnfs/generic-vnf/{vnfId}")
120     @Consumes(MediaType.APPLICATION_JSON)
121     @Produces("application/json")
122     public Response aaiGetVnfUsingVnfId(@PathParam("vnfId") final String vnfId) throws IOException {
123         return getResponse(vnfId, INVALID_VNF_FILE_NAME);
124     }
125
126     /**
127      * A&AI get VNF query using vnf-name.
128      *
129      * @return the result
130      * @throws IOException if a response file cannot be read
131      */
132     @GET
133     @Path("/{version:v16|v21}/network/generic-vnfs/generic-vnf")
134     @Consumes(MediaType.APPLICATION_JSON)
135     @Produces("application/json")
136     public Response aaiGetVnfUsingVnfName(@QueryParam("vnf-name") final String vnfName) throws IOException {
137         return getResponse(vnfName, INVALID_VNF_FILE_NAME);
138     }
139
140     private Response getResponse(final String expectedFileName, final String defaultFileName) {
141         String resourceLocation = getResourceLocation();
142         var responseString = ResourceUtils.getResourceAsString(resourceLocation + expectedFileName + DOT_JSON);
143         if (null == responseString) {
144             // if a response file is not found in expected location, look for it in default location
145             responseString = ResourceUtils.getResourceAsString(DEFAULT_RESOURCE_LOCATION + expectedFileName + DOT_JSON);
146         }
147         if (null != responseString) {
148             return Response.ok(responseString).build();
149         } else {
150             // if a response file is not available in expected or default location, return an appropriate 404 response
151             responseString = ResourceUtils.getResourceAsString(DEFAULT_RESOURCE_LOCATION + defaultFileName + DOT_JSON);
152             return Response.status(Response.Status.NOT_FOUND).entity(responseString).build();
153         }
154     }
155
156     private String getResourceLocation() {
157         return Registry.getOrDefault(this.getClass().getName() + "_RESOURCE_LOCATION", String.class,
158             DEFAULT_RESOURCE_LOCATION);
159     }
160 }