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