7cbcfc71327f93a4126ab9e3cf9c9b1ddafcf089
[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
27 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo;
29 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
30 import org.onap.policy.apex.model.basicmodel.handling.ApexModelStringWriter;
31 import org.onap.policy.apex.model.modelapi.ApexApiResult;
32 import org.onap.policy.apex.model.modelapi.ApexModel;
33
34 /**
35  * This class acts as a facade for operations towards a policy model for key information operations.
36  *
37  * @author Liam Fallon (liam.fallon@ericsson.com)
38  */
39 public class KeyInformationFacade {
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     // JSON output on list/delete if set
53     private final boolean jsonMode;
54
55     /**
56      * Constructor to create a key information facade for the Model API.
57      *
58      * @param apexModel the apex model
59      * @param apexProperties Properties for the model
60      * @param jsonMode set to true to return JSON strings in list and delete operations, otherwise
61      *        set to false
62      */
63     public KeyInformationFacade(final ApexModel apexModel, final Properties apexProperties, final boolean jsonMode) {
64         this.apexModel = apexModel;
65         this.apexProperties = apexProperties;
66         this.jsonMode = jsonMode;
67     }
68
69     /**
70      * Create key information.
71      *
72      * @param name name of the concept for the key information
73      * @param version version of the concept for the key information, set to null to use the default
74      *        version
75      * @param uuid key information UUID, set to null to generate a UUID
76      * @param description key information description, set to null to generate a description
77      * @return result of the operation
78      */
79     public ApexApiResult createKeyInformation(final String name, final String version, final String uuid,
80             final String description) {
81         try {
82             final AxArtifactKey key = new AxArtifactKey();
83             key.setName(name);
84             if (version != null) {
85                 key.setVersion(version);
86             } else {
87                 key.setVersion(apexProperties.getProperty("DEFAULT_CONCEPT_VERSION"));
88             }
89
90             if (apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().containsKey(key)) {
91                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS, CONCEPT + key.getId() + ALREADY_EXISTS);
92             }
93
94             final AxKeyInfo keyInfo = new AxKeyInfo(key);
95             if (description != null) {
96                 keyInfo.setDescription(description);
97             }
98             if (uuid != null) {
99                 keyInfo.setUuid(UUID.fromString(uuid));
100             } else {
101                 // generate a reproducible UUID
102                 keyInfo.setUuid(AxKeyInfo.generateReproducibleUuid(keyInfo.getId() + keyInfo.getDescription()));
103             }
104             apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().put(key, keyInfo);
105             return new ApexApiResult();
106         } catch (final Exception e) {
107             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
108         }
109     }
110
111     /**
112      * Update key information.
113      *
114      * @param name name of the concept for the key information
115      * @param version version of the concept for the key information, set to null to update the
116      *        latest version
117      * @param uuid key information UUID, set to null to not update
118      * @param description key information description, set to null to not update
119      * @return result of the operation
120      */
121     public ApexApiResult updateKeyInformation(final String name, final String version, final String uuid,
122             final String description) {
123         try {
124             final AxKeyInfo keyInfo = apexModel.getPolicyModel().getKeyInformation().get(name, version);
125             if (keyInfo == null) {
126                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
127                         CONCEPT + name + ":" + version + DOES_NOT_EXIST);
128             }
129
130             if (description != null) {
131                 keyInfo.setDescription(description);
132             }
133
134             if (uuid != null) {
135                 keyInfo.setUuid(UUID.fromString(uuid));
136             } else {
137                 // generate a reproducible UUID
138                 keyInfo.setUuid(AxKeyInfo.generateReproducibleUuid(keyInfo.getId() + keyInfo.getDescription()));
139             }
140
141             return new ApexApiResult();
142         } catch (final Exception e) {
143             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
144         }
145     }
146
147     /**
148      * List key information.
149      *
150      * @param name name of the concept for the key information, set to null to list all
151      * @param version starting version of the concept for the key information, set to null to list
152      *        all versions
153      * @return result of the operation
154      */
155     public ApexApiResult listKeyInformation(final String name, final String version) {
156         try {
157             final Set<AxKeyInfo> keyInfoSet = apexModel.getPolicyModel().getKeyInformation().getAll(name, version);
158             if (name != null && keyInfoSet.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 AxKeyInfo keyInfo : keyInfoSet) {
165                 result.addMessage(
166                         new ApexModelStringWriter<AxKeyInfo>(false).writeString(keyInfo, AxKeyInfo.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 key information.
176      *
177      * @param name name of the concept for the key information
178      * @param version version of the concept for the key information, set to null to delete all
179      *        versions
180      * @return result of the operation
181      */
182     public ApexApiResult deleteKeyInformation(final String name, final String version) {
183         try {
184             if (version != null) {
185                 final AxArtifactKey key = new AxArtifactKey(name, version);
186                 final AxKeyInfo removedKeyInfo =
187                         apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().remove(key);
188                 if (removedKeyInfo != null) {
189                     return new ApexApiResult(ApexApiResult.Result.SUCCESS, new ApexModelStringWriter<AxKeyInfo>(false)
190                             .writeString(removedKeyInfo, AxKeyInfo.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<AxKeyInfo> keyInfoSet = apexModel.getPolicyModel().getKeyInformation().getAll(name, version);
198             if (keyInfoSet.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 AxKeyInfo keyInfo : keyInfoSet) {
205                 result.addMessage(
206                         new ApexModelStringWriter<AxKeyInfo>(false).writeString(keyInfo, AxKeyInfo.class, jsonMode));
207                 apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().remove(keyInfo.getKey());
208             }
209             return result;
210         } catch (final Exception e) {
211             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
212         }
213     }
214
215     /**
216      * Validate key information.
217      *
218      * @param name name of the concept for the key information
219      * @param version version of the concept for the key information, set to null to validate all
220      *        versions
221      * @return result of the operation
222      */
223     public ApexApiResult validateKeyInformation(final String name, final String version) {
224         try {
225             final Set<AxKeyInfo> keyInfoSet = apexModel.getPolicyModel().getKeyInformation().getAll(name, version);
226             if (keyInfoSet.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 AxKeyInfo keyInfo : keyInfoSet) {
233                 final AxValidationResult validationResult = keyInfo.validate(new AxValidationResult());
234                 result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false).writeString(keyInfo.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
244 }