Merge "Remove commons-io dependency from models"
[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.List;
24 import java.util.concurrent.CompletableFuture;
25 import javax.ws.rs.core.Response;
26 import org.apache.commons.lang3.StringUtils;
27 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
28 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
29 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
30 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation;
31 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
32 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
33 import org.onap.policy.vfc.VfcHealActionVmInfo;
34 import org.onap.policy.vfc.VfcHealAdditionalParams;
35 import org.onap.policy.vfc.VfcHealRequest;
36 import org.onap.policy.vfc.VfcRequest;
37 import org.onap.policy.vfc.VfcResponse;
38
39 public abstract class VfcOperation extends HttpOperation<VfcResponse> {
40     public static final String FAILED = "FAILED";
41     public static final String COMPLETE = "COMPLETE";
42     public static final int VFC_RESPONSE_CODE = 999;
43     public static final String GENERIC_VNF_ID = "generic-vnf.vnf-id";
44
45     public static final String REQ_PARAM_NM = "requestParameters";
46     public static final String CONFIG_PARAM_NM = "configurationParameters";
47
48     // @formatter:off
49     private static final List<String> PROPERTY_NAMES = List.of(
50                             OperationProperties.ENRICHMENT_SERVICE_ID,
51                             OperationProperties.ENRICHMENT_VSERVER_ID,
52                             OperationProperties.ENRICHMENT_VSERVER_NAME,
53                             OperationProperties.ENRICHMENT_GENERIC_VNF_ID);
54     // @formatter:on
55
56     /**
57      * Job ID extracted from the first response.
58      */
59     private String jobId;
60
61
62     /**
63      * Constructs the object.
64      *
65      * @param params operation parameters
66      * @param config configuration for this operation
67      */
68     public VfcOperation(ControlLoopOperationParams params, HttpConfig config) {
69         super(params, config, VfcResponse.class, PROPERTY_NAMES);
70
71         setUsePolling();
72     }
73
74     @Override
75     protected void resetPollCount() {
76         super.resetPollCount();
77         jobId = null;
78     }
79
80     @Override
81     protected String getPollingPath() {
82         return super.getPollingPath() + jobId;
83     }
84
85     /**
86      * Starts the GUARD.
87      */
88     @Override
89     protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
90         return startGuardAsync();
91     }
92
93     @Override
94     protected Status detmStatus(Response rawResponse, VfcResponse response) {
95         if (rawResponse.getStatus() == 200) {
96             String requestState = getRequestState(response);
97             if ("finished".equalsIgnoreCase(requestState)) {
98                 extractJobId(response);
99                 return Status.SUCCESS;
100             }
101
102             if ("error".equalsIgnoreCase(requestState)) {
103                 extractJobId(response);
104                 return Status.FAILURE;
105             }
106         }
107
108         // still incomplete
109
110         // need a job ID with which to query
111         if (jobId == null && !extractJobId(response)) {
112             throw new IllegalArgumentException("missing job ID in response");
113         }
114
115         return Status.STILL_WAITING;
116     }
117
118     private boolean extractJobId(VfcResponse response) {
119         if (response == null || response.getJobId() == null) {
120             return false;
121         }
122
123         jobId = response.getJobId();
124         return true;
125     }
126
127     /**
128      * Gets the request state of a response.
129      *
130      * @param response response from which to get the state
131      * @return the request state of the response, or {@code null} if it does not exist
132      */
133     protected String getRequestState(VfcResponse response) {
134         if (response == null || response.getResponseDescriptor() == null
135                         || StringUtils.isBlank(response.getResponseDescriptor().getStatus())) {
136             return null;
137         }
138         return response.getResponseDescriptor().getStatus();
139     }
140
141     /**
142      * Treats everything as a success, so we always go into
143      * {@link #postProcessResponse(OperationOutcome, String, Response, SoResponse)}.
144      */
145     @Override
146     protected boolean isSuccess(Response rawResponse, VfcResponse response) {
147         return true;
148     }
149
150     /**
151      * Prepends the message with the http status code.
152      */
153     @Override
154     public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, Response rawResponse,
155                     VfcResponse response) {
156
157         // set default result and message
158         setOutcome(outcome, result);
159
160         int code = (result == OperationResult.FAILURE_TIMEOUT ? VFC_RESPONSE_CODE : rawResponse.getStatus());
161
162         outcome.setResponse(response);
163         outcome.setMessage(code + " " + outcome.getMessage());
164         return outcome;
165     }
166
167     /**
168      * Construct VfcRequestObject from the ControlLoopOperationParams.
169      *
170      * @return request
171      */
172     protected VfcRequest constructVfcRequest() {
173         final String serviceInstance = getOptProperty(OperationProperties.ENRICHMENT_SERVICE_ID,
174                         "service-instance.service-instance-id");
175         final String vmId = getOptProperty(OperationProperties.ENRICHMENT_VSERVER_ID, "vserver.vserver-id");
176         final String vmName = getOptProperty(OperationProperties.ENRICHMENT_VSERVER_NAME, "vserver.vserver-name");
177         final String vnfId = getOptProperty(OperationProperties.ENRICHMENT_GENERIC_VNF_ID, GENERIC_VNF_ID);
178
179         if (StringUtils.isBlank(serviceInstance) || StringUtils.isBlank(vmId) || StringUtils.isBlank(vmName)) {
180             // original code did not check the VNF id, so we won't check it either
181             throw new IllegalArgumentException(
182                             "Cannot extract enrichment data for service instance, server id, or server name.");
183         }
184
185         VfcHealActionVmInfo vmActionInfo = new VfcHealActionVmInfo();
186         vmActionInfo.setVmid(vmId);
187         vmActionInfo.setVmname(vmName);
188
189         VfcHealAdditionalParams additionalParams = new VfcHealAdditionalParams();
190         additionalParams.setAction(getName());
191         additionalParams.setActionInfo(vmActionInfo);
192
193         VfcHealRequest healRequest = new VfcHealRequest();
194         healRequest.setVnfInstanceId(vnfId);
195         healRequest.setCause(getName());
196         healRequest.setAdditionalParams(additionalParams);
197
198         VfcRequest request = new VfcRequest();
199         request.setHealRequest(healRequest);
200         request.setNsInstanceId(serviceInstance);
201         request.setRequestId(params.getRequestId());
202
203         return request;
204     }
205
206     /**
207      * Gets an optional property, first checking the properties, then checking the
208      * enrichment data.
209      *
210      * @param propName property name
211      * @param enrichmentName property name within the enrichment data
212      * @return the property's value, or {@code null} if it is not found
213      */
214     protected String getOptProperty(String propName, String enrichmentName) {
215         if (containsProperty(propName)) {
216             // return the value, even if it's null
217             return getProperty(propName);
218         }
219
220         return params.getContext().getEnrichment().get(enrichmentName);
221     }
222 }