2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.simulators;
24 import io.swagger.annotations.ApiParam;
25 import java.util.List;
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;
39 import org.onap.policy.common.utils.resources.ResourceUtils;
40 import org.onap.policy.so.SoRequest;
43 public class SoSimulatorJaxRs {
45 private static final String REPLACE_ME = "${replaceMe}";
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.
53 * This is static so request IDs are retained across servlets.
55 private static final Set<String> incomplete = ConcurrentHashMap.newKeySet();
58 * {@code True} if requests should require polling, {@code false}
59 * otherwise. This is used when junit testing the SO actor.
62 private static boolean requirePolling = false;
67 * @param serviceInstanceId the service instance Id
68 * @param vnfInstanceId the VNF Id
69 * @return the response
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) {
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
85 return (requirePolling ? makeStarted() : makeImmediateComplete());
91 * @param serviceInstanceId the service instance Id
92 * @param vnfInstanceId the VNF Id
93 * @return the response
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) {
103 return (requirePolling ? makeStarted() : makeImmediateComplete());
109 * @param requestId the ID of the request whose status is to be queried
110 * @return the response
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);
122 return makeComplete(requestId);
126 private String makeStarted() {
127 String requestId = UUID.randomUUID().toString();
129 String response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.started.json");
131 incomplete.add(requestId);
133 return response.replace(REPLACE_ME, requestId);
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());
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);
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);