Add Legacy Op Policy Persistence
[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.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 ToscaServiceTemplate 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         ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
63         serviceTemplate.setPolicyTypes(new ToscaPolicyTypes());
64
65         // Add the policy type to the TOSCA service template
66         ToscaPolicyType policyType = dao.get(ToscaPolicyType.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 ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
86             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
87
88         ToscaUtils.assertPolicyTypesExist(serviceTemplate);
89
90         for (ToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
91             dao.create(policyType);
92         }
93
94         // Return the created policy types
95         ToscaPolicyTypes returnPolicyTypes = new ToscaPolicyTypes();
96
97         for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
98             returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(ToscaPolicyType.class, policyTypeKey));
99         }
100
101         ToscaServiceTemplate returnServiceTemplate = new ToscaServiceTemplate();
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 ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
116             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
117
118         ToscaUtils.assertPolicyTypesExist(serviceTemplate);
119
120         for (ToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
121             dao.update(policyType);
122         }
123
124         // Return the created policy types
125         ToscaPolicyTypes returnPolicyTypes = new ToscaPolicyTypes();
126
127         for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
128             returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(ToscaPolicyType.class, policyTypeKey));
129         }
130
131         ToscaServiceTemplate returnServiceTemplate = new ToscaServiceTemplate();
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 ToscaServiceTemplate deletePolicyTypes(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey)
147             throws PfModelException {
148
149         ToscaServiceTemplate serviceTemplate = getPolicyTypes(dao, policyTypeKey);
150
151         dao.delete(ToscaPolicyType.class, policyTypeKey);
152
153         return serviceTemplate;
154     }
155
156     /**
157      * Get policies.
158      *
159      * @param dao the DAO to use to access the database
160      * @param policyKey the policy key for the policies to be retrieved. The parent name and version must be specified.
161      *        A null local name returns all policies for a parent policy type.
162      * @return the policies found
163      * @throws PfModelException on errors getting policies
164      */
165     public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
166             throws PfModelException {
167
168         // Create the structure of the TOSCA service template to contain the policy type
169         ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
170         serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate());
171         serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies());
172
173         // Add the policy to the TOSCA service template
174         ToscaPolicy policy = dao.get(ToscaPolicy.class, policyKey);
175         if (policy != null) {
176             serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, policy);
177             return serviceTemplate;
178         } else {
179             String errorMessage = "policy not found: " + policyKey.getId();
180             LOGGER.warn(errorMessage);
181             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
182         }
183     }
184
185     /**
186      * Create policies.
187      *
188      * @param dao the DAO to use to access the database
189      * @param serviceTemplate the service template containing the definitions of the new policies to be created.
190      * @return the TOSCA service template containing the policy types that were created
191      * @throws PfModelException on errors creating policies
192      */
193     public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
194             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
195
196         ToscaUtils.assertPoliciesExist(serviceTemplate);
197
198         for (ToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
199             dao.create(policy);
200         }
201
202         // Return the created policy types
203         ToscaPolicies returnPolicies = new ToscaPolicies();
204         returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
205
206         for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
207             returnPolicies.getConceptMap().put(policyKey, dao.get(ToscaPolicy.class, policyKey));
208         }
209
210         serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
211
212         return serviceTemplate;
213     }
214
215     /**
216      * Update policies.
217      *
218      * @param dao the DAO to use to access the database
219      * @param serviceTemplate the service template containing the definitions of the policies to be updated.
220      * @return the TOSCA service template containing the policies that were updated
221      * @throws PfModelException on errors updating policies
222      */
223     public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
224             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
225
226         ToscaUtils.assertPoliciesExist(serviceTemplate);
227
228         for (ToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
229             dao.update(policy);
230         }
231
232         // Return the created policy types
233         ToscaPolicies returnPolicies = new ToscaPolicies();
234         returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
235
236         for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
237             returnPolicies.getConceptMap().put(policyKey, dao.get(ToscaPolicy.class, policyKey));
238         }
239
240         serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
241
242         return serviceTemplate;
243     }
244
245     /**
246      * Delete policies.
247      *
248      * @param dao the DAO to use to access the database
249      * @param policyKey the policy key
250      * @return the TOSCA service template containing the policies that were deleted
251      * @throws PfModelException on errors deleting policies
252      */
253     public ToscaServiceTemplate deletePolicies(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
254             throws PfModelException {
255
256         ToscaServiceTemplate serviceTemplate = getPolicies(dao, policyKey);
257
258         dao.delete(ToscaPolicy.class, policyKey);
259
260         return serviceTemplate;
261     }
262 }