Merge "Restructure for authorative models"
[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.JpaToscaPolicies;
32 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
33 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType;
34 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes;
35 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
36 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
37 import org.onap.policy.models.tosca.utils.ToscaUtils;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * This class provides the provision of information on TOSCA concepts in the database to callers.
43  *
44  * @author Liam Fallon (liam.fallon@est.tech)
45  */
46 public class SimpleToscaProvider {
47     private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class);
48
49     /**
50      * Get policy types.
51      *
52      * @param dao the DAO to use to access the database
53      * @param policyTypeKey the policy type key for the policy types to be retrieved. A null key name returns all policy
54      *        types. A null key version returns all versions of the policy type name specified in the key.
55      * @return the policy types found
56      * @throws PfModelException on errors getting policy types
57      */
58     public JpaToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey)
59             throws PfModelException {
60
61         // Create the structure of the TOSCA service template to contain the policy type
62         JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
63         serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
64
65         // Add the policy type to the TOSCA service template
66         JpaToscaPolicyType policyType = dao.get(JpaToscaPolicyType.class, policyTypeKey);
67         if (policyType != null) {
68             serviceTemplate.getPolicyTypes().getConceptMap().put(policyTypeKey, policyType);
69             return serviceTemplate;
70         } else {
71             String errorMessage = "policy type not found: " + policyTypeKey.getId();
72             LOGGER.warn(errorMessage);
73             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
74         }
75     }
76
77     /**
78      * Create policy types.
79      *
80      * @param dao the DAO to use to access the database
81      * @param serviceTemplate the service template containing the definition of the policy types to be created
82      * @return the TOSCA service template containing the created policy types
83      * @throws PfModelException on errors creating policy types
84      */
85     public JpaToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
86             @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
87
88         ToscaUtils.assertPolicyTypesExist(serviceTemplate);
89
90         for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
91             dao.create(policyType);
92         }
93
94         // Return the created policy types
95         JpaToscaPolicyTypes returnPolicyTypes = new JpaToscaPolicyTypes();
96
97         for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
98             returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(JpaToscaPolicyType.class, policyTypeKey));
99         }
100
101         JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate();
102         returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
103
104         return returnServiceTemplate;
105     }
106
107     /**
108      * Create policy types.
109      *
110      * @param dao the DAO to use to access the database
111      * @param serviceTemplate the service template containing the definition of the policy types to be modified
112      * @return the TOSCA service template containing the modified policy types
113      * @throws PfModelException on errors updating policy types
114      */
115     public JpaToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
116             @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
117
118         ToscaUtils.assertPolicyTypesExist(serviceTemplate);
119
120         for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
121             dao.update(policyType);
122         }
123
124         // Return the created policy types
125         JpaToscaPolicyTypes returnPolicyTypes = new JpaToscaPolicyTypes();
126
127         for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
128             returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(JpaToscaPolicyType.class, policyTypeKey));
129         }
130
131         JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate();
132         returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
133
134         return returnServiceTemplate;
135     }
136
137     /**
138      * Delete policy types.
139      *
140      * @param dao the DAO to use to access the database
141      * @param policyTypeKey the policy type key for the policy types to be deleted, if the version of the key is null,
142      *        all versions of the policy type are deleted.
143      * @return the TOSCA service template containing the policy types that were deleted
144      * @throws PfModelException on errors deleting policy types
145      */
146     public JpaToscaServiceTemplate deletePolicyTypes(@NonNull final PfDao dao,
147             @NonNull final PfConceptKey policyTypeKey)
148             throws PfModelException {
149
150         JpaToscaServiceTemplate serviceTemplate = getPolicyTypes(dao, policyTypeKey);
151
152         dao.delete(JpaToscaPolicyType.class, policyTypeKey);
153
154         return serviceTemplate;
155     }
156
157     /**
158      * Get policies.
159      *
160      * @param dao the DAO to use to access the database
161      * @param policyKey the policy key for the policies to be retrieved. The parent name and version must be specified.
162      *        A null local name returns all policies for a parent policy type.
163      * @return the policies found
164      * @throws PfModelException on errors getting policies
165      */
166     public JpaToscaServiceTemplate getPolicies(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
167             throws PfModelException {
168
169         // Create the structure of the TOSCA service template to contain the policy type
170         JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
171         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
172         serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
173
174         // Add the policy to the TOSCA service template
175         JpaToscaPolicy policy = dao.get(JpaToscaPolicy.class, policyKey);
176         if (policy != null) {
177             serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, policy);
178             return serviceTemplate;
179         } else {
180             String errorMessage = "policy not found: " + policyKey.getId();
181             LOGGER.warn(errorMessage);
182             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
183         }
184     }
185
186     /**
187      * Create policies.
188      *
189      * @param dao the DAO to use to access the database
190      * @param serviceTemplate the service template containing the definitions of the new policies to be created.
191      * @return the TOSCA service template containing the policy types that were created
192      * @throws PfModelException on errors creating policies
193      */
194     public JpaToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
195             @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
196
197         ToscaUtils.assertPoliciesExist(serviceTemplate);
198
199         for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
200             dao.create(policy);
201         }
202
203         // Return the created policy types
204         JpaToscaPolicies returnPolicies = new JpaToscaPolicies();
205         returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
206
207         for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
208             returnPolicies.getConceptMap().put(policyKey, dao.get(JpaToscaPolicy.class, policyKey));
209         }
210
211         serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
212
213         return serviceTemplate;
214     }
215
216     /**
217      * Update policies.
218      *
219      * @param dao the DAO to use to access the database
220      * @param serviceTemplate the service template containing the definitions of the policies to be updated.
221      * @return the TOSCA service template containing the policies that were updated
222      * @throws PfModelException on errors updating policies
223      */
224     public JpaToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
225             @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
226
227         ToscaUtils.assertPoliciesExist(serviceTemplate);
228
229         for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
230             dao.update(policy);
231         }
232
233         // Return the created policy types
234         JpaToscaPolicies returnPolicies = new JpaToscaPolicies();
235         returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
236
237         for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
238             returnPolicies.getConceptMap().put(policyKey, dao.get(JpaToscaPolicy.class, policyKey));
239         }
240
241         serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
242
243         return serviceTemplate;
244     }
245
246     /**
247      * Delete policies.
248      *
249      * @param dao the DAO to use to access the database
250      * @param policyKey the policy key
251      * @return the TOSCA service template containing the policies that were deleted
252      * @throws PfModelException on errors deleting policies
253      */
254     public JpaToscaServiceTemplate deletePolicies(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
255             throws PfModelException {
256
257         JpaToscaServiceTemplate serviceTemplate = getPolicies(dao, policyKey);
258
259         dao.delete(JpaToscaPolicy.class, policyKey);
260
261         return serviceTemplate;
262     }
263 }