2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.modelapi.impl;
24 import java.util.Properties;
26 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
28 import org.onap.policy.apex.model.basicmodel.handling.ApexModelStringWriter;
29 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
30 import org.onap.policy.apex.model.modelapi.ApexApiResult;
31 import org.onap.policy.apex.model.modelapi.ApexModel;
32 import org.onap.policy.common.utils.validation.Assertions;
35 * This class acts as a facade for operations towards a policy model for context schema operations.
37 * @author Liam Fallon (liam.fallon@ericsson.com)
39 public class ContextSchemaFacade {
40 private static final String CONCEPT = "concept ";
41 private static final String CONCEPT_S = "concept(s) ";
42 private static final String DOES_NOT_EXIST = " does not exist";
43 private static final String DO_ES_NOT_EXIST = " do(es) not exist";
44 private static final String ALREADY_EXISTS = " already exists";
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 to create the context schema facade for the 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 set to false
65 public ContextSchemaFacade(final ApexModel apexModel, final Properties apexProperties, final boolean jsonMode) {
66 this.apexModel = apexModel;
67 this.apexProperties = apexProperties;
68 this.jsonMode = jsonMode;
70 keyInformationFacade = new KeyInformationFacade(apexModel, apexProperties, jsonMode);
74 * Create a context schema.
76 * @param name name of the context schema
77 * @param version version of the context schema, set to null to use the default version
78 * @param schemaFlavour a string identifying the flavour of this context schema
79 * @param schemaDefinition a string containing the definition of this context schema
80 * @param uuid context schema UUID, set to null to generate a UUID
81 * @param description context schema description, set to null to generate a description
82 * @return result of the operation
84 public ApexApiResult createContextSchema(final String name, final String version, final String schemaFlavour,
85 final String schemaDefinition, final String uuid, final String description) {
87 Assertions.argumentNotNull(schemaFlavour, "schemaFlavour may not be null");
89 final AxArtifactKey key = new AxArtifactKey();
91 if (version != null) {
92 key.setVersion(version);
94 key.setVersion(apexProperties.getProperty("DEFAULT_CONCEPT_VERSION"));
97 if (apexModel.getPolicyModel().getSchemas().getSchemasMap().containsKey(key)) {
98 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS, CONCEPT + key.getId() + ALREADY_EXISTS);
101 apexModel.getPolicyModel().getSchemas().getSchemasMap().put(key,
102 new AxContextSchema(key, schemaFlavour, schemaDefinition));
104 if (apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().containsKey(key)) {
105 return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
107 return keyInformationFacade.createKeyInformation(name, version, uuid, description);
109 } catch (final Exception e) {
110 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
115 * Update a context schema.
117 * @param name name of the context schema
118 * @param version version of the context schema, set to null to update the latest version
119 * @param schemaFlavour a string identifying the flavour of this context schema
120 * @param schemaDefinition a string containing the definition of this context schema
121 * @param uuid context schema UUID, set to null to not update
122 * @param description context schema description, set to null to not update
123 * @return result of the operation
125 public ApexApiResult updateContextSchema(final String name, final String version, final String schemaFlavour,
126 final String schemaDefinition, final String uuid, final String description) {
128 final AxContextSchema schema = apexModel.getPolicyModel().getSchemas().get(name, version);
129 if (schema == null) {
130 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
131 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
134 if (schemaFlavour != null) {
135 schema.setSchemaFlavour(schemaFlavour);
138 if (schemaDefinition != null) {
139 schema.setSchema(schemaDefinition);
142 return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
143 } catch (final Exception e) {
144 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
149 * List context schemas.
151 * @param name name of the context schema, set to null to list all
152 * @param version starting version of the context schema, set to null to list all versions
153 * @return result of the operation
155 public ApexApiResult listContextSchemas(final String name, final String version) {
157 final Set<AxContextSchema> schemaSet = apexModel.getPolicyModel().getSchemas().getAll(name, version);
158 if (name != null && schemaSet.isEmpty()) {
159 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
160 CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
163 final ApexApiResult result = new ApexApiResult();
164 for (final AxContextSchema schema : schemaSet) {
165 result.addMessage(new ApexModelStringWriter<AxContextSchema>(false).writeString(schema,
166 AxContextSchema.class, jsonMode));
169 } catch (final Exception e) {
170 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
175 * Delete a context schema.
177 * @param name name of the context schema
178 * @param version version of the context schema, set to null to delete all versions
179 * @return result of the operation
181 public ApexApiResult deleteContextSchema(final String name, final String version) {
183 if (version != null) {
184 final AxArtifactKey key = new AxArtifactKey(name, version);
185 final AxContextSchema removedSchema = apexModel.getPolicyModel().getSchemas().getSchemasMap()
187 if (removedSchema != null) {
188 return new ApexApiResult(ApexApiResult.Result.SUCCESS,
189 new ApexModelStringWriter<AxContextSchema>(false).writeString(removedSchema,
190 AxContextSchema.class, jsonMode));
192 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
193 CONCEPT + key.getId() + DOES_NOT_EXIST);
197 final Set<AxContextSchema> schemaSet = apexModel.getPolicyModel().getSchemas().getAll(name, version);
198 if (schemaSet.isEmpty()) {
199 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
200 CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
203 final ApexApiResult result = new ApexApiResult();
204 for (final AxContextSchema schema : schemaSet) {
205 result.addMessage(new ApexModelStringWriter<AxContextSchema>(false).writeString(schema,
206 AxContextSchema.class, jsonMode));
207 apexModel.getPolicyModel().getSchemas().getSchemasMap().remove(schema.getKey());
208 keyInformationFacade.deleteKeyInformation(name, version);
211 } catch (final Exception e) {
212 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
217 * Validate context schemas.
219 * @param name name of the context schema, set to null to list all
220 * @param version starting version of the context schema, set to null to list all versions
221 * @return result of the operation
223 public ApexApiResult validateContextSchemas(final String name, final String version) {
225 final Set<AxContextSchema> schemaSet = apexModel.getPolicyModel().getSchemas().getAll(name, version);
226 if (schemaSet.isEmpty()) {
227 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
228 CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
231 final ApexApiResult result = new ApexApiResult();
232 for (final AxContextSchema schema : schemaSet) {
233 final AxValidationResult validationResult = schema.validate(new AxValidationResult());
234 result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false).writeString(schema.getKey(),
235 AxArtifactKey.class, jsonMode));
236 result.addMessage(validationResult.toString());
239 } catch (final Exception e) {
240 return new ApexApiResult(ApexApiResult.Result.FAILED, e);