Moving common polling code into HttpOperation
[policy/models.git] / models-interactions / model-actors / actor.vfc / src / main / java / org / onap / policy / controlloop / actor / vfc / VfcOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.actor.vfc;
22
23 import java.util.concurrent.CompletableFuture;
24 import javax.ws.rs.core.Response;
25 import org.apache.commons.lang3.StringUtils;
26 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
27 import org.onap.policy.controlloop.actorserviceprovider.controlloop.ControlLoopEventContext;
28 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation;
29 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
30 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
31 import org.onap.policy.controlloop.policy.PolicyResult;
32 import org.onap.policy.vfc.VfcHealActionVmInfo;
33 import org.onap.policy.vfc.VfcHealAdditionalParams;
34 import org.onap.policy.vfc.VfcHealRequest;
35 import org.onap.policy.vfc.VfcRequest;
36 import org.onap.policy.vfc.VfcResponse;
37
38 public abstract class VfcOperation extends HttpOperation<VfcResponse> {
39     public static final String FAILED = "FAILED";
40     public static final String COMPLETE = "COMPLETE";
41     public static final int VFC_RESPONSE_CODE = 999;
42     public static final String GENERIC_VNF_ID = "generic-vnf.vnf-id";
43
44     public static final String REQ_PARAM_NM = "requestParameters";
45     public static final String CONFIG_PARAM_NM = "configurationParameters";
46
47     /**
48      * Job ID extracted from the first response.
49      */
50     private String jobId;
51
52
53     /**
54      * Constructs the object.
55      *
56      * @param params operation parameters
57      * @param config configuration for this operation
58      */
59     public VfcOperation(ControlLoopOperationParams params, HttpConfig config) {
60         super(params, config, VfcResponse.class);
61
62         setUsePolling();
63     }
64
65     @Override
66     protected void resetPollCount() {
67         super.resetPollCount();
68         jobId = null;
69     }
70
71     @Override
72     protected String getPollingPath() {
73         return super.getPollingPath() + jobId;
74     }
75
76     /**
77      * Starts the GUARD.
78      */
79     @Override
80     protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
81         return startGuardAsync();
82     }
83
84     @Override
85     protected Status detmStatus(Response rawResponse, VfcResponse response) {
86         if (rawResponse.getStatus() == 200) {
87             String requestState = getRequestState(response);
88             if ("finished".equalsIgnoreCase(requestState)) {
89                 extractJobId(response);
90                 return Status.SUCCESS;
91             }
92
93             if ("error".equalsIgnoreCase(requestState)) {
94                 extractJobId(response);
95                 return Status.FAILURE;
96             }
97         }
98
99         // still incomplete
100
101         // need a job ID with which to query
102         if (jobId == null && !extractJobId(response)) {
103             throw new IllegalArgumentException("missing job ID in response");
104         }
105
106         return Status.STILL_WAITING;
107     }
108
109     private boolean extractJobId(VfcResponse response) {
110         if (response == null || response.getJobId() == null) {
111             return false;
112         }
113
114         jobId = response.getJobId();
115         return true;
116     }
117
118     /**
119      * Gets the request state of a response.
120      *
121      * @param response response from which to get the state
122      * @return the request state of the response, or {@code null} if it does not exist
123      */
124     protected String getRequestState(VfcResponse response) {
125         if (response == null || response.getResponseDescriptor() == null
126                 || StringUtils.isBlank(response.getResponseDescriptor().getStatus())) {
127             return null;
128         }
129         return response.getResponseDescriptor().getStatus();
130     }
131
132     /**
133      * Treats everything as a success, so we always go into
134      * {@link #postProcessResponse(OperationOutcome, String, Response, SoResponse)}.
135      */
136     @Override
137     protected boolean isSuccess(Response rawResponse, VfcResponse response) {
138         return true;
139     }
140
141     /**
142      * Prepends the message with the http status code.
143      */
144     @Override
145     public OperationOutcome setOutcome(OperationOutcome outcome, PolicyResult result, Response rawResponse,
146             VfcResponse response) {
147
148         // set default result and message
149         setOutcome(outcome, result);
150
151         int code = (result == PolicyResult.FAILURE_TIMEOUT ? VFC_RESPONSE_CODE : rawResponse.getStatus());
152
153         outcome.setResponse(response);
154         outcome.setMessage(code + " " + outcome.getMessage());
155         return outcome;
156     }
157
158     /**
159      * Construct VfcRequestObject from the ControlLoopOperationParams.
160      *
161      * @return request
162      */
163     protected VfcRequest constructVfcRequest() {
164         ControlLoopEventContext context = params.getContext();
165         String serviceInstance = context.getEnrichment().get("service-instance.service-instance-id");
166         String vmId = context.getEnrichment().get("vserver.vserver-id");
167         String vmName = context.getEnrichment().get("vserver.vserver-name");
168
169         if (StringUtils.isBlank(serviceInstance) || StringUtils.isBlank(vmId) || StringUtils.isBlank(vmName)) {
170             throw new IllegalArgumentException(
171                     "Cannot extract enrichment data for service instance, server id, or server name.");
172         }
173
174         VfcHealActionVmInfo vmActionInfo = new VfcHealActionVmInfo();
175         vmActionInfo.setVmid(vmId);
176         vmActionInfo.setVmname(vmName);
177
178         VfcHealAdditionalParams additionalParams = new VfcHealAdditionalParams();
179         additionalParams.setAction(getName());
180         additionalParams.setActionInfo(vmActionInfo);
181
182         VfcHealRequest healRequest = new VfcHealRequest();
183         healRequest.setVnfInstanceId(params.getContext().getEvent().getAai().get(GENERIC_VNF_ID));
184         healRequest.setCause(getName());
185         healRequest.setAdditionalParams(additionalParams);
186
187         VfcRequest request = new VfcRequest();
188         request.setHealRequest(healRequest);
189         request.setNsInstanceId(serviceInstance);
190         request.setRequestId(context.getEvent().getRequestId());
191
192         return request;
193     }
194 }