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 SoDummyServer {
39 private static final String ONGOING = "ONGOING";
40 private static int postMessagesReceived = 0;
41 private static int putMessagesReceived = 0;
42 private static int statMessagesReceived = 0;
43 private static int getMessagesReceived = 0;
44 private static int deleteMessagesReceived = 0;
46 private static Map<String, SoResponse> ongoingRequestMap = new ConcurrentHashMap<>();
55 public Response serviceGetStats() {
56 statMessagesReceived++;
57 return Response.status(200).entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
58 + ",\"POST\": " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived
59 + ",\"DELETE\": " + deleteMessagesReceived + "}").build();
66 * @param statType the stat type
67 * @return http response
70 @Path("/OneStat/{statType}")
71 public Response serviceGetStat(@PathParam("statType") final String statType) {
72 statMessagesReceived++;
73 return Response.status(200).entity("{\"TYPE\": " + statType + "}").build();
77 * Post to service instantiation.
79 * @param jsonString string to send
80 * @return http response
83 @Path("/serviceInstantiation/v7")
84 public Response servicePostRequest(final String jsonString) {
85 postMessagesReceived++;
86 return buildResponse(jsonString);
92 * @param serviceInstanceId service instance id
93 * @param vnfInstanceId vnf instance id
94 * @param jsonString json body
95 * @return http response
98 @Path("/serviceInstantiation/v7/serviceInstances/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/scaleOut")
99 public Response servicePostRequestVfModules(@PathParam("serviceInstanceId") final String serviceInstanceId,
100 @PathParam("vnfInstanceId") final String vnfInstanceId, final String jsonString) {
101 postMessagesReceived++;
102 return buildResponse(jsonString);
108 * @param nsInstanceId node instance id
109 * @return http response
112 @Path("/orchestrationRequests/v5/{nsInstanceId}")
113 public Response soRequestStatus(@PathParam("nsInstanceId") final String nsInstanceId) {
115 SoResponse response = ongoingRequestMap.get(nsInstanceId);
117 int iterationsLeft = Integer.parseInt(response.getRequest().getRequestScope());
118 if (--iterationsLeft > 0) {
119 response.getRequest().setRequestScope(Integer.toString(iterationsLeft));
120 String responseString = new Gson().toJson(response, SoResponse.class);
121 return Response.status(response.getHttpResponseCode()).entity(responseString).build();
124 ongoingRequestMap.remove(nsInstanceId);
126 if ("ReturnBadAfterWait".equals(response.getRequest().getRequestType())) {
127 return Response.status(400).build();
130 response.getRequest().getRequestStatus().setRequestState("COMPLETE");
131 response.getRequest().setRequestScope("0");
132 response.setHttpResponseCode(200);
133 String responseString = new Gson().toJson(response, SoResponse.class);
134 return Response.status(response.getHttpResponseCode()).entity(responseString).build();
140 * @param serviceInstanceId service instance id
141 * @param vnfInstanceId vnf instance id
142 * @param vfModuleInstanceId vf module instance id
143 * @param jsonString json body
144 * @return http response
147 @Path("/serviceInstances/v7/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/{vfModuleInstanceId}")
148 public Response serviceDeleteRequestVfModules(
149 @PathParam("serviceInstanceId") final String serviceInstanceId,
150 @PathParam("vnfInstanceId") final String vnfInstanceId,
151 @PathParam("vfModuleInstanceId") final String vfModuleInstanceId,
152 final String jsonString) {
153 deleteMessagesReceived++;
154 return buildResponse(jsonString);
157 private Response buildResponse(String jsonString) {
158 if (jsonString == null) {
159 return Response.status(400).build();
162 SoRequest request = new Gson().fromJson(jsonString, SoRequest.class);
164 if (request == null) {
165 return Response.status(400).build();
168 if (request.getRequestType() == null) {
169 return Response.status(400).build();
172 if ("ReturnBadJson".equals(request.getRequestType())) {
173 return Response.status(200)
174 .entity("{\"GET\": , " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
175 + ",\"POST\":" + " , " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived
176 + ",\"DELETE\": " + deleteMessagesReceived + "}").build();
179 SoResponse response = new SoResponse();
180 response.setRequest(request);
181 response.setRequestReferences(new SoRequestReferences());
182 response.getRequestReferences().setRequestId(request.getRequestId().toString());
184 if ("ReturnCompleted".equals(request.getRequestType())) {
185 response.getRequest().getRequestStatus().setRequestState("COMPLETE");
186 response.setHttpResponseCode(200);
187 String responseString = new Gson().toJson(response, SoResponse.class);
188 return Response.status(response.getHttpResponseCode())
189 .entity(responseString)
193 if ("ReturnFailed".equals(request.getRequestType())) {
194 response.getRequest().getRequestStatus().setRequestState("FAILED");
195 response.setHttpResponseCode(200);
196 String responseString = new Gson().toJson(response, SoResponse.class);
197 return Response.status(response.getHttpResponseCode())
198 .entity(responseString)
202 if ("ReturnOnging202".equals(request.getRequestType())) {
203 ongoingRequestMap.put(request.getRequestId().toString(), response);
205 response.getRequest().getRequestStatus().setRequestState(ONGOING);
206 response.setHttpResponseCode(202);
207 String responseString = new Gson().toJson(response, SoResponse.class);
208 return Response.status(response.getHttpResponseCode())
209 .entity(responseString)
213 if ("ReturnOnging200".equals(request.getRequestType())) {
214 ongoingRequestMap.put(request.getRequestId().toString(), response);
216 response.getRequest().getRequestStatus().setRequestState(ONGOING);
217 response.setHttpResponseCode(200);
218 String responseString = new Gson().toJson(response, SoResponse.class);
219 return Response.status(response.getHttpResponseCode())
220 .entity(responseString)
224 if ("ReturnBadAfterWait".equals(request.getRequestType())) {
225 ongoingRequestMap.put(request.getRequestId().toString(), response);
227 response.getRequest().getRequestStatus().setRequestState(ONGOING);
228 response.setHttpResponseCode(200);
229 String responseString = new Gson().toJson(response, SoResponse.class);
230 return Response.status(response.getHttpResponseCode())
231 .entity(responseString)