TOSCA Compliant Guard Policies
[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
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.LegacyOperationalPolicy;
35 import org.onap.policy.models.tosca.legacy.mapping.LegacyOperationalPolicyMapper;
36 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
38 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
39 import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate;
40 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * This class provides the provision of information on TOSCA concepts in the database to callers in legacy formats.
46  *
47  * @author Liam Fallon (liam.fallon@est.tech)
48  */
49 public class LegacyProvider {
50     private static final Logger LOGGER = LoggerFactory.getLogger(LegacyProvider.class);
51
52     public static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";
53
54     // Recurring constants
55     private static final String NO_POLICY_FOUND_FOR_POLICY = "no policy found for policy: ";
56
57     /**
58      * Get legacy operational policy.
59      *
60      * @param dao the DAO to use to access the database
61      * @param policyId ID of the policy.
62      * @param policyVersion version of the policy.
63      * @return the policies found
64      * @throws PfModelException on errors getting policies
65      */
66     public LegacyOperationalPolicy getOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
67             final String policyVersion) throws PfModelException {
68
69         LOGGER.debug("->getOperationalPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
70
71         LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicyMapper()
72                 .fromToscaServiceTemplate(getLegacyPolicy(dao, policyId, policyVersion));
73
74         LOGGER.debug("<-getOperationalPolicy: policyId={}, policyVersion={}, legacyOperationalPolicy={}", policyId,
75                 policyVersion, legacyOperationalPolicy);
76         return legacyOperationalPolicy;
77     }
78
79     /**
80      * Create legacy operational policy.
81      *
82      * @param dao the DAO to use to access the database
83      * @param legacyOperationalPolicy the definition of the policy to be created.
84      * @return the created policy
85      * @throws PfModelException on errors creating policies
86      */
87     public LegacyOperationalPolicy createOperationalPolicy(@NonNull final PfDao dao,
88             @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
89
90         LOGGER.debug("->createOperationalPolicy: legacyOperationalPolicy={}", legacyOperationalPolicy);
91
92         JpaToscaServiceTemplate legacyOperationalServiceTemplate =
93                 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
94
95         new SimpleToscaProvider().createPolicies(dao, legacyOperationalServiceTemplate);
96
97         LegacyOperationalPolicy createdLegacyOperationalPolicy =
98                 new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(legacyOperationalServiceTemplate);
99
100         LOGGER.debug("<-createOperationalPolicy: createdLegacyOperationalPolicy={}", createdLegacyOperationalPolicy);
101         return createdLegacyOperationalPolicy;
102     }
103
104     /**
105      * Update legacy operational policy.
106      *
107      * @param dao the DAO to use to access the database
108      * @param legacyOperationalPolicy the definition of the policy to be updated
109      * @return the updated policy
110      * @throws PfModelException on errors updating policies
111      */
112     public LegacyOperationalPolicy updateOperationalPolicy(@NonNull final PfDao dao,
113             @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
114
115         LOGGER.debug("->updateOperationalPolicy: legacyOperationalPolicy={}", legacyOperationalPolicy);
116         JpaToscaServiceTemplate incomingServiceTemplate =
117                 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
118         JpaToscaServiceTemplate outgoingingServiceTemplate =
119                 new SimpleToscaProvider().updatePolicies(dao, incomingServiceTemplate);
120
121         LegacyOperationalPolicy updatedLegacyOperationalPolicy =
122                 new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
123
124         LOGGER.debug("<-updateOperationalPolicy: updatedLegacyOperationalPolicy={}", updatedLegacyOperationalPolicy);
125         return updatedLegacyOperationalPolicy;
126     }
127
128     /**
129      * Delete legacy operational policy.
130      *
131      * @param dao the DAO to use to access the database
132      * @param policyId ID of the policy.
133      * @param policyVersion version of the policy.
134      * @return the deleted policy
135      * @throws PfModelException on errors deleting policies
136      */
137     public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
138             @NonNull final String policyVersion) throws PfModelException {
139
140         LOGGER.debug("->deleteOperationalPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
141
142         LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicyMapper()
143                 .fromToscaServiceTemplate(deleteLegacyPolicy(dao, policyId, policyVersion));
144
145         LOGGER.debug("<-deleteOperationalPolicy: policyId={}, policyVersion={}, legacyOperationalPolicy={}", policyId,
146                 policyVersion, legacyOperationalPolicy);
147         return legacyOperationalPolicy;
148     }
149
150     /**
151      * Get the JPA Policy for a policy ID and version.
152      *
153      * @param dao The DAO to search
154      * @param policyId the policy ID to search for
155      * @param policyVersion the policy version to search for
156      * @return the JPA policy found
157      * @throws PfModelRuntimeException if a policy is not found
158      */
159     private JpaToscaServiceTemplate getLegacyPolicy(final PfDao dao, final String policyId,
160             final String policyVersion) {
161         JpaToscaPolicy foundPolicy = null;
162         if (policyVersion == null) {
163             foundPolicy = getLatestPolicy(dao, policyId);
164         } else {
165             foundPolicy = dao.get(JpaToscaPolicy.class,
166                     new PfConceptKey(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
167         }
168
169         if (foundPolicy == null) {
170             String errorMessage = NO_POLICY_FOUND_FOR_POLICY + policyId + ':' + policyVersion;
171             LOGGER.warn(errorMessage);
172             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
173         }
174
175         // Create the structure of the TOSCA service template to contain the policy type
176         JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
177         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
178         serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
179         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(foundPolicy.getKey(), foundPolicy);
180
181         return serviceTemplate;
182     }
183
184     /**
185      * Delete a legacy policy.
186      *
187      * @param dao the DAO to use for the deletion
188      * @param policyId the policy ID
189      * @param policyVersion the policy version
190      * @return a service template containing the policy that has been deleted
191      */
192     private JpaToscaServiceTemplate deleteLegacyPolicy(final PfDao dao, final String policyId,
193             final String policyVersion) {
194
195         final JpaToscaPolicy deletePolicy =
196                 dao.get(JpaToscaPolicy.class, new PfConceptKey(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
197
198         if (deletePolicy == null) {
199             String errorMessage = NO_POLICY_FOUND_FOR_POLICY + policyId + ':' + policyVersion;
200             LOGGER.warn(errorMessage);
201             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
202         }
203
204         // Delete the policy
205         dao.delete(deletePolicy);
206
207         // Create the structure of the TOSCA service template to contain the policy type
208         JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
209         serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
210         serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
211         serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(deletePolicy.getKey(), deletePolicy);
212
213         return serviceTemplate;
214     }
215
216     /**
217      * Get the latest policy for a policy ID.
218      *
219      * @param dao The DAO to read from
220      * @param policyId the ID of the policy
221      * @return the policy
222      */
223     private JpaToscaPolicy getLatestPolicy(final PfDao dao, final String policyId) {
224         // Get all the policies in the database and check the policy ID against the policies returned
225         List<JpaToscaPolicy> policyList = dao.getAll(JpaToscaPolicy.class);
226
227         // Find the latest policy that matches the ID
228         JpaToscaPolicy newestPolicy = null;
229
230         for (JpaToscaPolicy policy : policyList) {
231             if (!policyId.equals(policy.getKey().getName())) {
232                 continue;
233             }
234
235             // We found a matching policy
236             if (newestPolicy == null || policy.getKey().isNewerThan(newestPolicy.getKey())) {
237                 // First policy found
238                 newestPolicy = policy;
239             }
240         }
241         return newestPolicy;
242     }
243
244 }