2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2023 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.controlloop.actor.vfc;
24 import jakarta.ws.rs.core.Response;
25 import java.util.List;
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 protected 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;
86 protected Status detmStatus(Response rawResponse, VfcResponse response) {
87 if (rawResponse.getStatus() == 200) {
88 String requestState = getRequestState(response);
89 if ("finished".equalsIgnoreCase(requestState)) {
90 extractJobId(response);
91 return Status.SUCCESS;
94 if ("error".equalsIgnoreCase(requestState)) {
95 extractJobId(response);
96 return Status.FAILURE;
102 // need a job ID with which to query
103 if (jobId == null && !extractJobId(response)) {
104 throw new IllegalArgumentException("missing job ID in response");
107 return Status.STILL_WAITING;
110 private boolean extractJobId(VfcResponse response) {
111 if (response == null || response.getJobId() == null) {
115 jobId = response.getJobId();
120 * Gets the request state of a response.
122 * @param response response from which to get the state
123 * @return the request state of the response, or {@code null} if it does not exist
125 protected String getRequestState(VfcResponse response) {
126 if (response == null || response.getResponseDescriptor() == null
127 || StringUtils.isBlank(response.getResponseDescriptor().getStatus())) {
130 return response.getResponseDescriptor().getStatus();
134 * Treats everything as a success, so we always go into
135 * {@link #postProcessResponse (OperationOutcome, String, Response, SoResponse)}.
138 protected boolean isSuccess(Response rawResponse, VfcResponse response) {
143 * Prepends the message with the http status code.
146 public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, Response rawResponse,
147 VfcResponse response) {
149 // set default result and message
150 setOutcome(outcome, result);
152 int code = (result == OperationResult.FAILURE_TIMEOUT ? VFC_RESPONSE_CODE : rawResponse.getStatus());
154 outcome.setResponse(response);
155 outcome.setMessage(code + " " + outcome.getMessage());
160 * Construct VfcRequestObject from the ControlLoopOperationParams.
164 protected VfcRequest constructVfcRequest() {
165 final String serviceInstance = getProperty(OperationProperties.ENRICHMENT_SERVICE_ID);
166 final String vmId = getProperty(OperationProperties.ENRICHMENT_VSERVER_ID);
167 final String vmName = getProperty(OperationProperties.ENRICHMENT_VSERVER_NAME);
168 final String vnfId = getProperty(OperationProperties.ENRICHMENT_GENERIC_VNF_ID);
170 if (StringUtils.isBlank(serviceInstance) || StringUtils.isBlank(vmId) || StringUtils.isBlank(vmName)) {
171 // original code did not check the VNF id, so we won't check it either
172 throw new IllegalArgumentException(
173 "Missing enrichment data for service instance, server id, or server name.");
176 var vmActionInfo = new VfcHealActionVmInfo();
177 vmActionInfo.setVmid(vmId);
178 vmActionInfo.setVmname(vmName);
180 var additionalParams = new VfcHealAdditionalParams();
181 additionalParams.setAction(getName());
182 additionalParams.setActionInfo(vmActionInfo);
184 var healRequest = new VfcHealRequest();
185 healRequest.setVnfInstanceId(vnfId);
186 healRequest.setCause(getName());
187 healRequest.setAdditionalParams(additionalParams);
189 var request = new VfcRequest();
190 request.setHealRequest(healRequest);
191 request.setNsInstanceId(serviceInstance);
192 request.setRequestId(params.getRequestId());