5c302474a78cf71cb20aaaf7aef8566caf41657c
[policy/drools-applications.git] / controlloop / common / model-impl / so / src / test / java / org / onap / policy / so / SoDummyServerTest.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  * ================================================================================
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.policy.so;
24
25 import com.google.gson.Gson;
26 import java.util.Map;
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;
34
35 @Path("/SO")
36 public class SoDummyServerTest {
37
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;
43
44     private static Map<String, SoResponse> ongoingRequestMap = new ConcurrentHashMap<>();
45
46     /**
47      * Stats method.
48      * 
49      * @return response
50      */
51     @GET
52     @Path("/Stats")
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();
58
59     }
60
61     /**
62      * Get stat type.
63      * 
64      * @param statType the stat type
65      * @return http response
66      */
67     @GET
68     @Path("/OneStat/{statType}")
69     public Response serviceGetStat(@PathParam("statType") final String statType) {
70         statMessagesReceived++;
71         return Response.status(200).entity("{\"TYPE\": " + statType + "}").build();
72     }
73
74     /**
75      * Post to service instantiation.
76      * 
77      * @param jsonString string to send
78      * @return http response
79      */
80     @POST
81     @Path("/serviceInstantiation/v7")
82     public Response servicePostRequest(final String jsonString) {
83         postMessagesReceived++;
84         return buildResponse(jsonString);
85     }
86
87     /**
88      * Post.
89      * 
90      * @param serviceInstanceId service instance id
91      * @param vnfInstanceId vnf instance id
92      * @param jsonString json body
93      * @return http response
94      */
95     @POST
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);
101     }
102
103     /**
104      * Get instance ID.
105      * 
106      * @param nsInstanceId node instance id
107      * @return http response
108      */
109     @GET
110     @Path("/orchestrationRequests/v5/{nsInstanceId}")
111     public Response soRequestStatus(@PathParam("nsInstanceId") final String nsInstanceId) {
112
113         SoResponse response = ongoingRequestMap.get(nsInstanceId);
114
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();
120         }
121
122         ongoingRequestMap.remove(nsInstanceId);
123
124         if ("ReturnBadAfterWait".equals(response.getRequest().getRequestType())) {
125             return Response.status(400).build();
126         }
127
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();
133     }
134
135     /**
136      * Delete.
137      *
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
143      */
144     @DELETE
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);
153     }
154
155     private Response buildResponse(String jsonString) {
156         if (jsonString == null) {
157             return Response.status(400).build();
158         }
159
160         SoRequest request = null;
161         try {
162             request = new Gson().fromJson(jsonString, SoRequest.class);
163         } catch (Exception e) {
164             return Response.status(400).build();
165         }
166
167         if (request == null) {
168             return Response.status(400).build();
169         }
170
171         if (request.getRequestType() == null) {
172             return Response.status(400).build();
173         }
174
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();
180         }
181
182         SoResponse response = new SoResponse();
183         response.setRequest(request);
184         response.setRequestReferences(new SoRequestReferences());
185         response.getRequestReferences().setRequestId(request.getRequestId().toString());
186
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)
193                     .build();
194         }
195
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)
202                     .build();
203         }
204
205         if ("ReturnOnging202".equals(request.getRequestType())) {
206             ongoingRequestMap.put(request.getRequestId().toString(), response);
207
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)
213                     .build();
214         }
215
216         if ("ReturnOnging200".equals(request.getRequestType())) {
217             ongoingRequestMap.put(request.getRequestId().toString(), response);
218
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)
224                     .build();
225         }
226
227         if ("ReturnBadAfterWait".equals(request.getRequestType())) {
228             ongoingRequestMap.put(request.getRequestId().toString(), response);
229
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)
235                     .build();
236         }
237         return null;
238     }
239 }