8bd4630d682906404f75e3c68c5c49c46f5233ed
[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 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 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;
39
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";
45
46     public static final String REQ_PARAM_NM = "requestParameters";
47     public static final String CONFIG_PARAM_NM = "configurationParameters";
48
49     // @formatter:off
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);
55     // @formatter:on
56
57     /**
58      * Job ID extracted from the first response.
59      */
60     private String jobId;
61
62
63     /**
64      * Constructs the object.
65      *
66      * @param params operation parameters
67      * @param config configuration for this operation
68      */
69     public VfcOperation(ControlLoopOperationParams params, HttpConfig config) {
70         super(params, config, VfcResponse.class, PROPERTY_NAMES);
71
72         setUsePolling();
73     }
74
75     @Override
76     protected void resetPollCount() {
77         super.resetPollCount();
78         jobId = null;
79     }
80
81     @Override
82     protected String getPollingPath() {
83         return super.getPollingPath() + jobId;
84     }
85
86     /**
87      * Starts the GUARD.
88      */
89     @Override
90     protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
91         return startGuardAsync();
92     }
93
94     @Override
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;
101             }
102
103             if ("error".equalsIgnoreCase(requestState)) {
104                 extractJobId(response);
105                 return Status.FAILURE;
106             }
107         }
108
109         // still incomplete
110
111         // need a job ID with which to query
112         if (jobId == null && !extractJobId(response)) {
113             throw new IllegalArgumentException("missing job ID in response");
114         }
115
116         return Status.STILL_WAITING;
117     }
118
119     private boolean extractJobId(VfcResponse response) {
120         if (response == null || response.getJobId() == null) {
121             return false;
122         }
123
124         jobId = response.getJobId();
125         return true;
126     }
127
128     /**
129      * Gets the request state of a response.
130      *
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
133      */
134     protected String getRequestState(VfcResponse response) {
135         if (response == null || response.getResponseDescriptor() == null
136                 || StringUtils.isBlank(response.getResponseDescriptor().getStatus())) {
137             return null;
138         }
139         return response.getResponseDescriptor().getStatus();
140     }
141
142     /**
143      * Treats everything as a success, so we always go into
144      * {@link #postProcessResponse(OperationOutcome, String, Response, SoResponse)}.
145      */
146     @Override
147     protected boolean isSuccess(Response rawResponse, VfcResponse response) {
148         return true;
149     }
150
151     /**
152      * Prepends the message with the http status code.
153      */
154     @Override
155     public OperationOutcome setOutcome(OperationOutcome outcome, PolicyResult result, Response rawResponse,
156             VfcResponse response) {
157
158         // set default result and message
159         setOutcome(outcome, result);
160
161         int code = (result == PolicyResult.FAILURE_TIMEOUT ? VFC_RESPONSE_CODE : rawResponse.getStatus());
162
163         outcome.setResponse(response);
164         outcome.setMessage(code + " " + outcome.getMessage());
165         return outcome;
166     }
167
168     /**
169      * Construct VfcRequestObject from the ControlLoopOperationParams.
170      *
171      * @return request
172      */
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");
178
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.");
182         }
183
184         VfcHealActionVmInfo vmActionInfo = new VfcHealActionVmInfo();
185         vmActionInfo.setVmid(vmId);
186         vmActionInfo.setVmname(vmName);
187
188         VfcHealAdditionalParams additionalParams = new VfcHealAdditionalParams();
189         additionalParams.setAction(getName());
190         additionalParams.setActionInfo(vmActionInfo);
191
192         VfcHealRequest healRequest = new VfcHealRequest();
193         healRequest.setVnfInstanceId(params.getContext().getEnrichment().get(GENERIC_VNF_ID));
194         healRequest.setCause(getName());
195         healRequest.setAdditionalParams(additionalParams);
196
197         VfcRequest request = new VfcRequest();
198         request.setHealRequest(healRequest);
199         request.setNsInstanceId(serviceInstance);
200         request.setRequestId(params.getRequestId());
201
202         return request;
203     }
204 }