2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.client.editor.rest.handling;
23 import org.onap.policy.apex.client.editor.rest.handling.bean.BeanContextAlbum;
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.slf4j.ext.XLogger;
28 import org.slf4j.ext.XLoggerFactory;
31 * This class handles commands on context albums in Apex models.
33 public class ContextAlbumHandler implements RestCommandHandler {
34 // Get a reference to the logger
35 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextAlbumHandler.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_ALBUM.equals(commandType)) {
57 return getUnsupportedCommandResultMessage(session, commandType, command);
62 return createContextAlbum(session, jsonString);
64 return updateContextAlbum(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_ALBUM.equals(commandType)) {
77 return getUnsupportedCommandResultMessage(session, commandType, command);
82 return listContextAlbums(session, name, version);
84 return deleteContextAlbum(session, name, version);
86 return validateContextAlbum(session, name, version);
88 return getUnsupportedCommandResultMessage(session, commandType, command);
93 * Creates a context album with the information in the JSON string passed.
95 * @param session the Apex model editing session
96 * @param jsonString the JSON string to be parsed. See {@linkplain BeanContextAlbum}
97 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
98 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
100 private ApexApiResult createContextAlbum(final RestSession session, final String jsonString) {
101 LOGGER.entry(jsonString);
105 final BeanContextAlbum jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextAlbum.class);
107 ApexApiResult result = session.getApexModelEdited().createContextAlbum(jsonbean.getName(),
108 jsonbean.getVersion(), jsonbean.getScope(), Boolean.toString(jsonbean.getWriteable()),
109 jsonbean.getItemSchema().getName(), jsonbean.getItemSchema().getVersion(), jsonbean.getUuid(),
110 jsonbean.getDescription());
112 session.finishSession(result.isOk());
114 LOGGER.exit("ContextAlbum/Create" + (result != null && result.isOk() ? OK : NOT_OK));
119 * Update a context album with the information in the JSON string passed.
121 * @param session the Apex model editing session
122 * @param jsonString the JSON string to be parsed. See {@linkplain BeanContextAlbum}
123 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
124 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
126 private ApexApiResult updateContextAlbum(final RestSession session, final String jsonString) {
127 LOGGER.entry(jsonString);
131 final BeanContextAlbum jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextAlbum.class);
133 ApexApiResult result = session.getApexModelEdited().updateContextAlbum(jsonbean.getName(),
134 jsonbean.getVersion(), jsonbean.getScope(), Boolean.toString(jsonbean.getWriteable()),
135 jsonbean.getItemSchema().getName(), jsonbean.getItemSchema().getVersion(), jsonbean.getUuid(),
136 jsonbean.getDescription());
138 session.finishSession(result.isOk());
140 LOGGER.exit("ContextAlbum/Update" + (result != null && result.isOk() ? OK : NOT_OK));
145 * List context albums with the given key names/versions. If successful the result(s) will be available in the
146 * result messages. The returned value(s) will be similar to {@link AxContextAlbum}, with merged
147 * {@linkplain AxKeyInfo} for the root object.
149 * @param session the Apex model editing session
150 * @param name the name to search for. If null or empty, then all names will be queried
151 * @param version the version to search for. If null then all versions will be searched for.
152 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
153 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
155 private ApexApiResult listContextAlbums(final RestSession session, final String name, final String version) {
156 LOGGER.entry(name, version);
158 ApexApiResult result = session.getApexModel().listContextAlbum(blank2Null(name), blank2Null(version));
160 LOGGER.exit("ContextAlbum/Get" + (result != null && result.isOk() ? OK : NOT_OK));
165 * Delete context albums with the given key names/versions.
167 * @param session the Apex model editing session
168 * @param name the name to search for. If null or empty, then all names will be queried
169 * @param version the version to search for. If null then all versions will be searched for.
170 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
171 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
173 private ApexApiResult deleteContextAlbum(final RestSession session, final String name, final String version) {
174 LOGGER.entry(name, version);
178 ApexApiResult result = session.getApexModelEdited().deleteContextAlbum(blank2Null(name), blank2Null(version));
180 session.finishSession(result.isOk());
182 LOGGER.exit("ContextAlbum/Delete" + (result != null && result.isOk() ? OK : NOT_OK));
187 * Validate context albums with the given key names/versions. The result(s) will be available in the result
190 * @param session the Apex model editing session
191 * @param name the name to search for. If null or empty, then all names will be queried
192 * @param version the version to search for. If null then all versions will be searched for.
193 * @return an ApexAPIResult object. If successful then {@link ApexApiResult#isOk()} will return true. Any
194 * messages/errors can be retrieved using {@link ApexApiResult#getMessages()}
196 private ApexApiResult validateContextAlbum(final RestSession session, final String name, final String version) {
197 LOGGER.entry(name, version);
199 ApexApiResult result = session.getApexModel().validateContextAlbum(blank2Null(name), blank2Null(version));
201 LOGGER.exit("Validate/ContextAlbum" + (result != null && result.isOk() ? OK : NOT_OK));