3d2187895e708425f77687bad3e63b3112ddd70d
[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 Nordix Foundation.
7  * Modifications Copyright (C) 2020 Wipro Limited.
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.policy.simulators;
24
25 import io.swagger.annotations.ApiParam;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 import java.util.UUID;
30 import java.util.concurrent.ConcurrentHashMap;
31 import javax.ws.rs.Consumes;
32 import javax.ws.rs.DELETE;
33 import javax.ws.rs.GET;
34 import javax.ws.rs.POST;
35 import javax.ws.rs.PUT;
36 import javax.ws.rs.Path;
37 import javax.ws.rs.PathParam;
38 import javax.ws.rs.Produces;
39 import javax.ws.rs.core.MediaType;
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                     @ApiParam(required = true) 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(@ApiParam(required = true) SoRequest3gpp request) {
134         return ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.3gpp.success.json");
135     }
136
137     private String makeStarted() {
138         var requestId = UUID.randomUUID().toString();
139
140         var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.started.json");
141
142         incomplete.add(requestId);
143
144         return response.replace(REPLACE_ME, requestId);
145     }
146
147     private String makeImmediateComplete() {
148         var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.immediate.success.json");
149         return response.replace(REPLACE_ME, UUID.randomUUID().toString());
150     }
151
152     private String makeComplete(String requestId) {
153         var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.complete.success.json");
154         return response.replace(REPLACE_ME, requestId);
155     }
156
157     private String makeStillRunning(String requestId) {
158         var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.still.running.json");
159         return response.replace(REPLACE_ME, requestId);
160     }
161 }