Closed loop operation guarantee for ccvpn
[policy/models.git] / models-interactions / model-simulators / src / main / java / org / onap / policy / simulators / SoSimulatorJaxRs.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * simulators
4  * ================================================================================
5  * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
7  * Modifications Copyright (C) 2020 Wipro Limited.
8  * Modifications Copyright (C) 2022 CTC, Inc. and others.
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.simulators;
25
26 import io.swagger.annotations.ApiParam;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.UUID;
31 import java.util.concurrent.ConcurrentHashMap;
32 import javax.ws.rs.Consumes;
33 import javax.ws.rs.DELETE;
34 import javax.ws.rs.GET;
35 import javax.ws.rs.POST;
36 import javax.ws.rs.PUT;
37 import javax.ws.rs.Path;
38 import javax.ws.rs.PathParam;
39 import javax.ws.rs.Produces;
40 import javax.ws.rs.core.MediaType;
41 import lombok.Setter;
42 import org.onap.policy.common.utils.resources.ResourceUtils;
43 import org.onap.policy.so.SoRequest;
44 import org.onap.policy.so.SoRequest3gpp;
45
46 @Path("/")
47 public class SoSimulatorJaxRs {
48
49     private static final String REPLACE_ME = "${replaceMe}";
50     /**
51      * Set of incomplete request IDs. When a POST or DELETE is performed, the new request
52      * ID is added to the set. When the request is polled, the ID is removed and a "still
53      * running" response is returned. When the request is polled again, it sees that there
54      * is no entry and returns a completion indication.
55      *
56      * <p/>
57      * This is static so request IDs are retained across servlets.
58      */
59     private static final Set<String> incomplete = ConcurrentHashMap.newKeySet();
60
61     /**
62      * {@code True} if requests should require polling, {@code false}
63      * otherwise.  This is used when junit testing the SO actor.
64      */
65     @Setter
66     private static boolean requirePolling = false;
67
68     /**
69      * SO post query.
70      *
71      * @param serviceInstanceId the service instance Id
72      * @param vnfInstanceId the VNF Id
73      * @return the response
74      */
75     @POST
76     @Path("/serviceInstantiation/v7/serviceInstances/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/scaleOut")
77     @Consumes(MediaType.APPLICATION_JSON)
78     @Produces("application/json")
79     public String soPostQuery(@PathParam("serviceInstanceId") final String serviceInstanceId,
80                     @PathParam("vnfInstanceId") final String vnfInstanceId,
81                     @ApiParam(required = true) SoRequest request) {
82
83         List<Map<String, String>> userParam = null;
84         userParam = request.getRequestDetails().getRequestParameters().getUserParams();
85         if (!userParam.isEmpty() && userParam.toString().contains("FAIL")) {
86             // this will be treated as a failure by the SO actor as it's missing the request ID
87             return "{}";
88         }
89         return (requirePolling ? makeStarted() : makeImmediateComplete());
90     }
91
92     /**
93      * SO Delete.
94      *
95      * @param serviceInstanceId the service instance Id
96      * @param vnfInstanceId the VNF Id
97      * @return the response
98      */
99     @DELETE
100     @Path("/serviceInstances/v7/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/{vfModuleInstanceId}")
101     @Consumes(MediaType.APPLICATION_JSON)
102     @Produces("application/json")
103     public String soDelete(@PathParam("serviceInstanceId") final String serviceInstanceId,
104                     @PathParam("vnfInstanceId") final String vnfInstanceId,
105                     @PathParam("vfModuleInstanceId") final String vfModuleInstanceId) {
106
107         return (requirePolling ? makeStarted() : makeImmediateComplete());
108     }
109
110     /**
111      * Poll SO result.
112      *
113      * @param requestId the ID of the request whose status is to be queried
114      * @return the response
115      */
116     @GET
117     @Path("/orchestrationRequests/v5/{requestId}")
118     @Consumes(MediaType.APPLICATION_JSON)
119     @Produces("application/json")
120     public String soGetQuery(@PathParam("requestId") final String requestId) {
121         if (incomplete.remove(requestId)) {
122             // first poll - return "still running"
123             return makeStillRunning(requestId);
124
125         } else {
126             return makeComplete(requestId);
127         }
128     }
129
130     @PUT
131     @Path("/3gppservices/v7/modify")
132     @Consumes(MediaType.APPLICATION_JSON)
133     @Produces("application/json")
134     public String soPost3gpp(@ApiParam(required = true) SoRequest3gpp request) {
135         return ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.3gpp.success.json");
136     }
137
138     @PUT
139     @Path("/infra/serviceIntent/v1/modify")
140     @Consumes(MediaType.APPLICATION_JSON)
141     @Produces("application/json")
142     public String soPostModifyCll(@ApiParam(required = true) SoRequest3gpp request) {
143         return ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.cll.success.json");
144     }
145
146     private String makeStarted() {
147         var requestId = UUID.randomUUID().toString();
148
149         var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.started.json");
150
151         incomplete.add(requestId);
152
153         return response.replace(REPLACE_ME, requestId);
154     }
155
156     private String makeImmediateComplete() {
157         var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.immediate.success.json");
158         return response.replace(REPLACE_ME, UUID.randomUUID().toString());
159     }
160
161     private String makeComplete(String requestId) {
162         var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.complete.success.json");
163         return response.replace(REPLACE_ME, requestId);
164     }
165
166     private String makeStillRunning(String requestId) {
167         var response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.still.running.json");
168         return response.replace(REPLACE_ME, requestId);
169     }
170 }