aboutsummaryrefslogtreecommitdiffstats
path: root/model/src/main/java/org/onap/policy/apex/model/modelapi/ApexApiResult.java
blob: 83e3b53709569a2ad0b5405aa802362c080cfeda (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020,2022 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.model.modelapi;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import lombok.Setter;

/**
 * The Class ApexEditorAPIResult return the result of and messages from all model API method calls on the
 * {@link ApexModel} API.
 */
@Setter
public class ApexApiResult {

    /**
     * This enumeration is used to represent the result status of a call on the {@link ApexModel} API.
     */
    public enum Result {
        /** The method call succeeded. */
        SUCCESS,
        /** The method call succeeded and all operations are now completed. */
        FINISHED,
        /** The method call for a create operation failed because the concept already exists. */
        CONCEPT_EXISTS,
        /**
         * The method call for a create operation failed because multiple concepts already exists.
         */
        MULTIPLE_CONCEPTS_EXIST,
        /** The method call on a concept failed because the referenced concept does not exist. */
        CONCEPT_DOES_NOT_EXIST,
        /** The method call failed because no action was specified on the method call. */
        NO_ACTION_SPECIFIED,
        /**
         * The method call failed because of a structural error, a missing reference, or other error on the model.
         */
        FAILED,
        /**
         * The method call failed for another reason such as the method call is not implemented yet on the concept on
         * which it was called.
         */
        OTHER_ERROR;

        /**
         * Check if a result is OK.
         *
         * @param result the result
         * @return true if the result is not OK
         */
        public static boolean isOk(final Result result) {
            return result == Result.SUCCESS || result == Result.FINISHED;
        }

        /**
         * Check if a result is not OK.
         *
         * @param result the result
         * @return true if the result is not OK
         */
        public static boolean isNok(final Result result) {
            return !isOk(result);
        }
    }

    private Result result;
    private List<String> messages = new ArrayList<>();

    /**
     * The Default Constructor creates a result for a successful operation with no messages.
     */
    public ApexApiResult() {
        result = Result.SUCCESS;
    }

    /**
     * This Constructor creates a result with the given result status with no messages.
     *
     * @param result the result status to use on this result
     */
    public ApexApiResult(final Result result) {
        this.result = result;
    }

    /**
     * This Constructor creates a result with the given result status and message.
     *
     * @param result the result status to use on this result
     * @param message the message to return with the result
     */
    public ApexApiResult(final Result result, final String message) {
        this.result = result;
        addMessage(message);
    }

    /**
     * This Constructor creates a result with the given result status and {@link Throwable} object such as an exception.
     * The message and stack trace from the {@link Throwable} object are added to the message list of this message.
     *
     * @param result the result status to use on this result
     * @param throwable the throwable object from which to add the message and stack trace
     */
    public ApexApiResult(final Result result, final Throwable throwable) {
        this.result = result;
        addThrowable(throwable);
    }

    /**
     * This Constructor creates a result with the given result status, message, and {@link Throwable} object such as an
     * exception. The message and stack trace from the {@link Throwable} object are added to the message list of this
     * message.
     *
     * @param result the result status to use on this result
     * @param message the message to return with the result
     * @param throwable the throwable object from which to add the message and stack trace
     */
    public ApexApiResult(final Result result, final String message, final Throwable throwable) {
        this.result = result;
        addMessage(message);
        addThrowable(throwable);
    }

    /**
     * This message is a utility message that checks if the result of an operation on the API was OK.
     *
     * @return true, if the result indicates the API operation succeeded
     */
    public boolean isOk() {
        return Result.isOk(result);
    }

    /**
     * This message is a utility message that checks if the result of an operation on the API was not OK.
     *
     * @return true, if the result indicates the API operation did not succeed
     */
    public boolean isNok() {
        return Result.isNok(result);
    }

    /**
     * Gets the result status of an API operation.
     *
     * @return the result status
     */
    public Result getResult() {
        return result;
    }

    /**
     * Gets the list of messages returned by an API operation.
     *
     * @return the list of messages returned by an API operation
     */
    public List<String> getMessages() {
        return messages;
    }

    /**
     * Gets all the messages returned by an API operation as a single string.
     *
     * @return the messages returned by an API operation as a single string
     */
    public String getMessage() {
        final StringBuilder builder = new StringBuilder();
        for (final String message : messages) {
            builder.append(message);
            builder.append('\n');
        }

        return builder.toString();
    }

    /**
     * Adds a message from an API operation to the bottom of the list of messages to be returned.
     *
     * @param message the message from an API operation to add to the bottom of the list of messages to be returned
     */
    public void addMessage(final String message) {
        if (message != null && message.trim().length() > 0) {
            messages.add(message);
        }
    }

    /**
     * Adds the message and stack trace from a {@link Throwable} object such as an exception from an API operation to
     * the bottom of the list of messages to be returned.
     *
     * @param throwable the {@link Throwable} object such as an exception from an API operation from which the message
     *        and stack trace are to be extracted and placed at the bottom of the list of messages to be returned
     */
    public void addThrowable(final Throwable throwable) {
        final StringWriter throwableStringWriter = new StringWriter();
        final PrintWriter throwablePrintWriter = new PrintWriter(throwableStringWriter);
        throwable.printStackTrace(throwablePrintWriter);
        messages.add(throwable.getMessage());
        messages.add(throwableStringWriter.toString());
    }

    /**
     * Gets a representation of the {@link ApexApiResult} instance as a JSON string.
     *
     * @return the result instance JSON string
     */
    public String toJson() {
        final StringBuilder builder = new StringBuilder();
        builder.append("{\n");

        builder.append("\"result\": \"");
        builder.append(result.toString());
        builder.append("\",\n");

        builder.append("\"messages\": [");
        boolean first = true;
        for (final String message : messages) {
            if (first) {
                builder.append("\n\"");
                first = false;
            } else {
                builder.append(",\n\"");
            }
            builder.append(message.replace("\"", "\\\\\""));
            builder.append("\"");
        }
        builder.append("]\n");

        builder.append("}\n");

        return builder.toString();
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public String toString() {
        final StringBuilder builder = new StringBuilder();
        builder.append("result: ");
        builder.append(result);
        builder.append('\n');
        builder.append(getMessage());
        return builder.toString();
    }
}