030bdeb43af890b98fae24ff85b1266bd3389dd5
[policy/gui.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  *  Modifications Copyright (C) 2021 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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.gui.editors.apex.rest.handling;
24
25 import org.onap.policy.apex.model.modelapi.ApexApiResult;
26 import org.onap.policy.gui.editors.apex.rest.handling.bean.BeanContextSchema;
27 import org.slf4j.ext.XLogger;
28 import org.slf4j.ext.XLoggerFactory;
29
30 /**
31  * This class handles commands on context schemas in Apex models.
32  */
33 public class ContextSchemaHandler implements RestCommandHandler {
34     // Get a reference to the logger
35     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextSchemaHandler.class);
36
37     // Recurring string constants
38     private static final String OK = ": OK";
39     private static final String NOT_OK = ": Not OK";
40
41     /**
42      * {@inheritDoc}.
43      */
44     @Override
45     public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
46         final RestCommand command) {
47         return getUnsupportedCommandResultMessage(session, commandType, command);
48     }
49
50     /**
51      * {@inheritDoc}.
52      */
53     @Override
54     public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
55         final RestCommand command, final String jsonString) {
56         if (!RestCommandType.CONTEXT_SCHEMA.equals(commandType)) {
57             return getUnsupportedCommandResultMessage(session, commandType, command);
58         }
59
60         switch (command) {
61             case CREATE:
62                 return createContextSchema(session, jsonString);
63             case UPDATE:
64                 return updateContextSchema(session, jsonString);
65             default:
66                 return getUnsupportedCommandResultMessage(session, commandType, command);
67         }
68     }
69
70     /**
71      * {@inheritDoc}.
72      */
73     @Override
74     public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
75         final RestCommand command, final String name, final String version) {
76         if (!RestCommandType.CONTEXT_SCHEMA.equals(commandType)) {
77             return getUnsupportedCommandResultMessage(session, commandType, command);
78         }
79
80         switch (command) {
81             case LIST:
82                 return listContextSchemas(session, name, version);
83             case DELETE:
84                 return deleteContextSchema(session, name, version);
85             case VALIDATE:
86                 return validateContextSchemas(session, name, version);
87             default:
88                 return getUnsupportedCommandResultMessage(session, commandType, command);
89         }
90     }
91
92     /**
93      * Creates a context schema.
94      *
95      * @param session    the session holding the Apex model
96      * @param jsonString the JSON string with the context schema parameters
97      * @return the result of the operation
98      */
99     private ApexApiResult createContextSchema(final RestSession session, final String jsonString) {
100         LOGGER.entry(jsonString);
101
102         session.editModel();
103
104         final var jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextSchema.class);
105         ApexApiResult result = session.getApexModelEdited().createContextSchema(jsonbean.getName(),
106             jsonbean.getVersion(), jsonbean.getSchemaFlavour(), jsonbean.getSchemaDefinition(), jsonbean.getUuid(),
107             jsonbean.getDescription());
108
109         session.finishSession(result.isOk());
110
111         LOGGER.exit("ContextSchema/create" + (result.isOk() ? OK : NOT_OK));
112         return result;
113     }
114
115     /**
116      * Update a context schema.
117      *
118      * @param session    the session holding the Apex model
119      * @param jsonString the JSON string with the context schema parameters
120      * @return the result of the operation
121      */
122     private ApexApiResult updateContextSchema(final RestSession session, final String jsonString) {
123         LOGGER.entry(jsonString);
124
125         session.editModel();
126
127         final var jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextSchema.class);
128
129         ApexApiResult result = session.getApexModelEdited().updateContextSchema(jsonbean.getName(),
130             jsonbean.getVersion(), jsonbean.getSchemaFlavour(), jsonbean.getSchemaDefinition(), jsonbean.getUuid(),
131             jsonbean.getDescription());
132
133         session.finishSession(result.isOk());
134
135         LOGGER.exit("ContextSchema/Update" + (result.isOk() ? OK : NOT_OK));
136         return result;
137     }
138
139     /**
140      * List context schemas.
141      *
142      * @param session the session holding the Apex model
143      * @param name    the context schema name to operate on
144      * @param version the context schema version to operate on
145      * @return the result of the operation
146      */
147     private ApexApiResult listContextSchemas(final RestSession session, final String name, final String version) {
148         LOGGER.entry(name, version);
149
150         ApexApiResult result = session.getApexModel().listContextSchemas(blank2Null(name), blank2Null(version));
151
152         LOGGER.exit("ContextSchema/Get" + (result != null && result.isOk() ? OK : NOT_OK));
153         return result;
154     }
155
156     /**
157      * Delete a context schema.
158      *
159      * @param session the session holding the Apex model
160      * @param name    the context schema name to operate on
161      * @param version the context schema version to operate on
162      * @return the result of the operation
163      */
164     private ApexApiResult deleteContextSchema(final RestSession session, final String name, final String version) {
165         LOGGER.entry(name, version);
166
167         session.editModel();
168
169         ApexApiResult result = session.getApexModelEdited().deleteContextSchema(blank2Null(name), blank2Null(version));
170
171         session.finishSession(result.isOk());
172
173         LOGGER.exit("ContextSchema/Delete" + (result.isOk() ? OK : NOT_OK));
174         return result;
175     }
176
177     /**
178      * Validate a context schema.
179      *
180      * @param session the session holding the Apex model
181      * @param name    the context schema name to operate on
182      * @param version the context schema version to operate on
183      * @return the result of the operation
184      */
185     private ApexApiResult validateContextSchemas(final RestSession session, final String name, final String version) {
186         LOGGER.entry(name, version);
187
188         ApexApiResult result = session.getApexModel().validateContextSchemas(blank2Null(name), blank2Null(version));
189
190         LOGGER.exit("Validate/ContextSchema" + (result != null && result.isOk() ? OK : NOT_OK));
191         return result;
192     }
193 }