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.apex.client.editor.rest.handling;
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;
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(),
106 jsonbean.getUuid(), jsonbean.getDescription());
108 if (result != null) {
109 session.finishSession(result.isOk());
110 LOGGER.exit("ContextSchema/create" + (result.isOk() ? OK : NOT_OK));
117 * Update a context schema.
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
123 private ApexApiResult updateContextSchema(final RestSession session, final String jsonString) {
124 LOGGER.entry(jsonString);
128 final BeanContextSchema jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextSchema.class);
130 ApexApiResult result = session.getApexModelEdited().updateContextSchema(jsonbean.getName(),
131 jsonbean.getVersion(), jsonbean.getSchemaFlavour(), jsonbean.getSchemaDefinition(),
132 jsonbean.getUuid(), jsonbean.getDescription());
134 if (result != null) {
135 session.finishSession(result.isOk());
136 LOGGER.exit("ContextSchema/Update" + (result.isOk() ? OK : NOT_OK));
143 * List context schemas.
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
150 private ApexApiResult listContextSchemas(final RestSession session, final String name, final String version) {
151 LOGGER.entry(name, version);
153 ApexApiResult result = session.getApexModel().listContextSchemas(blank2Null(name), blank2Null(version));
155 LOGGER.exit("ContextSchema/Get" + (result != null && result.isOk() ? OK : NOT_OK));
160 * Delete a context schema.
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
167 private ApexApiResult deleteContextSchema(final RestSession session, final String name, final String version) {
168 LOGGER.entry(name, version);
172 ApexApiResult result = session.getApexModelEdited().deleteContextSchema(blank2Null(name), blank2Null(version));
174 if (result != null) {
175 session.finishSession(result.isOk());
176 LOGGER.exit("ContextSchema/Delete" + (result.isOk() ? OK : NOT_OK));
183 * Validate a context schema.
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
190 private ApexApiResult validateContextSchemas(final RestSession session, final String name, final String version) {
191 LOGGER.entry(name, version);
193 ApexApiResult result = session.getApexModel().validateContextSchemas(blank2Null(name), blank2Null(version));
195 LOGGER.exit("Validate/ContextSchema" + (result != null && result.isOk() ? OK : NOT_OK));