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