Fix nexus and sonar vulnerabilities
[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  * Modifications Copyright (C) 2023 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.controlloop.actor.sdnr;
23
24 import java.util.List;
25 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
26 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
27 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
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.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     private static final List<String> PROPERTY_NAMES = List.of(OperationProperties.EVENT_PAYLOAD);
50
51     /**
52      * Keys used to match the response with the request listener. The sub request ID is a
53      * UUID, so it can be used to uniquely identify the response.
54      * <p/>
55      * Note: if these change, then {@link #getExpectedKeyValues(int, PciMessage)} must be
56      * updated accordingly.
57      */
58     public static final List<SelectorKey> SELECTOR_KEYS =
59                     List.of(new SelectorKey("body", "output", "CommonHeader", "SubRequestID"));
60
61     public SdnrOperation(ControlLoopOperationParams params, BidirectionalTopicConfig config) {
62         super(params, config, PciMessage.class, PROPERTY_NAMES);
63     }
64
65     /**
66      * Note: these values must be in correspondence with {@link #SELECTOR_KEYS}.
67      */
68     @Override
69     protected List<String> getExpectedKeyValues(int attempt, PciMessage request) {
70         return List.of(getSubRequestId());
71     }
72
73     /*
74      * NOTE: This should avoid throwing exceptions, so that a ControlLoopResponse can be
75      * added to the outcome. Consequently, it returns FAILURE if a required field is
76      * missing from the response.
77      */
78     @Override
79     protected Status detmStatus(String rawResponse, PciMessage responseWrapper) {
80         PciResponse response = responseWrapper.getBody().getOutput();
81
82         if (response.getStatus() == null) {
83             logger.warn("SDNR response is missing the response status");
84             return Status.FAILURE;
85         }
86
87         var code = StatusCodeEnum.fromStatusCode(response.getStatus().getCode());
88
89         if (code == null) {
90             logger.warn("unknown SDNR response status code: {}", response.getStatus().getCode());
91             return Status.FAILURE;
92         }
93
94         return switch (code) {
95             case SUCCESS, PARTIAL_SUCCESS -> Status.SUCCESS;
96             case FAILURE, PARTIAL_FAILURE -> Status.FAILURE;
97             case ERROR, REJECT -> {
98                 logger.warn("SDNR request was not accepted, code={}", code);
99                 yield Status.FAILURE;
100             }
101             default ->
102                 // awaiting a "final" response
103                 Status.STILL_WAITING;
104         };
105     }
106
107     /**
108      * Sets the message to the status description, if available.
109      */
110     @Override
111     public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, PciMessage responseWrapper) {
112         outcome.setResponse(responseWrapper);
113
114         if (responseWrapper.getBody() == null || responseWrapper.getBody().getOutput() == null) {
115             return setOutcome(outcome, result);
116         }
117
118         var pciResponse = responseWrapper.getBody().getOutput();
119         if (pciResponse.getStatus() == null || pciResponse.getStatus().getValue() == null) {
120             return setOutcome(outcome, result);
121         }
122
123         outcome.setResult(result);
124         outcome.setMessage(pciResponse.getStatus().getValue());
125         return outcome;
126     }
127
128     @Override
129     protected PciMessage makeRequest(int attempt) {
130         String subRequestId = getSubRequestId();
131
132         /* Construct an SDNR request using pci Model */
133
134         var dmaapRequest = new PciMessage();
135         dmaapRequest.setVersion("1.0");
136         dmaapRequest.setCorrelationId(params.getRequestId() + "-" + subRequestId);
137         dmaapRequest.setType("request");
138         dmaapRequest.setRpcName(params.getOperation().toLowerCase());
139
140         /* This is the actual request that is placed in the dmaap wrapper. */
141         final var sdnrRequest = new PciRequest();
142
143         /* The common header is a required field for all SDNR requests. */
144         var requestCommonHeader = new PciCommonHeader();
145         requestCommonHeader.setRequestId(params.getRequestId());
146         requestCommonHeader.setSubRequestId(subRequestId);
147
148         sdnrRequest.setCommonHeader(requestCommonHeader);
149         sdnrRequest.setPayload(getRequiredProperty(OperationProperties.EVENT_PAYLOAD, "event payload"));
150         sdnrRequest.setAction(params.getOperation());
151
152         /*
153          * Once the pci request is constructed, add it into the body of the dmaap wrapper.
154          */
155         var body = new PciBody();
156         body.setInput(sdnrRequest);
157         dmaapRequest.setBody(body);
158
159         /* Return the request to be sent through dmaap. */
160         return dmaapRequest;
161     }
162 }