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;
25 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
26 import org.onap.policy.apex.model.basicmodel.handling.ApexModelStringWriter;
27 import org.onap.policy.apex.model.modelapi.ApexApiResult;
28 import org.onap.policy.apex.model.modelapi.ApexModel;
29 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
30 import org.onap.policy.common.utils.validation.Assertions;
33 * This class acts as a facade for operations towards a policy model.
35 * @author Liam Fallon (liam.fallon@ericsson.com)
37 public class ModelFacade {
38 private static final String CONCEPT = "concept ";
39 private static final String DOES_NOT_EXIST = " does not exist";
40 private static final String ALREADY_CREATED = " already created";
41 private static final String NO_VERSION_SPECIFIED = ", no version specified";
43 // Apex model we're working towards
44 private final ApexModel apexModel;
46 // Properties to use for the model
47 private final Properties apexProperties;
49 // Facade classes for working towards the real Apex model
50 private final KeyInformationFacade keyInformationFacade;
52 // JSON output on list/delete if set
53 private final boolean jsonMode;
56 * Constructor to create a model facade for the Apex model.
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
63 public ModelFacade(final ApexModel apexModel, final Properties apexProperties, final boolean jsonMode) {
64 Assertions.argumentNotNull(apexModel, "apexModel may not be null");
65 Assertions.argumentNotNull(apexProperties, "apexProperties may not be null");
67 this.apexModel = apexModel;
68 this.apexProperties = apexProperties;
69 this.jsonMode = jsonMode;
71 keyInformationFacade = new KeyInformationFacade(apexModel, apexProperties, jsonMode);
77 * @param name name of the model
78 * @param version version of the model, set to null to use the default version
79 * @param uuid model UUID, set to null to generate a UUID
80 * @param description model description, set to null to generate a description
81 * @return result of the operation
83 public ApexApiResult createModel(final String name, final String version, final String uuid,
84 final String description) {
86 final AxArtifactKey key = new AxArtifactKey();
88 if (version != null) {
89 key.setVersion(version);
91 final String defaultVersion = apexProperties.getProperty("DEFAULT_CONCEPT_VERSION");
92 if (defaultVersion != null) {
93 key.setVersion(defaultVersion);
95 return new ApexApiResult(ApexApiResult.Result.FAILED, CONCEPT + name + NO_VERSION_SPECIFIED);
99 if (!apexModel.getPolicyModel().getKey().equals(AxArtifactKey.getNullKey())) {
100 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
101 CONCEPT + apexModel.getPolicyModel().getKey().getId() + ALREADY_CREATED);
104 apexModel.setPolicyModel(new AxPolicyModel(key));
106 ApexApiResult result;
108 result = keyInformationFacade.createKeyInformation(name, version, uuid, description);
109 if (result.getResult().equals(ApexApiResult.Result.SUCCESS)) {
110 apexModel.getPolicyModel().getKeyInformation().generateKeyInfo(apexModel.getPolicyModel());
113 } catch (final Exception e) {
114 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
121 * @param name name of the model
122 * @param version version of the model, set to null to update the latest version
123 * @param uuid key information UUID, set to null to not update
124 * @param description policy description, set to null to not update
125 * @return result of the operation
127 public ApexApiResult updateModel(final String name, final String version, final String uuid,
128 final String description) {
130 final AxArtifactKey key = new AxArtifactKey();
132 if (version != null) {
133 key.setVersion(version);
135 final String defaultVersion = apexProperties.getProperty("DEFAULT_CONCEPT_VERSION");
136 if (defaultVersion != null) {
137 key.setVersion(defaultVersion);
139 return new ApexApiResult(ApexApiResult.Result.FAILED,
140 CONCEPT + apexModel.getPolicyModel().getKey().getId() + NO_VERSION_SPECIFIED);
144 if (apexModel.getPolicyModel().getKey().equals(AxArtifactKey.getNullKey())) {
145 return new ApexApiResult(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST,
146 CONCEPT + apexModel.getPolicyModel().getKey().getId() + DOES_NOT_EXIST);
149 return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
150 } catch (final Exception e) {
151 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
156 * Get the key of an Apex model.
158 * @return the result of the operation
160 public ApexApiResult getModelKey() {
162 final ApexApiResult result = new ApexApiResult();
163 final AxArtifactKey modelkey = apexModel.getPolicyModel().getKey();
164 result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false).writeString(modelkey, AxArtifactKey.class,
167 } catch (final Exception e) {
168 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
173 * List an Apex model.
175 * @return the result of the operation
177 public ApexApiResult listModel() {
179 final ApexApiResult result = new ApexApiResult();
180 result.addMessage(new ApexModelStringWriter<AxPolicyModel>(false).writeString(apexModel.getPolicyModel(),
181 AxPolicyModel.class, jsonMode));
183 } catch (final Exception e) {
184 return new ApexApiResult(ApexApiResult.Result.FAILED, e);
189 * Delete an Apex model, clear all the concepts in the model.
191 * @return the result of the operation
193 public ApexApiResult deleteModel() {
195 apexModel.getPolicyModel().getSchemas().getSchemasMap().clear();
196 apexModel.getPolicyModel().getEvents().getEventMap().clear();
197 apexModel.getPolicyModel().getAlbums().getAlbumsMap().clear();
198 apexModel.getPolicyModel().getTasks().getTaskMap().clear();
199 apexModel.getPolicyModel().getPolicies().getPolicyMap().clear();
200 apexModel.getPolicyModel().getKeyInformation().getKeyInfoMap().clear();
203 apexModel.setPolicyModel(new AxPolicyModel());
205 return new ApexApiResult();