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 * 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
14 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
24 package org.onap.policy.simulators;
26 import io.swagger.annotations.ApiParam;
27 import java.util.List;
30 import java.util.UUID;
31 import java.util.concurrent.ConcurrentHashMap;
32 import javax.ws.rs.Consumes;
33 import javax.ws.rs.DELETE;
34 import javax.ws.rs.GET;
35 import javax.ws.rs.POST;
36 import javax.ws.rs.PUT;
37 import javax.ws.rs.Path;
38 import javax.ws.rs.PathParam;
39 import javax.ws.rs.Produces;
40 import javax.ws.rs.core.MediaType;
42 import org.onap.policy.common.utils.resources.ResourceUtils;
43 import org.onap.policy.so.SoRequest;
44 import org.onap.policy.so.SoRequest3gpp;
47 public class SoSimulatorJaxRs {
49 private static final String REPLACE_ME = "${replaceMe}";
51 * Set of incomplete request IDs. When a POST or DELETE is performed, the new request
52 * ID is added to the set. When the request is polled, the ID is removed and a "still
53 * running" response is returned. When the request is polled again, it sees that there
54 * is no entry and returns a completion indication.
57 * This is static so request IDs are retained across servlets.
59 private static final Set<String> incomplete = ConcurrentHashMap.newKeySet();
62 * {@code True} if requests should require polling, {@code false}
63 * otherwise. This is used when junit testing the SO actor.
66 private static boolean requirePolling = false;
71 * @param serviceInstanceId the service instance Id
72 * @param vnfInstanceId the VNF Id
73 * @return the response
76 @Path("/serviceInstantiation/v7/serviceInstances/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/scaleOut")
77 @Consumes(MediaType.APPLICATION_JSON)
78 @Produces("application/json")
79 public String soPostQuery(@PathParam("serviceInstanceId") final String serviceInstanceId,
80 @PathParam("vnfInstanceId") final String vnfInstanceId,
81 @ApiParam(required = true) SoRequest request) {
83 List<Map<String, String>> userParam = null;
84 userParam = request.getRequestDetails().getRequestParameters().getUserParams();
85 if (!userParam.isEmpty() && userParam.toString().contains("FAIL")) {
86 // this will be treated as a failure by the SO actor as it's missing the request ID
89 return (requirePolling ? makeStarted() : makeImmediateComplete());
95 * @param serviceInstanceId the service instance Id
96 * @param vnfInstanceId the VNF Id
97 * @return the response
100 @Path("/serviceInstances/v7/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/{vfModuleInstanceId}")
101 @Consumes(MediaType.APPLICATION_JSON)
102 @Produces("application/json")
103 public String soDelete(@PathParam("serviceInstanceId") final String serviceInstanceId,
104 @PathParam("vnfInstanceId") final String vnfInstanceId,
105 @PathParam("vfModuleInstanceId") final String vfModuleInstanceId) {
107 return (requirePolling ? makeStarted() : makeImmediateComplete());
113 * @param requestId the ID of the request whose status is to be queried
114 * @return the response
117 @Path("/orchestrationRequests/v5/{requestId}")
118 @Consumes(MediaType.APPLICATION_JSON)
119 @Produces("application/json")
120 public String soGetQuery(@PathParam("requestId") final String requestId) {
121 if (incomplete.remove(requestId)) {
122 // first poll - return "still running"
123 return makeStillRunning(requestId);
126 return makeComplete(requestId);
131 @Path("/3gppservices/v7/modify")
132 @Consumes(MediaType.APPLICATION_JSON)
133 @Produces("application/json")
134 public String soPost3gpp(@ApiParam(required = true) SoRequest3gpp request) {
135 return ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.3gpp.success.json");
139 @Path("/infra/serviceIntent/v1/modify")
140 @Consumes(MediaType.APPLICATION_JSON)
141 @Produces("application/json")
142 public String soPostModifyCll(@ApiParam(required = true) SoRequest3gpp request) {
143 return ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.cll.success.json");
146 private String makeStarted() {
147 var requestId = UUID.randomUUID().toString();
149 var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.started.json");
151 incomplete.add(requestId);
153 return response.replace(REPLACE_ME, requestId);
156 private String makeImmediateComplete() {
157 var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.immediate.success.json");
158 return response.replace(REPLACE_ME, UUID.randomUUID().toString());
161 private String makeComplete(String requestId) {
162 var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.complete.success.json");
163 return response.replace(REPLACE_ME, requestId);
166 private String makeStillRunning(String requestId) {
167 var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.still.running.json");
168 return response.replace(REPLACE_ME, requestId);