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.sdnr;
23 import java.util.List;
24 import java.util.concurrent.CompletableFuture;
25 import org.onap.policy.controlloop.VirtualControlLoopEvent;
26 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
27 import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperation;
28 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
29 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
30 import org.onap.policy.controlloop.actorserviceprovider.topic.SelectorKey;
31 import org.onap.policy.controlloop.policy.PolicyResult;
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 abstract class SdnrOperation extends BidirectionalTopicOperation<PciMessage, PciMessage> {
42 private static final Logger logger = LoggerFactory.getLogger(SdnrOperation.class);
45 * Keys used to match the response with the request listener. The sub request ID is a
46 * UUID, so it can be used to uniquely identify the response.
48 * Note: if these change, then {@link #getExpectedKeyValues(int, PciMessage)} must be
49 * updated accordingly.
51 public static final List<SelectorKey> SELECTOR_KEYS =
52 List.of(new SelectorKey("body", "output", "CommonHeader", "SubRequestID"));
54 public SdnrOperation(ControlLoopOperationParams params, BidirectionalTopicConfig config) {
55 super(params, config, PciMessage.class);
59 * Note: these values must be in correspondence with {@link #SELECTOR_KEYS}.
62 protected List<String> getExpectedKeyValues(int attempt, PciMessage request) {
63 return List.of(getSubRequestId());
67 protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
68 return startGuardAsync();
72 protected Status detmStatus(String rawResponse, PciMessage responseWrapper) {
73 PciResponse response = responseWrapper.getBody().getOutput();
75 if (response.getStatus() == null) {
76 throw new IllegalArgumentException("SDNR response is missing the response status");
79 StatusCodeEnum code = StatusCodeEnum.fromStatusCode(response.getStatus().getCode());
82 throw new IllegalArgumentException("unknown SDNR response status code: " + response.getStatus().getCode());
86 * Response and Payload are just printed and no further action needed since
89 logger.info("SDNR Response Code {} Message is {}", code, response.getStatus().getValue());
90 logger.info("SDNR Response Payload is {}", response.getPayload());
95 return Status.SUCCESS;
98 return Status.FAILURE;
101 throw new IllegalArgumentException("SDNR request was not accepted, code=" + code);
104 // awaiting a "final" response
105 return Status.STILL_WAITING;
110 * Sets the message to the status description, if available.
113 public OperationOutcome setOutcome(OperationOutcome outcome, PolicyResult result, PciMessage responseWrapper) {
114 if (responseWrapper.getBody() == null || responseWrapper.getBody().getOutput() == null) {
115 return setOutcome(outcome, result);
118 PciResponse response = responseWrapper.getBody().getOutput();
119 if (response.getStatus() == null || response.getStatus().getValue() == null) {
120 return setOutcome(outcome, result);
123 outcome.setResult(result);
124 outcome.setMessage(response.getStatus().getValue());
129 protected PciMessage makeRequest(int attempt) {
130 VirtualControlLoopEvent onset = params.getContext().getEvent();
131 String subRequestId = getSubRequestId();
133 /* Construct an SDNR request using pci Model */
135 PciMessage dmaapRequest = new PciMessage();
136 dmaapRequest.setVersion("1.0");
137 dmaapRequest.setCorrelationId(onset.getRequestId() + "-" + subRequestId);
138 dmaapRequest.setType("request");
140 /* This is the actual request that is placed in the dmaap wrapper. */
141 final PciRequest sdnrRequest = new PciRequest();
143 /* The common header is a required field for all SDNR requests. */
144 PciCommonHeader requestCommonHeader = new PciCommonHeader();
145 requestCommonHeader.setRequestId(onset.getRequestId());
146 requestCommonHeader.setSubRequestId(subRequestId);
148 sdnrRequest.setCommonHeader(requestCommonHeader);
149 sdnrRequest.setPayload(onset.getPayload());
152 * Once the pci request is constructed, add it into the body of the dmaap wrapper.
154 PciBody body = new PciBody();
155 body.setInput(sdnrRequest);
156 dmaapRequest.setBody(body);
157 logger.info("SDNR Request to be sent is {}", dmaapRequest);
159 /* Return the request to be sent through dmaap. */