2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.policy.simulators;
25 import io.swagger.annotations.ApiParam;
26 import java.util.List;
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;
41 import org.onap.policy.common.utils.resources.ResourceUtils;
42 import org.onap.policy.so.SoRequest;
43 import org.onap.policy.so.SoRequest3gpp;
46 public class SoSimulatorJaxRs {
48 private static final String REPLACE_ME = "${replaceMe}";
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.
56 * This is static so request IDs are retained across servlets.
58 private static final Set<String> incomplete = ConcurrentHashMap.newKeySet();
61 * {@code True} if requests should require polling, {@code false}
62 * otherwise. This is used when junit testing the SO actor.
65 private static boolean requirePolling = false;
70 * @param serviceInstanceId the service instance Id
71 * @param vnfInstanceId the VNF Id
72 * @return the response
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) {
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
88 return (requirePolling ? makeStarted() : makeImmediateComplete());
94 * @param serviceInstanceId the service instance Id
95 * @param vnfInstanceId the VNF Id
96 * @return the response
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) {
106 return (requirePolling ? makeStarted() : makeImmediateComplete());
112 * @param requestId the ID of the request whose status is to be queried
113 * @return the response
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);
125 return makeComplete(requestId);
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");
137 private String makeStarted() {
138 var requestId = UUID.randomUUID().toString();
140 var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.started.json");
142 incomplete.add(requestId);
144 return response.replace(REPLACE_ME, requestId);
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());
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);
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);