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.sdnr;
24 import java.util.List;
25 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
26 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
27 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
28 import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperation;
29 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
30 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
31 import org.onap.policy.controlloop.actorserviceprovider.topic.SelectorKey;
32 import org.onap.policy.sdnr.PciBody;
33 import org.onap.policy.sdnr.PciCommonHeader;
34 import org.onap.policy.sdnr.PciMessage;
35 import org.onap.policy.sdnr.PciRequest;
36 import org.onap.policy.sdnr.PciResponse;
37 import org.onap.policy.sdnr.util.StatusCodeEnum;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class SdnrOperation extends BidirectionalTopicOperation<PciMessage, PciMessage> {
42 private static final Logger logger = LoggerFactory.getLogger(SdnrOperation.class);
45 * Operation name as it should appear within config files.
47 public static final String NAME = "any";
49 private static final List<String> PROPERTY_NAMES = List.of(OperationProperties.EVENT_PAYLOAD);
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, PciMessage)} must be
56 * updated accordingly.
58 public static final List<SelectorKey> SELECTOR_KEYS =
59 List.of(new SelectorKey("body", "output", "CommonHeader", "SubRequestID"));
61 public SdnrOperation(ControlLoopOperationParams params, BidirectionalTopicConfig config) {
62 super(params, config, PciMessage.class, PROPERTY_NAMES);
66 * Note: these values must be in correspondence with {@link #SELECTOR_KEYS}.
69 protected List<String> getExpectedKeyValues(int attempt, PciMessage request) {
70 return List.of(getSubRequestId());
74 * NOTE: This should avoid throwing exceptions, so that a ControlLoopResponse can be
75 * added to the outcome. Consequently, it returns FAILURE if a required field is
76 * missing from the response.
79 protected Status detmStatus(String rawResponse, PciMessage responseWrapper) {
80 PciResponse response = responseWrapper.getBody().getOutput();
82 if (response.getStatus() == null) {
83 logger.warn("SDNR response is missing the response status");
84 return Status.FAILURE;
87 var code = StatusCodeEnum.fromStatusCode(response.getStatus().getCode());
90 logger.warn("unknown SDNR response status code: {}", response.getStatus().getCode());
91 return Status.FAILURE;
94 return switch (code) {
95 case SUCCESS, PARTIAL_SUCCESS -> Status.SUCCESS;
96 case FAILURE, PARTIAL_FAILURE -> Status.FAILURE;
97 case ERROR, REJECT -> {
98 logger.warn("SDNR request was not accepted, code={}", code);
102 // awaiting a "final" response
103 Status.STILL_WAITING;
108 * Sets the message to the status description, if available.
111 public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, PciMessage responseWrapper) {
112 outcome.setResponse(responseWrapper);
114 if (responseWrapper.getBody() == null || responseWrapper.getBody().getOutput() == null) {
115 return setOutcome(outcome, result);
118 var pciResponse = responseWrapper.getBody().getOutput();
119 if (pciResponse.getStatus() == null || pciResponse.getStatus().getValue() == null) {
120 return setOutcome(outcome, result);
123 outcome.setResult(result);
124 outcome.setMessage(pciResponse.getStatus().getValue());
129 protected PciMessage makeRequest(int attempt) {
130 String subRequestId = getSubRequestId();
132 /* Construct an SDNR request using pci Model */
134 var dmaapRequest = new PciMessage();
135 dmaapRequest.setVersion("1.0");
136 dmaapRequest.setCorrelationId(params.getRequestId() + "-" + subRequestId);
137 dmaapRequest.setType("request");
138 dmaapRequest.setRpcName(params.getOperation().toLowerCase());
140 /* This is the actual request that is placed in the dmaap wrapper. */
141 final var sdnrRequest = new PciRequest();
143 /* The common header is a required field for all SDNR requests. */
144 var requestCommonHeader = new PciCommonHeader();
145 requestCommonHeader.setRequestId(params.getRequestId());
146 requestCommonHeader.setSubRequestId(subRequestId);
148 sdnrRequest.setCommonHeader(requestCommonHeader);
149 sdnrRequest.setPayload(getRequiredProperty(OperationProperties.EVENT_PAYLOAD, "event payload"));
150 sdnrRequest.setAction(params.getOperation());
153 * Once the pci request is constructed, add it into the body of the dmaap wrapper.
155 var body = new PciBody();
156 body.setInput(sdnrRequest);
157 dmaapRequest.setBody(body);
159 /* Return the request to be sent through dmaap. */