Changes for checkstyle 8.32
[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 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;
33
34 /**
35  * This class acts as a facade for operations towards a policy model for context schema operations.
36  *
37  * @author Liam Fallon (liam.fallon@ericsson.com)
38  */
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";
45
46     // Apex model we're working towards
47     private final ApexModel apexModel;
48
49     // Properties to use for the model
50     private final Properties apexProperties;
51
52     // Facade classes for working towards the real Apex model
53     private final KeyInformationFacade keyInformationFacade;
54
55     // JSON output on list/delete if set
56     private final boolean jsonMode;
57
58     /**
59      * Constructor to create the context schema facade for the Model API.
60      *
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
64      */
65     public ContextSchemaFacade(final ApexModel apexModel, final Properties apexProperties, final boolean jsonMode) {
66         this.apexModel = apexModel;
67         this.apexProperties = apexProperties;
68         this.jsonMode = jsonMode;
69
70         keyInformationFacade = new KeyInformationFacade(apexModel, apexProperties, jsonMode);
71     }
72
73     /**
74      * Create a context schema.
75      *
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
83      */
84     public ApexApiResult createContextSchema(final String name, final String version, final String schemaFlavour,
85                     final String schemaDefinition, final String uuid, final String description) {
86         try {
87             Assertions.argumentNotNull(schemaFlavour, "schemaFlavour may not be null");
88
89             final AxArtifactKey key = new AxArtifactKey();
90             key.setName(name);
91             if (version != null) {
92                 key.setVersion(version);
93             } else {
94                 key.setVersion(apexProperties.getProperty("DEFAULT_CONCEPT_VERSION"));
95             }
96
97             if (apexModel.getPolicyModel().getSchemas().getSchemasMap().containsKey(key)) {
98                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS, CONCEPT + key.getId() + ALREADY_EXISTS);
99             }
100
101             apexModel.getPolicyModel().getSchemas().getSchemasMap().put(key,
102                             new AxContextSchema(key, schemaFlavour, schemaDefinition));
103
104             if (apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().containsKey(key)) {
105                 return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
106             } else {
107                 return keyInformationFacade.createKeyInformation(name, version, uuid, description);
108             }
109         } catch (final Exception e) {
110             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
111         }
112     }
113
114     /**
115      * Update a context schema.
116      *
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
124      */
125     public ApexApiResult updateContextSchema(final String name, final String version, final String schemaFlavour,
126                     final String schemaDefinition, final String uuid, final String description) {
127         try {
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);
132             }
133
134             if (schemaFlavour != null) {
135                 schema.setSchemaFlavour(schemaFlavour);
136             }
137
138             if (schemaDefinition != null) {
139                 schema.setSchema(schemaDefinition);
140             }
141
142             return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
143         } catch (final Exception e) {
144             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
145         }
146     }
147
148     /**
149      * List context schemas.
150      *
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
154      */
155     public ApexApiResult listContextSchemas(final String name, final String version) {
156         try {
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);
161             }
162
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));
167             }
168             return result;
169         } catch (final Exception e) {
170             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
171         }
172     }
173
174     /**
175      * Delete a context schema.
176      *
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
180      */
181     public ApexApiResult deleteContextSchema(final String name, final String version) {
182         try {
183             if (version != null) {
184                 final AxArtifactKey key = new AxArtifactKey(name, version);
185                 final AxContextSchema removedSchema = apexModel.getPolicyModel().getSchemas().getSchemasMap()
186                                 .remove(key);
187                 if (removedSchema != null) {
188                     return new ApexApiResult(ApexApiResult.Result.SUCCESS,
189                                     new ApexModelStringWriter<AxContextSchema>(false).writeString(removedSchema,
190                                                     AxContextSchema.class, jsonMode));
191                 } else {
192                     return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
193                                     CONCEPT + key.getId() + DOES_NOT_EXIST);
194                 }
195             }
196
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);
201             }
202
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);
209             }
210             return result;
211         } catch (final Exception e) {
212             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
213         }
214     }
215
216     /**
217      * Validate context schemas.
218      *
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
222      */
223     public ApexApiResult validateContextSchemas(final String name, final String version) {
224         try {
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);
229             }
230
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());
237             }
238             return result;
239         } catch (final Exception e) {
240             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
241         }
242     }
243 }