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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.client.editor.rest.handling;
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;
29 * This class handles commands on context schemas in Apex models.
31 public class ContextSchemaHandler implements RestCommandHandler {
32 // Get a reference to the logger
33 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextSchemaHandler.class);
35 // Recurring string constants
36 private static final String OK = ": OK";
37 private static final String NOT_OK = ": Not OK";
43 public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
44 final RestCommand command) {
45 return getUnsupportedCommandResultMessage(session, commandType, command);
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);
60 return createContextSchema(session, jsonString);
62 return updateContextSchema(session, jsonString);
64 return getUnsupportedCommandResultMessage(session, commandType, command);
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);
80 return listContextSchemas(session, name, version);
82 return deleteContextSchema(session, name, version);
84 return validateContextSchemas(session, name, version);
86 return getUnsupportedCommandResultMessage(session, commandType, command);
91 * Creates a context schema.
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
97 private ApexApiResult createContextSchema(final RestSession session, final String jsonString) {
98 LOGGER.entry(jsonString);
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());
107 session.finishSession(result.isOk());
109 LOGGER.exit("ContextSchema/create" + (result != null && result.isOk() ? OK : NOT_OK));
114 * Update a context schema.
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
120 private ApexApiResult updateContextSchema(final RestSession session, final String jsonString) {
121 LOGGER.entry(jsonString);
125 final BeanContextSchema jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextSchema.class);
127 ApexApiResult result = session.getApexModelEdited().updateContextSchema(jsonbean.getName(),
128 jsonbean.getVersion(), jsonbean.getSchemaFlavour(), jsonbean.getSchemaDefinition(),
129 jsonbean.getUuid(), jsonbean.getDescription());
131 session.finishSession(result.isOk());
133 LOGGER.exit("ContextSchema/Update" + (result != null && result.isOk() ? OK : NOT_OK));
138 * List context schemas.
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
145 private ApexApiResult listContextSchemas(final RestSession session, final String name, final String version) {
146 LOGGER.entry(name, version);
148 ApexApiResult result = session.getApexModel().listContextSchemas(blank2Null(name), blank2Null(version));
150 LOGGER.exit("ContextSchema/Get" + (result != null && result.isOk() ? OK : NOT_OK));
155 * Delete a context schema.
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
162 private ApexApiResult deleteContextSchema(final RestSession session, final String name, final String version) {
163 LOGGER.entry(name, version);
167 ApexApiResult result = session.getApexModelEdited().deleteContextSchema(blank2Null(name), blank2Null(version));
169 session.finishSession(result.isOk());
171 LOGGER.exit("ContextSchema/Delete" + (result != null && result.isOk() ? OK : NOT_OK));
176 * Validate a context schema.
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
183 private ApexApiResult validateContextSchemas(final RestSession session, final String name, final String version) {
184 LOGGER.entry(name, version);
186 ApexApiResult result = session.getApexModel().validateContextSchemas(blank2Null(name), blank2Null(version));
188 LOGGER.exit("Validate/ContextSchema" + (result != null && result.isOk() ? OK : NOT_OK));