2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2023 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controlloop.actor.appc;
24 import java.util.List;
26 import java.util.Map.Entry;
27 import org.onap.aai.domain.yang.GenericVnf;
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.Coder;
33 import org.onap.policy.common.utils.coder.CoderException;
34 import org.onap.policy.common.utils.coder.StandardCoder;
35 import org.onap.policy.common.utils.coder.StandardCoderInstantAsMillis;
36 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
37 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
38 import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperation;
39 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
40 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
41 import org.onap.policy.controlloop.actorserviceprovider.topic.SelectorKey;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * Superclass for APPC Operations.
48 public abstract class AppcOperation extends BidirectionalTopicOperation<Request, Response> {
49 private static final Logger logger = LoggerFactory.getLogger(AppcOperation.class);
50 private static final StandardCoder coder = new StandardCoderInstantAsMillis();
51 public static final String VNF_ID_KEY = "generic-vnf.vnf-id";
54 * Keys used to match the response with the request listener. The sub request ID is a
55 * UUID, so it can be used to uniquely identify the response.
57 * Note: if these change, then {@link #getExpectedKeyValues(int, Request)} must be
58 * updated accordingly.
60 public static final List<SelectorKey> SELECTOR_KEYS = List.of(new SelectorKey("CommonHeader", "SubRequestID"));
63 * Constructs the object.
65 * @param params operation parameters
66 * @param config configuration for this operation
67 * @param propertyNames names of properties required by this operation
69 protected AppcOperation(ControlLoopOperationParams params, BidirectionalTopicConfig config,
70 List<String> propertyNames) {
71 super(params, config, Response.class, propertyNames);
75 * Makes a request, given the target VNF. This is a support function for
76 * {@link #makeRequest(int)}.
78 * @param targetVnf target VNF
79 * @return a new request
81 protected Request makeRequest(GenericVnf targetVnf) {
82 var request = new Request();
83 request.setCommonHeader(new CommonHeader());
84 request.getCommonHeader().setRequestId(params.getRequestId());
85 request.getCommonHeader().setSubRequestId(getSubRequestId());
87 request.setAction(getName());
89 // convert payload strings to objects
90 if (params.getPayload() == null) {
91 logger.info("{}: no payload specified for {}", getFullName(), params.getRequestId());
93 convertPayload(params.getPayload(), request.getPayload());
96 // add/replace specific values
97 request.getPayload().put(VNF_ID_KEY, targetVnf.getVnfId());
103 * Converts a payload. The original value is assumed to be a JSON string, which is
104 * decoded into an object.
106 * @param source source from which to get the values
107 * @param target where to place the decoded values
109 private static void convertPayload(Map<String, Object> source, Map<String, Object> target) {
110 for (Entry<String, Object> ent : source.entrySet()) {
111 Object value = ent.getValue();
113 target.put(ent.getKey(), null);
118 target.put(ent.getKey(), coder.decode(value.toString(), Object.class));
120 } catch (CoderException e) {
121 logger.warn("cannot decode JSON value {}: {}", ent.getKey(), ent.getValue(), e);
127 * Note: these values must match {@link #SELECTOR_KEYS}.
130 protected List<String> getExpectedKeyValues(int attempt, Request request) {
131 return List.of(getSubRequestId());
135 protected Status detmStatus(String rawResponse, Response response) {
136 if (response.getStatus() == null) {
137 // no status - this must be a request, not a response - just ignore it
138 logger.info("{}: ignoring request message for {}", getFullName(), params.getRequestId());
139 return Status.STILL_WAITING;
142 var code = ResponseCode.toResponseCode(response.getStatus().getCode());
144 throw new IllegalArgumentException(
145 "unknown APPC-C response status code: " + response.getStatus().getCode());
148 return switch (code) {
149 case SUCCESS -> Status.SUCCESS;
150 case FAILURE -> Status.FAILURE;
151 case ERROR, REJECT -> throw new IllegalArgumentException("APP-C request was not accepted, code=" + code);
152 // awaiting a "final" response
153 default -> Status.STILL_WAITING;
158 * Sets the message to the status description, if available.
161 public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, Response response) {
162 outcome.setResponse(response);
164 if (response.getStatus() == null || response.getStatus().getDescription() == null) {
165 return setOutcome(outcome, result);
168 outcome.setResult(result);
169 outcome.setMessage(response.getStatus().getDescription());
174 protected Coder getCoder() {