Merge "Supports new aai changes."
[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 java.util.LinkedHashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import lombok.NonNull;
28
29 import org.onap.policy.models.base.PfConcept;
30 import org.onap.policy.models.base.PfConceptKey;
31 import org.onap.policy.models.base.PfModelException;
32 import org.onap.policy.models.dao.PfDao;
33 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
34 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
35 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType;
36 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
38 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
39 import org.onap.policy.models.tosca.utils.ToscaUtils;
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     /**
48      * Get policy types.
49      *
50      * @param dao the DAO to use to access the database
51      * @param name the name of the policy type to get, set to null to get all policy types
52      * @param version the version of the policy type to get, set to null to get all versions
53      * @return the policy types found
54      * @throws PfModelException on errors getting policy types
55      */
56     public JpaToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, final String name, final String version)
57             throws PfModelException {
58
59         // Create the structure of the TOSCA service template to contain the policy type
60         JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
61         serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes());
62
63         // Add the policy type to the TOSCA service template
64         List<JpaToscaPolicyType> jpaPolicyTypeList = dao.getFiltered(JpaToscaPolicyType.class, name, version);
65         serviceTemplate.getPolicyTypes().getConceptMap().putAll(asConceptMap(jpaPolicyTypeList));
66
67         return serviceTemplate;
68     }
69
70     /**
71      * Create policy types.
72      *
73      * @param dao the DAO to use to access the database
74      * @param serviceTemplate the service template containing the definition of the policy types to be created
75      * @return the TOSCA service template containing the created policy types
76      * @throws PfModelException on errors creating policy types
77      */
78     public JpaToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
79             @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
80
81         ToscaUtils.assertPolicyTypesExist(serviceTemplate);
82
83         for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
84             dao.create(policyType);
85         }
86
87         // Return the created policy types
88         JpaToscaPolicyTypes returnPolicyTypes = new JpaToscaPolicyTypes();
89
90         for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
91             returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(JpaToscaPolicyType.class, policyTypeKey));
92         }
93
94         JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate();
95         returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
96
97         return returnServiceTemplate;
98     }
99
100     /**
101      * Create policy types.
102      *
103      * @param dao the DAO to use to access the database
104      * @param serviceTemplate the service template containing the definition of the policy types to be modified
105      * @return the TOSCA service template containing the modified policy types
106      * @throws PfModelException on errors updating policy types
107      */
108     public JpaToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
109             @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
110
111         ToscaUtils.assertPolicyTypesExist(serviceTemplate);
112
113         for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
114             dao.update(policyType);
115         }
116
117         // Return the created policy types
118         JpaToscaPolicyTypes returnPolicyTypes = new JpaToscaPolicyTypes();
119
120         for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
121             returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(JpaToscaPolicyType.class, policyTypeKey));
122         }
123
124         JpaToscaServiceTemplate returnServiceTemplate = new JpaToscaServiceTemplate();
125         returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
126
127         return returnServiceTemplate;
128     }
129
130     /**
131      * Delete policy types.
132      *
133      * @param dao the DAO to use to access the database
134      * @param policyTypeKey the policy type key for the policy types to be deleted, if the version of the key is null,
135      *        all versions of the policy type are deleted.
136      * @return the TOSCA service template containing the policy types that were deleted
137      * @throws PfModelException on errors deleting policy types
138      */
139     public JpaToscaServiceTemplate deletePolicyType(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey)
140             throws PfModelException {
141
142         JpaToscaServiceTemplate serviceTemplate =
143                 getPolicyTypes(dao, policyTypeKey.getName(), policyTypeKey.getVersion());
144
145         dao.delete(JpaToscaPolicyType.class, policyTypeKey);
146
147         return serviceTemplate;
148     }
149
150     /**
151      * Get policies.
152      *
153      * @param dao the DAO to use to access the database
154      * @param name the name of the policy to get, set to null to get all policy types
155      * @param version the version of the policy to get, set to null to get all versions
156      * @return the policies found
157      * @throws PfModelException on errors getting policies
158      */
159     public JpaToscaServiceTemplate getPolicies(@NonNull final PfDao dao, final String name, final String version)
160             throws PfModelException {
161
162         // Create the structure of the TOSCA service template to contain the policy type
163         JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
164         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
165         serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
166
167         // Add the policy type to the TOSCA service template
168         List<JpaToscaPolicy> jpaPolicyList = dao.getFiltered(JpaToscaPolicy.class, name, version);
169         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().putAll(asConceptMap(jpaPolicyList));
170         return serviceTemplate;
171     }
172
173     /**
174      * Create policies.
175      *
176      * @param dao the DAO to use to access the database
177      * @param serviceTemplate the service template containing the definitions of the new policies to be created.
178      * @return the TOSCA service template containing the policy types that were created
179      * @throws PfModelException on errors creating policies
180      */
181     public JpaToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
182             @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
183
184         ToscaUtils.assertPoliciesExist(serviceTemplate);
185
186         for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
187             dao.create(policy);
188         }
189
190         // Return the created policy types
191         JpaToscaPolicies returnPolicies = new JpaToscaPolicies();
192         returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
193
194         for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
195             returnPolicies.getConceptMap().put(policyKey, dao.get(JpaToscaPolicy.class, policyKey));
196         }
197
198         serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
199
200         return serviceTemplate;
201     }
202
203     /**
204      * Update policies.
205      *
206      * @param dao the DAO to use to access the database
207      * @param serviceTemplate the service template containing the definitions of the policies to be updated.
208      * @return the TOSCA service template containing the policies that were updated
209      * @throws PfModelException on errors updating policies
210      */
211     public JpaToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
212             @NonNull final JpaToscaServiceTemplate serviceTemplate) throws PfModelException {
213
214         ToscaUtils.assertPoliciesExist(serviceTemplate);
215
216         for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
217             dao.update(policy);
218         }
219
220         // Return the created policy types
221         JpaToscaPolicies returnPolicies = new JpaToscaPolicies();
222         returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
223
224         for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
225             returnPolicies.getConceptMap().put(policyKey, dao.get(JpaToscaPolicy.class, policyKey));
226         }
227
228         serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
229
230         return serviceTemplate;
231     }
232
233     /**
234      * Delete policies.
235      *
236      * @param dao the DAO to use to access the database
237      * @param policyKey the policy key
238      * @return the TOSCA service template containing the policies that were deleted
239      * @throws PfModelException on errors deleting policies
240      */
241     public JpaToscaServiceTemplate deletePolicy(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
242             throws PfModelException {
243
244         JpaToscaServiceTemplate serviceTemplate = getPolicies(dao, policyKey.getName(), policyKey.getVersion());
245
246         dao.delete(JpaToscaPolicy.class, policyKey);
247
248         return serviceTemplate;
249     }
250
251     /**
252      * Convert a list of concepts to a map of concepts.
253      *
254      * @param conceptList the concept list
255      * @return the concept map
256      */
257     private <T extends PfConcept> Map<PfConceptKey, T> asConceptMap(List<T> conceptList) {
258         Map<PfConceptKey, T> conceptMap = new LinkedHashMap<>();
259         for (T concept : conceptList) {
260             conceptMap.put((PfConceptKey) concept.getKey(), concept);
261         }
262
263         return conceptMap;
264     }
265 }