Merge "Add internal classes to models-pdp"
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / provider / SimpleToscaProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.simple.provider;
22
23 import javax.ws.rs.core.Response;
24
25 import lombok.NonNull;
26
27 import org.onap.policy.models.base.PfConceptKey;
28 import org.onap.policy.models.base.PfModelException;
29 import org.onap.policy.models.base.PfModelRuntimeException;
30 import org.onap.policy.models.dao.PfDao;
31 import org.onap.policy.models.tosca.simple.concepts.ToscaPolicies;
32 import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy;
33 import org.onap.policy.models.tosca.simple.concepts.ToscaPolicyType;
34 import org.onap.policy.models.tosca.simple.concepts.ToscaPolicyTypes;
35 import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
36 import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * This class provides the provision of information on TOSCA concepts in the database to callers.
42  *
43  * @author Liam Fallon (liam.fallon@est.tech)
44  */
45 public class SimpleToscaProvider {
46     private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class);
47
48     /**
49      * Get policy types.
50      *
51      * @param dao the DAO to use to access the database
52      * @param policyTypeKey the policy type key for the policy types to be retrieved. A null key name returns all policy
53      *        types. A null key version returns all versions of the policy type name specified in the key.
54      * @return the policy types found
55      * @throws PfModelException on errors getting policy types
56      */
57     public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey)
58             throws PfModelException {
59
60         // Create the structure of the TOSCA service template to contain the policy type
61         ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
62         serviceTemplate.setPolicyTypes(new ToscaPolicyTypes());
63
64         // Add the policy type to the TOSCA service template
65         ToscaPolicyType policyType = dao.get(ToscaPolicyType.class, policyTypeKey);
66         if (policyType != null) {
67             serviceTemplate.getPolicyTypes().getConceptMap().put(policyTypeKey, policyType);
68             return serviceTemplate;
69         } else {
70             String errorMessage = "policy type not found: " + policyTypeKey.getId();
71             LOGGER.warn(errorMessage);
72             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
73         }
74     }
75
76     /**
77      * Create policy types.
78      *
79      * @param dao the DAO to use to access the database
80      * @param serviceTemplate the service template containing the definition of the policy types to be created
81      * @return the TOSCA service template containing the created policy types
82      * @throws PfModelException on errors creating policy types
83      */
84     public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
85             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
86
87         assertPolicyTypesExist(serviceTemplate);
88
89         for (ToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
90             dao.create(policyType);
91         }
92
93         // Return the created policy types
94         ToscaPolicyTypes returnPolicyTypes = new ToscaPolicyTypes();
95
96         for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
97             returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(ToscaPolicyType.class, policyTypeKey));
98         }
99
100         ToscaServiceTemplate returnServiceTemplate = new ToscaServiceTemplate();
101         returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
102
103         return returnServiceTemplate;
104     }
105
106     /**
107      * Create policy types.
108      *
109      * @param dao the DAO to use to access the database
110      * @param serviceTemplate the service template containing the definition of the policy types to be modified
111      * @return the TOSCA service template containing the modified policy types
112      * @throws PfModelException on errors updating policy types
113      */
114     public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
115             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
116
117         assertPolicyTypesExist(serviceTemplate);
118
119         for (ToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
120             dao.update(policyType);
121         }
122
123         // Return the created policy types
124         ToscaPolicyTypes returnPolicyTypes = new ToscaPolicyTypes();
125
126         for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
127             returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(ToscaPolicyType.class, policyTypeKey));
128         }
129
130         ToscaServiceTemplate returnServiceTemplate = new ToscaServiceTemplate();
131         returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
132
133         return returnServiceTemplate;
134     }
135
136     /**
137      * Delete policy types.
138      *
139      * @param dao the DAO to use to access the database
140      * @param policyTypeKey the policy type key for the policy types to be deleted, if the version of the key is null,
141      *        all versions of the policy type are deleted.
142      * @return the TOSCA service template containing the policy types that were deleted
143      * @throws PfModelException on errors deleting policy types
144      */
145     public ToscaServiceTemplate deletePolicyTypes(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey)
146             throws PfModelException {
147
148         ToscaServiceTemplate serviceTemplate = getPolicyTypes(dao, policyTypeKey);
149
150         dao.delete(ToscaPolicyType.class, policyTypeKey);
151
152         return serviceTemplate;
153     }
154
155     /**
156      * Get policies.
157      *
158      * @param dao the DAO to use to access the database
159      * @param policyKey the policy key for the policies to be retrieved. The parent name and version must be specified.
160      *        A null local name returns all policies for a parent policy type.
161      * @return the policies found
162      * @throws PfModelException on errors getting policies
163      */
164     public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
165             throws PfModelException {
166
167         // Create the structure of the TOSCA service template to contain the policy type
168         ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
169         serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate());
170         serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies());
171
172         // Add the policy to the TOSCA service template
173         ToscaPolicy policy = dao.get(ToscaPolicy.class, policyKey);
174         if (policy != null) {
175             serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, policy);
176             return serviceTemplate;
177         } else {
178             String errorMessage = "policy not found: " + policyKey.getId();
179             LOGGER.warn(errorMessage);
180             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
181         }
182     }
183
184     /**
185      * Create policies.
186      *
187      * @param dao the DAO to use to access the database
188      * @param serviceTemplate the service template containing the definitions of the new policies to be created.
189      * @return the TOSCA service template containing the policy types that were created
190      * @throws PfModelException on errors creating policies
191      */
192     public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
193             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
194
195         assertPoliciesExist(serviceTemplate);
196
197         for (ToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
198             dao.create(policy);
199         }
200
201         // Return the created policy types
202         ToscaPolicies returnPolicies = new ToscaPolicies();
203         returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
204
205         for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
206             returnPolicies.getConceptMap().put(policyKey, dao.get(ToscaPolicy.class, policyKey));
207         }
208
209         serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
210
211         return serviceTemplate;
212     }
213
214     /**
215      * Update policies.
216      *
217      * @param dao the DAO to use to access the database
218      * @param serviceTemplate the service template containing the definitions of the policies to be updated.
219      * @return the TOSCA service template containing the policies that were updated
220      * @throws PfModelException on errors updating policies
221      */
222     public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
223             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
224
225         assertPoliciesExist(serviceTemplate);
226
227         for (ToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
228             dao.update(policy);
229         }
230
231         // Return the created policy types
232         ToscaPolicies returnPolicies = new ToscaPolicies();
233         returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
234
235         for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
236             returnPolicies.getConceptMap().put(policyKey, dao.get(ToscaPolicy.class, policyKey));
237         }
238
239         serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
240
241         return serviceTemplate;
242     }
243
244     /**
245      * Delete policies.
246      *
247      * @param dao the DAO to use to access the database
248      * @param policyKey the policy key
249      * @return the TOSCA service template containing the policies that were deleted
250      * @throws PfModelException on errors deleting policies
251      */
252     public ToscaServiceTemplate deletePolicies(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
253             throws PfModelException {
254
255         ToscaServiceTemplate serviceTemplate = getPolicies(dao, policyKey);
256
257         dao.delete(ToscaPolicy.class, policyKey);
258
259         return serviceTemplate;
260     }
261
262     /**
263      * Check if policy types have been specified is initialized.
264      */
265     private void assertPolicyTypesExist(final ToscaServiceTemplate serviceTemplate) {
266         if (serviceTemplate.getPolicyTypes() == null) {
267             String errorMessage = "no policy types specified on service template";
268             LOGGER.warn(errorMessage);
269             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
270         }
271
272         if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) {
273             String errorMessage = "list of policy types specified on service template is empty";
274             LOGGER.warn(errorMessage);
275             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
276         }
277     }
278
279     /**
280      * Check if policy types have been specified is initialized.
281      */
282     private void assertPoliciesExist(final ToscaServiceTemplate serviceTemplate) {
283         if (serviceTemplate.getTopologyTemplate() == null) {
284             String errorMessage = "topology template not specified on service template";
285             LOGGER.warn(errorMessage);
286             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
287         }
288
289         if (serviceTemplate.getTopologyTemplate().getPolicies() == null) {
290             String errorMessage = "no policies specified on topology template of service template";
291             LOGGER.warn(errorMessage);
292             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
293         }
294
295         if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) {
296             String errorMessage = "list of policies specified on topology template of service template is empty";
297             LOGGER.warn(errorMessage);
298             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
299         }
300     }
301 }