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 * Modifications Copyright (C) 2019 Nordix Foundation.
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.so;
26 import com.google.gson.Gson;
28 import java.util.concurrent.ConcurrentHashMap;
29 import javax.ws.rs.DELETE;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.Path;
33 import javax.ws.rs.PathParam;
34 import javax.ws.rs.core.Response;
37 public class SoDummyServerTest {
39 private static int postMessagesReceived = 0;
40 private static int putMessagesReceived = 0;
41 private static int statMessagesReceived = 0;
42 private static int getMessagesReceived = 0;
43 private static int deleteMessagesReceived = 0;
45 private static Map<String, SoResponse> ongoingRequestMap = new ConcurrentHashMap<>();
54 public Response serviceGetStats() {
55 statMessagesReceived++;
56 return Response.status(200).entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
57 + ",\"POST\": " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived
58 + ",\"DELETE\": " + deleteMessagesReceived + "}").build();
65 * @param statType the stat type
66 * @return http response
69 @Path("/OneStat/{statType}")
70 public Response serviceGetStat(@PathParam("statType") final String statType) {
71 statMessagesReceived++;
72 return Response.status(200).entity("{\"TYPE\": " + statType + "}").build();
76 * Post to service instantiation.
78 * @param jsonString string to send
79 * @return http response
82 @Path("/serviceInstantiation/v7")
83 public Response servicePostRequest(final String jsonString) {
84 postMessagesReceived++;
85 return buildResponse(jsonString);
91 * @param serviceInstanceId service instance id
92 * @param vnfInstanceId vnf instance id
93 * @param jsonString json body
94 * @return http response
97 @Path("/serviceInstantiation/v7/serviceInstances/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/scaleOut")
98 public Response servicePostRequestVfModules(@PathParam("serviceInstanceId") final String serviceInstanceId,
99 @PathParam("vnfInstanceId") final String vnfInstanceId, final String jsonString) {
100 postMessagesReceived++;
101 return buildResponse(jsonString);
107 * @param nsInstanceId node instance id
108 * @return http response
111 @Path("/orchestrationRequests/v5/{nsInstanceId}")
112 public Response soRequestStatus(@PathParam("nsInstanceId") final String nsInstanceId) {
114 SoResponse response = ongoingRequestMap.get(nsInstanceId);
116 int iterationsLeft = Integer.valueOf(response.getRequest().getRequestScope());
117 if (--iterationsLeft > 0) {
118 response.getRequest().setRequestScope(new Integer(iterationsLeft).toString());
119 String responseString = new Gson().toJson(response, SoResponse.class);
120 return Response.status(response.getHttpResponseCode()).entity(responseString).build();
123 ongoingRequestMap.remove(nsInstanceId);
125 if ("ReturnBadAfterWait".equals(response.getRequest().getRequestType())) {
126 return Response.status(400).build();
129 response.getRequest().getRequestStatus().setRequestState("COMPLETE");
130 response.getRequest().setRequestScope("0");
131 response.setHttpResponseCode(200);
132 String responseString = new Gson().toJson(response, SoResponse.class);
133 return Response.status(response.getHttpResponseCode()).entity(responseString).build();
139 * @param serviceInstanceId service instance id
140 * @param vnfInstanceId vnf instance id
141 * @param vfModuleInstanceId vf module instance id
142 * @param jsonString json body
143 * @return http response
146 @Path("/serviceInstances/v7/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/{vfModuleInstanceId}")
147 public Response serviceDeleteRequestVfModules(
148 @PathParam("serviceInstanceId") final String serviceInstanceId,
149 @PathParam("vnfInstanceId") final String vnfInstanceId,
150 @PathParam("vfModuleInstanceId") final String vfModuleInstanceId,
151 final String jsonString) {
152 deleteMessagesReceived++;
153 return buildResponse(jsonString);
156 private Response buildResponse(String jsonString) {
157 if (jsonString == null) {
158 return Response.status(400).build();
161 SoRequest request = null;
163 request = new Gson().fromJson(jsonString, SoRequest.class);
164 } catch (Exception e) {
165 return Response.status(400).build();
168 if (request == null) {
169 return Response.status(400).build();
172 if (request.getRequestType() == null) {
173 return Response.status(400).build();
176 if ("ReturnBadJson".equals(request.getRequestType())) {
177 return Response.status(200)
178 .entity("{\"GET\": , " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
179 + ",\"POST\":" + " , " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived
180 + ",\"DELETE\": " + deleteMessagesReceived + "}").build();
183 SoResponse response = new SoResponse();
184 response.setRequest(request);
185 response.setRequestReferences(new SoRequestReferences());
186 response.getRequestReferences().setRequestId(request.getRequestId().toString());
188 if ("ReturnCompleted".equals(request.getRequestType())) {
189 response.getRequest().getRequestStatus().setRequestState("COMPLETE");
190 response.setHttpResponseCode(200);
191 String responseString = new Gson().toJson(response, SoResponse.class);
192 return Response.status(response.getHttpResponseCode())
193 .entity(responseString)
197 if ("ReturnFailed".equals(request.getRequestType())) {
198 response.getRequest().getRequestStatus().setRequestState("FAILED");
199 response.setHttpResponseCode(200);
200 String responseString = new Gson().toJson(response, SoResponse.class);
201 return Response.status(response.getHttpResponseCode())
202 .entity(responseString)
206 if ("ReturnOnging202".equals(request.getRequestType())) {
207 ongoingRequestMap.put(request.getRequestId().toString(), response);
209 response.getRequest().getRequestStatus().setRequestState("ONGOING");
210 response.setHttpResponseCode(202);
211 String responseString = new Gson().toJson(response, SoResponse.class);
212 return Response.status(response.getHttpResponseCode())
213 .entity(responseString)
217 if ("ReturnOnging200".equals(request.getRequestType())) {
218 ongoingRequestMap.put(request.getRequestId().toString(), response);
220 response.getRequest().getRequestStatus().setRequestState("ONGOING");
221 response.setHttpResponseCode(200);
222 String responseString = new Gson().toJson(response, SoResponse.class);
223 return Response.status(response.getHttpResponseCode())
224 .entity(responseString)
228 if ("ReturnBadAfterWait".equals(request.getRequestType())) {
229 ongoingRequestMap.put(request.getRequestId().toString(), response);
231 response.getRequest().getRequestStatus().setRequestState("ONGOING");
232 response.setHttpResponseCode(200);
233 String responseString = new Gson().toJson(response, SoResponse.class);
234 return Response.status(response.getHttpResponseCode())
235 .entity(responseString)