SO poll should not require request ID
[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.coder.Coder;
37 import org.onap.policy.common.utils.coder.CoderException;
38 import org.onap.policy.common.utils.coder.StandardCoder;
39 import org.onap.policy.so.SoRequest;
40 import org.onap.policy.so.SoRequestReferences;
41 import org.onap.policy.so.SoRequestStatus;
42 import org.onap.policy.so.SoResponse;
43
44
45 @Path("/")
46 public class SoSimulatorJaxRs {
47     private final Coder coder = new StandardCoder();
48
49     /**
50      * Set of incomplete request IDs. When a POST or DELETE is performed, the new request
51      * ID is added to the set. When the request is polled, the ID is removed and an empty
52      * response is returned. When the request is polled again, it sees that there is no
53      * entry and returns a completion indication.
54      *
55      * <p/>
56      * This is static so request IDs are retained across servlets.
57      */
58     private static final Set<String> incomplete = ConcurrentHashMap.newKeySet();
59
60     /**
61      * {@code True} if the initial request should yield an incomplete, {@code false}
62      * otherwise.  This is used when junit testing the SO actor.
63      */
64     @Setter
65     private static boolean yieldIncomplete = false;
66
67     /**
68      * SO post query.
69      *
70      * @param serviceInstanceId the service instance Id
71      * @param vnfInstanceId the VNF Id
72      * @return the response
73      */
74     @POST
75     @Path("/serviceInstantiation/v7/serviceInstances/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/scaleOut")
76     @Consumes(MediaType.APPLICATION_JSON)
77     @Produces("application/json")
78     public String soPostQuery(@PathParam("serviceInstanceId") final String serviceInstanceId,
79                     @PathParam("vnfInstanceId") final String vnfInstanceId) throws CoderException {
80
81         return coder.encode(yieldIncomplete ? makeIncomplete() : makeComplete(UUID.randomUUID().toString()));
82     }
83
84     /**
85      * SO Delete.
86      *
87      * @param serviceInstanceId the service instance Id
88      * @param vnfInstanceId the VNF Id
89      * @return the response
90      */
91     @DELETE
92     @Path("/serviceInstances/v7/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/{vfModuleInstanceId}")
93     @Consumes(MediaType.APPLICATION_JSON)
94     @Produces("application/json")
95     public String soDelete(@PathParam("serviceInstanceId") final String serviceInstanceId,
96                     @PathParam("vnfInstanceId") final String vnfInstanceId,
97                     @PathParam("vfModuleInstanceId") final String vfModuleInstanceId) throws CoderException {
98
99         return coder.encode(yieldIncomplete ? makeIncomplete() : makeComplete(UUID.randomUUID().toString()));
100     }
101
102     /**
103      * Poll SO result.
104      *
105      * @param requestId the ID of the request whose status is to be queried
106      * @return the response
107      */
108     @GET
109     @Path("/orchestrationRequests/v5/{requestId}")
110     @Consumes(MediaType.APPLICATION_JSON)
111     @Produces("application/json")
112     public String soGetQuery(@PathParam("requestId") final String requestId) throws CoderException {
113         if (incomplete.remove(requestId)) {
114             // first poll - return empty response
115             return coder.encode(new SoResponse());
116
117         } else {
118             return coder.encode(makeComplete(requestId));
119         }
120     }
121
122     private SoResponse makeIncomplete() {
123         final SoResponse response = makeResponse();
124         response.getRequest().getRequestStatus().setRequestState("INCOMPLETE");
125
126         incomplete.add(response.getRequestReferences().getRequestId());
127
128         return response;
129     }
130
131     private SoResponse makeComplete(String requestId) {
132         final SoResponse response = makeResponse();
133
134         response.getRequest().getRequestStatus().setRequestState("COMPLETE");
135         response.getRequest().setRequestId(UUID.fromString(requestId));
136
137         return response;
138     }
139
140     private SoResponse makeResponse() {
141         final SoRequest request = new SoRequest();
142         final SoRequestStatus requestStatus = new SoRequestStatus();
143         request.setRequestStatus(requestStatus);
144         request.setRequestId(UUID.randomUUID());
145
146         final SoResponse response = new SoResponse();
147
148         final SoRequestReferences requestReferences = new SoRequestReferences();
149         final String requestId = UUID.randomUUID().toString();
150         requestReferences.setRequestId(requestId);
151         response.setRequestReferences(requestReferences);
152
153         response.setRequest(request);
154
155         return response;
156     }
157 }