40c147e7a35b3b0d365cccb4921cae3488922b1d
[policy/apex-pdp.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.apex.client.editor.rest.handling;
23
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;
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 {@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()}
100      */
101     private ApexApiResult createContextAlbum(final RestSession session, final String jsonString) {
102         LOGGER.entry(jsonString);
103
104         session.editModel();
105
106         final BeanContextAlbum jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextAlbum.class);
107
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());
112
113         if (result != null) {
114             session.finishSession(result.isOk());
115             LOGGER.exit("ContextAlbum/Create" + (result.isOk() ? OK : NOT_OK));
116         }
117
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 {@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()}
128      */
129     private ApexApiResult updateContextAlbum(final RestSession session, final String jsonString) {
130         LOGGER.entry(jsonString);
131
132         session.editModel();
133
134         final BeanContextAlbum jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextAlbum.class);
135
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());
140
141         if (result != null) {
142             session.finishSession(result.isOk());
143             LOGGER.exit("ContextAlbum/Update" + (result.isOk() ? OK : NOT_OK));
144         }
145
146         return result;
147     }
148
149     /**
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.
153      *
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()}
159      */
160     private ApexApiResult listContextAlbums(final RestSession session, final String name, final String version) {
161         LOGGER.entry(name, version);
162
163         ApexApiResult result = session.getApexModel().listContextAlbum(blank2Null(name), blank2Null(version));
164
165         LOGGER.exit("ContextAlbum/Get" + (result != null && result.isOk() ? OK : NOT_OK));
166         return result;
167     }
168
169     /**
170      * Delete context albums with the given key names/versions.
171      *
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()}
177      */
178     private ApexApiResult deleteContextAlbum(final RestSession session, final String name, final String version) {
179         LOGGER.entry(name, version);
180
181         session.editModel();
182
183         ApexApiResult result = session.getApexModelEdited().deleteContextAlbum(blank2Null(name), blank2Null(version));
184
185         if (result != null) {
186             session.finishSession(result.isOk());
187             LOGGER.exit("ContextAlbum/Delete" + (result.isOk() ? OK : NOT_OK));
188         }
189
190         return result;
191     }
192
193     /**
194      * Validate context albums with the given key names/versions. The result(s) will be available in the result
195      * messages.
196      *
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()}
202      */
203     private ApexApiResult validateContextAlbum(final RestSession session, final String name, final String version) {
204         LOGGER.entry(name, version);
205
206         ApexApiResult result = session.getApexModel().validateContextAlbum(blank2Null(name), blank2Null(version));
207
208         LOGGER.exit("Validate/ContextAlbum" + (result != null && result.isOk() ? OK : NOT_OK));
209         return result;
210     }
211 }