Java 17 Upgrade
[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-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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.controlloop.actor.vfc;
23
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;
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     protected 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     @Override
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;
92             }
93
94             if ("error".equalsIgnoreCase(requestState)) {
95                 extractJobId(response);
96                 return Status.FAILURE;
97             }
98         }
99
100         // still incomplete
101
102         // need a job ID with which to query
103         if (jobId == null && !extractJobId(response)) {
104             throw new IllegalArgumentException("missing job ID in response");
105         }
106
107         return Status.STILL_WAITING;
108     }
109
110     private boolean extractJobId(VfcResponse response) {
111         if (response == null || response.getJobId() == null) {
112             return false;
113         }
114
115         jobId = response.getJobId();
116         return true;
117     }
118
119     /**
120      * Gets the request state of a response.
121      *
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
124      */
125     protected String getRequestState(VfcResponse response) {
126         if (response == null || response.getResponseDescriptor() == null
127                         || StringUtils.isBlank(response.getResponseDescriptor().getStatus())) {
128             return null;
129         }
130         return response.getResponseDescriptor().getStatus();
131     }
132
133     /**
134      * Treats everything as a success, so we always go into
135      * {@link #postProcessResponse (OperationOutcome, String, Response, SoResponse)}.
136      */
137     @Override
138     protected boolean isSuccess(Response rawResponse, VfcResponse response) {
139         return true;
140     }
141
142     /**
143      * Prepends the message with the http status code.
144      */
145     @Override
146     public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, Response rawResponse,
147                     VfcResponse response) {
148
149         // set default result and message
150         setOutcome(outcome, result);
151
152         int code = (result == OperationResult.FAILURE_TIMEOUT ? VFC_RESPONSE_CODE : rawResponse.getStatus());
153
154         outcome.setResponse(response);
155         outcome.setMessage(code + " " + outcome.getMessage());
156         return outcome;
157     }
158
159     /**
160      * Construct VfcRequestObject from the ControlLoopOperationParams.
161      *
162      * @return request
163      */
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);
169
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.");
174         }
175
176         var vmActionInfo = new VfcHealActionVmInfo();
177         vmActionInfo.setVmid(vmId);
178         vmActionInfo.setVmname(vmName);
179
180         var additionalParams = new VfcHealAdditionalParams();
181         additionalParams.setAction(getName());
182         additionalParams.setActionInfo(vmActionInfo);
183
184         var healRequest = new VfcHealRequest();
185         healRequest.setVnfInstanceId(vnfId);
186         healRequest.setCause(getName());
187         healRequest.setAdditionalParams(additionalParams);
188
189         var request = new VfcRequest();
190         request.setHealRequest(healRequest);
191         request.setNsInstanceId(serviceInstance);
192         request.setRequestId(params.getRequestId());
193
194         return request;
195     }
196 }