dc8affc779299a6f1c210c924a8b63a920bfe4e8
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / legacy / provider / LegacyProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
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.models.tosca.legacy.provider;
23
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.ws.rs.core.Response;
28
29 import lombok.NonNull;
30
31 import org.onap.policy.models.base.PfConceptKey;
32 import org.onap.policy.models.base.PfModelException;
33 import org.onap.policy.models.base.PfModelRuntimeException;
34 import org.onap.policy.models.dao.PfDao;
35 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
36 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
37 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
38 import org.onap.policy.models.tosca.legacy.mapping.LegacyGuardPolicyMapper;
39 import org.onap.policy.models.tosca.legacy.mapping.LegacyOperationalPolicyMapper;
40 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
41 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
42 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
43 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
44 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * This class provides the provision of information on TOSCA concepts in the database to callers in legacy formats.
50  *
51  * @author Liam Fallon (liam.fallon@est.tech)
52  */
53 public class LegacyProvider {
54     private static final Logger LOGGER = LoggerFactory.getLogger(LegacyProvider.class);
55
56     public static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";
57
58     // Recurring constants
59     private static final String NO_POLICY_FOUND_FOR_POLICY = "no policy found for policy: ";
60
61     /**
62      * Get legacy operational policy.
63      *
64      * @param dao the DAO to use to access the database
65      * @param policyId ID of the policy.
66      * @param policyVersion version of the policy.
67      * @return the policies found
68      * @throws PfModelException on errors getting policies
69      */
70     public LegacyOperationalPolicy getOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
71             final String policyVersion) throws PfModelException {
72
73         LOGGER.debug("->getOperationalPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
74
75         LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicyMapper()
76                 .fromToscaServiceTemplate(getLegacyPolicy(dao, policyId, policyVersion));
77
78         LOGGER.debug("<-getOperationalPolicy: policyId={}, policyVersion={}, legacyOperationalPolicy={}", policyId,
79                 policyVersion, legacyOperationalPolicy);
80         return legacyOperationalPolicy;
81     }
82
83     /**
84      * Create legacy operational policy.
85      *
86      * @param dao the DAO to use to access the database
87      * @param legacyOperationalPolicy the definition of the policy to be created.
88      * @return the created policy
89      * @throws PfModelException on errors creating policies
90      */
91     public LegacyOperationalPolicy createOperationalPolicy(@NonNull final PfDao dao,
92             @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
93
94         LOGGER.debug("->createOperationalPolicy: legacyOperationalPolicy={}", legacyOperationalPolicy);
95
96         JpaToscaServiceTemplate legacyOperationalServiceTemplate =
97                 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
98
99         new SimpleToscaProvider().createPolicies(dao, legacyOperationalServiceTemplate);
100
101         LegacyOperationalPolicy createdLegacyOperationalPolicy =
102                 new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(legacyOperationalServiceTemplate);
103
104         LOGGER.debug("<-createOperationalPolicy: createdLegacyOperationalPolicy={}", createdLegacyOperationalPolicy);
105         return createdLegacyOperationalPolicy;
106     }
107
108     /**
109      * Update legacy operational policy.
110      *
111      * @param dao the DAO to use to access the database
112      * @param legacyOperationalPolicy the definition of the policy to be updated
113      * @return the updated policy
114      * @throws PfModelException on errors updating policies
115      */
116     public LegacyOperationalPolicy updateOperationalPolicy(@NonNull final PfDao dao,
117             @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
118
119         LOGGER.debug("->updateOperationalPolicy: legacyOperationalPolicy={}", legacyOperationalPolicy);
120         JpaToscaServiceTemplate incomingServiceTemplate =
121                 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
122         JpaToscaServiceTemplate outgoingingServiceTemplate =
123                 new SimpleToscaProvider().updatePolicies(dao, incomingServiceTemplate);
124
125         LegacyOperationalPolicy updatedLegacyOperationalPolicy =
126                 new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
127
128         LOGGER.debug("<-updateOperationalPolicy: updatedLegacyOperationalPolicy={}", updatedLegacyOperationalPolicy);
129         return updatedLegacyOperationalPolicy;
130     }
131
132     /**
133      * Delete legacy operational policy.
134      *
135      * @param dao the DAO to use to access the database
136      * @param policyId ID of the policy.
137      * @param policyVersion version of the policy.
138      * @return the deleted policy
139      * @throws PfModelException on errors deleting policies
140      */
141     public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
142             @NonNull final String policyVersion) throws PfModelException {
143
144         LOGGER.debug("->deleteOperationalPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
145
146         LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicyMapper()
147                 .fromToscaServiceTemplate(deleteLegacyPolicy(dao, policyId, policyVersion));
148
149         LOGGER.debug("<-deleteOperationalPolicy: policyId={}, policyVersion={}, legacyOperationalPolicy={}", policyId,
150                 policyVersion, legacyOperationalPolicy);
151         return legacyOperationalPolicy;
152     }
153
154     /**
155      * Get legacy guard policy.
156      *
157      * @param dao the DAO to use to access the database
158      * @param policyId ID of the policy.
159      * @param policyVersion version of the policy.
160      * @return the policies found
161      * @throws PfModelException on errors getting policies
162      */
163     public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
164             final String policyVersion) throws PfModelException {
165
166         LOGGER.debug("->getGuardPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
167
168         Map<String, LegacyGuardPolicyOutput> legacyGuardPolicyMap =
169                 new LegacyGuardPolicyMapper().fromToscaServiceTemplate(getLegacyPolicy(dao, policyId, policyVersion));
170
171         LOGGER.debug("<-getGuardPolicy: policyId={}, policyVersion={}, legacyGuardPolicyMap={}", policyId,
172                 policyVersion, legacyGuardPolicyMap);
173         return legacyGuardPolicyMap;
174     }
175
176     /**
177      * Create legacy guard policy.
178      *
179      * @param dao the DAO to use to access the database
180      * @param legacyGuardPolicy the definition of the policy to be created.
181      * @return the created policy
182      * @throws PfModelException on errors creating policies
183      */
184     public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(@NonNull final PfDao dao,
185             @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
186
187         LOGGER.debug("->createGuardPolicy: legacyGuardPolicy={}", legacyGuardPolicy);
188
189         JpaToscaServiceTemplate incomingServiceTemplate =
190                 new LegacyGuardPolicyMapper().toToscaServiceTemplate(legacyGuardPolicy);
191         JpaToscaServiceTemplate outgoingingServiceTemplate =
192                 new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate);
193
194         Map<String, LegacyGuardPolicyOutput> createdLegacyGuardPolicyMap =
195                 new LegacyGuardPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
196
197         LOGGER.debug("<-createGuardPolicy: createdLegacyGuardPolicyMap={}", createdLegacyGuardPolicyMap);
198         return createdLegacyGuardPolicyMap;
199     }
200
201     /**
202      * Update legacy guard policy.
203      *
204      * @param dao the DAO to use to access the database
205      * @param legacyGuardPolicy the definition of the policy to be updated
206      * @return the updated policy
207      * @throws PfModelException on errors updating policies
208      */
209     public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(@NonNull final PfDao dao,
210             @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
211
212         LOGGER.debug("->updateGuardPolicy: legacyGuardPolicy={}", legacyGuardPolicy);
213
214         JpaToscaServiceTemplate incomingServiceTemplate =
215                 new LegacyGuardPolicyMapper().toToscaServiceTemplate(legacyGuardPolicy);
216         JpaToscaServiceTemplate outgoingingServiceTemplate =
217                 new SimpleToscaProvider().updatePolicies(dao, incomingServiceTemplate);
218
219         Map<String, LegacyGuardPolicyOutput> updatedLegacyGuardPolicyMap =
220                 new LegacyGuardPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
221
222         LOGGER.debug("<-updateGuardPolicy: updatedLegacyGuardPolicyMap={}", updatedLegacyGuardPolicyMap);
223         return updatedLegacyGuardPolicyMap;
224     }
225
226     /**
227      * Delete legacy guard policy.
228      *
229      * @param dao the DAO to use to access the database
230      * @param policyId ID of the policy.
231      * @param policyVersion version of the policy.
232      * @return the deleted policy
233      * @throws PfModelException on errors deleting policies
234      */
235     public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final PfDao dao,
236             @NonNull final String policyId, @NonNull final String policyVersion) throws PfModelException {
237
238         LOGGER.debug("->deleteGuardPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
239         Map<String, LegacyGuardPolicyOutput> legacyGuardPolicyMap = new LegacyGuardPolicyMapper()
240                 .fromToscaServiceTemplate(deleteLegacyPolicy(dao, policyId, policyVersion));
241
242         LOGGER.debug("<-deleteGuardPolicy: policyId={}, policyVersion={}, legacyGuardPolicyMap={}", policyId,
243                 policyVersion, legacyGuardPolicyMap);
244         return legacyGuardPolicyMap;
245     }
246
247     /**
248      * Get the JPA Policy for a policy ID and version.
249      *
250      * @param dao The DAO to search
251      * @param policyId the policy ID to search for
252      * @param policyVersion the policy version to search for
253      * @return the JPA policy found
254      * @throws PfModelRuntimeException if a policy is not found
255      */
256     private JpaToscaServiceTemplate getLegacyPolicy(final PfDao dao, final String policyId,
257             final String policyVersion) {
258         JpaToscaPolicy foundPolicy = null;
259         if (policyVersion == null) {
260             foundPolicy = getLatestPolicy(dao, policyId);
261         } else {
262             foundPolicy = dao.get(JpaToscaPolicy.class,
263                     new PfConceptKey(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
264         }
265
266         if (foundPolicy == null) {
267             String errorMessage = NO_POLICY_FOUND_FOR_POLICY + policyId + ':' + policyVersion;
268             LOGGER.warn(errorMessage);
269             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
270         }
271
272         // Create the structure of the TOSCA service template to contain the policy type
273         JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
274         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
275         serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
276         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(foundPolicy.getKey(), foundPolicy);
277
278         return serviceTemplate;
279     }
280
281     /**
282      * Delete a legacy policy.
283      *
284      * @param dao the DAO to use for the deletion
285      * @param policyId the policy ID
286      * @param policyVersion the policy version
287      * @return a service template containing the policy that has been deleted
288      */
289     private JpaToscaServiceTemplate deleteLegacyPolicy(final PfDao dao, final String policyId,
290             final String policyVersion) {
291
292         final JpaToscaPolicy deletePolicy =
293                 dao.get(JpaToscaPolicy.class, new PfConceptKey(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
294
295         if (deletePolicy == null) {
296             String errorMessage = NO_POLICY_FOUND_FOR_POLICY + policyId + ':' + policyVersion;
297             LOGGER.warn(errorMessage);
298             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
299         }
300
301         // Delete the policy
302         dao.delete(deletePolicy);
303
304         // Create the structure of the TOSCA service template to contain the policy type
305         JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
306         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
307         serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
308         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(deletePolicy.getKey(), deletePolicy);
309
310         return serviceTemplate;
311     }
312
313     /**
314      * Get the latest policy for a policy ID.
315      *
316      * @param dao The DAO to read from
317      * @param policyId the ID of the policy
318      * @return the policy
319      */
320     private JpaToscaPolicy getLatestPolicy(final PfDao dao, final String policyId) {
321         // Get all the policies in the database and check the policy ID against the policies returned
322         List<JpaToscaPolicy> policyList = dao.getAll(JpaToscaPolicy.class);
323
324         // Find the latest policy that matches the ID
325         JpaToscaPolicy newestPolicy = null;
326
327         for (JpaToscaPolicy policy : policyList) {
328             if (!policyId.equals(policy.getKey().getName())) {
329                 continue;
330             }
331
332             // We found a matching policy
333             if (newestPolicy == null || policy.getKey().isNewerThan(newestPolicy.getKey())) {
334                 // First policy found
335                 newestPolicy = policy;
336             }
337         }
338         return newestPolicy;
339     }
340
341 }