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.coder.Coder;
37 import org.onap.policy.common.utils.coder.CoderException;
38 import org.onap.policy.common.utils.coder.StandardCoder;
39 import org.onap.policy.so.SoRequest;
40 import org.onap.policy.so.SoRequestReferences;
41 import org.onap.policy.so.SoRequestStatus;
42 import org.onap.policy.so.SoResponse;
46 public class SoSimulatorJaxRs {
47 private final Coder coder = new StandardCoder();
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 an empty
52 * response is returned. When the request is polled again, it sees that there is no
53 * 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 the initial request should yield an incomplete, {@code false}
62 * otherwise. This is used when junit testing the SO actor.
65 private static boolean yieldIncomplete = 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) throws CoderException {
81 return coder.encode(yieldIncomplete ? makeIncomplete() : makeComplete(UUID.randomUUID().toString()));
87 * @param serviceInstanceId the service instance Id
88 * @param vnfInstanceId the VNF Id
89 * @return the response
92 @Path("/serviceInstances/v7/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/{vfModuleInstanceId}")
93 @Consumes(MediaType.APPLICATION_JSON)
94 @Produces("application/json")
95 public String soDelete(@PathParam("serviceInstanceId") final String serviceInstanceId,
96 @PathParam("vnfInstanceId") final String vnfInstanceId,
97 @PathParam("vfModuleInstanceId") final String vfModuleInstanceId) throws CoderException {
99 return coder.encode(yieldIncomplete ? makeIncomplete() : makeComplete(UUID.randomUUID().toString()));
105 * @param requestId the ID of the request whose status is to be queried
106 * @return the response
109 @Path("/orchestrationRequests/v5/{requestId}")
110 @Consumes(MediaType.APPLICATION_JSON)
111 @Produces("application/json")
112 public String soGetQuery(@PathParam("requestId") final String requestId) throws CoderException {
113 if (incomplete.remove(requestId)) {
114 // first poll - return empty response
115 return coder.encode(new SoResponse());
118 return coder.encode(makeComplete(requestId));
122 private SoResponse makeIncomplete() {
123 final SoResponse response = makeResponse();
124 response.getRequest().getRequestStatus().setRequestState("INCOMPLETE");
126 incomplete.add(response.getRequestReferences().getRequestId());
131 private SoResponse makeComplete(String requestId) {
132 final SoResponse response = makeResponse();
134 response.getRequest().getRequestStatus().setRequestState("COMPLETE");
135 response.getRequest().setRequestId(UUID.fromString(requestId));
140 private SoResponse makeResponse() {
141 final SoRequest request = new SoRequest();
142 final SoRequestStatus requestStatus = new SoRequestStatus();
143 request.setRequestStatus(requestStatus);
144 request.setRequestId(UUID.randomUUID());
146 final SoResponse response = new SoResponse();
148 final SoRequestReferences requestReferences = new SoRequestReferences();
149 final String requestId = UUID.randomUUID().toString();
150 requestReferences.setRequestId(requestId);
151 response.setRequestReferences(requestReferences);
153 response.setRequest(request);