2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.actor.vfc;
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;
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";
45 public static final String REQ_PARAM_NM = "requestParameters";
46 public static final String CONFIG_PARAM_NM = "configurationParameters";
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);
57 * Job ID extracted from the first response.
63 * Constructs the object.
65 * @param params operation parameters
66 * @param config configuration for this operation
68 public VfcOperation(ControlLoopOperationParams params, HttpConfig config) {
69 super(params, config, VfcResponse.class, PROPERTY_NAMES);
75 protected void resetPollCount() {
76 super.resetPollCount();
81 protected String getPollingPath() {
82 return super.getPollingPath() + jobId;
89 protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
90 return startGuardAsync();
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;
102 if ("error".equalsIgnoreCase(requestState)) {
103 extractJobId(response);
104 return Status.FAILURE;
110 // need a job ID with which to query
111 if (jobId == null && !extractJobId(response)) {
112 throw new IllegalArgumentException("missing job ID in response");
115 return Status.STILL_WAITING;
118 private boolean extractJobId(VfcResponse response) {
119 if (response == null || response.getJobId() == null) {
123 jobId = response.getJobId();
128 * Gets the request state of a response.
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
133 protected String getRequestState(VfcResponse response) {
134 if (response == null || response.getResponseDescriptor() == null
135 || StringUtils.isBlank(response.getResponseDescriptor().getStatus())) {
138 return response.getResponseDescriptor().getStatus();
142 * Treats everything as a success, so we always go into
143 * {@link #postProcessResponse(OperationOutcome, String, Response, SoResponse)}.
146 protected boolean isSuccess(Response rawResponse, VfcResponse response) {
151 * Prepends the message with the http status code.
154 public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, Response rawResponse,
155 VfcResponse response) {
157 // set default result and message
158 setOutcome(outcome, result);
160 int code = (result == OperationResult.FAILURE_TIMEOUT ? VFC_RESPONSE_CODE : rawResponse.getStatus());
162 outcome.setResponse(response);
163 outcome.setMessage(code + " " + outcome.getMessage());
168 * Construct VfcRequestObject from the ControlLoopOperationParams.
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);
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.");
185 VfcHealActionVmInfo vmActionInfo = new VfcHealActionVmInfo();
186 vmActionInfo.setVmid(vmId);
187 vmActionInfo.setVmname(vmName);
189 VfcHealAdditionalParams additionalParams = new VfcHealAdditionalParams();
190 additionalParams.setAction(getName());
191 additionalParams.setActionInfo(vmActionInfo);
193 VfcHealRequest healRequest = new VfcHealRequest();
194 healRequest.setVnfInstanceId(vnfId);
195 healRequest.setCause(getName());
196 healRequest.setAdditionalParams(additionalParams);
198 VfcRequest request = new VfcRequest();
199 request.setHealRequest(healRequest);
200 request.setNsInstanceId(serviceInstance);
201 request.setRequestId(params.getRequestId());
207 * Gets an optional property, first checking the properties, then checking the
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
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);
220 return params.getContext().getEnrichment().get(enrichmentName);