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.basicmodel.concepts.AxKeyInfo;
25 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
26 import org.onap.policy.apex.model.modelapi.ApexApiResult;
27 import org.onap.policy.gui.editors.apex.rest.handling.bean.BeanContextAlbum;
28 import org.slf4j.ext.XLogger;
29 import org.slf4j.ext.XLoggerFactory;
32 * This class handles commands on context albums in Apex models.
34 public class ContextAlbumHandler implements RestCommandHandler {
35 // Get a reference to the logger
36 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextAlbumHandler.class);
38 // Recurring string constants
39 private static final String OK = ": OK";
40 private static final String NOT_OK = ": Not OK";
46 public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
47 final RestCommand command) {
48 return getUnsupportedCommandResultMessage(session, commandType, command);
55 public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
56 final RestCommand command, final String jsonString) {
57 if (!RestCommandType.CONTEXT_ALBUM.equals(commandType)) {
58 return getUnsupportedCommandResultMessage(session, commandType, command);
63 return createContextAlbum(session, jsonString);
65 return updateContextAlbum(session, jsonString);
67 return getUnsupportedCommandResultMessage(session, commandType, command);
75 public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
76 final RestCommand command, final String name, final String version) {
77 if (!RestCommandType.CONTEXT_ALBUM.equals(commandType)) {
78 return getUnsupportedCommandResultMessage(session, commandType, command);
83 return listContextAlbums(session, name, version);
85 return deleteContextAlbum(session, name, version);
87 return validateContextAlbum(session, name, version);
89 return getUnsupportedCommandResultMessage(session, commandType, command);
94 * Creates a context album with the information in the JSON string passed.
96 * @param session the Apex model editing session
97 * @param jsonString the JSON string to be parsed. See
98 * {@linkplain BeanContextAlbum}
99 * @return an ApexAPIResult object. If successful then
100 * {@link ApexApiResult#isOk()} will return true. Any messages/errors
101 * can be retrieved using {@link ApexApiResult#getMessages()}
103 private ApexApiResult createContextAlbum(final RestSession session, final String jsonString) {
104 LOGGER.entry(jsonString);
108 final BeanContextAlbum jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextAlbum.class);
110 ApexApiResult result = session.getApexModelEdited().createContextAlbum(jsonbean.getName(),
111 jsonbean.getVersion(), jsonbean.getScope(), Boolean.toString(jsonbean.getWriteable()),
112 jsonbean.getItemSchema().getName(), jsonbean.getItemSchema().getVersion(), jsonbean.getUuid(),
113 jsonbean.getDescription());
115 session.finishSession(result.isOk());
117 LOGGER.exit("ContextAlbum/Create" + (result != null && result.isOk() ? OK : NOT_OK));
122 * Update a context album with the information in the JSON string passed.
124 * @param session the Apex model editing session
125 * @param jsonString the JSON string to be parsed. See
126 * {@linkplain BeanContextAlbum}
127 * @return an ApexAPIResult object. If successful then
128 * {@link ApexApiResult#isOk()} will return true. Any messages/errors
129 * can be retrieved using {@link ApexApiResult#getMessages()}
131 private ApexApiResult updateContextAlbum(final RestSession session, final String jsonString) {
132 LOGGER.entry(jsonString);
136 final BeanContextAlbum jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextAlbum.class);
138 ApexApiResult result = session.getApexModelEdited().updateContextAlbum(jsonbean.getName(),
139 jsonbean.getVersion(), jsonbean.getScope(), Boolean.toString(jsonbean.getWriteable()),
140 jsonbean.getItemSchema().getName(), jsonbean.getItemSchema().getVersion(), jsonbean.getUuid(),
141 jsonbean.getDescription());
143 session.finishSession(result.isOk());
145 LOGGER.exit("ContextAlbum/Update" + (result != null && result.isOk() ? OK : NOT_OK));
150 * List context albums with the given key names/versions. If successful the
151 * result(s) will be available in the result messages. The returned value(s)
152 * will be similar to {@link AxContextAlbum}, with merged {@linkplain AxKeyInfo}
153 * for the root object.
155 * @param session the Apex model editing session
156 * @param name the name to search for. If null or empty, then all names will
158 * @param version the version to search for. If null then all versions will be
160 * @return an ApexAPIResult object. If successful then
161 * {@link ApexApiResult#isOk()} will return true. Any messages/errors
162 * can be retrieved using {@link ApexApiResult#getMessages()}
164 private ApexApiResult listContextAlbums(final RestSession session, final String name, final String version) {
165 LOGGER.entry(name, version);
167 ApexApiResult result = session.getApexModel().listContextAlbum(blank2Null(name), blank2Null(version));
169 LOGGER.exit("ContextAlbum/Get" + (result != null && result.isOk() ? OK : NOT_OK));
174 * Delete context albums with the given key names/versions.
176 * @param session the Apex model editing session
177 * @param name the name to search for. If null or empty, then all names will
179 * @param version the version to search for. If null then all versions will be
181 * @return an ApexAPIResult object. If successful then
182 * {@link ApexApiResult#isOk()} will return true. Any messages/errors
183 * can be retrieved using {@link ApexApiResult#getMessages()}
185 private ApexApiResult deleteContextAlbum(final RestSession session, final String name, final String version) {
186 LOGGER.entry(name, version);
190 ApexApiResult result = session.getApexModelEdited().deleteContextAlbum(blank2Null(name), blank2Null(version));
192 session.finishSession(result.isOk());
194 LOGGER.exit("ContextAlbum/Delete" + (result != null && result.isOk() ? OK : NOT_OK));
199 * Validate context albums with the given key names/versions. The result(s) will
200 * be available in the result messages.
202 * @param session the Apex model editing session
203 * @param name the name to search for. If null or empty, then all names will
205 * @param version the version to search for. If null then all versions will be
207 * @return an ApexAPIResult object. If successful then
208 * {@link ApexApiResult#isOk()} will return true. Any messages/errors
209 * can be retrieved using {@link ApexApiResult#getMessages()}
211 private ApexApiResult validateContextAlbum(final RestSession session, final String name, final String version) {
212 LOGGER.entry(name, version);
214 ApexApiResult result = session.getApexModel().validateContextAlbum(blank2Null(name), blank2Null(version));
216 LOGGER.exit("Validate/ContextAlbum" + (result != null && result.isOk() ? OK : NOT_OK));