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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.modelapi.impl;
24 import java.util.Properties;
26 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
27 import org.onap.policy.apex.model.basicmodel.handling.ApexModelStringWriter;
28 import org.onap.policy.apex.model.modelapi.ApexApiResult;
29 import org.onap.policy.apex.model.modelapi.ApexModel;
30 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
31 import org.onap.policy.common.utils.validation.Assertions;
34 * This class acts as a facade for operations towards a policy model.
36 * @author Liam Fallon (liam.fallon@ericsson.com)
38 public class ModelFacade {
39 private static final String CONCEPT = "concept ";
40 private static final String DOES_NOT_EXIST = " does not exist";
41 private static final String ALREADY_CREATED = " already created";
42 private static final String NO_VERSION_SPECIFIED = ", no version specified";
44 // Apex model we're working towards
45 private final ApexModel apexModel;
47 // Properties to use for the model
48 private final Properties apexProperties;
50 // Facade classes for working towards the real Apex model
51 private final KeyInformationFacade keyInformationFacade;
53 // JSON output on list/delete if set
54 private final boolean jsonMode;
57 * Constructor to create a model facade for the Apex model.
59 * @param apexModel the apex model
60 * @param apexProperties Properties for the model
61 * @param jsonMode set to true to return JSON strings in list and delete operations, otherwise
64 public ModelFacade(final ApexModel apexModel, final Properties apexProperties, final boolean jsonMode) {
65 Assertions.argumentNotNull(apexModel, "apexModel may not be null");
66 Assertions.argumentNotNull(apexProperties, "apexProperties may not be null");
68 this.apexModel = apexModel;
69 this.apexProperties = apexProperties;
70 this.jsonMode = jsonMode;
72 keyInformationFacade = new KeyInformationFacade(apexModel, apexProperties, jsonMode);
78 * @param name name of the model
79 * @param version version of the model, set to null to use the default version
80 * @param uuid model UUID, set to null to generate a UUID
81 * @param description model description, set to null to generate a description
82 * @return result of the operation
84 public ApexApiResult createModel(final String name, final String version, final String uuid,
85 final String description) {
87 final AxArtifactKey key = new AxArtifactKey();
89 if (version != null) {
90 key.setVersion(version);
92 final String defaultVersion = apexProperties.getProperty("DEFAULT_CONCEPT_VERSION");
93 if (defaultVersion != null) {
94 key.setVersion(defaultVersion);
96 return new ApexApiResult(ApexApiResult.Result.FAILED, CONCEPT + name + NO_VERSION_SPECIFIED);
100 if (!apexModel.getPolicyModel().getKey().equals(AxArtifactKey.getNullKey())) {
101 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
102 CONCEPT + apexModel.getPolicyModel().getKey().getId() + ALREADY_CREATED);
105 apexModel.setPolicyModel(new AxPolicyModel(key));
107 ApexApiResult result;
109 result = keyInformationFacade.createKeyInformation(name, version, uuid, description);
110 if (result.getResult().equals(ApexApiResult.Result.SUCCESS)) {
111 apexModel.getPolicyModel().getKeyInformation().generateKeyInfo(apexModel.getPolicyModel());
114 } catch (final Exception e) {
115 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
122 * @param name name of the model
123 * @param version version of the model, set to null to update the latest version
124 * @param uuid key information UUID, set to null to not update
125 * @param description policy description, set to null to not update
126 * @return result of the operation
128 public ApexApiResult updateModel(final String name, final String version, final String uuid,
129 final String description) {
131 final AxArtifactKey key = new AxArtifactKey();
133 if (version != null) {
134 key.setVersion(version);
136 final String defaultVersion = apexProperties.getProperty("DEFAULT_CONCEPT_VERSION");
137 if (defaultVersion != null) {
138 key.setVersion(defaultVersion);
140 return new ApexApiResult(ApexApiResult.Result.FAILED,
141 CONCEPT + apexModel.getPolicyModel().getKey().getId() + NO_VERSION_SPECIFIED);
145 if (apexModel.getPolicyModel().getKey().equals(AxArtifactKey.getNullKey())) {
146 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
147 CONCEPT + apexModel.getPolicyModel().getKey().getId() + DOES_NOT_EXIST);
150 return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
151 } catch (final Exception e) {
152 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
157 * Get the key of an Apex model.
159 * @return the result of the operation
161 public ApexApiResult getModelKey() {
163 final ApexApiResult result = new ApexApiResult();
164 final AxArtifactKey modelkey = apexModel.getPolicyModel().getKey();
165 result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false).writeString(modelkey, AxArtifactKey.class,
168 } catch (final Exception e) {
169 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
174 * List an Apex model.
176 * @return the result of the operation
178 public ApexApiResult listModel() {
180 final ApexApiResult result = new ApexApiResult();
181 result.addMessage(new ApexModelStringWriter<AxPolicyModel>(false).writeString(apexModel.getPolicyModel(),
182 AxPolicyModel.class, jsonMode));
184 } catch (final Exception e) {
185 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
190 * Delete an Apex model, clear all the concepts in the model.
192 * @return the result of the operation
194 public ApexApiResult deleteModel() {
196 apexModel.getPolicyModel().getSchemas() .getSchemasMap() .clear();
197 apexModel.getPolicyModel().getEvents() .getEventMap() .clear();
198 apexModel.getPolicyModel().getAlbums() .getAlbumsMap() .clear();
199 apexModel.getPolicyModel().getTasks() .getTaskMap() .clear();
200 apexModel.getPolicyModel().getPolicies() .getPolicyMap() .clear();
201 apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap() .clear();
204 apexModel.setPolicyModel(new AxPolicyModel());
206 return new ApexApiResult();