Add version on legacy get/delete
[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         return new LegacyOperationalPolicyMapper()
74                 .fromToscaServiceTemplate(getLegacyPolicy(dao, policyId, policyVersion));
75     }
76
77     /**
78      * Create legacy operational policy.
79      *
80      * @param dao the DAO to use to access the database
81      * @param legacyOperationalPolicy the definition of the policy to be created.
82      * @return the created policy
83      * @throws PfModelException on errors creating policies
84      */
85     public LegacyOperationalPolicy createOperationalPolicy(@NonNull final PfDao dao,
86             @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
87
88         // We need to find the latest policy and update the major version, if there is no policy with this ID, then
89         // we set it to the first version
90         JpaToscaPolicy newestPolicy = getLatestPolicy(dao, legacyOperationalPolicy.getPolicyId());
91
92         if (newestPolicy == null) {
93             legacyOperationalPolicy.setPolicyVersion(FIRST_POLICY_VERSION);
94         } else {
95             legacyOperationalPolicy.setPolicyVersion(Integer.toString(newestPolicy.getKey().getMajorVersion() + 1));
96         }
97
98         JpaToscaServiceTemplate incomingServiceTemplate =
99                 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
100         JpaToscaServiceTemplate outgoingingServiceTemplate =
101                 new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate);
102
103         return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
104     }
105
106     /**
107      * Update legacy operational policy.
108      *
109      * @param dao the DAO to use to access the database
110      * @param legacyOperationalPolicy the definition of the policy to be updated
111      * @return the updated policy
112      * @throws PfModelException on errors updating policies
113      */
114     public LegacyOperationalPolicy updateOperationalPolicy(@NonNull final PfDao dao,
115             @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
116
117         JpaToscaServiceTemplate incomingServiceTemplate =
118                 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
119         JpaToscaServiceTemplate outgoingingServiceTemplate =
120                 new SimpleToscaProvider().updatePolicies(dao, incomingServiceTemplate);
121
122         return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
123     }
124
125     /**
126      * Delete legacy operational policy.
127      *
128      * @param dao the DAO to use to access the database
129      * @param policyId ID of the policy.
130      * @param policyVersion version of the policy.
131      * @return the deleted policy
132      * @throws PfModelException on errors deleting policies
133      */
134     public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
135             @NonNull final String policyVersion) throws PfModelException {
136
137         return new LegacyOperationalPolicyMapper()
138                 .fromToscaServiceTemplate(deleteLegacyPolicy(dao, policyId, policyVersion));
139     }
140
141     /**
142      * Get legacy guard policy.
143      *
144      * @param dao the DAO to use to access the database
145      * @param policyId ID of the policy.
146      * @param policyVersion version of the policy.
147      * @return the policies found
148      * @throws PfModelException on errors getting policies
149      */
150     public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
151             final String policyVersion) throws PfModelException {
152
153         return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(getLegacyPolicy(dao, policyId, policyVersion));
154     }
155
156     /**
157      * Create legacy guard policy.
158      *
159      * @param dao the DAO to use to access the database
160      * @param legacyGuardPolicy the definition of the policy to be created.
161      * @return the created policy
162      * @throws PfModelException on errors creating policies
163      */
164     public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(@NonNull final PfDao dao,
165             @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
166
167         JpaToscaServiceTemplate incomingServiceTemplate =
168                 new LegacyGuardPolicyMapper().toToscaServiceTemplate(legacyGuardPolicy);
169         JpaToscaServiceTemplate outgoingingServiceTemplate =
170                 new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate);
171
172         return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
173     }
174
175     /**
176      * Update legacy guard policy.
177      *
178      * @param dao the DAO to use to access the database
179      * @param legacyGuardPolicy the definition of the policy to be updated
180      * @return the updated policy
181      * @throws PfModelException on errors updating policies
182      */
183     public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(@NonNull final PfDao dao,
184             @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
185
186         JpaToscaServiceTemplate incomingServiceTemplate =
187                 new LegacyGuardPolicyMapper().toToscaServiceTemplate(legacyGuardPolicy);
188         JpaToscaServiceTemplate outgoingingServiceTemplate =
189                 new SimpleToscaProvider().updatePolicies(dao, incomingServiceTemplate);
190
191         return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
192     }
193
194
195     /**
196      * Delete legacy guard policy.
197      *
198      * @param dao the DAO to use to access the database
199      * @param policyId ID of the policy.
200      * @param policyVersion version of the policy.
201      * @return the deleted policy
202      * @throws PfModelException on errors deleting policies
203      */
204     public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final PfDao dao,
205             @NonNull final String policyId, @NonNull final String policyVersion) throws PfModelException {
206
207         return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(deleteLegacyPolicy(dao, policyId, policyVersion));
208     }
209
210     /**
211      * Get the JPA Policy for a policy ID and version.
212      *
213      * @param dao The DAO to search
214      * @param policyId the policy ID to search for
215      * @param policyVersion the policy version to search for
216      * @return the JPA policy found
217      * @throws PfModelRuntimeException if a policy is not found
218      */
219     private JpaToscaServiceTemplate getLegacyPolicy(final PfDao dao, final String policyId,
220             final String policyVersion) {
221         JpaToscaPolicy foundPolicy = null;
222         if (policyVersion == null) {
223             foundPolicy = getLatestPolicy(dao, policyId);
224         } else {
225             foundPolicy = dao.get(JpaToscaPolicy.class,
226                     new PfConceptKey(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
227         }
228
229         if (foundPolicy == null) {
230             String errorMessage = NO_POLICY_FOUND_FOR_POLICY + policyId + ':' + policyVersion;
231             LOGGER.warn(errorMessage);
232             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
233         }
234
235         // Create the structure of the TOSCA service template to contain the policy type
236         JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
237         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
238         serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
239         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(foundPolicy.getKey(), foundPolicy);
240
241         return serviceTemplate;
242     }
243
244     /**
245      * Delete a legacy policy.
246      *
247      * @param dao the DAO to use for the deletion
248      * @param policyId the policy ID
249      * @param policyVersion the policy version
250      * @return a service template containing the policy that has been deleted
251      */
252     private JpaToscaServiceTemplate deleteLegacyPolicy(final PfDao dao, final String policyId,
253             final String policyVersion) {
254
255         final JpaToscaPolicy deletePolicy =
256                 dao.get(JpaToscaPolicy.class, new PfConceptKey(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
257
258         if (deletePolicy == null) {
259             String errorMessage = NO_POLICY_FOUND_FOR_POLICY + policyId + ':' + policyVersion;
260             LOGGER.warn(errorMessage);
261             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
262         }
263
264         // Delete the policy
265         dao.delete(deletePolicy);
266
267         // Create the structure of the TOSCA service template to contain the policy type
268         JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
269         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
270         serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
271         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(deletePolicy.getKey(), deletePolicy);
272
273         return serviceTemplate;
274     }
275
276     /**
277      * Get the latest policy for a policy ID.
278      *
279      * @param dao The DAO to read from
280      * @param policyId the ID of the policy
281      * @return the policy
282      */
283     private JpaToscaPolicy getLatestPolicy(final PfDao dao, final String policyId) {
284         // Get all the policies in the database and check the policy ID against the policies returned
285         List<JpaToscaPolicy> policyList = dao.getAll(JpaToscaPolicy.class);
286
287         // Find the latest policy that matches the ID
288         JpaToscaPolicy newestPolicy = null;
289
290         for (JpaToscaPolicy policy : policyList) {
291             if (!policyId.equals(policy.getKey().getName())) {
292                 continue;
293             }
294
295             // We found a matching policy
296             if (newestPolicy == null || policy.getKey().isNewerThan(newestPolicy.getKey())) {
297                 // First policy found
298                 newestPolicy = policy;
299             }
300         }
301         return newestPolicy;
302     }
303
304 }