30886db5ddb20526dfa8864415cadeff88b82831
[policy/apex-pdp.git] /
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.client.editor.rest.handling;
22
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;
29
30 /**
31  * This class handles commands on context albums in Apex models.
32  */
33 public class ContextAlbumHandler implements RestCommandHandler {
34     // Get a reference to the logger
35     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextAlbumHandler.class);
36
37     // Recurring string constants
38     private static final String OK = ": OK";
39     private static final String NOT_OK = ": Not OK";
40
41     /**
42      * {@inheritDoc}
43      */
44     @Override
45     public ApexApiResult executeRestCommand(final RestSession session, final RestCommandType commandType,
46                     final RestCommand command) {
47         return getUnsupportedCommandResultMessage(session, commandType, command);
48     }
49
50     /**
51      * {@inheritDoc}
52      */
53     @Override
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);
58         }
59
60         switch (command) {
61             case CREATE:
62                 return createContextAlbum(session, jsonString);
63             case UPDATE:
64                 return updateContextAlbum(session, jsonString);
65             default:
66                 return getUnsupportedCommandResultMessage(session, commandType, command);
67         }
68     }
69
70     /**
71      * {@inheritDoc}
72      */
73     @Override
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);
78         }
79
80         switch (command) {
81             case LIST:
82                 return listContextAlbums(session, name, version);
83             case DELETE:
84                 return deleteContextAlbum(session, name, version);
85             case VALIDATE:
86                 return validateContextAlbum(session, name, version);
87             default:
88                 return getUnsupportedCommandResultMessage(session, commandType, command);
89         }
90     }
91
92     /**
93      * Creates a context album with the information in the JSON string passed.
94      *
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()}
99      */
100     private ApexApiResult createContextAlbum(final RestSession session, final String jsonString) {
101         LOGGER.entry(jsonString);
102
103         session.editModel();
104
105         final BeanContextAlbum jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextAlbum.class);
106
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());
111
112         session.finishSession(result.isOk());
113
114         LOGGER.exit("ContextAlbum/Create" + (result != null && result.isOk() ? OK : NOT_OK));
115         return result;
116     }
117
118     /**
119      * Update a context album with the information in the JSON string passed.
120      *
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()}
125      */
126     private ApexApiResult updateContextAlbum(final RestSession session, final String jsonString) {
127         LOGGER.entry(jsonString);
128
129         session.editModel();
130
131         final BeanContextAlbum jsonbean = RestUtils.getJsonParameters(jsonString, BeanContextAlbum.class);
132
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());
137
138         session.finishSession(result.isOk());
139
140         LOGGER.exit("ContextAlbum/Update" + (result != null && result.isOk() ? OK : NOT_OK));
141         return result;
142     }
143
144     /**
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.
148      *
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()}
154      */
155     private ApexApiResult listContextAlbums(final RestSession session, final String name, final String version) {
156         LOGGER.entry(name, version);
157
158         ApexApiResult result = session.getApexModel().listContextAlbum(blank2Null(name), blank2Null(version));
159
160         LOGGER.exit("ContextAlbum/Get" + (result != null && result.isOk() ? OK : NOT_OK));
161         return result;
162     }
163
164     /**
165      * Delete context albums with the given key names/versions.
166      *
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()}
172      */
173     private ApexApiResult deleteContextAlbum(final RestSession session, final String name, final String version) {
174         LOGGER.entry(name, version);
175
176         session.editModel();
177
178         ApexApiResult result = session.getApexModelEdited().deleteContextAlbum(blank2Null(name), blank2Null(version));
179
180         session.finishSession(result.isOk());
181
182         LOGGER.exit("ContextAlbum/Delete" + (result != null && result.isOk() ? OK : NOT_OK));
183         return result;
184     }
185
186     /**
187      * Validate context albums with the given key names/versions. The result(s) will be available in the result
188      * messages.
189      *
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()}
195      */
196     private ApexApiResult validateContextAlbum(final RestSession session, final String name, final String version) {
197         LOGGER.entry(name, version);
198
199         ApexApiResult result = session.getApexModel().validateContextAlbum(blank2Null(name), blank2Null(version));
200
201         LOGGER.exit("Validate/ContextAlbum" + (result != null && result.isOk() ? OK : NOT_OK));
202         return result;
203     }
204 }