3e64506bf70ed2a55efc9e84bf099c963ece46f4
[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  * ================================================================================
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.actor.vfc;
22
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;
37
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";
43
44     public static final String REQ_PARAM_NM = "requestParameters";
45     public static final String CONFIG_PARAM_NM = "configurationParameters";
46
47     // @formatter:off
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);
53     // @formatter:on
54
55     /**
56      * Job ID extracted from the first response.
57      */
58     private String jobId;
59
60
61     /**
62      * Constructs the object.
63      *
64      * @param params operation parameters
65      * @param config configuration for this operation
66      */
67     protected VfcOperation(ControlLoopOperationParams params, HttpConfig config) {
68         super(params, config, VfcResponse.class, PROPERTY_NAMES);
69
70         setUsePolling();
71     }
72
73     @Override
74     protected void resetPollCount() {
75         super.resetPollCount();
76         jobId = null;
77     }
78
79     @Override
80     protected String getPollingPath() {
81         return super.getPollingPath() + jobId;
82     }
83
84     @Override
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;
91             }
92
93             if ("error".equalsIgnoreCase(requestState)) {
94                 extractJobId(response);
95                 return Status.FAILURE;
96             }
97         }
98
99         // still incomplete
100
101         // need a job ID with which to query
102         if (jobId == null && !extractJobId(response)) {
103             throw new IllegalArgumentException("missing job ID in response");
104         }
105
106         return Status.STILL_WAITING;
107     }
108
109     private boolean extractJobId(VfcResponse response) {
110         if (response == null || response.getJobId() == null) {
111             return false;
112         }
113
114         jobId = response.getJobId();
115         return true;
116     }
117
118     /**
119      * Gets the request state of a response.
120      *
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
123      */
124     protected String getRequestState(VfcResponse response) {
125         if (response == null || response.getResponseDescriptor() == null
126                         || StringUtils.isBlank(response.getResponseDescriptor().getStatus())) {
127             return null;
128         }
129         return response.getResponseDescriptor().getStatus();
130     }
131
132     /**
133      * Treats everything as a success, so we always go into
134      * {@link #postProcessResponse(OperationOutcome, String, Response, SoResponse)}.
135      */
136     @Override
137     protected boolean isSuccess(Response rawResponse, VfcResponse response) {
138         return true;
139     }
140
141     /**
142      * Prepends the message with the http status code.
143      */
144     @Override
145     public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, Response rawResponse,
146                     VfcResponse response) {
147
148         // set default result and message
149         setOutcome(outcome, result);
150
151         int code = (result == OperationResult.FAILURE_TIMEOUT ? VFC_RESPONSE_CODE : rawResponse.getStatus());
152
153         outcome.setResponse(response);
154         outcome.setMessage(code + " " + outcome.getMessage());
155         return outcome;
156     }
157
158     /**
159      * Construct VfcRequestObject from the ControlLoopOperationParams.
160      *
161      * @return request
162      */
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);
168
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.");
173         }
174
175         var vmActionInfo = new VfcHealActionVmInfo();
176         vmActionInfo.setVmid(vmId);
177         vmActionInfo.setVmname(vmName);
178
179         var additionalParams = new VfcHealAdditionalParams();
180         additionalParams.setAction(getName());
181         additionalParams.setActionInfo(vmActionInfo);
182
183         var healRequest = new VfcHealRequest();
184         healRequest.setVnfInstanceId(vnfId);
185         healRequest.setCause(getName());
186         healRequest.setAdditionalParams(additionalParams);
187
188         var request = new VfcRequest();
189         request.setHealRequest(healRequest);
190         request.setNsInstanceId(serviceInstance);
191         request.setRequestId(params.getRequestId());
192
193         return request;
194     }
195 }