aboutsummaryrefslogtreecommitdiffstats
path: root/models-interactions/model-actors/actor.sdnr/src/main/java/org/onap/policy/controlloop/actor/sdnr/SdnrOperation.java
blob: c466963065090424cb142a456f5b6f8711533e53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*-
 * ============LICENSE_START=======================================================
 * SdnrOperation
 * ================================================================================
 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2023-2024 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.controlloop.actor.sdnr;

import java.util.List;
import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
import org.onap.policy.controlloop.actorserviceprovider.impl.BidirectionalTopicOperation;
import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
import org.onap.policy.controlloop.actorserviceprovider.topic.SelectorKey;
import org.onap.policy.sdnr.PciBody;
import org.onap.policy.sdnr.PciCommonHeader;
import org.onap.policy.sdnr.PciMessage;
import org.onap.policy.sdnr.PciRequest;
import org.onap.policy.sdnr.PciResponse;
import org.onap.policy.sdnr.util.StatusCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SdnrOperation extends BidirectionalTopicOperation<PciMessage, PciMessage> {
    private static final Logger logger = LoggerFactory.getLogger(SdnrOperation.class);

    /**
     * Operation name as it should appear within config files.
     */
    public static final String NAME = "any";

    private static final List<String> PROPERTY_NAMES = List.of(OperationProperties.EVENT_PAYLOAD);

    /**
     * Keys used to match the response with the request listener. The sub request ID is a
     * UUID, so it can be used to uniquely identify the response.
     * <p/>
     * Note: if these change, then {@link #getExpectedKeyValues(int, PciMessage)} must be
     * updated accordingly.
     */
    public static final List<SelectorKey> SELECTOR_KEYS =
                    List.of(new SelectorKey("body", "output", "CommonHeader", "SubRequestID"));

    public SdnrOperation(ControlLoopOperationParams params, BidirectionalTopicConfig config) {
        super(params, config, PciMessage.class, PROPERTY_NAMES);
    }

    /**
     * Note: these values must be in correspondence with {@link #SELECTOR_KEYS}.
     */
    @Override
    protected List<String> getExpectedKeyValues(int attempt, PciMessage request) {
        return List.of(getSubRequestId());
    }

    /*
     * NOTE: This should avoid throwing exceptions, so that a ControlLoopResponse can be
     * added to the outcome. Consequently, it returns FAILURE if a required field is
     * missing from the response.
     */
    @Override
    protected Status detmStatus(String rawResponse, PciMessage responseWrapper) {
        PciResponse response = responseWrapper.getBody().getOutput();

        if (response.getStatus() == null) {
            logger.warn("SDNR response is missing the response status");
            return Status.FAILURE;
        }

        var code = StatusCodeEnum.fromStatusCode(response.getStatus().getCode());

        if (code == null) {
            logger.warn("unknown SDNR response status code: {}", response.getStatus().getCode());
            return Status.FAILURE;
        }

        return switch (code) {
            case SUCCESS, PARTIAL_SUCCESS -> Status.SUCCESS;
            case FAILURE, PARTIAL_FAILURE -> Status.FAILURE;
            case ERROR, REJECT -> {
                logger.warn("SDNR request was not accepted, code={}", code);
                yield Status.FAILURE;
            }
            default ->
                // awaiting a "final" response
                Status.STILL_WAITING;
        };
    }

    /**
     * Sets the message to the status description, if available.
     */
    @Override
    public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, PciMessage responseWrapper) {
        outcome.setResponse(responseWrapper);

        if (responseWrapper.getBody() == null || responseWrapper.getBody().getOutput() == null) {
            return setOutcome(outcome, result);
        }

        var pciResponse = responseWrapper.getBody().getOutput();
        if (pciResponse.getStatus() == null || pciResponse.getStatus().getValue() == null) {
            return setOutcome(outcome, result);
        }

        outcome.setResult(result);
        outcome.setMessage(pciResponse.getStatus().getValue());
        return outcome;
    }

    @Override
    protected PciMessage makeRequest(int attempt) {
        String subRequestId = getSubRequestId();

        /* Construct an SDNR request using pci Model */

        var messageRequest = new PciMessage();
        messageRequest.setVersion("1.0");
        messageRequest.setCorrelationId(params.getRequestId() + "-" + subRequestId);
        messageRequest.setType("request");
        messageRequest.setRpcName(params.getOperation().toLowerCase());

        /* This is the actual request that is placed in the message wrapper. */
        final var sdnrRequest = new PciRequest();

        /* The common header is a required field for all SDNR requests. */
        var requestCommonHeader = new PciCommonHeader();
        requestCommonHeader.setRequestId(params.getRequestId());
        requestCommonHeader.setSubRequestId(subRequestId);

        sdnrRequest.setCommonHeader(requestCommonHeader);
        sdnrRequest.setPayload(getRequiredProperty(OperationProperties.EVENT_PAYLOAD, "event payload"));
        sdnrRequest.setAction(params.getOperation());

        /*
         * Once the pci request is constructed, add it into the body of the message wrapper.
         */
        var body = new PciBody();
        body.setInput(sdnrRequest);
        messageRequest.setBody(body);

        /* Return the request to be sent through kafka. */
        return messageRequest;
    }
}