2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.policy.simulators;
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;
38 public class AaiSimulatorJaxRs {
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";
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\"}]}";
71 * @param req the request
72 * @return the response
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");
83 * A&AI get PNF query using pnfName.
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);
96 * A&AI get PNF query using pnf-id.
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);
109 * A&AI get VNF query using vnf-id.
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);
122 * A&AI get VNF query using vnf-name.
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);
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);
141 if (null != responseString) {
142 return Response.ok(responseString).build();
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();
150 private String getResourceLocation() {
151 return Registry.getOrDefault(this.getClass().getName() + "_RESOURCE_LOCATION", String.class,
152 DEFAULT_RESOURCE_LOCATION);