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