Merge "fix code smell in model-impl"
[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 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;
44
45     private static Map<String, SoResponse> ongoingRequestMap = new ConcurrentHashMap<>();
46
47     /**
48      * Stats method.
49      *
50      * @return response
51      */
52     @GET
53     @Path("/Stats")
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();
59
60     }
61
62     /**
63      * Get stat type.
64      *
65      * @param statType the stat type
66      * @return http response
67      */
68     @GET
69     @Path("/OneStat/{statType}")
70     public Response serviceGetStat(@PathParam("statType") final String statType) {
71         statMessagesReceived++;
72         return Response.status(200).entity("{\"TYPE\": " + statType + "}").build();
73     }
74
75     /**
76      * Post to service instantiation.
77      *
78      * @param jsonString string to send
79      * @return http response
80      */
81     @POST
82     @Path("/serviceInstantiation/v7")
83     public Response servicePostRequest(final String jsonString) {
84         postMessagesReceived++;
85         return buildResponse(jsonString);
86     }
87
88     /**
89      * Post.
90      *
91      * @param serviceInstanceId service instance id
92      * @param vnfInstanceId vnf instance id
93      * @param jsonString json body
94      * @return http response
95      */
96     @POST
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);
102     }
103
104     /**
105      * Get instance ID.
106      *
107      * @param nsInstanceId node instance id
108      * @return http response
109      */
110     @GET
111     @Path("/orchestrationRequests/v5/{nsInstanceId}")
112     public Response soRequestStatus(@PathParam("nsInstanceId") final String nsInstanceId) {
113
114         SoResponse response = ongoingRequestMap.get(nsInstanceId);
115
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();
121         }
122
123         ongoingRequestMap.remove(nsInstanceId);
124
125         if ("ReturnBadAfterWait".equals(response.getRequest().getRequestType())) {
126             return Response.status(400).build();
127         }
128
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();
134     }
135
136     /**
137      * Delete.
138      *
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
144      */
145     @DELETE
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);
154     }
155
156     private Response buildResponse(String jsonString) {
157         if (jsonString == null) {
158             return Response.status(400).build();
159         }
160
161         SoRequest request = null;
162         try {
163             request = new Gson().fromJson(jsonString, SoRequest.class);
164         } catch (Exception e) {
165             return Response.status(400).build();
166         }
167
168         if (request == null) {
169             return Response.status(400).build();
170         }
171
172         if (request.getRequestType() == null) {
173             return Response.status(400).build();
174         }
175
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();
181         }
182
183         SoResponse response = new SoResponse();
184         response.setRequest(request);
185         response.setRequestReferences(new SoRequestReferences());
186         response.getRequestReferences().setRequestId(request.getRequestId().toString());
187
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)
194                     .build();
195         }
196
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)
203                     .build();
204         }
205
206         if ("ReturnOnging202".equals(request.getRequestType())) {
207             ongoingRequestMap.put(request.getRequestId().toString(), response);
208
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)
214                     .build();
215         }
216
217         if ("ReturnOnging200".equals(request.getRequestType())) {
218             ongoingRequestMap.put(request.getRequestId().toString(), response);
219
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)
225                     .build();
226         }
227
228         if ("ReturnBadAfterWait".equals(request.getRequestType())) {
229             ongoingRequestMap.put(request.getRequestId().toString(), response);
230
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)
236                     .build();
237         }
238         return null;
239     }
240 }