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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.model.modelapi.impl;
25 import java.util.Properties;
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;
36 * This class acts as a facade for operations towards a policy model for context album operations.
38 * @author Liam Fallon (liam.fallon@ericsson.com)
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";
46 // Apex model we're working towards
47 private final ApexModel apexModel;
49 // Properties to use for the model
50 private final Properties apexProperties;
52 // Facade classes for working towards the real Apex model
53 private final KeyInformationFacade keyInformationFacade;
55 // JSON output on list/delete if set
56 private final boolean jsonMode;
59 * Constructor that creates a context album facade for the Apex Model API.
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.
67 public ContextAlbumFacade(final ApexModel apexModel, final Properties apexProperties, final boolean jsonMode) {
68 this.apexModel = apexModel;
69 this.apexProperties = apexProperties;
70 this.jsonMode = jsonMode;
72 keyInformationFacade = new KeyInformationFacade(apexModel, apexProperties, jsonMode);
76 * Create a context album.
78 * @param builder the builder for the context album parameters
79 * @return result of the operation
81 // CHECKSTYLE:OFF: checkstyle:parameterNumber
82 public ApexApiResult createContextAlbum(ContextAlbumBuilder builder) {
84 final AxArtifactKey key = new AxArtifactKey();
85 key.setName(builder.getName());
86 if (builder.getVersion() != null) {
87 key.setVersion(builder.getVersion());
89 key.setVersion(apexProperties.getProperty("DEFAULT_CONCEPT_VERSION"));
92 if (apexModel.getPolicyModel().getAlbums().getAlbumsMap().containsKey(key)) {
93 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
94 CONCEPT + key.getId() + " already exists");
97 final AxContextSchema schema = apexModel.getPolicyModel().getSchemas().get(builder.getContextSchemaName(),
98 builder.getContextSchemaVersion());
100 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, CONCEPT
101 + builder.getContextSchemaName() + ':' + builder.getContextSchemaVersion() + DOES_NOT_EXIST);
104 final AxContextAlbum contextAlbum = new AxContextAlbum(key);
105 contextAlbum.setScope(builder.getScope());
106 contextAlbum.setItemSchema(schema.getKey());
109 .setWritable(builder.getWritable() != null && ("true".equalsIgnoreCase(builder.getWritable().trim())
110 || "t".equalsIgnoreCase(builder.getWritable().trim())));
112 apexModel.getPolicyModel().getAlbums().getAlbumsMap().put(key, contextAlbum);
114 if (apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().containsKey(key)) {
115 return keyInformationFacade.updateKeyInformation(builder.getName(), builder.getVersion(),
116 builder.getUuid(), builder.getDescription());
118 return keyInformationFacade.createKeyInformation(builder.getName(), builder.getVersion(),
119 builder.getUuid(), builder.getDescription());
121 } catch (final Exception e) {
122 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
125 // CHECKSTYLE:ON: checkstyle:parameterNumber
128 * Update a context album.
130 * @param builder the builder for the context album parameters
131 * @return result of the operation
133 // CHECKSTYLE:OFF: checkstyle:parameterNumber
134 public ApexApiResult updateContextAlbum(ContextAlbumBuilder builder) {
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);
143 if (builder.getScope() != null) {
144 contextAlbum.setScope(builder.getScope());
148 .setWritable(builder.getWritable() != null && ("true".equalsIgnoreCase(builder.getWritable().trim())
149 || "t".equalsIgnoreCase(builder.getWritable().trim())));
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()
159 contextAlbum.setItemSchema(schema.getKey());
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);
168 // CHECKSTYLE:ON: checkstyle:parameterNumber
171 * List context albums.
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
177 public ApexApiResult listContextAlbum(final String name, final String version) {
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);
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));
191 } catch (final Exception e) {
192 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
197 * Delete a context album.
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
203 public ApexApiResult deleteContextAlbum(final String name, final String version) {
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();
210 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
211 CONCEPT + key.getId() + DOES_NOT_EXIST);
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);
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);
229 } catch (final Exception e) {
230 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
235 * Validate context albums.
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
241 public ApexApiResult validateContextAlbum(final String name, final String version) {
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);
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());
257 } catch (final Exception e) {
258 return new ApexApiResult(ApexApiResult.Result.FAILED, e);