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 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,
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(SoRequest3gpp request) {
134 return ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.3gpp.success.json");
138 @Path("/infra/serviceIntent/v1/modify")
139 @Consumes(MediaType.APPLICATION_JSON)
140 @Produces("application/json")
141 public String soPostModifyCll(SoRequest3gpp request) {
142 return ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.cll.success.json");
145 private String makeStarted() {
146 var requestId = UUID.randomUUID().toString();
148 var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.started.json");
150 incomplete.add(requestId);
152 return response.replace(REPLACE_ME, requestId);
155 private String makeImmediateComplete() {
156 var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.immediate.success.json");
157 return response.replace(REPLACE_ME, UUID.randomUUID().toString());
160 private String makeComplete(String requestId) {
161 var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.complete.success.json");
162 return response.replace(REPLACE_ME, requestId);
165 private String makeStillRunning(String requestId) {
166 var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.still.running.json");
167 return response.replace(REPLACE_ME, requestId);