Fix actors interactions for drools issues.
[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, 2023 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 jakarta.ws.rs.Consumes;
26 import jakarta.ws.rs.GET;
27 import jakarta.ws.rs.PUT;
28 import jakarta.ws.rs.Path;
29 import jakarta.ws.rs.PathParam;
30 import jakarta.ws.rs.Produces;
31 import jakarta.ws.rs.QueryParam;
32 import jakarta.ws.rs.core.MediaType;
33 import jakarta.ws.rs.core.Response;
34 import org.onap.policy.common.utils.resources.ResourceUtils;
35 import org.onap.policy.common.utils.services.Registry;
36
37 @Path("/aai")
38 public class AaiSimulatorJaxRs {
39
40     private static final String DOT_JSON = ".json";
41     private static final String DEFAULT_RESOURCE_LOCATION = "org/onap/policy/simulators/aai/";
42     private static final String INVALID_VNF_FILE_NAME = "invalid-vnf";
43     private static final String INVALID_PNF_FILE_NAME = "invalid-pnf";
44
45     /**
46      * A&AI get query.
47      *
48      * @return the result
49      */
50     @GET
51     @Path("/{version:v16|v21}/search/nodes-query")
52     @Consumes(MediaType.APPLICATION_JSON)
53     @Produces("application/json")
54     public String aaiGetVserverQuery(@QueryParam("filter") final String filter) {
55         if (filter.equals("vserver-name:EQUALS:f953c499-4b1e-426b-8c6d-e9e9f1fc730f")
56             || filter.equals("vserver-name:EQUALS:Ete_vFWCLvFWSNK_7ba1fbde_0")
57             || filter.equals("vserver-name:EQUALS:OzVServer")
58             || filter.equals("vserver-name:EQUALS:testVserverName")) {
59             return "{\"result-data\":[{\"resource-type\": \"vserver\",\"resource-link\":\"/aai/v15/"
60                 + "cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/RegionOne/tenants"
61                 + "/tenant/3f2aaef74ecb4b19b35e26d0849fe9a2/vservers/vserver/"
62                 + "6c3b3714-e36c-45af-9f16-7d3a73d99497\"}]}";
63         } else {
64             return null;
65         }
66     }
67
68     /**
69      * A&AI put query.
70      *
71      * @param req the request
72      * @return the response
73      */
74     @PUT
75     @Path("/{version:v16|v21}/query")
76     @Consumes(MediaType.APPLICATION_JSON)
77     @Produces("application/json")
78     public Response aaiPutQuery(final String req) {
79         return getResponse("AaiCqResponse", "invalid-cq");
80     }
81
82     /**
83      * A&AI get PNF query using pnfName.
84      *
85      * @return the result
86      */
87     @GET
88     @Path("/{version:v16|v21}/network/pnfs/pnf/{pnfName}")
89     @Consumes(MediaType.APPLICATION_JSON)
90     @Produces("application/json")
91     public Response aaiGetPnfUsingPnfName(@PathParam("pnfName") final String pnfName) {
92         return getResponse(pnfName, INVALID_PNF_FILE_NAME);
93     }
94
95     /**
96      * A&AI get PNF query using pnf-id.
97      *
98      * @return the result
99      */
100     @GET
101     @Path("/{version:v16|v21}/network/pnfs/pnf")
102     @Consumes(MediaType.APPLICATION_JSON)
103     @Produces("application/json")
104     public Response aaiGetPnfUsingPnfId(@QueryParam("pnf-id") final String pnfId) {
105         return getResponse(pnfId, INVALID_PNF_FILE_NAME);
106     }
107
108     /**
109      * A&AI get VNF query using vnf-id.
110      *
111      * @return the result
112      */
113     @GET
114     @Path("/{version:v16|v21}/network/generic-vnfs/generic-vnf/{vnfId}")
115     @Consumes(MediaType.APPLICATION_JSON)
116     @Produces("application/json")
117     public Response aaiGetVnfUsingVnfId(@PathParam("vnfId") final String vnfId) {
118         return getResponse(vnfId, INVALID_VNF_FILE_NAME);
119     }
120
121     /**
122      * A&AI get VNF query using vnf-name.
123      *
124      * @return the result
125      */
126     @GET
127     @Path("/{version:v16|v21}/network/generic-vnfs/generic-vnf")
128     @Consumes(MediaType.APPLICATION_JSON)
129     @Produces("application/json")
130     public Response aaiGetVnfUsingVnfName(@QueryParam("vnf-name") final String vnfName) {
131         return getResponse(vnfName, INVALID_VNF_FILE_NAME);
132     }
133
134     private Response getResponse(final String expectedFileName, final String defaultFileName) {
135         String resourceLocation = getResourceLocation();
136         var responseString = ResourceUtils.getResourceAsString(resourceLocation + expectedFileName + DOT_JSON);
137         if (null == responseString) {
138             // if a response file is not found in expected location, look for it in default location
139             responseString = ResourceUtils.getResourceAsString(DEFAULT_RESOURCE_LOCATION + expectedFileName + DOT_JSON);
140         }
141         if (null != responseString) {
142             return Response.ok(responseString).build();
143         } else {
144             // if a response file is not available in expected or default location, return an appropriate 404 response
145             responseString = ResourceUtils.getResourceAsString(DEFAULT_RESOURCE_LOCATION + defaultFileName + DOT_JSON);
146             return Response.status(Response.Status.NOT_FOUND).entity(responseString).build();
147         }
148     }
149
150     private String getResourceLocation() {
151         return Registry.getOrDefault(this.getClass().getName() + "_RESOURCE_LOCATION", String.class,
152             DEFAULT_RESOURCE_LOCATION);
153     }
154 }