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.basicmodel.concepts.AxKeyInfo;
26 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
27 import org.onap.policy.apex.model.modelapi.ApexApiResult;
28 import org.onap.policy.gui.editors.apex.rest.handling.bean.BeanContextAlbum;
29 import org.slf4j.ext.XLogger;
30 import org.slf4j.ext.XLoggerFactory;
33 * This class handles commands on context albums in Apex models.
35 public class ContextAlbumHandler implements RestCommandHandler {
36 // Get a reference to the logger
37 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextAlbumHandler.class);
39 // Recurring string constants
40 private static final String OK = ": OK";
41 private static final String NOT_OK = ": Not OK";
47 public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
48 final RestCommand command) {
49 return getUnsupportedCommandResultMessage(session, commandType, command);
56 public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
57 final RestCommand command, final String jsonString) {
58 if (!RestCommandType.CONTEXT_ALBUM.equals(commandType)) {
59 return getUnsupportedCommandResultMessage(session, commandType, command);
64 return createContextAlbum(session, jsonString);
66 return updateContextAlbum(session, jsonString);
68 return getUnsupportedCommandResultMessage(session, commandType, command);
76 public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
77 final RestCommand command, final String name, final String version) {
78 if (!RestCommandType.CONTEXT_ALBUM.equals(commandType)) {
79 return getUnsupportedCommandResultMessage(session, commandType, command);
84 return listContextAlbums(session, name, version);
86 return deleteContextAlbum(session, name, version);
88 return validateContextAlbum(session, name, version);
90 return getUnsupportedCommandResultMessage(session, commandType, command);
95 * Creates a context album with the information in the JSON string passed.
97 * @param session the Apex model editing session
98 * @param jsonString the JSON string to be parsed. See
99 * {@linkplain BeanContextAlbum}
100 * @return an ApexAPIResult object. If successful then
101 * {@link ApexApiResult#isOk()} will return true. Any messages/errors
102 * can be retrieved using {@link ApexApiResult#getMessages()}
104 private ApexApiResult createContextAlbum(final RestSession session, final String jsonString) {
105 LOGGER.entry(jsonString);
109 final var jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextAlbum.class);
111 ApexApiResult result = session.getApexModelEdited().createContextAlbum(jsonbean.getName(),
112 jsonbean.getVersion(), jsonbean.getScope(), Boolean.toString(jsonbean.isWriteable()),
113 jsonbean.getItemSchema().getName(), jsonbean.getItemSchema().getVersion(), jsonbean.getUuid(),
114 jsonbean.getDescription());
116 session.finishSession(result.isOk());
118 LOGGER.exit("ContextAlbum/Create" + (result.isOk() ? OK : NOT_OK));
123 * Update a context album with the information in the JSON string passed.
125 * @param session the Apex model editing session
126 * @param jsonString the JSON string to be parsed. See
127 * {@linkplain BeanContextAlbum}
128 * @return an ApexAPIResult object. If successful then
129 * {@link ApexApiResult#isOk()} will return true. Any messages/errors
130 * can be retrieved using {@link ApexApiResult#getMessages()}
132 private ApexApiResult updateContextAlbum(final RestSession session, final String jsonString) {
133 LOGGER.entry(jsonString);
137 final var jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextAlbum.class);
139 ApexApiResult result = session.getApexModelEdited().updateContextAlbum(jsonbean.getName(),
140 jsonbean.getVersion(), jsonbean.getScope(), Boolean.toString(jsonbean.isWriteable()),
141 jsonbean.getItemSchema().getName(), jsonbean.getItemSchema().getVersion(), jsonbean.getUuid(),
142 jsonbean.getDescription());
144 session.finishSession(result.isOk());
146 LOGGER.exit("ContextAlbum/Update" + (result.isOk() ? OK : NOT_OK));
151 * List context albums with the given key names/versions. If successful the
152 * result(s) will be available in the result messages. The returned value(s)
153 * will be similar to {@link AxContextAlbum}, with merged {@linkplain AxKeyInfo}
154 * for the root object.
156 * @param session the Apex model editing session
157 * @param name the name to search for. If null or empty, then all names will
159 * @param version the version to search for. If null then all versions will be
161 * @return an ApexAPIResult object. If successful then
162 * {@link ApexApiResult#isOk()} will return true. Any messages/errors
163 * can be retrieved using {@link ApexApiResult#getMessages()}
165 private ApexApiResult listContextAlbums(final RestSession session, final String name, final String version) {
166 LOGGER.entry(name, version);
168 ApexApiResult result = session.getApexModel().listContextAlbum(blank2Null(name), blank2Null(version));
170 LOGGER.exit("ContextAlbum/Get" + (result != null && result.isOk() ? OK : NOT_OK));
175 * Delete context albums with the given key names/versions.
177 * @param session the Apex model editing session
178 * @param name the name to search for. If null or empty, then all names will
180 * @param version the version to search for. If null then all versions will be
182 * @return an ApexAPIResult object. If successful then
183 * {@link ApexApiResult#isOk()} will return true. Any messages/errors
184 * can be retrieved using {@link ApexApiResult#getMessages()}
186 private ApexApiResult deleteContextAlbum(final RestSession session, final String name, final String version) {
187 LOGGER.entry(name, version);
191 ApexApiResult result = session.getApexModelEdited().deleteContextAlbum(blank2Null(name), blank2Null(version));
193 session.finishSession(result.isOk());
195 LOGGER.exit("ContextAlbum/Delete" + (result.isOk() ? OK : NOT_OK));
200 * Validate context albums with the given key names/versions. The result(s) will
201 * be available in the result messages.
203 * @param session the Apex model editing session
204 * @param name the name to search for. If null or empty, then all names will
206 * @param version the version to search for. If null then all versions will be
208 * @return an ApexAPIResult object. If successful then
209 * {@link ApexApiResult#isOk()} will return true. Any messages/errors
210 * can be retrieved using {@link ApexApiResult#getMessages()}
212 private ApexApiResult validateContextAlbum(final RestSession session, final String name, final String version) {
213 LOGGER.entry(name, version);
215 ApexApiResult result = session.getApexModel().validateContextAlbum(blank2Null(name), blank2Null(version));
217 LOGGER.exit("Validate/ContextAlbum" + (result != null && result.isOk() ? OK : NOT_OK));