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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.model.modelapi.impl;
23 import java.util.Properties;
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;
34 * This class acts as a facade for operations towards a policy model for key information operations.
36 * @author Liam Fallon (liam.fallon@ericsson.com)
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";
45 // Apex model we're working towards
46 private final ApexModel apexModel;
48 // Properties to use for the model
49 private final Properties apexProperties;
51 // JSON output on list/delete if set
52 private final boolean jsonMode;
55 * Constructor to create a key information facade for the Model API.
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
62 public KeyInformationFacade(final ApexModel apexModel, final Properties apexProperties, final boolean jsonMode) {
63 this.apexModel = apexModel;
64 this.apexProperties = apexProperties;
65 this.jsonMode = jsonMode;
69 * Create key information.
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
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
78 public ApexApiResult createKeyInformation(final String name, final String version, final String uuid,
79 final String description) {
81 final AxArtifactKey key = new AxArtifactKey();
83 if (version != null) {
84 key.setVersion(version);
86 key.setVersion(apexProperties.getProperty("DEFAULT_CONCEPT_VERSION"));
89 if (apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().containsKey(key)) {
90 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS, CONCEPT + key.getId() + ALREADY_EXISTS);
93 final AxKeyInfo keyInfo = new AxKeyInfo(key);
94 if (description != null) {
95 keyInfo.setDescription(description);
98 keyInfo.setUuid(UUID.fromString(uuid));
100 // generate a reproducible UUID
101 keyInfo.setUuid(AxKeyInfo.generateReproducibleUuid(keyInfo.getId() + keyInfo.getDescription()));
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);
111 * Update key information.
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
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
120 public ApexApiResult updateKeyInformation(final String name, final String version, final String uuid,
121 final String description) {
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);
129 if (description != null) {
130 keyInfo.setDescription(description);
134 keyInfo.setUuid(UUID.fromString(uuid));
136 // generate a reproducible UUID
137 keyInfo.setUuid(AxKeyInfo.generateReproducibleUuid(keyInfo.getId() + keyInfo.getDescription()));
140 return new ApexApiResult();
141 } catch (final Exception e) {
142 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
147 * List key information.
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
152 * @return result of the operation
154 public ApexApiResult listKeyInformation(final String name, final String version) {
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);
162 final ApexApiResult result = new ApexApiResult();
163 for (final AxKeyInfo keyInfo : keyInfoSet) {
165 new ApexModelStringWriter<AxKeyInfo>(false).writeString(keyInfo, AxKeyInfo.class, jsonMode));
168 } catch (final Exception e) {
169 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
174 * Delete key information.
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
179 * @return result of the operation
181 public ApexApiResult deleteKeyInformation(final String name, final String version) {
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));
191 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
192 CONCEPT + key.getId() + DOES_NOT_EXIST);
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);
202 final ApexApiResult result = new ApexApiResult();
203 for (final AxKeyInfo keyInfo : keyInfoSet) {
205 new ApexModelStringWriter<AxKeyInfo>(false).writeString(keyInfo, AxKeyInfo.class, jsonMode));
206 apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().remove(keyInfo.getKey());
209 } catch (final Exception e) {
210 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
215 * Validate key information.
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
220 * @return result of the operation
222 public ApexApiResult validateKeyInformation(final String name, final String version) {
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);
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());
238 } catch (final Exception e) {
239 return new ApexApiResult(ApexApiResult.Result.FAILED, e);