Remove Target and TargetType
[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.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     @Override
74     protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
75         return startGuardAsync();
76     }
77
78     /*
79      * NOTE: This should avoid throwing exceptions, so that a ControlLoopResponse can be
80      * added to the outcome. Consequently, it returns FAILURE if a required field is
81      * missing from the response.
82      */
83     @Override
84     protected Status detmStatus(String rawResponse, PciMessage responseWrapper) {
85         PciResponse response = responseWrapper.getBody().getOutput();
86
87         if (response.getStatus() == null) {
88             logger.warn("SDNR response is missing the response status");
89             return Status.FAILURE;
90         }
91
92         StatusCodeEnum code = StatusCodeEnum.fromStatusCode(response.getStatus().getCode());
93
94         if (code == null) {
95             logger.warn("unknown SDNR response status code: {}", response.getStatus().getCode());
96             return Status.FAILURE;
97         }
98
99         switch (code) {
100             case SUCCESS:
101             case PARTIAL_SUCCESS:
102                 return Status.SUCCESS;
103             case FAILURE:
104             case PARTIAL_FAILURE:
105                 return Status.FAILURE;
106             case ERROR:
107             case REJECT:
108                 logger.warn("SDNR request was not accepted, code={}", code);
109                 return Status.FAILURE;
110             case ACCEPTED:
111             default:
112                 // awaiting a "final" response
113                 return Status.STILL_WAITING;
114         }
115     }
116
117     /**
118      * Sets the message to the status description, if available.
119      */
120     @Override
121     public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, PciMessage responseWrapper) {
122         outcome.setResponse(responseWrapper);
123
124         if (responseWrapper.getBody() == null || responseWrapper.getBody().getOutput() == null) {
125             return setOutcome(outcome, result);
126         }
127
128         PciResponse pciResponse = responseWrapper.getBody().getOutput();
129         if (pciResponse.getStatus() == null || pciResponse.getStatus().getValue() == null) {
130             return setOutcome(outcome, result);
131         }
132
133         outcome.setResult(result);
134         outcome.setMessage(pciResponse.getStatus().getValue());
135         return outcome;
136     }
137
138     @Override
139     protected PciMessage makeRequest(int attempt) {
140         String subRequestId = getSubRequestId();
141
142         /* Construct an SDNR request using pci Model */
143
144         PciMessage dmaapRequest = new PciMessage();
145         dmaapRequest.setVersion("1.0");
146         dmaapRequest.setCorrelationId(params.getRequestId() + "-" + subRequestId);
147         dmaapRequest.setType("request");
148         dmaapRequest.setRpcName(params.getOperation().toLowerCase());
149
150         /* This is the actual request that is placed in the dmaap wrapper. */
151         final PciRequest sdnrRequest = new PciRequest();
152
153         /* The common header is a required field for all SDNR requests. */
154         PciCommonHeader requestCommonHeader = new PciCommonHeader();
155         requestCommonHeader.setRequestId(params.getRequestId());
156         requestCommonHeader.setSubRequestId(subRequestId);
157
158         sdnrRequest.setCommonHeader(requestCommonHeader);
159         sdnrRequest.setPayload(getEventPayload());
160         sdnrRequest.setAction(params.getOperation());
161
162         /*
163          * Once the pci request is constructed, add it into the body of the dmaap wrapper.
164          */
165         PciBody body = new PciBody();
166         body.setInput(sdnrRequest);
167         dmaapRequest.setBody(body);
168
169         /* Return the request to be sent through dmaap. */
170         return dmaapRequest;
171     }
172
173     /**
174      * Gets the event payload, first checking for it in the properties and then in the
175      * event.
176      *
177      * @return the event payload
178      */
179     protected String getEventPayload() {
180         if (containsProperty(OperationProperties.EVENT_PAYLOAD)) {
181             return getProperty(OperationProperties.EVENT_PAYLOAD);
182         }
183
184         return params.getContext().getEvent().getPayload();
185     }
186 }