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.UUID;
25 import java.util.concurrent.CompletableFuture;
26 import org.apache.commons.lang3.tuple.Pair;
27 import org.onap.policy.controlloop.VirtualControlLoopEvent;
28 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
29 import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperation;
30 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
31 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
32 import org.onap.policy.controlloop.actorserviceprovider.topic.SelectorKey;
33 import org.onap.policy.controlloop.policy.PolicyResult;
34 import org.onap.policy.sdnr.PciCommonHeader;
35 import org.onap.policy.sdnr.PciRequest;
36 import org.onap.policy.sdnr.PciRequestWrapper;
37 import org.onap.policy.sdnr.PciResponse;
38 import org.onap.policy.sdnr.PciResponseWrapper;
39 import org.onap.policy.sdnr.util.StatusCodeEnum;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public abstract class SdnrOperation extends BidirectionalTopicOperation<PciRequestWrapper, PciResponseWrapper> {
44 private static final Logger logger = LoggerFactory.getLogger(SdnrOperation.class);
47 * Keys used to match the response with the request listener. The sub request ID is a
48 * UUID, so it can be used to uniquely identify the response.
50 * Note: if these change, then {@link #getExpectedKeyValues(int, Request)} must be
51 * updated accordingly.
53 public static final List<SelectorKey> SELECTOR_KEYS = List.of(new SelectorKey("CommonHeader", "SubRequestID"));
55 public SdnrOperation(ControlLoopOperationParams params, BidirectionalTopicConfig config) {
56 super(params, config, PciResponseWrapper.class);
60 * Note: these values must match {@link #SELECTOR_KEYS}.
63 protected List<String> getExpectedKeyValues(int attempt, PciRequestWrapper request) {
64 return List.of(request.getBody().getCommonHeader().getSubRequestId());
68 protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
69 return startGuardAsync();
73 protected Status detmStatus(String rawResponse, PciResponseWrapper responseWrapper) {
74 PciResponse response = responseWrapper.getBody();
76 if (response == null || response.getStatus() == null) {
77 throw new IllegalArgumentException("SDNR response is missing the response status");
80 StatusCodeEnum code = StatusCodeEnum.fromStatusCode(response.getStatus().getCode());
83 throw new IllegalArgumentException(
84 "unknown SDNR response status code: " + response.getStatus().getCode());
88 * Response and Payload are just printed and no further action needed since
91 logger.info("SDNR Response Code {} Message is {}", code, response.getStatus().getValue());
92 logger.info("SDNR Response Payload is {}", response.getPayload());
97 return Status.SUCCESS;
100 return Status.FAILURE;
103 throw new IllegalArgumentException("SDNR request was not accepted, code=" + code);
106 // awaiting a "final" response
107 return Status.STILL_WAITING;
112 * Sets the message to the status description, if available.
115 public OperationOutcome setOutcome(OperationOutcome outcome, PolicyResult result,
116 PciResponseWrapper responseWrapper) {
117 PciResponse response = responseWrapper.getBody();
118 if (response.getStatus() == null || response.getStatus().getValue() == null) {
119 return setOutcome(outcome, result);
122 outcome.setResult(result);
123 outcome.setMessage(response.getStatus().getValue());
128 protected Pair<String, PciRequestWrapper> makeRequest(int attempt) {
129 VirtualControlLoopEvent onset = params.getContext().getEvent();
130 String subRequestId = UUID.randomUUID().toString();
132 /* Construct an SDNR request using pci Model */
135 * The actual pci request is placed in a wrapper used to send through dmaap. The
136 * current version is 2.0 as of R1.
138 PciRequestWrapper dmaapRequest = new PciRequestWrapper();
139 dmaapRequest.setVersion("1.0");
140 dmaapRequest.setCorrelationId(onset.getRequestId() + "-" + subRequestId);
141 dmaapRequest.setType("request");
143 /* This is the actual request that is placed in the dmaap wrapper. */
144 final PciRequest sdnrRequest = new PciRequest();
146 /* The common header is a required field for all SDNR requests. */
147 PciCommonHeader requestCommonHeader = new PciCommonHeader();
148 requestCommonHeader.setRequestId(onset.getRequestId());
149 requestCommonHeader.setSubRequestId(subRequestId);
151 sdnrRequest.setCommonHeader(requestCommonHeader);
152 sdnrRequest.setPayload(onset.getPayload());
155 * Once the pci request is constructed, add it into the body of the dmaap
158 dmaapRequest.setBody(sdnrRequest);
159 logger.info("SDNR Request to be sent is {}", dmaapRequest);
161 /* Return the request to be sent through dmaap. */
162 return Pair.of(subRequestId, dmaapRequest);