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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.gui.editors.apex.rest.handling;
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;
30 * This class handles commands on context schemas in Apex models.
32 public class ContextSchemaHandler implements RestCommandHandler {
33 // Get a reference to the logger
34 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextSchemaHandler.class);
36 // Recurring string constants
37 private static final String OK = ": OK";
38 private static final String NOT_OK = ": Not OK";
44 public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
45 final RestCommand command) {
46 return getUnsupportedCommandResultMessage(session, commandType, command);
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);
61 return createContextSchema(session, jsonString);
63 return updateContextSchema(session, jsonString);
65 return getUnsupportedCommandResultMessage(session, commandType, command);
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);
81 return listContextSchemas(session, name, version);
83 return deleteContextSchema(session, name, version);
85 return validateContextSchemas(session, name, version);
87 return getUnsupportedCommandResultMessage(session, commandType, command);
92 * Creates a context schema.
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
98 private ApexApiResult createContextSchema(final RestSession session, final String jsonString) {
99 LOGGER.entry(jsonString);
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());
108 session.finishSession(result.isOk());
110 LOGGER.exit("ContextSchema/create" + (result != null && result.isOk() ? OK : NOT_OK));
115 * Update a context schema.
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
121 private ApexApiResult updateContextSchema(final RestSession session, final String jsonString) {
122 LOGGER.entry(jsonString);
126 final BeanContextSchema jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextSchema.class);
128 ApexApiResult result = session.getApexModelEdited().updateContextSchema(jsonbean.getName(),
129 jsonbean.getVersion(), jsonbean.getSchemaFlavour(), jsonbean.getSchemaDefinition(), jsonbean.getUuid(),
130 jsonbean.getDescription());
132 session.finishSession(result.isOk());
134 LOGGER.exit("ContextSchema/Update" + (result != null && result.isOk() ? OK : NOT_OK));
139 * List context schemas.
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
146 private ApexApiResult listContextSchemas(final RestSession session, final String name, final String version) {
147 LOGGER.entry(name, version);
149 ApexApiResult result = session.getApexModel().listContextSchemas(blank2Null(name), blank2Null(version));
151 LOGGER.exit("ContextSchema/Get" + (result != null && result.isOk() ? OK : NOT_OK));
156 * Delete a context schema.
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
163 private ApexApiResult deleteContextSchema(final RestSession session, final String name, final String version) {
164 LOGGER.entry(name, version);
168 ApexApiResult result = session.getApexModelEdited().deleteContextSchema(blank2Null(name), blank2Null(version));
170 session.finishSession(result.isOk());
172 LOGGER.exit("ContextSchema/Delete" + (result != null && result.isOk() ? OK : NOT_OK));
177 * Validate a context schema.
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
184 private ApexApiResult validateContextSchemas(final RestSession session, final String name, final String version) {
185 LOGGER.entry(name, version);
187 ApexApiResult result = session.getApexModel().validateContextSchemas(blank2Null(name), blank2Null(version));
189 LOGGER.exit("Validate/ContextSchema" + (result != null && result.isOk() ? OK : NOT_OK));