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;
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.AxContextSchema;
31 import org.onap.policy.apex.model.modelapi.ApexApiResult;
32 import org.onap.policy.apex.model.modelapi.ApexModel;
33 import org.onap.policy.common.utils.validation.Assertions;
36 * This class acts as a facade for operations towards a policy model for context schema operations.
38 * @author Liam Fallon (liam.fallon@ericsson.com)
40 public class ContextSchemaFacade {
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 private static final String ALREADY_EXISTS = " already exists";
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 to create the context schema facade for the 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 set to false
66 public ContextSchemaFacade(final ApexModel apexModel, final Properties apexProperties, final boolean jsonMode) {
67 this.apexModel = apexModel;
68 this.apexProperties = apexProperties;
69 this.jsonMode = jsonMode;
71 keyInformationFacade = new KeyInformationFacade(apexModel, apexProperties, jsonMode);
75 * Create a context schema.
77 * @param name name of the context schema
78 * @param version version of the context schema, set to null to use the default version
79 * @param schemaFlavour a string identifying the flavour of this context schema
80 * @param schemaDefinition a string containing the definition of this context schema
81 * @param uuid context schema UUID, set to null to generate a UUID
82 * @param description context schema description, set to null to generate a description
83 * @return result of the operation
85 public ApexApiResult createContextSchema(final String name, final String version, final String schemaFlavour,
86 final String schemaDefinition, final String uuid, final String description) {
88 Assertions.argumentNotNull(schemaFlavour, "schemaFlavour may not be null");
90 final AxArtifactKey key = new AxArtifactKey();
92 if (version != null) {
93 key.setVersion(version);
95 key.setVersion(apexProperties.getProperty("DEFAULT_CONCEPT_VERSION"));
98 if (apexModel.getPolicyModel().getSchemas().getSchemasMap().containsKey(key)) {
99 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS, CONCEPT + key.getId() + ALREADY_EXISTS);
102 apexModel.getPolicyModel().getSchemas().getSchemasMap().put(key,
103 new AxContextSchema(key, schemaFlavour, schemaDefinition));
105 if (apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().containsKey(key)) {
106 return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
108 return keyInformationFacade.createKeyInformation(name, version, uuid, description);
110 } catch (final Exception e) {
111 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
116 * Update a context schema.
118 * @param name name of the context schema
119 * @param version version of the context schema, set to null to update the latest version
120 * @param schemaFlavour a string identifying the flavour of this context schema
121 * @param schemaDefinition a string containing the definition of this context schema
122 * @param uuid context schema UUID, set to null to not update
123 * @param description context schema description, set to null to not update
124 * @return result of the operation
126 public ApexApiResult updateContextSchema(final String name, final String version, final String schemaFlavour,
127 final String schemaDefinition, final String uuid, final String description) {
129 final AxContextSchema schema = apexModel.getPolicyModel().getSchemas().get(name, version);
130 if (schema == null) {
131 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
132 CONCEPT + name + ':' + version + DOES_NOT_EXIST);
135 if (schemaFlavour != null) {
136 schema.setSchemaFlavour(schemaFlavour);
139 if (schemaDefinition != null) {
140 schema.setSchema(schemaDefinition);
143 return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
144 } catch (final Exception e) {
145 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
150 * List context schemas.
152 * @param name name of the context schema, set to null to list all
153 * @param version starting version of the context schema, set to null to list all versions
154 * @return result of the operation
156 public ApexApiResult listContextSchemas(final String name, final String version) {
158 final Set<AxContextSchema> schemaSet = apexModel.getPolicyModel().getSchemas().getAll(name, version);
159 if (name != null && schemaSet.isEmpty()) {
160 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
161 CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
164 final ApexApiResult result = new ApexApiResult();
165 for (final AxContextSchema schema : schemaSet) {
166 result.addMessage(new ApexModelStringWriter<AxContextSchema>(false).writeString(schema,
167 AxContextSchema.class, jsonMode));
170 } catch (final Exception e) {
171 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
176 * Delete a context schema.
178 * @param name name of the context schema
179 * @param version version of the context schema, set to null to delete all versions
180 * @return result of the operation
182 public ApexApiResult deleteContextSchema(final String name, final String version) {
184 if (version != null) {
185 final AxArtifactKey key = new AxArtifactKey(name, version);
186 final AxContextSchema removedSchema = apexModel.getPolicyModel().getSchemas().getSchemasMap()
188 if (removedSchema != null) {
189 return new ApexApiResult(ApexApiResult.Result.SUCCESS,
190 new ApexModelStringWriter<AxContextSchema>(false).writeString(removedSchema,
191 AxContextSchema.class, jsonMode));
193 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
194 CONCEPT + key.getId() + DOES_NOT_EXIST);
198 final Set<AxContextSchema> schemaSet = apexModel.getPolicyModel().getSchemas().getAll(name, version);
199 if (schemaSet.isEmpty()) {
200 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
201 CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
204 final ApexApiResult result = new ApexApiResult();
205 for (final AxContextSchema schema : schemaSet) {
206 result.addMessage(new ApexModelStringWriter<AxContextSchema>(false).writeString(schema,
207 AxContextSchema.class, jsonMode));
208 apexModel.getPolicyModel().getSchemas().getSchemasMap().remove(schema.getKey());
209 keyInformationFacade.deleteKeyInformation(name, version);
212 } catch (final Exception e) {
213 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
218 * Validate context schemas.
220 * @param name name of the context schema, set to null to list all
221 * @param version starting version of the context schema, set to null to list all versions
222 * @return result of the operation
224 public ApexApiResult validateContextSchemas(final String name, final String version) {
226 final Set<AxContextSchema> schemaSet = apexModel.getPolicyModel().getSchemas().getAll(name, version);
227 if (schemaSet.isEmpty()) {
228 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
229 CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
232 final ApexApiResult result = new ApexApiResult();
233 for (final AxContextSchema schema : schemaSet) {
234 final AxValidationResult validationResult = schema.validate(new AxValidationResult());
235 result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false).writeString(schema.getKey(),
236 AxArtifactKey.class, jsonMode));
237 result.addMessage(validationResult.toString());
240 } catch (final Exception e) {
241 return new ApexApiResult(ApexApiResult.Result.FAILED, e);