dfc29b30265eb31829bea389165a12ce210b19b5
[policy/apex-pdp.git] / model / model-api / src / main / java / org / onap / policy / apex / model / modelapi / impl / ContextAlbumFacade.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 Samsung Electronics Co., Ltd.
5  *  Modifications Copyright (C) 2019 Nordix Foundation.
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.apex.model.modelapi.impl;
24
25 import java.util.Properties;
26 import java.util.Set;
27
28 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
29 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
30 import org.onap.policy.apex.model.basicmodel.handling.ApexModelStringWriter;
31 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
32 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
33 import org.onap.policy.apex.model.modelapi.ApexApiResult;
34 import org.onap.policy.apex.model.modelapi.ApexModel;
35
36 /**
37  * This class acts as a facade for operations towards a policy model for context album operations.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 public class ContextAlbumFacade {
42     private static final String CONCEPT = "concept ";
43     private static final String CONCEPT_S = "concept(s) ";
44     private static final String DOES_NOT_EXIST = " does not exist";
45     private static final String DO_ES_NOT_EXIST = " do(es) not exist";
46
47     // Apex model we're working towards
48     private final ApexModel apexModel;
49
50     // Properties to use for the model
51     private final Properties apexProperties;
52
53     // Facade classes for working towards the real Apex model
54     private final KeyInformationFacade keyInformationFacade;
55
56     // JSON output on list/delete if set
57     private final boolean jsonMode;
58
59     /**
60      * Constructor that creates a context album facade for the Apex Model API.
61      *
62      * @param apexModel the apex model
63      * @param apexProperties Properties for the model
64      * @param jsonMode set to true to return JSON strings in list and delete operations, otherwise
65      *        set to false *  Modifications Copyright (C) 2019 Nordix Foundation.
66
67      */
68     public ContextAlbumFacade(final ApexModel apexModel, final Properties apexProperties, final boolean jsonMode) {
69         this.apexModel = apexModel;
70         this.apexProperties = apexProperties;
71         this.jsonMode = jsonMode;
72
73         keyInformationFacade = new KeyInformationFacade(apexModel, apexProperties, jsonMode);
74     }
75
76     /**
77      * Create a context album.
78      *
79      * @param builder the builder for the context album parameters
80      * @return result of the operation
81      */
82     // CHECKSTYLE:OFF: checkstyle:parameterNumber
83     public ApexApiResult createContextAlbum(ContextAlbumBuilder builder) {
84         try {
85             final AxArtifactKey key = new AxArtifactKey();
86             key.setName(builder.getName());
87             if (builder.getVersion() != null) {
88                 key.setVersion(builder.getVersion());
89             } else {
90                 key.setVersion(apexProperties.getProperty("DEFAULT_CONCEPT_VERSION"));
91             }
92
93             if (apexModel.getPolicyModel().getAlbums().getAlbumsMap().containsKey(key)) {
94                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
95                         CONCEPT + key.getId() + " already exists");
96             }
97
98             final AxContextSchema schema = apexModel.getPolicyModel().getSchemas().get(builder.getContextSchemaName(),
99                     builder.getContextSchemaVersion());
100             if (schema == null) {
101                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, CONCEPT
102                         + builder.getContextSchemaName() + ':' + builder.getContextSchemaVersion() + DOES_NOT_EXIST);
103             }
104
105             final AxContextAlbum contextAlbum = new AxContextAlbum(key);
106             contextAlbum.setScope(builder.getScope());
107             contextAlbum.setItemSchema(schema.getKey());
108
109             contextAlbum
110                     .setWritable(builder.getWritable() != null && ("true".equalsIgnoreCase(builder.getWritable().trim())
111                             || "t".equalsIgnoreCase(builder.getWritable().trim())));
112
113             apexModel.getPolicyModel().getAlbums().getAlbumsMap().put(key, contextAlbum);
114
115             if (apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().containsKey(key)) {
116                 return keyInformationFacade.updateKeyInformation(builder.getName(), builder.getVersion(),
117                         builder.getUuid(), builder.getDescription());
118             } else {
119                 return keyInformationFacade.createKeyInformation(builder.getName(), builder.getVersion(),
120                         builder.getUuid(), builder.getDescription());
121             }
122         } catch (final Exception e) {
123             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
124         }
125     }
126     // CHECKSTYLE:ON: checkstyle:parameterNumber
127
128     /**
129      * Update a context album.
130      *
131      * @param builder the builder for the context album parameters
132      * @return result of the operation
133      */
134     // CHECKSTYLE:OFF: checkstyle:parameterNumber
135     public ApexApiResult updateContextAlbum(ContextAlbumBuilder builder) {
136         try {
137             final AxContextAlbum contextAlbum =
138                     apexModel.getPolicyModel().getAlbums().get(builder.getName(), builder.getVersion());
139             if (contextAlbum == null) {
140                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
141                         CONCEPT + builder.getName() + ':' + builder.getVersion() + DOES_NOT_EXIST);
142             }
143
144             if (builder.getScope() != null) {
145                 contextAlbum.setScope(builder.getScope());
146             }
147
148             contextAlbum
149                     .setWritable(builder.getWritable() != null && ("true".equalsIgnoreCase(builder.getWritable().trim())
150                             || "t".equalsIgnoreCase(builder.getWritable().trim())));
151
152             if (builder.getContextSchemaName() != null) {
153                 final AxContextSchema schema = apexModel.getPolicyModel().getSchemas()
154                         .get(builder.getContextSchemaName(), builder.getContextSchemaVersion());
155                 if (schema == null) {
156                     return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
157                             CONCEPT + builder.getContextSchemaName() + ':' + builder.getContextSchemaVersion()
158                                     + DOES_NOT_EXIST);
159                 }
160                 contextAlbum.setItemSchema(schema.getKey());
161             }
162
163             return keyInformationFacade.updateKeyInformation(builder.getName(), builder.getVersion(), builder.getUuid(),
164                     builder.getDescription());
165         } catch (final Exception e) {
166             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
167         }
168     }
169     // CHECKSTYLE:ON: checkstyle:parameterNumber
170
171     /**
172      * List context albums.
173      *
174      * @param name name of the context album, set to null to list all
175      * @param version starting version of the context album, set to null to list all versions
176      * @return result of the operation
177      */
178     public ApexApiResult listContextAlbum(final String name, final String version) {
179         try {
180             final Set<AxContextAlbum> contextAlbumSet = apexModel.getPolicyModel().getAlbums().getAll(name, version);
181             if (name != null && contextAlbumSet.isEmpty()) {
182                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
183                         CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
184             }
185
186             final ApexApiResult result = new ApexApiResult();
187             for (final AxContextAlbum contextAlbum : contextAlbumSet) {
188                 result.addMessage(new ApexModelStringWriter<AxContextAlbum>(false).writeString(contextAlbum,
189                         AxContextAlbum.class, jsonMode));
190             }
191             return result;
192         } catch (final Exception e) {
193             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
194         }
195     }
196
197     /**
198      * Delete a context album.
199      *
200      * @param name name of the context album
201      * @param version version of the context album, set to null to delete versions
202      * @return result of the operation
203      */
204     public ApexApiResult deleteContextAlbum(final String name, final String version) {
205         try {
206             if (version != null) {
207                 final AxArtifactKey key = new AxArtifactKey(name, version);
208                 if (apexModel.getPolicyModel().getAlbums().getAlbumsMap().remove(key) != null) {
209                     return new ApexApiResult();
210                 } else {
211                     return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
212                             CONCEPT + key.getId() + DOES_NOT_EXIST);
213                 }
214             }
215
216             final Set<AxContextAlbum> contextAlbumSet = apexModel.getPolicyModel().getAlbums().getAll(name, version);
217             if (contextAlbumSet.isEmpty()) {
218                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
219                         CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
220             }
221
222             final ApexApiResult result = new ApexApiResult();
223             for (final AxContextAlbum contextAlbum : contextAlbumSet) {
224                 result.addMessage(new ApexModelStringWriter<AxContextAlbum>(false).writeString(contextAlbum,
225                         AxContextAlbum.class, jsonMode));
226                 apexModel.getPolicyModel().getAlbums().getAlbumsMap().remove(contextAlbum.getKey());
227                 keyInformationFacade.deleteKeyInformation(name, version);
228             }
229             return result;
230         } catch (final Exception e) {
231             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
232         }
233     }
234
235     /**
236      * Validate context albums.
237      *
238      * @param name name of the context album, set to null to list all
239      * @param version starting version of the context album, set to null to list all versions
240      * @return result of the operation
241      */
242     public ApexApiResult validateContextAlbum(final String name, final String version) {
243         try {
244             final Set<AxContextAlbum> contextAlbumSet = apexModel.getPolicyModel().getAlbums().getAll(name, version);
245             if (contextAlbumSet.isEmpty()) {
246                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
247                         CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
248             }
249
250             final ApexApiResult result = new ApexApiResult();
251             for (final AxContextAlbum contextAlbum : contextAlbumSet) {
252                 final AxValidationResult validationResult = contextAlbum.validate(new AxValidationResult());
253                 result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false).writeString(contextAlbum.getKey(),
254                         AxArtifactKey.class, jsonMode));
255                 result.addMessage(validationResult.toString());
256             }
257             return result;
258         } catch (final Exception e) {
259             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
260         }
261     }
262 }