8b479ebfdbb3aae0cdb38a597789707147be58a8
[policy/apex-pdp.git] / model / model-api / src / main / java / org / onap / policy / apex / model / modelapi / impl / ContextSchemaFacade.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.modelapi.impl;
23
24 import java.util.Properties;
25 import java.util.Set;
26
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;
34
35 /**
36  * This class acts as a facade for operations towards a policy model for context schema operations.
37  *
38  * @author Liam Fallon (liam.fallon@ericsson.com)
39  */
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";
46
47     // Apex model we're working towards
48     private final ApexModel apexModel;
49
50     // Properties to use for the model
51     private final Properties apexProperties;
52
53     // Facade classes for working towards the real Apex model
54     private final KeyInformationFacade keyInformationFacade;
55
56     // JSON output on list/delete if set
57     private final boolean jsonMode;
58
59     /**
60      * Constructor to create the context schema facade for the Model API.
61      *
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
65      */
66     public ContextSchemaFacade(final ApexModel apexModel, final Properties apexProperties, final boolean jsonMode) {
67         this.apexModel = apexModel;
68         this.apexProperties = apexProperties;
69         this.jsonMode = jsonMode;
70
71         keyInformationFacade = new KeyInformationFacade(apexModel, apexProperties, jsonMode);
72     }
73
74     /**
75      * Create a context schema.
76      *
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
84      */
85     public ApexApiResult createContextSchema(final String name, final String version, final String schemaFlavour,
86                     final String schemaDefinition, final String uuid, final String description) {
87         try {
88             Assertions.argumentNotNull(schemaFlavour, "schemaFlavour may not be null");
89
90             final AxArtifactKey key = new AxArtifactKey();
91             key.setName(name);
92             if (version != null) {
93                 key.setVersion(version);
94             } else {
95                 key.setVersion(apexProperties.getProperty("DEFAULT_CONCEPT_VERSION"));
96             }
97
98             if (apexModel.getPolicyModel().getSchemas().getSchemasMap().containsKey(key)) {
99                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS, CONCEPT + key.getId() + ALREADY_EXISTS);
100             }
101
102             apexModel.getPolicyModel().getSchemas().getSchemasMap().put(key,
103                             new AxContextSchema(key, schemaFlavour, schemaDefinition));
104
105             if (apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().containsKey(key)) {
106                 return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
107             } else {
108                 return keyInformationFacade.createKeyInformation(name, version, uuid, description);
109             }
110         } catch (final Exception e) {
111             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
112         }
113     }
114
115     /**
116      * Update a context schema.
117      *
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
125      */
126     public ApexApiResult updateContextSchema(final String name, final String version, final String schemaFlavour,
127                     final String schemaDefinition, final String uuid, final String description) {
128         try {
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);
133             }
134
135             if (schemaFlavour != null) {
136                 schema.setSchemaFlavour(schemaFlavour);
137             }
138
139             if (schemaDefinition != null) {
140                 schema.setSchema(schemaDefinition);
141             }
142
143             return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
144         } catch (final Exception e) {
145             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
146         }
147     }
148
149     /**
150      * List context schemas.
151      *
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
155      */
156     public ApexApiResult listContextSchemas(final String name, final String version) {
157         try {
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);
162             }
163
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));
168             }
169             return result;
170         } catch (final Exception e) {
171             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
172         }
173     }
174
175     /**
176      * Delete a context schema.
177      *
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
181      */
182     public ApexApiResult deleteContextSchema(final String name, final String version) {
183         try {
184             if (version != null) {
185                 final AxArtifactKey key = new AxArtifactKey(name, version);
186                 final AxContextSchema removedSchema = apexModel.getPolicyModel().getSchemas().getSchemasMap()
187                                 .remove(key);
188                 if (removedSchema != null) {
189                     return new ApexApiResult(ApexApiResult.Result.SUCCESS,
190                                     new ApexModelStringWriter<AxContextSchema>(false).writeString(removedSchema,
191                                                     AxContextSchema.class, jsonMode));
192                 } else {
193                     return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
194                                     CONCEPT + key.getId() + DOES_NOT_EXIST);
195                 }
196             }
197
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);
202             }
203
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);
210             }
211             return result;
212         } catch (final Exception e) {
213             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
214         }
215     }
216
217     /**
218      * Validate context schemas.
219      *
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
223      */
224     public ApexApiResult validateContextSchemas(final String name, final String version) {
225         try {
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);
230             }
231
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());
238             }
239             return result;
240         } catch (final Exception e) {
241             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
242         }
243     }
244 }