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