Merge "Copy Dbao from drools-apps to models"
[policy/models.git] / models-interactions / model-actors / actor.sdnr / src / main / java / org / onap / policy / controlloop / actor / sdnr / SdnrOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SdnrOperation
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.actor.sdnr;
22
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;
40
41 public class SdnrOperation extends BidirectionalTopicOperation<PciMessage, PciMessage> {
42     private static final Logger logger = LoggerFactory.getLogger(SdnrOperation.class);
43
44     /**
45      * Operation name as it should appear within config files.
46      */
47     public static final String NAME = "any";
48
49     /**
50      * Keys used to match the response with the request listener. The sub request ID is a
51      * UUID, so it can be used to uniquely identify the response.
52      * <p/>
53      * Note: if these change, then {@link #getExpectedKeyValues(int, PciMessage)} must be
54      * updated accordingly.
55      */
56     public static final List<SelectorKey> SELECTOR_KEYS =
57                     List.of(new SelectorKey("body", "output", "CommonHeader", "SubRequestID"));
58
59     public SdnrOperation(ControlLoopOperationParams params, BidirectionalTopicConfig config) {
60         super(params, config, PciMessage.class);
61     }
62
63     /**
64      * Note: these values must be in correspondence with {@link #SELECTOR_KEYS}.
65      */
66     @Override
67     protected List<String> getExpectedKeyValues(int attempt, PciMessage request) {
68         return List.of(getSubRequestId());
69     }
70
71     @Override
72     protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
73         return startGuardAsync();
74     }
75
76     /*
77      * NOTE: This should avoid throwing exceptions, so that a ControlLoopResponse can be
78      * added to the outcome. Consequently, it returns FAILURE if a required field is
79      * missing from the response.
80      */
81     @Override
82     protected Status detmStatus(String rawResponse, PciMessage responseWrapper) {
83         PciResponse response = responseWrapper.getBody().getOutput();
84
85         if (response.getStatus() == null) {
86             logger.warn("SDNR response is missing the response status");
87             return Status.FAILURE;
88         }
89
90         StatusCodeEnum code = StatusCodeEnum.fromStatusCode(response.getStatus().getCode());
91
92         if (code == null) {
93             logger.warn("unknown SDNR response status code: {}", response.getStatus().getCode());
94             return Status.FAILURE;
95         }
96
97         switch (code) {
98             case SUCCESS:
99             case PARTIAL_SUCCESS:
100                 return Status.SUCCESS;
101             case FAILURE:
102             case PARTIAL_FAILURE:
103                 return Status.FAILURE;
104             case ERROR:
105             case REJECT:
106                 logger.warn("SDNR request was not accepted, code={}", code);
107                 return Status.FAILURE;
108             case ACCEPTED:
109             default:
110                 // awaiting a "final" response
111                 return Status.STILL_WAITING;
112         }
113     }
114
115     /**
116      * Sets the message to the status description, if available.
117      */
118     @Override
119     public OperationOutcome setOutcome(OperationOutcome outcome, PolicyResult result, PciMessage responseWrapper) {
120         outcome.setResponse(responseWrapper);
121
122         if (responseWrapper.getBody() == null || responseWrapper.getBody().getOutput() == null) {
123             return setOutcome(outcome, result);
124         }
125
126         PciResponse pciResponse = responseWrapper.getBody().getOutput();
127         if (pciResponse.getStatus() == null || pciResponse.getStatus().getValue() == null) {
128             return setOutcome(outcome, result);
129         }
130
131         outcome.setResult(result);
132         outcome.setMessage(pciResponse.getStatus().getValue());
133         return outcome;
134     }
135
136     @Override
137     protected PciMessage makeRequest(int attempt) {
138         VirtualControlLoopEvent onset = params.getContext().getEvent();
139         String subRequestId = getSubRequestId();
140
141         /* Construct an SDNR request using pci Model */
142
143         PciMessage dmaapRequest = new PciMessage();
144         dmaapRequest.setVersion("1.0");
145         dmaapRequest.setCorrelationId(onset.getRequestId() + "-" + subRequestId);
146         dmaapRequest.setType("request");
147         dmaapRequest.setRpcName(params.getOperation().toLowerCase());
148
149         /* This is the actual request that is placed in the dmaap wrapper. */
150         final PciRequest sdnrRequest = new PciRequest();
151
152         /* The common header is a required field for all SDNR requests. */
153         PciCommonHeader requestCommonHeader = new PciCommonHeader();
154         requestCommonHeader.setRequestId(onset.getRequestId());
155         requestCommonHeader.setSubRequestId(subRequestId);
156
157         sdnrRequest.setCommonHeader(requestCommonHeader);
158         sdnrRequest.setPayload(onset.getPayload());
159         sdnrRequest.setAction(params.getOperation());
160
161         /*
162          * Once the pci request is constructed, add it into the body of the dmaap wrapper.
163          */
164         PciBody body = new PciBody();
165         body.setInput(sdnrRequest);
166         dmaapRequest.setBody(body);
167
168         /* Return the request to be sent through dmaap. */
169         return dmaapRequest;
170     }
171 }