cba72018fc7b4fc885f97da7fac94b58e31604d3
[policy/apex-pdp.git] / model / model-api / src / main / java / org / onap / policy / apex / model / modelapi / impl / ModelFacade.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.modelapi.impl;
23
24 import java.util.Properties;
25
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;
32
33 /**
34  * This class acts as a facade for operations towards a policy model.
35  *
36  * @author Liam Fallon (liam.fallon@ericsson.com)
37  */
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";
43
44     // Apex model we're working towards
45     private final ApexModel apexModel;
46
47     // Properties to use for the model
48     private final Properties apexProperties;
49
50     // Facade classes for working towards the real Apex model
51     private final KeyInformationFacade keyInformationFacade;
52
53     // JSON output on list/delete if set
54     private final boolean jsonMode;
55
56     /**
57      * Constructor to create a model facade for the Apex model.
58      *
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
62      *        set to false
63      */
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");
67
68         this.apexModel = apexModel;
69         this.apexProperties = apexProperties;
70         this.jsonMode = jsonMode;
71
72         keyInformationFacade = new KeyInformationFacade(apexModel, apexProperties, jsonMode);
73     }
74
75     /**
76      * Create model.
77      *
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
83      */
84     public ApexApiResult createModel(final String name, final String version, final String uuid,
85             final String description) {
86         try {
87             final AxArtifactKey key = new AxArtifactKey();
88             key.setName(name);
89             if (version != null) {
90                 key.setVersion(version);
91             } else {
92                 final String defaultVersion = apexProperties.getProperty("DEFAULT_CONCEPT_VERSION");
93                 if (defaultVersion != null) {
94                     key.setVersion(defaultVersion);
95                 } else {
96                     return new ApexApiResult(ApexApiResult.Result.FAILED, CONCEPT + name + NO_VERSION_SPECIFIED);
97                 }
98             }
99
100             if (!apexModel.getPolicyModel().getKey().equals(AxArtifactKey.getNullKey())) {
101                 return new ApexApiResult(ApexApiResult.Result.CONCEPT_EXISTS,
102                         CONCEPT + apexModel.getPolicyModel().getKey().getId() + ALREADY_CREATED);
103             }
104
105             apexModel.setPolicyModel(new AxPolicyModel(key));
106
107             ApexApiResult result;
108
109             result = keyInformationFacade.createKeyInformation(name, version, uuid, description);
110             if (result.getResult().equals(ApexApiResult.Result.SUCCESS)) {
111                 apexModel.getPolicyModel().getKeyInformation().generateKeyInfo(apexModel.getPolicyModel());
112             }
113             return result;
114         } catch (final Exception e) {
115             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
116         }
117     }
118
119     /**
120      * Update model.
121      *
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
127      */
128     public ApexApiResult updateModel(final String name, final String version, final String uuid,
129             final String description) {
130         try {
131             final AxArtifactKey key = new AxArtifactKey();
132             key.setName(name);
133             if (version != null) {
134                 key.setVersion(version);
135             } else {
136                 final String defaultVersion = apexProperties.getProperty("DEFAULT_CONCEPT_VERSION");
137                 if (defaultVersion != null) {
138                     key.setVersion(defaultVersion);
139                 } else {
140                     return new ApexApiResult(ApexApiResult.Result.FAILED,
141                             CONCEPT + apexModel.getPolicyModel().getKey().getId() + NO_VERSION_SPECIFIED);
142                 }
143             }
144
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);
148             }
149
150             return keyInformationFacade.updateKeyInformation(name, version, uuid, description);
151         } catch (final Exception e) {
152             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
153         }
154     }
155
156     /**
157      * Get the key of an Apex model.
158      *
159      * @return the result of the operation
160      */
161     public ApexApiResult getModelKey() {
162         try {
163             final ApexApiResult result = new ApexApiResult();
164             final AxArtifactKey modelkey = apexModel.getPolicyModel().getKey();
165             result.addMessage(new ApexModelStringWriter<AxArtifactKey>(false).writeString(modelkey, AxArtifactKey.class,
166                     jsonMode));
167             return result;
168         } catch (final Exception e) {
169             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
170         }
171     }
172
173     /**
174      * List an Apex model.
175      *
176      * @return the result of the operation
177      */
178     public ApexApiResult listModel() {
179         try {
180             final ApexApiResult result = new ApexApiResult();
181             result.addMessage(new ApexModelStringWriter<AxPolicyModel>(false).writeString(apexModel.getPolicyModel(),
182                     AxPolicyModel.class, jsonMode));
183             return result;
184         } catch (final Exception e) {
185             return new ApexApiResult(ApexApiResult.Result.FAILED, e);
186         }
187     }
188
189     /**
190      * Delete an Apex model, clear all the concepts in the model.
191      *
192      * @return the result of the operation
193      */
194     public ApexApiResult deleteModel() {
195         // @formatter:off
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();
202         // @formatter:on
203
204         apexModel.setPolicyModel(new AxPolicyModel());
205
206         return new ApexApiResult();
207     }
208 }