2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Ericsson. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2018-2019 AT&T. All rights reserved.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.so;
25 import com.google.gson.Gson;
27 import java.util.concurrent.ConcurrentHashMap;
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.core.Response;
36 public class SoDummyServerTest {
38 private static int postMessagesReceived = 0;
39 private static int putMessagesReceived = 0;
40 private static int statMessagesReceived = 0;
41 private static int getMessagesReceived = 0;
42 private static int deleteMessagesReceived = 0;
44 private static Map<String, SoResponse> ongoingRequestMap = new ConcurrentHashMap<>();
53 public Response serviceGetStats() {
54 statMessagesReceived++;
55 return Response.status(200).entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
56 + ",\"POST\": " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived
57 + ",\"DELETE\": " + deleteMessagesReceived + "}").build();
64 * @param statType the stat type
65 * @return http response
68 @Path("/OneStat/{statType}")
69 public Response serviceGetStat(@PathParam("statType") final String statType) {
70 statMessagesReceived++;
71 return Response.status(200).entity("{\"TYPE\": " + statType + "}").build();
75 * Post to service instantiation.
77 * @param jsonString string to send
78 * @return http response
81 @Path("/serviceInstantiation/v7")
82 public Response servicePostRequest(final String jsonString) {
83 postMessagesReceived++;
84 return buildResponse(jsonString);
90 * @param serviceInstanceId service instance id
91 * @param vnfInstanceId vnf instance id
92 * @param jsonString json body
93 * @return http response
96 @Path("/serviceInstantiation/v7/serviceInstances/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/scaleOut")
97 public Response servicePostRequestVfModules(@PathParam("serviceInstanceId") final String serviceInstanceId,
98 @PathParam("vnfInstanceId") final String vnfInstanceId, final String jsonString) {
99 postMessagesReceived++;
100 return buildResponse(jsonString);
106 * @param nsInstanceId node instance id
107 * @return http response
110 @Path("/orchestrationRequests/v5/{nsInstanceId}")
111 public Response soRequestStatus(@PathParam("nsInstanceId") final String nsInstanceId) {
113 SoResponse response = ongoingRequestMap.get(nsInstanceId);
115 int iterationsLeft = Integer.valueOf(response.getRequest().getRequestScope());
116 if (--iterationsLeft > 0) {
117 response.getRequest().setRequestScope(new Integer(iterationsLeft).toString());
118 String responseString = new Gson().toJson(response, SoResponse.class);
119 return Response.status(response.getHttpResponseCode()).entity(responseString).build();
122 ongoingRequestMap.remove(nsInstanceId);
124 if ("ReturnBadAfterWait".equals(response.getRequest().getRequestType())) {
125 return Response.status(400).build();
128 response.getRequest().getRequestStatus().setRequestState("COMPLETE");
129 response.getRequest().setRequestScope("0");
130 response.setHttpResponseCode(200);
131 String responseString = new Gson().toJson(response, SoResponse.class);
132 return Response.status(response.getHttpResponseCode()).entity(responseString).build();
138 * @param serviceInstanceId service instance id
139 * @param vnfInstanceId vnf instance id
140 * @param vfModuleInstanceId vf module instance id
141 * @param jsonString json body
142 * @return http response
145 @Path("/serviceInstances/v7/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/{vfModuleInstanceId}")
146 public Response serviceDeleteRequestVfModules(
147 @PathParam("serviceInstanceId") final String serviceInstanceId,
148 @PathParam("vnfInstanceId") final String vnfInstanceId,
149 @PathParam("vfModuleInstanceId") final String vfModuleInstanceId,
150 final String jsonString) {
151 deleteMessagesReceived++;
152 return buildResponse(jsonString);
155 private Response buildResponse(String jsonString) {
156 if (jsonString == null) {
157 return Response.status(400).build();
160 SoRequest request = null;
162 request = new Gson().fromJson(jsonString, SoRequest.class);
163 } catch (Exception e) {
164 return Response.status(400).build();
167 if (request == null) {
168 return Response.status(400).build();
171 if (request.getRequestType() == null) {
172 return Response.status(400).build();
175 if ("ReturnBadJson".equals(request.getRequestType())) {
176 return Response.status(200)
177 .entity("{\"GET\": , " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
178 + ",\"POST\":" + " , " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived
179 + ",\"DELETE\": " + deleteMessagesReceived + "}").build();
182 SoResponse response = new SoResponse();
183 response.setRequest(request);
184 response.setRequestReferences(new SoRequestReferences());
185 response.getRequestReferences().setRequestId(request.getRequestId().toString());
187 if ("ReturnCompleted".equals(request.getRequestType())) {
188 response.getRequest().getRequestStatus().setRequestState("COMPLETE");
189 response.setHttpResponseCode(200);
190 String responseString = new Gson().toJson(response, SoResponse.class);
191 return Response.status(response.getHttpResponseCode())
192 .entity(responseString)
196 if ("ReturnFailed".equals(request.getRequestType())) {
197 response.getRequest().getRequestStatus().setRequestState("FAILED");
198 response.setHttpResponseCode(200);
199 String responseString = new Gson().toJson(response, SoResponse.class);
200 return Response.status(response.getHttpResponseCode())
201 .entity(responseString)
205 if ("ReturnOnging202".equals(request.getRequestType())) {
206 ongoingRequestMap.put(request.getRequestId().toString(), response);
208 response.getRequest().getRequestStatus().setRequestState("ONGOING");
209 response.setHttpResponseCode(202);
210 String responseString = new Gson().toJson(response, SoResponse.class);
211 return Response.status(response.getHttpResponseCode())
212 .entity(responseString)
216 if ("ReturnOnging200".equals(request.getRequestType())) {
217 ongoingRequestMap.put(request.getRequestId().toString(), response);
219 response.getRequest().getRequestStatus().setRequestState("ONGOING");
220 response.setHttpResponseCode(200);
221 String responseString = new Gson().toJson(response, SoResponse.class);
222 return Response.status(response.getHttpResponseCode())
223 .entity(responseString)
227 if ("ReturnBadAfterWait".equals(request.getRequestType())) {
228 ongoingRequestMap.put(request.getRequestId().toString(), response);
230 response.getRequest().getRequestStatus().setRequestState("ONGOING");
231 response.setHttpResponseCode(200);
232 String responseString = new Gson().toJson(response, SoResponse.class);
233 return Response.status(response.getHttpResponseCode())
234 .entity(responseString)