aboutsummaryrefslogtreecommitdiffstats
path: root/models-interactions/model-impl/so/src/test/java/org/onap/policy/so/SoDummyServer.java
blob: bb7e851443dd8e3f7ce88f5ec3ce082a80d3a436 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*-
 * ============LICENSE_START=======================================================
 * so
 * ================================================================================
 * Copyright (C) 2018 Ericsson. All rights reserved.
 * ================================================================================
 * Modifications Copyright (C) 2018-2019 AT&T. All rights reserved.
 * Modifications Copyright (C) 2019, 2024 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.so;

import com.google.gson.Gson;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.core.Response;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Path("/SO")
public class SoDummyServer {

    private static final String ONGOING = "ONGOING";
    private static int postMessagesReceived = 0;
    private static int putMessagesReceived = 0;
    private static int statMessagesReceived = 0;
    private static int getMessagesReceived = 0;
    private static int deleteMessagesReceived = 0;

    private static Map<String, SoResponse> ongoingRequestMap = new ConcurrentHashMap<>();

    /**
     * Stats method.
     *
     * @return response
     */
    @GET
    @Path("/Stats")
    public Response serviceGetStats() {
        statMessagesReceived++;
        return Response.status(200).entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
                + ",\"POST\": " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived
                + ",\"DELETE\": " + deleteMessagesReceived + "}").build();

    }

    /**
     * Get stat type.
     *
     * @param statType the stat type
     * @return http response
     */
    @GET
    @Path("/OneStat/{statType}")
    public Response serviceGetStat(@PathParam("statType") final String statType) {
        statMessagesReceived++;
        return Response.status(200).entity("{\"TYPE\": " + statType + "}").build();
    }

    /**
     * Post to service instantiation.
     *
     * @param jsonString string to send
     * @return http response
     */
    @POST
    @Path("/serviceInstantiation/v7")
    public Response servicePostRequest(final String jsonString) {
        postMessagesReceived++;
        return buildResponse(jsonString);
    }

    /**
     * Post.
     *
     * @param serviceInstanceId service instance id
     * @param vnfInstanceId vnf instance id
     * @param jsonString json body
     * @return http response
     */
    @POST
    @Path("/serviceInstantiation/v7/serviceInstances/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/scaleOut")
    public Response servicePostRequestVfModules(@PathParam("serviceInstanceId") final String serviceInstanceId,
                    @PathParam("vnfInstanceId") final String vnfInstanceId, final String jsonString) {
        postMessagesReceived++;
        return buildResponse(jsonString);
    }

    /**
     * Get instance ID.
     *
     * @param nsInstanceId node instance id
     * @return http response
     */
    @GET
    @Path("/orchestrationRequests/v5/{nsInstanceId}")
    public Response soRequestStatus(@PathParam("nsInstanceId") final String nsInstanceId) {

        SoResponse response = ongoingRequestMap.get(nsInstanceId);

        int iterationsLeft = Integer.parseInt(response.getRequest().getRequestScope());
        if (--iterationsLeft > 0) {
            response.getRequest().setRequestScope(Integer.toString(iterationsLeft));
            String responseString = new Gson().toJson(response, SoResponse.class);
            return Response.status(response.getHttpResponseCode()).entity(responseString).build();
        }

        ongoingRequestMap.remove(nsInstanceId);

        if ("ReturnBadAfterWait".equals(response.getRequest().getRequestType())) {
            return Response.status(400).build();
        }

        response.getRequest().getRequestStatus().setRequestState("COMPLETE");
        response.getRequest().setRequestScope("0");
        response.setHttpResponseCode(200);
        String responseString = new Gson().toJson(response, SoResponse.class);
        return Response.status(response.getHttpResponseCode()).entity(responseString).build();
    }

    /**
     * Delete.
     *
     * @param serviceInstanceId service instance id
     * @param vnfInstanceId vnf instance id
     * @param vfModuleInstanceId vf module instance id
     * @param jsonString json body
     * @return http response
     */
    @DELETE
    @Path("/serviceInstances/v7/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/{vfModuleInstanceId}")
    public Response serviceDeleteRequestVfModules(
            @PathParam("serviceInstanceId") final String serviceInstanceId,
            @PathParam("vnfInstanceId") final String vnfInstanceId,
            @PathParam("vfModuleInstanceId") final String vfModuleInstanceId,
            final String jsonString) {
        deleteMessagesReceived++;
        return buildResponse(jsonString);
    }

    private Response buildResponse(String jsonString) {
        if (jsonString == null) {
            return Response.status(400).build();
        }

        SoRequest request = new Gson().fromJson(jsonString, SoRequest.class);

        if (request == null) {
            return Response.status(400).build();
        }

        if (request.getRequestType() == null) {
            return Response.status(400).build();
        }

        if ("ReturnBadJson".equals(request.getRequestType())) {
            return Response.status(200)
                    .entity("{\"GET\": , " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived
                            + ",\"POST\":" + " , " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived
                            + ",\"DELETE\": " + deleteMessagesReceived + "}").build();
        }

        SoResponse response = new SoResponse();
        response.setRequest(request);
        response.setRequestReferences(new SoRequestReferences());
        response.getRequestReferences().setRequestId(request.getRequestId().toString());

        if ("ReturnCompleted".equals(request.getRequestType())) {
            response.getRequest().getRequestStatus().setRequestState("COMPLETE");
            response.setHttpResponseCode(200);
            String responseString = new Gson().toJson(response, SoResponse.class);
            return Response.status(response.getHttpResponseCode())
                    .entity(responseString)
                    .build();
        }

        if ("ReturnFailed".equals(request.getRequestType())) {
            response.getRequest().getRequestStatus().setRequestState("FAILED");
            response.setHttpResponseCode(200);
            String responseString = new Gson().toJson(response, SoResponse.class);
            return Response.status(response.getHttpResponseCode())
                    .entity(responseString)
                    .build();
        }

        if ("ReturnOnging202".equals(request.getRequestType())) {
            ongoingRequestMap.put(request.getRequestId().toString(), response);

            response.getRequest().getRequestStatus().setRequestState(ONGOING);
            response.setHttpResponseCode(202);
            String responseString = new Gson().toJson(response, SoResponse.class);
            return Response.status(response.getHttpResponseCode())
                    .entity(responseString)
                    .build();
        }

        if ("ReturnOnging200".equals(request.getRequestType())) {
            ongoingRequestMap.put(request.getRequestId().toString(), response);

            response.getRequest().getRequestStatus().setRequestState(ONGOING);
            response.setHttpResponseCode(200);
            String responseString = new Gson().toJson(response, SoResponse.class);
            return Response.status(response.getHttpResponseCode())
                    .entity(responseString)
                    .build();
        }

        if ("ReturnBadAfterWait".equals(request.getRequestType())) {
            ongoingRequestMap.put(request.getRequestId().toString(), response);

            response.getRequest().getRequestStatus().setRequestState(ONGOING);
            response.setHttpResponseCode(200);
            String responseString = new Gson().toJson(response, SoResponse.class);
            return Response.status(response.getHttpResponseCode())
                    .entity(responseString)
                    .build();
        }
        return null;
    }
}