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.controlloop.ControlLoopEventContext;
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.controlloop.policy.PolicyResult;
34 import org.onap.policy.vfc.VfcHealActionVmInfo;
35 import org.onap.policy.vfc.VfcHealAdditionalParams;
36 import org.onap.policy.vfc.VfcHealRequest;
37 import org.onap.policy.vfc.VfcRequest;
38 import org.onap.policy.vfc.VfcResponse;
40 public abstract class VfcOperation extends HttpOperation<VfcResponse> {
41 public static final String FAILED = "FAILED";
42 public static final String COMPLETE = "COMPLETE";
43 public static final int VFC_RESPONSE_CODE = 999;
44 public static final String GENERIC_VNF_ID = "generic-vnf.vnf-id";
46 public static final String REQ_PARAM_NM = "requestParameters";
47 public static final String CONFIG_PARAM_NM = "configurationParameters";
50 private static final List<String> PROPERTY_NAMES = List.of(
51 OperationProperties.ENRICHMENT_SERVICE_INSTANCE_ID,
52 OperationProperties.ENRICHMENT_VSERVER_ID,
53 OperationProperties.ENRICHMENT_VSERVER_NAME,
54 OperationProperties.ENRICHMENT_GENERIC_VNF_ID);
58 * Job ID extracted from the first response.
64 * Constructs the object.
66 * @param params operation parameters
67 * @param config configuration for this operation
69 public VfcOperation(ControlLoopOperationParams params, HttpConfig config) {
70 super(params, config, VfcResponse.class, PROPERTY_NAMES);
76 protected void resetPollCount() {
77 super.resetPollCount();
82 protected String getPollingPath() {
83 return super.getPollingPath() + jobId;
90 protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
91 return startGuardAsync();
95 protected Status detmStatus(Response rawResponse, VfcResponse response) {
96 if (rawResponse.getStatus() == 200) {
97 String requestState = getRequestState(response);
98 if ("finished".equalsIgnoreCase(requestState)) {
99 extractJobId(response);
100 return Status.SUCCESS;
103 if ("error".equalsIgnoreCase(requestState)) {
104 extractJobId(response);
105 return Status.FAILURE;
111 // need a job ID with which to query
112 if (jobId == null && !extractJobId(response)) {
113 throw new IllegalArgumentException("missing job ID in response");
116 return Status.STILL_WAITING;
119 private boolean extractJobId(VfcResponse response) {
120 if (response == null || response.getJobId() == null) {
124 jobId = response.getJobId();
129 * Gets the request state of a response.
131 * @param response response from which to get the state
132 * @return the request state of the response, or {@code null} if it does not exist
134 protected String getRequestState(VfcResponse response) {
135 if (response == null || response.getResponseDescriptor() == null
136 || StringUtils.isBlank(response.getResponseDescriptor().getStatus())) {
139 return response.getResponseDescriptor().getStatus();
143 * Treats everything as a success, so we always go into
144 * {@link #postProcessResponse(OperationOutcome, String, Response, SoResponse)}.
147 protected boolean isSuccess(Response rawResponse, VfcResponse response) {
152 * Prepends the message with the http status code.
155 public OperationOutcome setOutcome(OperationOutcome outcome, PolicyResult result, Response rawResponse,
156 VfcResponse response) {
158 // set default result and message
159 setOutcome(outcome, result);
161 int code = (result == PolicyResult.FAILURE_TIMEOUT ? VFC_RESPONSE_CODE : rawResponse.getStatus());
163 outcome.setResponse(response);
164 outcome.setMessage(code + " " + outcome.getMessage());
169 * Construct VfcRequestObject from the ControlLoopOperationParams.
173 protected VfcRequest constructVfcRequest() {
174 ControlLoopEventContext context = params.getContext();
175 String serviceInstance = context.getEnrichment().get("service-instance.service-instance-id");
176 String vmId = context.getEnrichment().get("vserver.vserver-id");
177 String vmName = context.getEnrichment().get("vserver.vserver-name");
179 if (StringUtils.isBlank(serviceInstance) || StringUtils.isBlank(vmId) || StringUtils.isBlank(vmName)) {
180 throw new IllegalArgumentException(
181 "Cannot extract enrichment data for service instance, server id, or server name.");
184 VfcHealActionVmInfo vmActionInfo = new VfcHealActionVmInfo();
185 vmActionInfo.setVmid(vmId);
186 vmActionInfo.setVmname(vmName);
188 VfcHealAdditionalParams additionalParams = new VfcHealAdditionalParams();
189 additionalParams.setAction(getName());
190 additionalParams.setActionInfo(vmActionInfo);
192 VfcHealRequest healRequest = new VfcHealRequest();
193 healRequest.setVnfInstanceId(params.getContext().getEnrichment().get(GENERIC_VNF_ID));
194 healRequest.setCause(getName());
195 healRequest.setAdditionalParams(additionalParams);
197 VfcRequest request = new VfcRequest();
198 request.setHealRequest(healRequest);
199 request.setNsInstanceId(serviceInstance);
200 request.setRequestId(params.getRequestId());