aboutsummaryrefslogtreecommitdiffstats
path: root/gui-editors/gui-editor-apex/src/main/java/org/onap/policy/gui/editors/apex/rest/handling/ContextSchemaHandler.java
blob: 65aa2fdfee85ef1cd10699e0b5913be4918f6b47 (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
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020 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.gui.editors.apex.rest.handling;

import org.onap.policy.apex.model.modelapi.ApexApiResult;
import org.onap.policy.gui.editors.apex.rest.handling.bean.BeanContextSchema;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;

/**
 * This class handles commands on context schemas in Apex models.
 */
public class ContextSchemaHandler implements RestCommandHandler {
    // Get a reference to the logger
    private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextSchemaHandler.class);

    // Recurring string constants
    private static final String OK = ": OK";
    private static final String NOT_OK = ": Not OK";

    /**
     * {@inheritDoc}.
     */
    @Override
    public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
        final RestCommand command) {
        return getUnsupportedCommandResultMessage(session, commandType, command);
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
        final RestCommand command, final String jsonString) {
        if (!RestCommandType.CONTEXT_SCHEMA.equals(commandType)) {
            return getUnsupportedCommandResultMessage(session, commandType, command);
        }

        switch (command) {
            case CREATE:
                return createContextSchema(session, jsonString);
            case UPDATE:
                return updateContextSchema(session, jsonString);
            default:
                return getUnsupportedCommandResultMessage(session, commandType, command);
        }
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
        final RestCommand command, final String name, final String version) {
        if (!RestCommandType.CONTEXT_SCHEMA.equals(commandType)) {
            return getUnsupportedCommandResultMessage(session, commandType, command);
        }

        switch (command) {
            case LIST:
                return listContextSchemas(session, name, version);
            case DELETE:
                return deleteContextSchema(session, name, version);
            case VALIDATE:
                return validateContextSchemas(session, name, version);
            default:
                return getUnsupportedCommandResultMessage(session, commandType, command);
        }
    }

    /**
     * Creates a context schema.
     *
     * @param session    the session holding the Apex model
     * @param jsonString the JSON string with the context schema parameters
     * @return the result of the operation
     */
    private ApexApiResult createContextSchema(final RestSession session, final String jsonString) {
        LOGGER.entry(jsonString);

        session.editModel();

        final BeanContextSchema jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextSchema.class);
        ApexApiResult result = session.getApexModelEdited().createContextSchema(jsonbean.getName(),
            jsonbean.getVersion(), jsonbean.getSchemaFlavour(), jsonbean.getSchemaDefinition(), jsonbean.getUuid(),
            jsonbean.getDescription());

        session.finishSession(result.isOk());

        LOGGER.exit("ContextSchema/create" + (result != null && result.isOk() ? OK : NOT_OK));
        return result;
    }

    /**
     * Update a context schema.
     *
     * @param session    the session holding the Apex model
     * @param jsonString the JSON string with the context schema parameters
     * @return the result of the operation
     */
    private ApexApiResult updateContextSchema(final RestSession session, final String jsonString) {
        LOGGER.entry(jsonString);

        session.editModel();

        final BeanContextSchema jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextSchema.class);

        ApexApiResult result = session.getApexModelEdited().updateContextSchema(jsonbean.getName(),
            jsonbean.getVersion(), jsonbean.getSchemaFlavour(), jsonbean.getSchemaDefinition(), jsonbean.getUuid(),
            jsonbean.getDescription());

        session.finishSession(result.isOk());

        LOGGER.exit("ContextSchema/Update" + (result != null && result.isOk() ? OK : NOT_OK));
        return result;
    }

    /**
     * List context schemas.
     *
     * @param session the session holding the Apex model
     * @param name    the context schema name to operate on
     * @param version the context schema version to operate on
     * @return the result of the operation
     */
    private ApexApiResult listContextSchemas(final RestSession session, final String name, final String version) {
        LOGGER.entry(name, version);

        ApexApiResult result = session.getApexModel().listContextSchemas(blank2Null(name), blank2Null(version));

        LOGGER.exit("ContextSchema/Get" + (result != null && result.isOk() ? OK : NOT_OK));
        return result;
    }

    /**
     * Delete a context schema.
     *
     * @param session the session holding the Apex model
     * @param name    the context schema name to operate on
     * @param version the context schema version to operate on
     * @return the result of the operation
     */
    private ApexApiResult deleteContextSchema(final RestSession session, final String name, final String version) {
        LOGGER.entry(name, version);

        session.editModel();

        ApexApiResult result = session.getApexModelEdited().deleteContextSchema(blank2Null(name), blank2Null(version));

        session.finishSession(result.isOk());

        LOGGER.exit("ContextSchema/Delete" + (result != null && result.isOk() ? OK : NOT_OK));
        return result;
    }

    /**
     * Validate a context schema.
     *
     * @param session the session holding the Apex model
     * @param name    the context schema name to operate on
     * @param version the context schema version to operate on
     * @return the result of the operation
     */
    private ApexApiResult validateContextSchemas(final RestSession session, final String name, final String version) {
        LOGGER.entry(name, version);

        ApexApiResult result = session.getApexModel().validateContextSchemas(blank2Null(name), blank2Null(version));

        LOGGER.exit("Validate/ContextSchema" + (result != null && result.isOk() ? OK : NOT_OK));
        return result;
    }
}