ffe445c06effcb1f55a0e03d734a2916b0fb74f7
[policy/gui.git] /
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.gui.editors.apex.rest.handling;
24
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;
31
32 /**
33  * This class handles commands on context albums in Apex models.
34  */
35 public class ContextAlbumHandler implements RestCommandHandler {
36     // Get a reference to the logger
37     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextAlbumHandler.class);
38
39     // Recurring string constants
40     private static final String OK = ": OK";
41     private static final String NOT_OK = ": Not OK";
42
43     /**
44      * {@inheritDoc}.
45      */
46     @Override
47     public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
48         final RestCommand command) {
49         return getUnsupportedCommandResultMessage(session, commandType, command);
50     }
51
52     /**
53      * {@inheritDoc}.
54      */
55     @Override
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);
60         }
61
62         switch (command) {
63             case CREATE:
64                 return createContextAlbum(session, jsonString);
65             case UPDATE:
66                 return updateContextAlbum(session, jsonString);
67             default:
68                 return getUnsupportedCommandResultMessage(session, commandType, command);
69         }
70     }
71
72     /**
73      * {@inheritDoc}.
74      */
75     @Override
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);
80         }
81
82         switch (command) {
83             case LIST:
84                 return listContextAlbums(session, name, version);
85             case DELETE:
86                 return deleteContextAlbum(session, name, version);
87             case VALIDATE:
88                 return validateContextAlbum(session, name, version);
89             default:
90                 return getUnsupportedCommandResultMessage(session, commandType, command);
91         }
92     }
93
94     /**
95      * Creates a context album with the information in the JSON string passed.
96      *
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()}
103      */
104     private ApexApiResult createContextAlbum(final RestSession session, final String jsonString) {
105         LOGGER.entry(jsonString);
106
107         session.editModel();
108
109         final var jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextAlbum.class);
110
111         ApexApiResult result = session.getApexModelEdited().createContextAlbum(jsonbean.getName(),
112             jsonbean.getVersion(), jsonbean.getScope(), Boolean.toString(jsonbean.getWriteable()),
113             jsonbean.getItemSchema().getName(), jsonbean.getItemSchema().getVersion(), jsonbean.getUuid(),
114             jsonbean.getDescription());
115
116         session.finishSession(result.isOk());
117
118         LOGGER.exit("ContextAlbum/Create" + (result.isOk() ? OK : NOT_OK));
119         return result;
120     }
121
122     /**
123      * Update a context album with the information in the JSON string passed.
124      *
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()}
131      */
132     private ApexApiResult updateContextAlbum(final RestSession session, final String jsonString) {
133         LOGGER.entry(jsonString);
134
135         session.editModel();
136
137         final var jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextAlbum.class);
138
139         ApexApiResult result = session.getApexModelEdited().updateContextAlbum(jsonbean.getName(),
140             jsonbean.getVersion(), jsonbean.getScope(), Boolean.toString(jsonbean.getWriteable()),
141             jsonbean.getItemSchema().getName(), jsonbean.getItemSchema().getVersion(), jsonbean.getUuid(),
142             jsonbean.getDescription());
143
144         session.finishSession(result.isOk());
145
146         LOGGER.exit("ContextAlbum/Update" + (result.isOk() ? OK : NOT_OK));
147         return result;
148     }
149
150     /**
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.
155      *
156      * @param session the Apex model editing session
157      * @param name    the name to search for. If null or empty, then all names will
158      *                be queried
159      * @param version the version to search for. If null then all versions will be
160      *                searched for.
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()}
164      */
165     private ApexApiResult listContextAlbums(final RestSession session, final String name, final String version) {
166         LOGGER.entry(name, version);
167
168         ApexApiResult result = session.getApexModel().listContextAlbum(blank2Null(name), blank2Null(version));
169
170         LOGGER.exit("ContextAlbum/Get" + (result != null && result.isOk() ? OK : NOT_OK));
171         return result;
172     }
173
174     /**
175      * Delete context albums with the given key names/versions.
176      *
177      * @param session the Apex model editing session
178      * @param name    the name to search for. If null or empty, then all names will
179      *                be queried
180      * @param version the version to search for. If null then all versions will be
181      *                searched for.
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()}
185      */
186     private ApexApiResult deleteContextAlbum(final RestSession session, final String name, final String version) {
187         LOGGER.entry(name, version);
188
189         session.editModel();
190
191         ApexApiResult result = session.getApexModelEdited().deleteContextAlbum(blank2Null(name), blank2Null(version));
192
193         session.finishSession(result.isOk());
194
195         LOGGER.exit("ContextAlbum/Delete" + (result.isOk() ? OK : NOT_OK));
196         return result;
197     }
198
199     /**
200      * Validate context albums with the given key names/versions. The result(s) will
201      * be available in the result messages.
202      *
203      * @param session the Apex model editing session
204      * @param name    the name to search for. If null or empty, then all names will
205      *                be queried
206      * @param version the version to search for. If null then all versions will be
207      *                searched for.
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()}
211      */
212     private ApexApiResult validateContextAlbum(final RestSession session, final String name, final String version) {
213         LOGGER.entry(name, version);
214
215         ApexApiResult result = session.getApexModel().validateContextAlbum(blank2Null(name), blank2Null(version));
216
217         LOGGER.exit("Validate/ContextAlbum" + (result != null && result.isOk() ? OK : NOT_OK));
218         return result;
219     }
220 }