aboutsummaryrefslogtreecommitdiffstats
path: root/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/AaiSimulatorJaxRs.java
blob: ea50240babcb24da21295883c57c6f0bee9bff2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*-
 * ============LICENSE_START=======================================================
 * simulators
 * ================================================================================
 * Copyright (C) 2017-2018, 2020-2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2019 Nordix Foundation.
 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.simulators;

import java.io.IOException;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.common.utils.services.Registry;

@Path("/aai")
public class AaiSimulatorJaxRs {

    private static final String DOT_JSON = ".json";
    private static final String DEFAULT_RESOURCE_LOCATION = "org/onap/policy/simulators/aai/";
    private static final String INVALID_VNF_FILE_NAME = "invalid-vnf";
    private static final String INVALID_PNF_FILE_NAME = "invalid-pnf";

    /**
     * A&AI get query.
     *
     * @return the result
     */
    @GET
    @Path("/{version:v16|v21}/search/nodes-query")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces("application/json")
    public String aaiGetVserverQuery(@QueryParam("filter") final String filter) {
        if (filter.equals("vserver-name:EQUALS:f953c499-4b1e-426b-8c6d-e9e9f1fc730f")
            || filter.equals("vserver-name:EQUALS:Ete_vFWCLvFWSNK_7ba1fbde_0")
            || filter.equals("vserver-name:EQUALS:OzVServer")
            || filter.equals("vserver-name:EQUALS:testVserverName")) {
            return "{\"result-data\":[{\"resource-type\": \"vserver\",\"resource-link\":\"/aai/v15/"
                + "cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/RegionOne/tenants"
                + "/tenant/3f2aaef74ecb4b19b35e26d0849fe9a2/vservers/vserver/"
                + "6c3b3714-e36c-45af-9f16-7d3a73d99497\"}]}";
        } else {
            return null;
        }
    }

    /**
     * A&AI put query.
     *
     * @param req the request
     * @return the response
     * @throws IOException if a response file cannot be read
     */
    @PUT
    @Path("/{version:v16|v21}/query")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces("application/json")
    public Response aaiPutQuery(final String req) throws IOException {
        return getResponse("AaiCqResponse", "invalid-cq");
    }

    /**
     * A&AI get PNF query using pnfName.
     *
     * @return the result
     * @throws IOException if a response file cannot be read
     */
    @GET
    @Path("/{version:v16|v21}/network/pnfs/pnf/{pnfName}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces("application/json")
    public Response aaiGetPnfUsingPnfName(@PathParam("pnfName") final String pnfName) throws IOException {
        return getResponse(pnfName, INVALID_PNF_FILE_NAME);
    }

    /**
     * A&AI get PNF query using pnf-id.
     *
     * @return the result
     * @throws IOException if a response file cannot be read
     */
    @GET
    @Path("/{version:v16|v21}/network/pnfs/pnf")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces("application/json")
    public Response aaiGetPnfUsingPnfId(@QueryParam("pnf-id") final String pnfId) throws IOException {
        return getResponse(pnfId, INVALID_PNF_FILE_NAME);
    }

    /**
     * A&AI get VNF query using vnf-id.
     *
     * @return the result
     * @throws IOException if a response file cannot be read
     */
    @GET
    @Path("/{version:v16|v21}/network/generic-vnfs/generic-vnf/{vnfId}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces("application/json")
    public Response aaiGetVnfUsingVnfId(@PathParam("vnfId") final String vnfId) throws IOException {
        return getResponse(vnfId, INVALID_VNF_FILE_NAME);
    }

    /**
     * A&AI get VNF query using vnf-name.
     *
     * @return the result
     * @throws IOException if a response file cannot be read
     */
    @GET
    @Path("/{version:v16|v21}/network/generic-vnfs/generic-vnf")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces("application/json")
    public Response aaiGetVnfUsingVnfName(@QueryParam("vnf-name") final String vnfName) throws IOException {
        return getResponse(vnfName, INVALID_VNF_FILE_NAME);
    }

    private Response getResponse(final String expectedFileName, final String defaultFileName) {
        String resourceLocation = getResourceLocation();
        var responseString = ResourceUtils.getResourceAsString(resourceLocation + expectedFileName + DOT_JSON);
        if (null == responseString) {
            // if a response file is not found in expected location, look for it in default location
            responseString = ResourceUtils.getResourceAsString(DEFAULT_RESOURCE_LOCATION + expectedFileName + DOT_JSON);
        }
        if (null != responseString) {
            return Response.ok(responseString).build();
        } else {
            // if a response file is not available in expected or default location, return an appropriate 404 response
            responseString = ResourceUtils.getResourceAsString(DEFAULT_RESOURCE_LOCATION + defaultFileName + DOT_JSON);
            return Response.status(Response.Status.NOT_FOUND).entity(responseString).build();
        }
    }

    private String getResourceLocation() {
        return Registry.getOrDefault(this.getClass().getName() + "_RESOURCE_LOCATION", String.class,
            DEFAULT_RESOURCE_LOCATION);
    }
}