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.appclcm;
23 import java.util.List;
25 import java.util.concurrent.CompletableFuture;
26 import org.apache.commons.lang3.StringUtils;
27 import org.onap.policy.appclcm.AppcLcmBody;
28 import org.onap.policy.appclcm.AppcLcmCommonHeader;
29 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
30 import org.onap.policy.appclcm.AppcLcmInput;
31 import org.onap.policy.appclcm.AppcLcmOutput;
32 import org.onap.policy.appclcm.AppcLcmResponseCode;
33 import org.onap.policy.appclcm.AppcLcmResponseStatus;
34 import org.onap.policy.common.utils.coder.CoderException;
35 import org.onap.policy.controlloop.VirtualControlLoopEvent;
36 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
37 import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperation;
38 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
39 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
40 import org.onap.policy.controlloop.actorserviceprovider.topic.SelectorKey;
41 import org.onap.policy.controlloop.policy.PolicyResult;
43 public class AppcLcmOperation extends BidirectionalTopicOperation<AppcLcmDmaapWrapper, AppcLcmDmaapWrapper> {
45 private static final String MISSING_STATUS = "APPC-LCM response is missing the response status";
46 public static final String VNF_ID_KEY = "vnf-id";
49 * Keys used to match the response with the request listener. The sub request ID is a
50 * UUID, so it can be used to uniquely identify the response.
52 * Note: if these change, then {@link #getExpectedKeyValues(int, AppcLcmDmaapWrapper)}
53 * must be updated accordingly.
55 public static final List<SelectorKey> SELECTOR_KEYS =
56 List.of(new SelectorKey("body", "output", "common-header", "sub-request-id"));
59 * Constructs the object.
61 * @param params operation parameters
62 * @param config configuration for this operation
64 public AppcLcmOperation(ControlLoopOperationParams params, BidirectionalTopicConfig config) {
65 super(params, config, AppcLcmDmaapWrapper.class);
67 if (StringUtils.isBlank(params.getTargetEntity())) {
68 throw new IllegalArgumentException("missing targetEntity");
73 * Ensures that A&AI customer query has been performed, and then runs the guard query.
74 * Starts the GUARD using startGuardAsync.
77 protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
78 return startGuardAsync();
82 protected AppcLcmDmaapWrapper makeRequest(int attempt) {
83 VirtualControlLoopEvent onset = params.getContext().getEvent();
84 String subRequestId = getSubRequestId();
86 AppcLcmCommonHeader header = new AppcLcmCommonHeader();
87 header.setOriginatorId(onset.getRequestId().toString());
88 header.setRequestId(onset.getRequestId());
89 header.setSubRequestId(subRequestId);
91 AppcLcmInput inputRequest = new AppcLcmInput();
92 inputRequest.setCommonHeader(header);
94 AppcLcmRecipeFormatter recipeFormatter = new AppcLcmRecipeFormatter(getName());
95 inputRequest.setAction(recipeFormatter.getBodyRecipe());
98 * Action Identifiers are required for APPC LCM requests. For R1, the recipes
99 * supported by Policy only require a vnf-id.
101 inputRequest.setActionIdentifiers(Map.of(VNF_ID_KEY, params.getTargetEntity()));
104 * For R1, the payloads will not be required for the Restart, Rebuild, or Migrate
105 * recipes. APPC will populate the payload based on A&AI look up of the vnd-id
106 * provided in the action identifiers. The payload is set when converPayload() is
109 if (operationSupportsPayload()) {
110 convertPayload(params.getPayload(), inputRequest);
112 inputRequest.setPayload(null);
115 AppcLcmBody body = new AppcLcmBody();
116 body.setInput(inputRequest);
118 AppcLcmDmaapWrapper dmaapRequest = new AppcLcmDmaapWrapper();
119 dmaapRequest.setBody(body);
120 dmaapRequest.setVersion("2.0");
121 dmaapRequest.setCorrelationId(onset.getRequestId() + "-" + subRequestId);
122 dmaapRequest.setRpcName(recipeFormatter.getUrlRecipe());
123 dmaapRequest.setType("request");
125 body.setInput(inputRequest);
126 dmaapRequest.setBody(body);
131 * Converts a payload. The original value is assumed to be a JSON string, which is
132 * decoded into an object.
134 * @param source source from which to get the values
135 * @param map where to place the decoded values
137 private void convertPayload(Map<String, Object> source, AppcLcmInput request) {
139 String encodedPayloadString = makeCoder().encode(source);
140 request.setPayload(encodedPayloadString);
141 } catch (CoderException e) {
142 throw new IllegalArgumentException("Cannot convert payload", e);
147 * Note: these values must match {@link #SELECTOR_KEYS}.
150 protected List<String> getExpectedKeyValues(int attempt, AppcLcmDmaapWrapper request) {
151 return List.of(getSubRequestId());
155 protected Status detmStatus(String rawResponse, AppcLcmDmaapWrapper response) {
156 AppcLcmResponseStatus status = getStatus(response);
157 if (status == null) {
158 throw new IllegalArgumentException(MISSING_STATUS);
161 String code = AppcLcmResponseCode.toResponseValue(status.getCode());
163 throw new IllegalArgumentException("unknown APPC-LCM response status code: " + status.getCode());
167 case AppcLcmResponseCode.SUCCESS:
168 return Status.SUCCESS;
169 case AppcLcmResponseCode.FAILURE:
170 return Status.FAILURE;
171 case AppcLcmResponseCode.ERROR:
172 case AppcLcmResponseCode.REJECT:
173 throw new IllegalArgumentException("APPC-LCM request was not accepted, code=" + code);
174 case AppcLcmResponseCode.ACCEPTED:
176 return Status.STILL_WAITING;
181 * Sets the message to the status description, if available.
184 public OperationOutcome setOutcome(OperationOutcome outcome, PolicyResult result, AppcLcmDmaapWrapper response) {
185 outcome.setResponse(response);
187 AppcLcmResponseStatus status = getStatus(response);
188 if (status == null) {
189 return setOutcome(outcome, result);
192 String message = status.getMessage();
193 if (message == null) {
194 return setOutcome(outcome, result);
197 outcome.setResult(result);
198 outcome.setMessage(message);
203 * Gets the status from the response.
205 * @param response the response from which to extract the status, or {@code null}
206 * @return the status, or {@code null} if it does not exist
208 protected AppcLcmResponseStatus getStatus(AppcLcmDmaapWrapper response) {
209 if (response == null) {
213 AppcLcmBody body = response.getBody();
218 AppcLcmOutput output = body.getOutput();
219 if (output == null) {
223 return output.getStatus();
227 * Determines if the operation supports a payload.
229 * @return {@code true} if the operation supports a payload, {@code false} otherwise
231 protected boolean operationSupportsPayload() {
232 return params.getPayload() != null && !params.getPayload().isEmpty()
233 && AppcLcmConstants.SUPPORTS_PAYLOAD.contains(params.getOperation().toLowerCase());