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;
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;
37 * This class acts as a facade for operations towards a policy model for context album operations.
39 * @author Liam Fallon (liam.fallon@ericsson.com)
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";
47 // Apex model we're working towards
48 private final ApexModel apexModel;
50 // Properties to use for the model
51 private final Properties apexProperties;
53 // Facade classes for working towards the real Apex model
54 private final KeyInformationFacade keyInformationFacade;
56 // JSON output on list/delete if set
57 private final boolean jsonMode;
60 * Constructor that creates a context album facade for the Apex Model API.
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.
68 public ContextAlbumFacade(final ApexModel apexModel, final Properties apexProperties, final boolean jsonMode) {
69 this.apexModel = apexModel;
70 this.apexProperties = apexProperties;
71 this.jsonMode = jsonMode;
73 keyInformationFacade = new KeyInformationFacade(apexModel, apexProperties, jsonMode);
77 * Create a context album.
79 * @param builder the builder for the context album parameters
80 * @return result of the operation
82 // CHECKSTYLE:OFF: checkstyle:parameterNumber
83 public ApexApiResult createContextAlbum(ContextAlbumBuilder builder) {
85 final AxArtifactKey key = new AxArtifactKey();
86 key.setName(builder.getName());
87 if (builder.getVersion() != null) {
88 key.setVersion(builder.getVersion());
90 key.setVersion(apexProperties.getProperty("DEFAULT_CONCEPT_VERSION"));
93 if (apexModel.getPolicyModel().getAlbums().getAlbumsMap().containsKey(key)) {
94 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
95 CONCEPT + key.getId() + " already exists");
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);
105 final AxContextAlbum contextAlbum = new AxContextAlbum(key);
106 contextAlbum.setScope(builder.getScope());
107 contextAlbum.setItemSchema(schema.getKey());
110 .setWritable(builder.getWritable() != null && ("true".equalsIgnoreCase(builder.getWritable().trim())
111 || "t".equalsIgnoreCase(builder.getWritable().trim())));
113 apexModel.getPolicyModel().getAlbums().getAlbumsMap().put(key, contextAlbum);
115 if (apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().containsKey(key)) {
116 return keyInformationFacade.updateKeyInformation(builder.getName(), builder.getVersion(),
117 builder.getUuid(), builder.getDescription());
119 return keyInformationFacade.createKeyInformation(builder.getName(), builder.getVersion(),
120 builder.getUuid(), builder.getDescription());
122 } catch (final Exception e) {
123 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
126 // CHECKSTYLE:ON: checkstyle:parameterNumber
129 * Update a context album.
131 * @param builder the builder for the context album parameters
132 * @return result of the operation
134 // CHECKSTYLE:OFF: checkstyle:parameterNumber
135 public ApexApiResult updateContextAlbum(ContextAlbumBuilder builder) {
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);
144 if (builder.getScope() != null) {
145 contextAlbum.setScope(builder.getScope());
149 .setWritable(builder.getWritable() != null && ("true".equalsIgnoreCase(builder.getWritable().trim())
150 || "t".equalsIgnoreCase(builder.getWritable().trim())));
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()
160 contextAlbum.setItemSchema(schema.getKey());
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);
169 // CHECKSTYLE:ON: checkstyle:parameterNumber
172 * List context albums.
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
178 public ApexApiResult listContextAlbum(final String name, final String version) {
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);
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));
192 } catch (final Exception e) {
193 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
198 * Delete a context album.
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
204 public ApexApiResult deleteContextAlbum(final String name, final String version) {
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();
211 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
212 CONCEPT + key.getId() + DOES_NOT_EXIST);
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);
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);
230 } catch (final Exception e) {
231 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
236 * Validate context albums.
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
242 public ApexApiResult validateContextAlbum(final String name, final String version) {
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);
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());
258 } catch (final Exception e) {
259 return new ApexApiResult(ApexApiResult.Result.FAILED, e);