2 * ============LICENSE_START=======================================================
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
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 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;
39 public class AaiSimulatorJaxRs {
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";
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\"}]}";
72 * @param req the request
73 * @return the response
74 * @throws IOException if a response file cannot be read
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");
85 * A&AI get PNF query using pnfName.
88 * @throws IOException if a response file cannot be read
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);
99 * A&AI get PNF query using pnf-id.
102 * @throws IOException if a response file cannot be read
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);
113 * A&AI get VNF query using vnf-id.
116 * @throws IOException if a response file cannot be read
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);
127 * A&AI get VNF query using vnf-name.
130 * @throws IOException if a response file cannot be read
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);
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);
147 if (null != responseString) {
148 return Response.ok(responseString).build();
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();
156 private String getResourceLocation() {
157 return Registry.getOrDefault(this.getClass().getName() + "_RESOURCE_LOCATION", String.class,
158 DEFAULT_RESOURCE_LOCATION);