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 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.OperationProperties;
28 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
29 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation;
30 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
31 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
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;
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";
44 public static final String REQ_PARAM_NM = "requestParameters";
45 public static final String CONFIG_PARAM_NM = "configurationParameters";
48 private static final List<String> PROPERTY_NAMES = List.of(
49 OperationProperties.ENRICHMENT_SERVICE_ID,
50 OperationProperties.ENRICHMENT_VSERVER_ID,
51 OperationProperties.ENRICHMENT_VSERVER_NAME,
52 OperationProperties.ENRICHMENT_GENERIC_VNF_ID);
56 * Job ID extracted from the first response.
62 * Constructs the object.
64 * @param params operation parameters
65 * @param config configuration for this operation
67 public VfcOperation(ControlLoopOperationParams params, HttpConfig config) {
68 super(params, config, VfcResponse.class, PROPERTY_NAMES);
74 protected void resetPollCount() {
75 super.resetPollCount();
80 protected String getPollingPath() {
81 return super.getPollingPath() + jobId;
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;
93 if ("error".equalsIgnoreCase(requestState)) {
94 extractJobId(response);
95 return Status.FAILURE;
101 // need a job ID with which to query
102 if (jobId == null && !extractJobId(response)) {
103 throw new IllegalArgumentException("missing job ID in response");
106 return Status.STILL_WAITING;
109 private boolean extractJobId(VfcResponse response) {
110 if (response == null || response.getJobId() == null) {
114 jobId = response.getJobId();
119 * Gets the request state of a response.
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
124 protected String getRequestState(VfcResponse response) {
125 if (response == null || response.getResponseDescriptor() == null
126 || StringUtils.isBlank(response.getResponseDescriptor().getStatus())) {
129 return response.getResponseDescriptor().getStatus();
133 * Treats everything as a success, so we always go into
134 * {@link #postProcessResponse(OperationOutcome, String, Response, SoResponse)}.
137 protected boolean isSuccess(Response rawResponse, VfcResponse response) {
142 * Prepends the message with the http status code.
145 public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, Response rawResponse,
146 VfcResponse response) {
148 // set default result and message
149 setOutcome(outcome, result);
151 int code = (result == OperationResult.FAILURE_TIMEOUT ? VFC_RESPONSE_CODE : rawResponse.getStatus());
153 outcome.setResponse(response);
154 outcome.setMessage(code + " " + outcome.getMessage());
159 * Construct VfcRequestObject from the ControlLoopOperationParams.
163 protected VfcRequest constructVfcRequest() {
164 final String serviceInstance = getProperty(OperationProperties.ENRICHMENT_SERVICE_ID);
165 final String vmId = getProperty(OperationProperties.ENRICHMENT_VSERVER_ID);
166 final String vmName = getProperty(OperationProperties.ENRICHMENT_VSERVER_NAME);
167 final String vnfId = getProperty(OperationProperties.ENRICHMENT_GENERIC_VNF_ID);
169 if (StringUtils.isBlank(serviceInstance) || StringUtils.isBlank(vmId) || StringUtils.isBlank(vmName)) {
170 // original code did not check the VNF id, so we won't check it either
171 throw new IllegalArgumentException(
172 "Missing enrichment data for service instance, server id, or server name.");
175 VfcHealActionVmInfo vmActionInfo = new VfcHealActionVmInfo();
176 vmActionInfo.setVmid(vmId);
177 vmActionInfo.setVmname(vmName);
179 VfcHealAdditionalParams additionalParams = new VfcHealAdditionalParams();
180 additionalParams.setAction(getName());
181 additionalParams.setActionInfo(vmActionInfo);
183 VfcHealRequest healRequest = new VfcHealRequest();
184 healRequest.setVnfInstanceId(vnfId);
185 healRequest.setCause(getName());
186 healRequest.setAdditionalParams(additionalParams);
188 VfcRequest request = new VfcRequest();
189 request.setHealRequest(healRequest);
190 request.setNsInstanceId(serviceInstance);
191 request.setRequestId(params.getRequestId());