Cannot parse finishTime in SO responses
[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-2020 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.simulators;
23
24 import java.util.Set;
25 import java.util.UUID;
26 import java.util.concurrent.ConcurrentHashMap;
27 import javax.ws.rs.Consumes;
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.Produces;
34 import javax.ws.rs.core.MediaType;
35 import lombok.Setter;
36 import org.onap.policy.common.utils.resources.ResourceUtils;
37
38
39 @Path("/")
40 public class SoSimulatorJaxRs {
41
42     private static final String REPLACE_ME = "${replaceMe}";
43
44     /**
45      * Set of incomplete request IDs. When a POST or DELETE is performed, the new request
46      * ID is added to the set. When the request is polled, the ID is removed and a "still
47      * running" response is returned. When the request is polled again, it sees that there
48      * is no entry and returns a completion indication.
49      *
50      * <p/>
51      * This is static so request IDs are retained across servlets.
52      */
53     private static final Set<String> incomplete = ConcurrentHashMap.newKeySet();
54
55     /**
56      * {@code True} if requests should require polling, {@code false}
57      * otherwise.  This is used when junit testing the SO actor.
58      */
59     @Setter
60     private static boolean requirePolling = false;
61
62     /**
63      * SO post query.
64      *
65      * @param serviceInstanceId the service instance Id
66      * @param vnfInstanceId the VNF Id
67      * @return the response
68      */
69     @POST
70     @Path("/serviceInstantiation/v7/serviceInstances/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/scaleOut")
71     @Consumes(MediaType.APPLICATION_JSON)
72     @Produces("application/json")
73     public String soPostQuery(@PathParam("serviceInstanceId") final String serviceInstanceId,
74                     @PathParam("vnfInstanceId") final String vnfInstanceId) {
75
76         return (requirePolling ? makeStarted() : makeComplete(UUID.randomUUID().toString()));
77     }
78
79     /**
80      * SO Delete.
81      *
82      * @param serviceInstanceId the service instance Id
83      * @param vnfInstanceId the VNF Id
84      * @return the response
85      */
86     @DELETE
87     @Path("/serviceInstances/v7/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/{vfModuleInstanceId}")
88     @Consumes(MediaType.APPLICATION_JSON)
89     @Produces("application/json")
90     public String soDelete(@PathParam("serviceInstanceId") final String serviceInstanceId,
91                     @PathParam("vnfInstanceId") final String vnfInstanceId,
92                     @PathParam("vfModuleInstanceId") final String vfModuleInstanceId) {
93
94         return (requirePolling ? makeStarted() : makeComplete(UUID.randomUUID().toString()));
95     }
96
97     /**
98      * Poll SO result.
99      *
100      * @param requestId the ID of the request whose status is to be queried
101      * @return the response
102      */
103     @GET
104     @Path("/orchestrationRequests/v5/{requestId}")
105     @Consumes(MediaType.APPLICATION_JSON)
106     @Produces("application/json")
107     public String soGetQuery(@PathParam("requestId") final String requestId) {
108         if (incomplete.remove(requestId)) {
109             // first poll - return "still running"
110             return makeStillRunning(requestId);
111
112         } else {
113             return makeComplete(requestId);
114         }
115     }
116
117     private String makeStarted() {
118         String requestId = UUID.randomUUID().toString();
119
120         String response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.started.json");
121
122         incomplete.add(requestId);
123
124         return response.replace(REPLACE_ME, requestId);
125     }
126
127     private String makeComplete(String requestId) {
128         String response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.complete.success.json");
129         return response.replace(REPLACE_ME, requestId);
130     }
131
132     private String makeStillRunning(String requestId) {
133         String response = ResourceUtils.getResourceAsString("org/onap/policy/simulators/so/so.still.running.json");
134         return response.replace(REPLACE_ME, requestId);
135     }
136 }