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