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.BeanContextAlbum;
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.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 {@linkplain BeanContextAlbum}
98 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
99 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
101 private ApexApiResult createContextAlbum(final RestSession session, final String jsonString) {
102 LOGGER.entry(jsonString);
106 final BeanContextAlbum jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextAlbum.class);
108 ApexApiResult result = session.getApexModelEdited().createContextAlbum(jsonbean.getName(),
109 jsonbean.getVersion(), jsonbean.getScope(), Boolean.toString(jsonbean.getWriteable()),
110 jsonbean.getItemSchema().getName(), jsonbean.getItemSchema().getVersion(), jsonbean.getUuid(),
111 jsonbean.getDescription());
113 if (result != null) {
114 session.finishSession(result.isOk());
115 LOGGER.exit("ContextAlbum/Create" + (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 {@linkplain BeanContextAlbum}
126 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
127 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
129 private ApexApiResult updateContextAlbum(final RestSession session, final String jsonString) {
130 LOGGER.entry(jsonString);
134 final BeanContextAlbum jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextAlbum.class);
136 ApexApiResult result = session.getApexModelEdited().updateContextAlbum(jsonbean.getName(),
137 jsonbean.getVersion(), jsonbean.getScope(), Boolean.toString(jsonbean.getWriteable()),
138 jsonbean.getItemSchema().getName(), jsonbean.getItemSchema().getVersion(), jsonbean.getUuid(),
139 jsonbean.getDescription());
141 if (result != null) {
142 session.finishSession(result.isOk());
143 LOGGER.exit("ContextAlbum/Update" + (result.isOk() ? OK : NOT_OK));
150 * List context albums with the given key names/versions. If successful the result(s) will be available in the
151 * result messages. The returned value(s) will be similar to {@link AxContextAlbum}, with merged
152 * {@linkplain AxKeyInfo} for the root object.
154 * @param session the Apex model editing session
155 * @param name the name to search for. If null or empty, then all names will be queried
156 * @param version the version to search for. If null then all versions will be searched for.
157 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
158 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
160 private ApexApiResult listContextAlbums(final RestSession session, final String name, final String version) {
161 LOGGER.entry(name, version);
163 ApexApiResult result = session.getApexModel().listContextAlbum(blank2Null(name), blank2Null(version));
165 LOGGER.exit("ContextAlbum/Get" + (result != null && result.isOk() ? OK : NOT_OK));
170 * Delete context albums with the given key names/versions.
172 * @param session the Apex model editing session
173 * @param name the name to search for. If null or empty, then all names will be queried
174 * @param version the version to search for. If null then all versions will be searched for.
175 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
176 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
178 private ApexApiResult deleteContextAlbum(final RestSession session, final String name, final String version) {
179 LOGGER.entry(name, version);
183 ApexApiResult result = session.getApexModelEdited().deleteContextAlbum(blank2Null(name), blank2Null(version));
185 if (result != null) {
186 session.finishSession(result.isOk());
187 LOGGER.exit("ContextAlbum/Delete" + (result.isOk() ? OK : NOT_OK));
194 * Validate context albums with the given key names/versions. The result(s) will be available in the result
197 * @param session the Apex model editing session
198 * @param name the name to search for. If null or empty, then all names will be queried
199 * @param version the version to search for. If null then all versions will be searched for.
200 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
201 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
203 private ApexApiResult validateContextAlbum(final RestSession session, final String name, final String version) {
204 LOGGER.entry(name, version);
206 ApexApiResult result = session.getApexModel().validateContextAlbum(blank2Null(name), blank2Null(version));
208 LOGGER.exit("Validate/ContextAlbum" + (result != null && result.isOk() ? OK : NOT_OK));