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.Collections;
24 import java.util.List;
26 import java.util.concurrent.CompletableFuture;
27 import org.apache.commons.lang3.StringUtils;
28 import org.onap.policy.appclcm.AppcLcmBody;
29 import org.onap.policy.appclcm.AppcLcmCommonHeader;
30 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
31 import org.onap.policy.appclcm.AppcLcmInput;
32 import org.onap.policy.appclcm.AppcLcmOutput;
33 import org.onap.policy.appclcm.AppcLcmResponseCode;
34 import org.onap.policy.appclcm.AppcLcmResponseStatus;
35 import org.onap.policy.common.utils.coder.CoderException;
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, Collections.emptyList());
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 String subRequestId = getSubRequestId();
85 AppcLcmCommonHeader header = new AppcLcmCommonHeader();
86 header.setOriginatorId(params.getRequestId().toString());
87 header.setRequestId(params.getRequestId());
88 header.setSubRequestId(subRequestId);
90 AppcLcmInput inputRequest = new AppcLcmInput();
91 inputRequest.setCommonHeader(header);
93 AppcLcmRecipeFormatter recipeFormatter = new AppcLcmRecipeFormatter(getName());
94 inputRequest.setAction(recipeFormatter.getBodyRecipe());
97 * Action Identifiers are required for APPC LCM requests. For R1, the recipes
98 * supported by Policy only require a vnf-id.
100 inputRequest.setActionIdentifiers(Map.of(VNF_ID_KEY, params.getTargetEntity()));
103 * For R1, the payloads will not be required for the Restart, Rebuild, or Migrate
104 * recipes. APPC will populate the payload based on A&AI look up of the vnd-id
105 * provided in the action identifiers. The payload is set when converPayload() is
108 if (operationSupportsPayload()) {
109 convertPayload(params.getPayload(), inputRequest);
111 inputRequest.setPayload(null);
114 AppcLcmBody body = new AppcLcmBody();
115 body.setInput(inputRequest);
117 AppcLcmDmaapWrapper dmaapRequest = new AppcLcmDmaapWrapper();
118 dmaapRequest.setBody(body);
119 dmaapRequest.setVersion("2.0");
120 dmaapRequest.setCorrelationId(params.getRequestId() + "-" + subRequestId);
121 dmaapRequest.setRpcName(recipeFormatter.getUrlRecipe());
122 dmaapRequest.setType("request");
124 body.setInput(inputRequest);
125 dmaapRequest.setBody(body);
130 * Converts a payload. The original value is assumed to be a JSON string, which is
131 * decoded into an object.
133 * @param source source from which to get the values
134 * @param map where to place the decoded values
136 private void convertPayload(Map<String, Object> source, AppcLcmInput request) {
138 String encodedPayloadString = getCoder().encode(source);
139 request.setPayload(encodedPayloadString);
140 } catch (CoderException e) {
141 throw new IllegalArgumentException("Cannot convert payload", e);
146 * Note: these values must match {@link #SELECTOR_KEYS}.
149 protected List<String> getExpectedKeyValues(int attempt, AppcLcmDmaapWrapper request) {
150 return List.of(getSubRequestId());
154 protected Status detmStatus(String rawResponse, AppcLcmDmaapWrapper response) {
155 AppcLcmResponseStatus status = getStatus(response);
156 if (status == null) {
157 throw new IllegalArgumentException(MISSING_STATUS);
160 String code = AppcLcmResponseCode.toResponseValue(status.getCode());
162 throw new IllegalArgumentException("unknown APPC-LCM response status code: " + status.getCode());
166 case AppcLcmResponseCode.SUCCESS:
167 return Status.SUCCESS;
168 case AppcLcmResponseCode.FAILURE:
169 return Status.FAILURE;
170 case AppcLcmResponseCode.ERROR:
171 case AppcLcmResponseCode.REJECT:
172 throw new IllegalArgumentException("APPC-LCM request was not accepted, code=" + code);
173 case AppcLcmResponseCode.ACCEPTED:
175 return Status.STILL_WAITING;
180 * Sets the message to the status description, if available.
183 public OperationOutcome setOutcome(OperationOutcome outcome, PolicyResult result, AppcLcmDmaapWrapper response) {
184 outcome.setResponse(response);
186 AppcLcmResponseStatus status = getStatus(response);
187 if (status == null) {
188 return setOutcome(outcome, result);
191 String message = status.getMessage();
192 if (message == null) {
193 return setOutcome(outcome, result);
196 outcome.setResult(result);
197 outcome.setMessage(message);
202 * Gets the status from the response.
204 * @param response the response from which to extract the status, or {@code null}
205 * @return the status, or {@code null} if it does not exist
207 protected AppcLcmResponseStatus getStatus(AppcLcmDmaapWrapper response) {
208 if (response == null) {
212 AppcLcmBody body = response.getBody();
217 AppcLcmOutput output = body.getOutput();
218 if (output == null) {
222 return output.getStatus();
226 * Determines if the operation supports a payload.
228 * @return {@code true} if the operation supports a payload, {@code false} otherwise
230 protected boolean operationSupportsPayload() {
231 return params.getPayload() != null && !params.getPayload().isEmpty()
232 && AppcLcmConstants.SUPPORTS_PAYLOAD.contains(params.getOperation().toLowerCase());