Support vDNS RainyDay test Case.
[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-2020 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 io.swagger.annotations.ApiParam;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.UUID;
29 import java.util.concurrent.ConcurrentHashMap;
30 import javax.ws.rs.Consumes;
31 import javax.ws.rs.DELETE;
32 import javax.ws.rs.GET;
33 import javax.ws.rs.POST;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.PathParam;
36 import javax.ws.rs.Produces;
37 import javax.ws.rs.core.MediaType;
38 import lombok.Setter;
39 import org.onap.policy.common.utils.resources.ResourceUtils;
40 import org.onap.policy.so.SoRequest;
41
42 @Path("/")
43 public class SoSimulatorJaxRs {
44
45     private static final String REPLACE_ME = "${replaceMe}";
46     /**
47      * Set of incomplete request IDs. When a POST or DELETE is performed, the new request
48      * ID is added to the set. When the request is polled, the ID is removed and a "still
49      * running" response is returned. When the request is polled again, it sees that there
50      * is no entry and returns a completion indication.
51      *
52      * <p/>
53      * This is static so request IDs are retained across servlets.
54      */
55     private static final Set<String> incomplete = ConcurrentHashMap.newKeySet();
56
57     /**
58      * {@code True} if requests should require polling, {@code false}
59      * otherwise.  This is used when junit testing the SO actor.
60      */
61     @Setter
62     private static boolean requirePolling = false;
63
64     /**
65      * SO post query.
66      *
67      * @param serviceInstanceId the service instance Id
68      * @param vnfInstanceId the VNF Id
69      * @return the response
70      */
71     @POST
72     @Path("/serviceInstantiation/v7/serviceInstances/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/scaleOut")
73     @Consumes(MediaType.APPLICATION_JSON)
74     @Produces("application/json")
75     public String soPostQuery(@PathParam("serviceInstanceId") final String serviceInstanceId,
76                     @PathParam("vnfInstanceId") final String vnfInstanceId,
77                     @ApiParam(required = true) SoRequest request) {
78
79         List<Map<String, String>> userParam = null;
80         userParam = request.getRequestDetails().getRequestParameters().getUserParams();
81         if (!userParam.isEmpty() && userParam.toString().contains("FAIL")) {
82             // this will be treated as a failure by the SO actor as it's missing the request ID
83             return "{}";
84         }
85         return (requirePolling ? makeStarted() : makeImmediateComplete());
86     }
87
88     /**
89      * SO Delete.
90      *
91      * @param serviceInstanceId the service instance Id
92      * @param vnfInstanceId the VNF Id
93      * @return the response
94      */
95     @DELETE
96     @Path("/serviceInstances/v7/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/{vfModuleInstanceId}")
97     @Consumes(MediaType.APPLICATION_JSON)
98     @Produces("application/json")
99     public String soDelete(@PathParam("serviceInstanceId") final String serviceInstanceId,
100                     @PathParam("vnfInstanceId") final String vnfInstanceId,
101                     @PathParam("vfModuleInstanceId") final String vfModuleInstanceId) {
102
103         return (requirePolling ? makeStarted() : makeImmediateComplete());
104     }
105
106     /**
107      * Poll SO result.
108      *
109      * @param requestId the ID of the request whose status is to be queried
110      * @return the response
111      */
112     @GET
113     @Path("/orchestrationRequests/v5/{requestId}")
114     @Consumes(MediaType.APPLICATION_JSON)
115     @Produces("application/json")
116     public String soGetQuery(@PathParam("requestId") final String requestId) {
117         if (incomplete.remove(requestId)) {
118             // first poll - return "still running"
119             return makeStillRunning(requestId);
120
121         } else {
122             return makeComplete(requestId);
123         }
124     }
125
126     private String makeStarted() {
127         String requestId = UUID.randomUUID().toString();
128
129         String response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.started.json");
130
131         incomplete.add(requestId);
132
133         return response.replace(REPLACE_ME, requestId);
134     }
135
136     private String makeImmediateComplete() {
137         String response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.immediate.success.json");
138         return response.replace(REPLACE_ME, UUID.randomUUID().toString());
139     }
140
141     private String makeComplete(String requestId) {
142         String response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.complete.success.json");
143         return response.replace(REPLACE_ME, requestId);
144     }
145
146     private String makeStillRunning(String requestId) {
147         String response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.still.running.json");
148         return response.replace(REPLACE_ME, requestId);
149     }
150 }