d7e7a1166be173ff6ebc41d26122700869fe4aa0
[policy/models.git] / models-interactions / model-impl / so / src / test / java / org / onap / policy / so / SoDummyServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * so
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
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
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=========================================================
22  */
23
24 package org.onap.policy.so;
25
26 import com.google.gson.Gson;
27 import java.util.Map;
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;
35
36 @Path("/SO")
37 public class SoDummyServer {
38
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;
45
46     private static Map<String, SoResponse> ongoingRequestMap = new ConcurrentHashMap<>();
47
48     /**
49      * Stats method.
50      *
51      * @return response
52      */
53     @GET
54     @Path("/Stats")
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();
60
61     }
62
63     /**
64      * Get stat type.
65      *
66      * @param statType the stat type
67      * @return http response
68      */
69     @GET
70     @Path("/OneStat/{statType}")
71     public Response serviceGetStat(@PathParam("statType") final String statType) {
72         statMessagesReceived++;
73         return Response.status(200).entity("{\"TYPE\": " + statType + "}").build();
74     }
75
76     /**
77      * Post to service instantiation.
78      *
79      * @param jsonString string to send
80      * @return http response
81      */
82     @POST
83     @Path("/serviceInstantiation/v7")
84     public Response servicePostRequest(final String jsonString) {
85         postMessagesReceived++;
86         return buildResponse(jsonString);
87     }
88
89     /**
90      * Post.
91      *
92      * @param serviceInstanceId service instance id
93      * @param vnfInstanceId vnf instance id
94      * @param jsonString json body
95      * @return http response
96      */
97     @POST
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);
103     }
104
105     /**
106      * Get instance ID.
107      *
108      * @param nsInstanceId node instance id
109      * @return http response
110      */
111     @GET
112     @Path("/orchestrationRequests/v5/{nsInstanceId}")
113     public Response soRequestStatus(@PathParam("nsInstanceId") final String nsInstanceId) {
114
115         SoResponse response = ongoingRequestMap.get(nsInstanceId);
116
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();
122         }
123
124         ongoingRequestMap.remove(nsInstanceId);
125
126         if ("ReturnBadAfterWait".equals(response.getRequest().getRequestType())) {
127             return Response.status(400).build();
128         }
129
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();
135     }
136
137     /**
138      * Delete.
139      *
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
145      */
146     @DELETE
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);
155     }
156
157     private Response buildResponse(String jsonString) {
158         if (jsonString == null) {
159             return Response.status(400).build();
160         }
161
162         SoRequest request = new Gson().fromJson(jsonString, SoRequest.class);
163
164         if (request == null) {
165             return Response.status(400).build();
166         }
167
168         if (request.getRequestType() == null) {
169             return Response.status(400).build();
170         }
171
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();
177         }
178
179         SoResponse response = new SoResponse();
180         response.setRequest(request);
181         response.setRequestReferences(new SoRequestReferences());
182         response.getRequestReferences().setRequestId(request.getRequestId().toString());
183
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)
190                     .build();
191         }
192
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)
199                     .build();
200         }
201
202         if ("ReturnOnging202".equals(request.getRequestType())) {
203             ongoingRequestMap.put(request.getRequestId().toString(), response);
204
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)
210                     .build();
211         }
212
213         if ("ReturnOnging200".equals(request.getRequestType())) {
214             ongoingRequestMap.put(request.getRequestId().toString(), response);
215
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)
221                     .build();
222         }
223
224         if ("ReturnBadAfterWait".equals(request.getRequestType())) {
225             ongoingRequestMap.put(request.getRequestId().toString(), response);
226
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)
232                     .build();
233         }
234         return null;
235     }
236 }