5faa31d7c0fad8835b581b13433508e5579e492e
[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.UUID;
25 import java.util.concurrent.CompletableFuture;
26 import org.onap.policy.controlloop.VirtualControlLoopEvent;
27 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
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.controlloop.policy.PolicyResult;
33 import org.onap.policy.sdnr.PciCommonHeader;
34 import org.onap.policy.sdnr.PciRequest;
35 import org.onap.policy.sdnr.PciRequestWrapper;
36 import org.onap.policy.sdnr.PciResponse;
37 import org.onap.policy.sdnr.PciResponseWrapper;
38 import org.onap.policy.sdnr.util.StatusCodeEnum;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public abstract class SdnrOperation extends BidirectionalTopicOperation<PciRequestWrapper, PciResponseWrapper> {
43     private static final Logger logger = LoggerFactory.getLogger(SdnrOperation.class);
44
45     /**
46      * Keys used to match the response with the request listener. The sub request ID is a
47      * UUID, so it can be used to uniquely identify the response.
48      * <p/>
49      * Note: if these change, then {@link #getExpectedKeyValues(int, Request)} must be
50      * updated accordingly.
51      */
52     public static final List<SelectorKey> SELECTOR_KEYS = List.of(new SelectorKey("CommonHeader", "SubRequestID"));
53
54     public SdnrOperation(ControlLoopOperationParams params, BidirectionalTopicConfig config) {
55         super(params, config, PciResponseWrapper.class);
56     }
57
58     /**
59      * Note: these values must match {@link #SELECTOR_KEYS}.
60      */
61     @Override
62     protected List<String> getExpectedKeyValues(int attempt, PciRequestWrapper request) {
63         return List.of(request.getBody().getCommonHeader().getSubRequestId());
64     }
65
66     @Override
67     protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
68         return startGuardAsync();
69     }
70
71     @Override
72     protected Status detmStatus(String rawResponse, PciResponseWrapper responseWrapper) {
73         PciResponse response = responseWrapper.getBody();
74
75         if (response == null || response.getStatus() == null) {
76             throw new IllegalArgumentException("SDNR response is missing the response status");
77         }
78
79         StatusCodeEnum code = StatusCodeEnum.fromStatusCode(response.getStatus().getCode());
80
81         if (code == null) {
82             throw new IllegalArgumentException(
83                             "unknown SDNR response status code: " + response.getStatus().getCode());
84         }
85
86         /*
87          * Response and Payload are just printed and no further action needed since
88          * casablanca release
89          */
90         logger.info("SDNR Response Code {} Message is {}", code, response.getStatus().getValue());
91         logger.info("SDNR Response Payload is {}", response.getPayload());
92
93         switch (code) {
94             case SUCCESS:
95             case PARTIAL_SUCCESS:
96                 return Status.SUCCESS;
97             case FAILURE:
98             case PARTIAL_FAILURE:
99                 return Status.FAILURE;
100             case ERROR:
101             case REJECT:
102                 throw new IllegalArgumentException("SDNR request was not accepted, code=" + code);
103             case ACCEPTED:
104             default:
105                 // awaiting a "final" response
106                 return Status.STILL_WAITING;
107         }
108     }
109
110     /**
111      * Sets the message to the status description, if available.
112      */
113     @Override
114     public OperationOutcome setOutcome(OperationOutcome outcome, PolicyResult result,
115             PciResponseWrapper responseWrapper) {
116         PciResponse response = responseWrapper.getBody();
117         if (response.getStatus() == null || response.getStatus().getValue() == null) {
118             return setOutcome(outcome, result);
119         }
120
121         outcome.setResult(result);
122         outcome.setMessage(response.getStatus().getValue());
123         return outcome;
124     }
125
126     @Override
127     protected PciRequestWrapper makeRequest(int attempt) {
128         VirtualControlLoopEvent onset = params.getContext().getEvent();
129         String subRequestId = UUID.randomUUID().toString();
130
131         /* Construct an SDNR request using pci Model */
132
133         /*
134          * The actual pci request is placed in a wrapper used to send through dmaap. The
135          * current version is 2.0 as of R1.
136          */
137         PciRequestWrapper dmaapRequest = new PciRequestWrapper();
138         dmaapRequest.setVersion("1.0");
139         dmaapRequest.setCorrelationId(onset.getRequestId() + "-" + subRequestId);
140         dmaapRequest.setType("request");
141
142         /* This is the actual request that is placed in the dmaap wrapper. */
143         final PciRequest sdnrRequest = new PciRequest();
144
145         /* The common header is a required field for all SDNR requests. */
146         PciCommonHeader requestCommonHeader = new PciCommonHeader();
147         requestCommonHeader.setRequestId(onset.getRequestId());
148         requestCommonHeader.setSubRequestId(subRequestId);
149
150         sdnrRequest.setCommonHeader(requestCommonHeader);
151         sdnrRequest.setPayload(onset.getPayload());
152
153         /*
154          * Once the pci request is constructed, add it into the body of the dmaap
155          * wrapper.
156          */
157         dmaapRequest.setBody(sdnrRequest);
158         logger.info("SDNR Request to be sent is {}", dmaapRequest);
159
160         /* Return the request to be sent through dmaap. */
161         return dmaapRequest;
162     }
163 }