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.appc;
23 import java.util.List;
25 import java.util.Map.Entry;
26 import java.util.UUID;
27 import java.util.concurrent.CompletableFuture;
28 import org.onap.policy.appc.CommonHeader;
29 import org.onap.policy.appc.Request;
30 import org.onap.policy.appc.Response;
31 import org.onap.policy.appc.ResponseCode;
32 import org.onap.policy.common.utils.coder.CoderException;
33 import org.onap.policy.common.utils.coder.StandardCoder;
34 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
35 import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperation;
36 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
37 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
38 import org.onap.policy.controlloop.actorserviceprovider.topic.SelectorKey;
39 import org.onap.policy.controlloop.policy.PolicyResult;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * Superclass for APPC Operations.
46 public abstract class AppcOperation extends BidirectionalTopicOperation<Request, Response> {
47 private static final Logger logger = LoggerFactory.getLogger(AppcOperation.class);
48 private static final StandardCoder coder = new StandardCoder();
49 public static final String VNF_ID_KEY = "generic-vnf.vnf-id";
52 * Keys used to match the response with the request listener. The sub request ID is a
53 * UUID, so it can be used to uniquely identify the response.
55 * Note: if these change, then {@link #getExpectedKeyValues(int, Request)} must be
56 * updated accordingly.
58 public static final List<SelectorKey> SELECTOR_KEYS = List.of(new SelectorKey("CommonHeader", "SubRequestID"));
61 * Constructs the object.
63 * @param params operation parameters
64 * @param config configuration for this operation
66 public AppcOperation(ControlLoopOperationParams params, BidirectionalTopicConfig config) {
67 super(params, config, Response.class);
74 protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
75 return startGuardAsync();
79 * Makes a request, given the target VNF. This is a support function for
80 * {@link #makeRequest(int)}.
82 * @param attempt attempt number
83 * @param targetVnf target VNF
84 * @return a new request
86 protected Request makeRequest(int attempt, String targetVnf) {
87 Request request = new Request();
88 request.setCommonHeader(new CommonHeader());
89 request.getCommonHeader().setRequestId(params.getRequestId());
91 // TODO ok to use UUID, or does it have to be the "attempt"?
92 request.getCommonHeader().setSubRequestId(UUID.randomUUID().toString());
94 request.setAction(getName());
96 // convert payload strings to objects
97 if (params.getPayload() == null) {
98 logger.info("{}: no payload specified for {}", getFullName(), params.getRequestId());
100 convertPayload(params.getPayload(), request.getPayload());
103 // add/replace specific values
104 request.getPayload().put(VNF_ID_KEY, targetVnf);
110 * Converts a payload. The original value is assumed to be a JSON string, which is
111 * decoded into an object.
113 * @param source source from which to get the values
114 * @param target where to place the decoded values
116 private static void convertPayload(Map<String, Object> source, Map<String, Object> target) {
117 for (Entry<String, Object> ent : source.entrySet()) {
118 Object value = ent.getValue();
120 target.put(ent.getKey(), null);
125 target.put(ent.getKey(), coder.decode(value.toString(), Object.class));
127 } catch (CoderException e) {
128 logger.warn("cannot decode JSON value {}: {}", ent.getKey(), ent.getValue(), e);
134 * Note: these values must match {@link #SELECTOR_KEYS}.
137 protected List<String> getExpectedKeyValues(int attempt, Request request) {
138 return List.of(request.getCommonHeader().getSubRequestId());
142 protected Status detmStatus(String rawResponse, Response response) {
143 if (response.getStatus() == null) {
144 throw new IllegalArgumentException("APP-C response is missing the response status");
147 ResponseCode code = ResponseCode.toResponseCode(response.getStatus().getCode());
149 throw new IllegalArgumentException(
150 "unknown APPC-C response status code: " + response.getStatus().getCode());
155 return Status.SUCCESS;
157 return Status.FAILURE;
160 throw new IllegalArgumentException("APP-C request was not accepted, code=" + code);
163 // awaiting a "final" response
164 return Status.STILL_WAITING;
169 * Sets the message to the status description, if available.
172 public OperationOutcome setOutcome(OperationOutcome outcome, PolicyResult result, Response response) {
173 if (response.getStatus() == null || response.getStatus().getDescription() == null) {
174 return setOutcome(outcome, result);
177 outcome.setResult(result);
178 outcome.setMessage(response.getStatus().getDescription());