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