Update snapshot and/or references of policy/models to latest snapshots
[policy/models.git] / models-interactions / model-simulators / src / main / java / org / onap / policy / simulators / SoSimulatorJaxRs.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * simulators
4  * ================================================================================
5  * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019, 2023 Nordix Foundation.
7  * Modifications Copyright (C) 2020 Wipro Limited.
8  * Modifications Copyright (C) 2022 CTC, Inc. and others.
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.simulators;
25
26 import jakarta.ws.rs.Consumes;
27 import jakarta.ws.rs.DELETE;
28 import jakarta.ws.rs.GET;
29 import jakarta.ws.rs.POST;
30 import jakarta.ws.rs.PUT;
31 import jakarta.ws.rs.Path;
32 import jakarta.ws.rs.PathParam;
33 import jakarta.ws.rs.Produces;
34 import jakarta.ws.rs.core.MediaType;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Set;
38 import java.util.UUID;
39 import java.util.concurrent.ConcurrentHashMap;
40 import lombok.Setter;
41 import org.onap.policy.common.utils.resources.ResourceUtils;
42 import org.onap.policy.so.SoRequest;
43 import org.onap.policy.so.SoRequest3gpp;
44
45 @Path("/")
46 public class SoSimulatorJaxRs {
47
48     private static final String REPLACE_ME = "${replaceMe}";
49     /**
50      * Set of incomplete request IDs. When a POST or DELETE is performed, the new request
51      * ID is added to the set. When the request is polled, the ID is removed and a "still
52      * running" response is returned. When the request is polled again, it sees that there
53      * is no entry and returns a completion indication.
54      *
55      * <p/>
56      * This is static so request IDs are retained across servlets.
57      */
58     private static final Set<String> incomplete = ConcurrentHashMap.newKeySet();
59
60     /**
61      * {@code True} if requests should require polling, {@code false}
62      * otherwise.  This is used when junit testing the SO actor.
63      */
64     @Setter
65     private static boolean requirePolling = false;
66
67     /**
68      * SO post query.
69      *
70      * @param serviceInstanceId the service instance ID
71      * @param vnfInstanceId the VNF ID
72      * @return the response
73      */
74     @POST
75     @Path("/serviceInstantiation/v7/serviceInstances/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/scaleOut")
76     @Consumes(MediaType.APPLICATION_JSON)
77     @Produces("application/json")
78     public String soPostQuery(@PathParam("serviceInstanceId") final String serviceInstanceId,
79                     @PathParam("vnfInstanceId") final String vnfInstanceId,
80                     SoRequest request) {
81
82         List<Map<String, String>> userParam = null;
83         userParam = request.getRequestDetails().getRequestParameters().getUserParams();
84         if (!userParam.isEmpty() && userParam.toString().contains("FAIL")) {
85             // this will be treated as a failure by the SO actor as it's missing the request ID
86             return "{}";
87         }
88         return (requirePolling ? makeStarted() : makeImmediateComplete());
89     }
90
91     /**
92      * SO Delete.
93      *
94      * @param serviceInstanceId the service instance ID
95      * @param vnfInstanceId the VNF ID
96      * @return the response
97      */
98     @DELETE
99     @Path("/serviceInstances/v7/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/{vfModuleInstanceId}")
100     @Consumes(MediaType.APPLICATION_JSON)
101     @Produces("application/json")
102     public String soDelete(@PathParam("serviceInstanceId") final String serviceInstanceId,
103                     @PathParam("vnfInstanceId") final String vnfInstanceId,
104                     @PathParam("vfModuleInstanceId") final String vfModuleInstanceId) {
105
106         return (requirePolling ? makeStarted() : makeImmediateComplete());
107     }
108
109     /**
110      * Poll SO result.
111      *
112      * @param requestId the ID of the request whose status is to be queried
113      * @return the response
114      */
115     @GET
116     @Path("/orchestrationRequests/v5/{requestId}")
117     @Consumes(MediaType.APPLICATION_JSON)
118     @Produces("application/json")
119     public String soGetQuery(@PathParam("requestId") final String requestId) {
120         if (incomplete.remove(requestId)) {
121             // first poll - return "still running"
122             return makeStillRunning(requestId);
123
124         } else {
125             return makeComplete(requestId);
126         }
127     }
128
129     @PUT
130     @Path("/3gppservices/v7/modify")
131     @Consumes(MediaType.APPLICATION_JSON)
132     @Produces("application/json")
133     public String soPost3gpp(SoRequest3gpp request) {
134         return ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.3gpp.success.json");
135     }
136
137     @PUT
138     @Path("/infra/serviceIntent/v1/modify")
139     @Consumes(MediaType.APPLICATION_JSON)
140     @Produces("application/json")
141     public String soPostModifyCll(SoRequest3gpp request) {
142         return ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.cll.success.json");
143     }
144
145     private String makeStarted() {
146         var requestId = UUID.randomUUID().toString();
147
148         var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.started.json");
149
150         incomplete.add(requestId);
151
152         return response.replace(REPLACE_ME, requestId);
153     }
154
155     private String makeImmediateComplete() {
156         var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.immediate.success.json");
157         return response.replace(REPLACE_ME, UUID.randomUUID().toString());
158     }
159
160     private String makeComplete(String requestId) {
161         var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.complete.success.json");
162         return response.replace(REPLACE_ME, requestId);
163     }
164
165     private String makeStillRunning(String requestId) {
166         var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.still.running.json");
167         return response.replace(REPLACE_ME, requestId);
168     }
169 }