Changes for checkstyle 8.32
[policy/apex-pdp.git] / model / model-api / src / main / java / org / onap / policy / apex / model / modelapi / impl / KeyInformationFacade.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.model.modelapi.impl;
22
23 import java.util.Properties;
24 import java.util.Set;
25 import java.util.UUID;
26 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
27 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo;
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.modelapi.ApexApiResult;
31 import org.onap.policy.apex.model.modelapi.ApexModel;
32
33 /**
34  * This class acts as a facade for operations towards a policy model for key information operations.
35  *
36  * @author Liam Fallon (liam.fallon@ericsson.com)
37  */
38 public class KeyInformationFacade {
39     private static final String CONCEPT = "concept ";
40     private static final String CONCEPT_S = "concept(s) ";
41     private static final String DOES_NOT_EXIST = " does not exist";
42     private static final String DO_ES_NOT_EXIST = " do(es) not exist";
43     private static final String ALREADY_EXISTS = " already exists";
44
45     // Apex model we're working towards
46     private final ApexModel apexModel;
47
48     // Properties to use for the model
49     private final Properties apexProperties;
50
51     // JSON output on list/delete if set
52     private final boolean jsonMode;
53
54     /**
55      * Constructor to create a key information facade for the Model API.
56      *
57      * @param apexModel the apex model
58      * @param apexProperties Properties for the model
59      * @param jsonMode set to true to return JSON strings in list and delete operations, otherwise
60      *        set to false
61      */
62     public KeyInformationFacade(final ApexModel apexModel, final Properties apexProperties, final boolean jsonMode) {
63         this.apexModel = apexModel;
64         this.apexProperties = apexProperties;
65         this.jsonMode = jsonMode;
66     }
67
68     /**
69      * Create key information.
70      *
71      * @param name name of the concept for the key information
72      * @param version version of the concept for the key information, set to null to use the default
73      *        version
74      * @param uuid key information UUID, set to null to generate a UUID
75      * @param description key information description, set to null to generate a description
76      * @return result of the operation
77      */
78     public ApexApiResult createKeyInformation(final String name, final String version, final String uuid,
79             final String description) {
80         try {
81             final AxArtifactKey key = new AxArtifactKey();
82             key.setName(name);
83             if (version != null) {
84                 key.setVersion(version);
85             } else {
86                 key.setVersion(apexProperties.getProperty("DEFAULT_CONCEPT_VERSION"));
87             }
88
89             if (apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().containsKey(key)) {
90                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS, CONCEPT + key.getId() + ALREADY_EXISTS);
91             }
92
93             final AxKeyInfo keyInfo = new AxKeyInfo(key);
94             if (description != null) {
95                 keyInfo.setDescription(description);
96             }
97             if (uuid != null) {
98                 keyInfo.setUuid(UUID.fromString(uuid));
99             } else {
100                 // generate a reproducible UUID
101                 keyInfo.setUuid(AxKeyInfo.generateReproducibleUuid(keyInfo.getId() + keyInfo.getDescription()));
102             }
103             apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().put(key, keyInfo);
104             return new ApexApiResult();
105         } catch (final Exception e) {
106             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
107         }
108     }
109
110     /**
111      * Update key information.
112      *
113      * @param name name of the concept for the key information
114      * @param version version of the concept for the key information, set to null to update the
115      *        latest version
116      * @param uuid key information UUID, set to null to not update
117      * @param description key information description, set to null to not update
118      * @return result of the operation
119      */
120     public ApexApiResult updateKeyInformation(final String name, final String version, final String uuid,
121             final String description) {
122         try {
123             final AxKeyInfo keyInfo = apexModel.getPolicyModel().getKeyInformation().get(name, version);
124             if (keyInfo == null) {
125                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
126                         CONCEPT + name + ":" + version + DOES_NOT_EXIST);
127             }
128
129             if (description != null) {
130                 keyInfo.setDescription(description);
131             }
132
133             if (uuid != null) {
134                 keyInfo.setUuid(UUID.fromString(uuid));
135             } else {
136                 // generate a reproducible UUID
137                 keyInfo.setUuid(AxKeyInfo.generateReproducibleUuid(keyInfo.getId() + keyInfo.getDescription()));
138             }
139
140             return new ApexApiResult();
141         } catch (final Exception e) {
142             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
143         }
144     }
145
146     /**
147      * List key information.
148      *
149      * @param name name of the concept for the key information, set to null to list all
150      * @param version starting version of the concept for the key information, set to null to list
151      *        all versions
152      * @return result of the operation
153      */
154     public ApexApiResult listKeyInformation(final String name, final String version) {
155         try {
156             final Set<AxKeyInfo> keyInfoSet = apexModel.getPolicyModel().getKeyInformation().getAll(name, version);
157             if (name != null && keyInfoSet.isEmpty()) {
158                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
159                         CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
160             }
161
162             final ApexApiResult result = new ApexApiResult();
163             for (final AxKeyInfo keyInfo : keyInfoSet) {
164                 result.addMessage(
165                         new ApexModelStringWriter<AxKeyInfo>(false).writeString(keyInfo, AxKeyInfo.class, jsonMode));
166             }
167             return result;
168         } catch (final Exception e) {
169             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
170         }
171     }
172
173     /**
174      * Delete key information.
175      *
176      * @param name name of the concept for the key information
177      * @param version version of the concept for the key information, set to null to delete all
178      *        versions
179      * @return result of the operation
180      */
181     public ApexApiResult deleteKeyInformation(final String name, final String version) {
182         try {
183             if (version != null) {
184                 final AxArtifactKey key = new AxArtifactKey(name, version);
185                 final AxKeyInfo removedKeyInfo =
186                         apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().remove(key);
187                 if (removedKeyInfo != null) {
188                     return new ApexApiResult(ApexApiResult.Result.SUCCESS, new ApexModelStringWriter<AxKeyInfo>(false)
189                             .writeString(removedKeyInfo, AxKeyInfo.class, jsonMode));
190                 } else {
191                     return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
192                             CONCEPT + key.getId() + DOES_NOT_EXIST);
193                 }
194             }
195
196             final Set<AxKeyInfo> keyInfoSet = apexModel.getPolicyModel().getKeyInformation().getAll(name, version);
197             if (keyInfoSet.isEmpty()) {
198                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
199                         CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
200             }
201
202             final ApexApiResult result = new ApexApiResult();
203             for (final AxKeyInfo keyInfo : keyInfoSet) {
204                 result.addMessage(
205                         new ApexModelStringWriter<AxKeyInfo>(false).writeString(keyInfo, AxKeyInfo.class, jsonMode));
206                 apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().remove(keyInfo.getKey());
207             }
208             return result;
209         } catch (final Exception e) {
210             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
211         }
212     }
213
214     /**
215      * Validate key information.
216      *
217      * @param name name of the concept for the key information
218      * @param version version of the concept for the key information, set to null to validate all
219      *        versions
220      * @return result of the operation
221      */
222     public ApexApiResult validateKeyInformation(final String name, final String version) {
223         try {
224             final Set<AxKeyInfo> keyInfoSet = apexModel.getPolicyModel().getKeyInformation().getAll(name, version);
225             if (keyInfoSet.isEmpty()) {
226                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
227                         CONCEPT_S + name + ':' + version + DO_ES_NOT_EXIST);
228             }
229
230             final ApexApiResult result = new ApexApiResult();
231             for (final AxKeyInfo keyInfo : keyInfoSet) {
232                 final AxValidationResult validationResult = keyInfo.validate(new AxValidationResult());
233                 result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false).writeString(keyInfo.getKey(),
234                         AxArtifactKey.class, jsonMode));
235                 result.addMessage(validationResult.toString());
236             }
237             return result;
238         } catch (final Exception e) {
239             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
240         }
241     }
242
243 }