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