2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.gui.editors.apex.rest.handling;
25 import org.onap.policy.apex.model.modelapi.ApexApiResult;
26 import org.onap.policy.gui.editors.apex.rest.handling.bean.BeanContextSchema;
27 import org.slf4j.ext.XLogger;
28 import org.slf4j.ext.XLoggerFactory;
31 * This class handles commands on context schemas in Apex models.
33 public class ContextSchemaHandler implements RestCommandHandler {
34 // Get a reference to the logger
35 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextSchemaHandler.class);
37 // Recurring string constants
38 private static final String OK = ": OK";
39 private static final String NOT_OK = ": Not OK";
45 public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
46 final RestCommand command) {
47 return getUnsupportedCommandResultMessage(session, commandType, command);
54 public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
55 final RestCommand command, final String jsonString) {
56 if (!RestCommandType.CONTEXT_SCHEMA.equals(commandType)) {
57 return getUnsupportedCommandResultMessage(session, commandType, command);
62 return createContextSchema(session, jsonString);
64 return updateContextSchema(session, jsonString);
66 return getUnsupportedCommandResultMessage(session, commandType, command);
74 public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
75 final RestCommand command, final String name, final String version) {
76 if (!RestCommandType.CONTEXT_SCHEMA.equals(commandType)) {
77 return getUnsupportedCommandResultMessage(session, commandType, command);
82 return listContextSchemas(session, name, version);
84 return deleteContextSchema(session, name, version);
86 return validateContextSchemas(session, name, version);
88 return getUnsupportedCommandResultMessage(session, commandType, command);
93 * Creates a context schema.
95 * @param session the session holding the Apex model
96 * @param jsonString the JSON string with the context schema parameters
97 * @return the result of the operation
99 private ApexApiResult createContextSchema(final RestSession session, final String jsonString) {
100 LOGGER.entry(jsonString);
104 final var jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextSchema.class);
105 ApexApiResult result = session.getApexModelEdited().createContextSchema(jsonbean.getName(),
106 jsonbean.getVersion(), jsonbean.getSchemaFlavour(), jsonbean.getSchemaDefinition(), jsonbean.getUuid(),
107 jsonbean.getDescription());
109 session.finishSession(result.isOk());
111 LOGGER.exit("ContextSchema/create" + (result.isOk() ? OK : NOT_OK));
116 * Update a context schema.
118 * @param session the session holding the Apex model
119 * @param jsonString the JSON string with the context schema parameters
120 * @return the result of the operation
122 private ApexApiResult updateContextSchema(final RestSession session, final String jsonString) {
123 LOGGER.entry(jsonString);
127 final var jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextSchema.class);
129 ApexApiResult result = session.getApexModelEdited().updateContextSchema(jsonbean.getName(),
130 jsonbean.getVersion(), jsonbean.getSchemaFlavour(), jsonbean.getSchemaDefinition(), jsonbean.getUuid(),
131 jsonbean.getDescription());
133 session.finishSession(result.isOk());
135 LOGGER.exit("ContextSchema/Update" + (result.isOk() ? OK : NOT_OK));
140 * List context schemas.
142 * @param session the session holding the Apex model
143 * @param name the context schema name to operate on
144 * @param version the context schema version to operate on
145 * @return the result of the operation
147 private ApexApiResult listContextSchemas(final RestSession session, final String name, final String version) {
148 LOGGER.entry(name, version);
150 ApexApiResult result = session.getApexModel().listContextSchemas(blank2Null(name), blank2Null(version));
152 LOGGER.exit("ContextSchema/Get" + (result != null && result.isOk() ? OK : NOT_OK));
157 * Delete a context schema.
159 * @param session the session holding the Apex model
160 * @param name the context schema name to operate on
161 * @param version the context schema version to operate on
162 * @return the result of the operation
164 private ApexApiResult deleteContextSchema(final RestSession session, final String name, final String version) {
165 LOGGER.entry(name, version);
169 ApexApiResult result = session.getApexModelEdited().deleteContextSchema(blank2Null(name), blank2Null(version));
171 session.finishSession(result.isOk());
173 LOGGER.exit("ContextSchema/Delete" + (result.isOk() ? OK : NOT_OK));
178 * Validate a context schema.
180 * @param session the session holding the Apex model
181 * @param name the context schema name to operate on
182 * @param version the context schema version to operate on
183 * @return the result of the operation
185 private ApexApiResult validateContextSchemas(final RestSession session, final String name, final String version) {
186 LOGGER.entry(name, version);
188 ApexApiResult result = session.getApexModel().validateContextSchemas(blank2Null(name), blank2Null(version));
190 LOGGER.exit("Validate/ContextSchema" + (result != null && result.isOk() ? OK : NOT_OK));