5b30c673a4de9464cda9567cefc6a6bab0a169e7
[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-2021 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 org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
25 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
26 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
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.sdnr.PciBody;
32 import org.onap.policy.sdnr.PciCommonHeader;
33 import org.onap.policy.sdnr.PciMessage;
34 import org.onap.policy.sdnr.PciRequest;
35 import org.onap.policy.sdnr.PciResponse;
36 import org.onap.policy.sdnr.util.StatusCodeEnum;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class SdnrOperation extends BidirectionalTopicOperation<PciMessage, PciMessage> {
41     private static final Logger logger = LoggerFactory.getLogger(SdnrOperation.class);
42
43     /**
44      * Operation name as it should appear within config files.
45      */
46     public static final String NAME = "any";
47
48     private static final List<String> PROPERTY_NAMES = List.of(OperationProperties.EVENT_PAYLOAD);
49
50     /**
51      * Keys used to match the response with the request listener. The sub request ID is a
52      * UUID, so it can be used to uniquely identify the response.
53      * <p/>
54      * Note: if these change, then {@link #getExpectedKeyValues(int, PciMessage)} must be
55      * updated accordingly.
56      */
57     public static final List<SelectorKey> SELECTOR_KEYS =
58                     List.of(new SelectorKey("body", "output", "CommonHeader", "SubRequestID"));
59
60     public SdnrOperation(ControlLoopOperationParams params, BidirectionalTopicConfig config) {
61         super(params, config, PciMessage.class, PROPERTY_NAMES);
62     }
63
64     /**
65      * Note: these values must be in correspondence with {@link #SELECTOR_KEYS}.
66      */
67     @Override
68     protected List<String> getExpectedKeyValues(int attempt, PciMessage request) {
69         return List.of(getSubRequestId());
70     }
71
72     /*
73      * NOTE: This should avoid throwing exceptions, so that a ControlLoopResponse can be
74      * added to the outcome. Consequently, it returns FAILURE if a required field is
75      * missing from the response.
76      */
77     @Override
78     protected Status detmStatus(String rawResponse, PciMessage responseWrapper) {
79         PciResponse response = responseWrapper.getBody().getOutput();
80
81         if (response.getStatus() == null) {
82             logger.warn("SDNR response is missing the response status");
83             return Status.FAILURE;
84         }
85
86         var code = StatusCodeEnum.fromStatusCode(response.getStatus().getCode());
87
88         if (code == null) {
89             logger.warn("unknown SDNR response status code: {}", response.getStatus().getCode());
90             return Status.FAILURE;
91         }
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                 logger.warn("SDNR request was not accepted, code={}", code);
103                 return Status.FAILURE;
104             case ACCEPTED:
105             default:
106                 // awaiting a "final" response
107                 return Status.STILL_WAITING;
108         }
109     }
110
111     /**
112      * Sets the message to the status description, if available.
113      */
114     @Override
115     public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, PciMessage responseWrapper) {
116         outcome.setResponse(responseWrapper);
117
118         if (responseWrapper.getBody() == null || responseWrapper.getBody().getOutput() == null) {
119             return setOutcome(outcome, result);
120         }
121
122         var pciResponse = responseWrapper.getBody().getOutput();
123         if (pciResponse.getStatus() == null || pciResponse.getStatus().getValue() == null) {
124             return setOutcome(outcome, result);
125         }
126
127         outcome.setResult(result);
128         outcome.setMessage(pciResponse.getStatus().getValue());
129         return outcome;
130     }
131
132     @Override
133     protected PciMessage makeRequest(int attempt) {
134         String subRequestId = getSubRequestId();
135
136         /* Construct an SDNR request using pci Model */
137
138         var dmaapRequest = new PciMessage();
139         dmaapRequest.setVersion("1.0");
140         dmaapRequest.setCorrelationId(params.getRequestId() + "-" + subRequestId);
141         dmaapRequest.setType("request");
142         dmaapRequest.setRpcName(params.getOperation().toLowerCase());
143
144         /* This is the actual request that is placed in the dmaap wrapper. */
145         final var sdnrRequest = new PciRequest();
146
147         /* The common header is a required field for all SDNR requests. */
148         var requestCommonHeader = new PciCommonHeader();
149         requestCommonHeader.setRequestId(params.getRequestId());
150         requestCommonHeader.setSubRequestId(subRequestId);
151
152         sdnrRequest.setCommonHeader(requestCommonHeader);
153         sdnrRequest.setPayload(getRequiredProperty(OperationProperties.EVENT_PAYLOAD, "event payload"));
154         sdnrRequest.setAction(params.getOperation());
155
156         /*
157          * Once the pci request is constructed, add it into the body of the dmaap wrapper.
158          */
159         var body = new PciBody();
160         body.setInput(sdnrRequest);
161         dmaapRequest.setBody(body);
162
163         /* Return the request to be sent through dmaap. */
164         return dmaapRequest;
165     }
166 }