Merge "Use new version of A&AI APIs"
[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  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.simulators;
23
24 import java.io.IOException;
25 import java.nio.charset.StandardCharsets;
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 org.apache.commons.io.IOUtils;
35
36 @Path("/aai")
37 public class AaiSimulatorJaxRs {
38
39     private static final String GETFAIL = "getFail";
40
41     /**
42      * A&AI get query.
43      *
44      * @return the result
45      */
46     @GET
47     @Path("/{version:v16|v21}/search/nodes-query")
48     @Consumes(MediaType.APPLICATION_JSON)
49     @Produces("application/json")
50     public String aaiGetVserverQuery(@QueryParam("filter") final String filter) {
51         if (filter.equals("vserver-name:EQUALS:f953c499-4b1e-426b-8c6d-e9e9f1fc730f")
52             || filter.equals("vserver-name:EQUALS:Ete_vFWCLvFWSNK_7ba1fbde_0")
53             || filter.equals("vserver-name:EQUALS:OzVServer")
54             || filter.equals("vserver-name:EQUALS:testVserverName")) {
55             return "{\"result-data\":[{\"resource-type\": \"vserver\",\"resource-link\":\"/aai/v15/"
56                 + "cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/RegionOne/tenants"
57                 + "/tenant/3f2aaef74ecb4b19b35e26d0849fe9a2/vservers/vserver/"
58                 + "6c3b3714-e36c-45af-9f16-7d3a73d99497\"}]}";
59         } else {
60             return null;
61         }
62     }
63
64     /**
65      * A&AI put query.
66      *
67      * @param req the request
68      * @return the response
69      * @throws IOException if a response file cannot be read
70      */
71     @PUT
72     @Path("/{version:v16|v21}/query")
73     @Consumes(MediaType.APPLICATION_JSON)
74     @Produces("application/json")
75     public String aaiPutQuery(final String req) throws IOException {
76         return IOUtils.toString(getClass().getResource("aai/AaiCqResponse.json"),
77             StandardCharsets.UTF_8);
78     }
79
80     /**
81      * A&AI get PNF query.
82      *
83      * @return the result
84      * @throws IOException if a response file cannot be read
85      */
86     @GET
87     @Path("/{version:v16|v21}/network/pnfs/pnf/{pnfName}")
88     @Consumes(MediaType.APPLICATION_JSON)
89     @Produces("application/json")
90     public String aaiGetPnfQuery(@PathParam("pnfName") final String pnfName) throws IOException {
91         if (GETFAIL.equals(pnfName)) {
92             throw new IllegalArgumentException("query failed, as requested");
93         }
94
95         return IOUtils.toString(getClass().getResource("aai/AaiGetPnfResponse.json"),
96                         StandardCharsets.UTF_8);
97     }
98 }