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;
25 import java.util.UUID;
26 import java.util.concurrent.ConcurrentHashMap;
27 import javax.ws.rs.Consumes;
28 import javax.ws.rs.DELETE;
29 import javax.ws.rs.GET;
30 import javax.ws.rs.POST;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.PathParam;
33 import javax.ws.rs.Produces;
34 import javax.ws.rs.core.MediaType;
36 import org.onap.policy.common.utils.resources.ResourceUtils;
40 public class SoSimulatorJaxRs {
42 private static final String REPLACE_ME = "${replaceMe}";
45 * Set of incomplete request IDs. When a POST or DELETE is performed, the new request
46 * ID is added to the set. When the request is polled, the ID is removed and a "still
47 * running" response is returned. When the request is polled again, it sees that there
48 * is no entry and returns a completion indication.
51 * This is static so request IDs are retained across servlets.
53 private static final Set<String> incomplete = ConcurrentHashMap.newKeySet();
56 * {@code True} if requests should require polling, {@code false}
57 * otherwise. This is used when junit testing the SO actor.
60 private static boolean requirePolling = false;
65 * @param serviceInstanceId the service instance Id
66 * @param vnfInstanceId the VNF Id
67 * @return the response
70 @Path("/serviceInstantiation/v7/serviceInstances/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/scaleOut")
71 @Consumes(MediaType.APPLICATION_JSON)
72 @Produces("application/json")
73 public String soPostQuery(@PathParam("serviceInstanceId") final String serviceInstanceId,
74 @PathParam("vnfInstanceId") final String vnfInstanceId) {
76 return (requirePolling ? makeStarted() : makeComplete(UUID.randomUUID().toString()));
82 * @param serviceInstanceId the service instance Id
83 * @param vnfInstanceId the VNF Id
84 * @return the response
87 @Path("/serviceInstances/v7/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/{vfModuleInstanceId}")
88 @Consumes(MediaType.APPLICATION_JSON)
89 @Produces("application/json")
90 public String soDelete(@PathParam("serviceInstanceId") final String serviceInstanceId,
91 @PathParam("vnfInstanceId") final String vnfInstanceId,
92 @PathParam("vfModuleInstanceId") final String vfModuleInstanceId) {
94 return (requirePolling ? makeStarted() : makeComplete(UUID.randomUUID().toString()));
100 * @param requestId the ID of the request whose status is to be queried
101 * @return the response
104 @Path("/orchestrationRequests/v5/{requestId}")
105 @Consumes(MediaType.APPLICATION_JSON)
106 @Produces("application/json")
107 public String soGetQuery(@PathParam("requestId") final String requestId) {
108 if (incomplete.remove(requestId)) {
109 // first poll - return "still running"
110 return makeStillRunning(requestId);
113 return makeComplete(requestId);
117 private String makeStarted() {
118 String requestId = UUID.randomUUID().toString();
120 String response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.started.json");
122 incomplete.add(requestId);
124 return response.replace(REPLACE_ME, requestId);
127 private String makeComplete(String requestId) {
128 String response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.complete.success.json");
129 return response.replace(REPLACE_ME, requestId);
132 private String makeStillRunning(String requestId) {
133 String response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.still.running.json");
134 return response.replace(REPLACE_ME, requestId);