3805e1e1c72a80493b09168907d770e4557aa4cc
[policy/apex-pdp.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.apex.client.editor.rest.handling;
23
24 import org.onap.policy.apex.client.editor.rest.handling.bean.BeanContextSchema;
25 import org.onap.policy.apex.model.modelapi.ApexApiResult;
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(),
106                         jsonbean.getUuid(), jsonbean.getDescription());
107
108         if (result != null) {
109             session.finishSession(result.isOk());
110             LOGGER.exit("ContextSchema/create" + (result.isOk() ? OK : NOT_OK));
111         }
112
113         return result;
114     }
115
116     /**
117      * Update a context schema.
118      *
119      * @param session the session holding the Apex model
120      * @param jsonString the JSON string with the context schema parameters
121      * @return the result of the operation
122      */
123     private ApexApiResult updateContextSchema(final RestSession session, final String jsonString) {
124         LOGGER.entry(jsonString);
125
126         session.editModel();
127
128         final BeanContextSchema jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextSchema.class);
129
130         ApexApiResult result = session.getApexModelEdited().updateContextSchema(jsonbean.getName(),
131                         jsonbean.getVersion(), jsonbean.getSchemaFlavour(), jsonbean.getSchemaDefinition(),
132                         jsonbean.getUuid(), jsonbean.getDescription());
133
134         if (result != null) {
135             session.finishSession(result.isOk());
136             LOGGER.exit("ContextSchema/Update" + (result.isOk() ? OK : NOT_OK));
137         }
138
139         return result;
140     }
141
142     /**
143      * List context schemas.
144      *
145      * @param session the session holding the Apex model
146      * @param name the context schema name to operate on
147      * @param version the context schema version to operate on
148      * @return the result of the operation
149      */
150     private ApexApiResult listContextSchemas(final RestSession session, final String name, final String version) {
151         LOGGER.entry(name, version);
152
153         ApexApiResult result = session.getApexModel().listContextSchemas(blank2Null(name), blank2Null(version));
154
155         LOGGER.exit("ContextSchema/Get" + (result != null && result.isOk() ? OK : NOT_OK));
156         return result;
157     }
158
159     /**
160      * Delete a context schema.
161      *
162      * @param session the session holding the Apex model
163      * @param name the context schema name to operate on
164      * @param version the context schema version to operate on
165      * @return the result of the operation
166      */
167     private ApexApiResult deleteContextSchema(final RestSession session, final String name, final String version) {
168         LOGGER.entry(name, version);
169
170         session.editModel();
171
172         ApexApiResult result = session.getApexModelEdited().deleteContextSchema(blank2Null(name), blank2Null(version));
173
174         if (result != null) {
175             session.finishSession(result.isOk());
176             LOGGER.exit("ContextSchema/Delete" + (result.isOk() ? OK : NOT_OK));
177         }
178
179         return result;
180     }
181
182     /**
183      * Validate a context schema.
184      *
185      * @param session the session holding the Apex model
186      * @param name the context schema name to operate on
187      * @param version the context schema version to operate on
188      * @return the result of the operation
189      */
190     private ApexApiResult validateContextSchemas(final RestSession session, final String name, final String version) {
191         LOGGER.entry(name, version);
192
193         ApexApiResult result = session.getApexModel().validateContextSchemas(blank2Null(name), blank2Null(version));
194
195         LOGGER.exit("Validate/ContextSchema" + (result != null && result.isOk() ? OK : NOT_OK));
196         return result;
197     }
198 }