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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.legacy.provider;
24 import java.util.List;
27 import javax.ws.rs.core.Response;
29 import lombok.NonNull;
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;
49 * This class provides the provision of information on TOSCA concepts in the database to callers in legacy formats.
51 * @author Liam Fallon (liam.fallon@est.tech)
53 public class LegacyProvider {
54 private static final Logger LOGGER = LoggerFactory.getLogger(LegacyProvider.class);
56 public static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";
58 // Recurring constants
59 private static final String NO_POLICY_FOUND_FOR_POLICY = "no policy found for policy: ";
62 * Get legacy operational policy.
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
70 public LegacyOperationalPolicy getOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
71 final String policyVersion) throws PfModelException {
73 LOGGER.debug("->getOperationalPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
75 LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicyMapper()
76 .fromToscaServiceTemplate(getLegacyPolicy(dao, policyId, policyVersion));
78 LOGGER.debug("<-getOperationalPolicy: policyId={}, policyVersion={}, legacyOperationalPolicy={}", policyId,
79 policyVersion, legacyOperationalPolicy);
80 return legacyOperationalPolicy;
84 * Create legacy operational policy.
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
91 public LegacyOperationalPolicy createOperationalPolicy(@NonNull final PfDao dao,
92 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
94 LOGGER.debug("->createOperationalPolicy: legacyOperationalPolicy={}", legacyOperationalPolicy);
96 JpaToscaServiceTemplate legacyOperationalServiceTemplate =
97 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
99 new SimpleToscaProvider().createPolicies(dao, legacyOperationalServiceTemplate);
101 LegacyOperationalPolicy createdLegacyOperationalPolicy =
102 new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(legacyOperationalServiceTemplate);
104 LOGGER.debug("<-createOperationalPolicy: createdLegacyOperationalPolicy={}", createdLegacyOperationalPolicy);
105 return createdLegacyOperationalPolicy;
109 * Update legacy operational policy.
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
116 public LegacyOperationalPolicy updateOperationalPolicy(@NonNull final PfDao dao,
117 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
119 LOGGER.debug("->updateOperationalPolicy: legacyOperationalPolicy={}", legacyOperationalPolicy);
120 JpaToscaServiceTemplate incomingServiceTemplate =
121 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
122 JpaToscaServiceTemplate outgoingingServiceTemplate =
123 new SimpleToscaProvider().updatePolicies(dao, incomingServiceTemplate);
125 LegacyOperationalPolicy updatedLegacyOperationalPolicy =
126 new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
128 LOGGER.debug("<-updateOperationalPolicy: updatedLegacyOperationalPolicy={}", updatedLegacyOperationalPolicy);
129 return updatedLegacyOperationalPolicy;
133 * Delete legacy operational policy.
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
141 public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
142 @NonNull final String policyVersion) throws PfModelException {
144 LOGGER.debug("->deleteOperationalPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
146 LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicyMapper()
147 .fromToscaServiceTemplate(deleteLegacyPolicy(dao, policyId, policyVersion));
149 LOGGER.debug("<-deleteOperationalPolicy: policyId={}, policyVersion={}, legacyOperationalPolicy={}", policyId,
150 policyVersion, legacyOperationalPolicy);
151 return legacyOperationalPolicy;
155 * Get legacy guard policy.
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
163 public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
164 final String policyVersion) throws PfModelException {
166 LOGGER.debug("->getGuardPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
168 Map<String, LegacyGuardPolicyOutput> legacyGuardPolicyMap =
169 new LegacyGuardPolicyMapper().fromToscaServiceTemplate(getLegacyPolicy(dao, policyId, policyVersion));
171 LOGGER.debug("<-getGuardPolicy: policyId={}, policyVersion={}, legacyGuardPolicyMap={}", policyId,
172 policyVersion, legacyGuardPolicyMap);
173 return legacyGuardPolicyMap;
177 * Create legacy guard policy.
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
184 public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(@NonNull final PfDao dao,
185 @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
187 LOGGER.debug("->createGuardPolicy: legacyGuardPolicy={}", legacyGuardPolicy);
189 JpaToscaServiceTemplate incomingServiceTemplate =
190 new LegacyGuardPolicyMapper().toToscaServiceTemplate(legacyGuardPolicy);
191 JpaToscaServiceTemplate outgoingingServiceTemplate =
192 new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate);
194 Map<String, LegacyGuardPolicyOutput> createdLegacyGuardPolicyMap =
195 new LegacyGuardPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
197 LOGGER.debug("<-createGuardPolicy: createdLegacyGuardPolicyMap={}", createdLegacyGuardPolicyMap);
198 return createdLegacyGuardPolicyMap;
202 * Update legacy guard policy.
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
209 public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(@NonNull final PfDao dao,
210 @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
212 LOGGER.debug("->updateGuardPolicy: legacyGuardPolicy={}", legacyGuardPolicy);
214 JpaToscaServiceTemplate incomingServiceTemplate =
215 new LegacyGuardPolicyMapper().toToscaServiceTemplate(legacyGuardPolicy);
216 JpaToscaServiceTemplate outgoingingServiceTemplate =
217 new SimpleToscaProvider().updatePolicies(dao, incomingServiceTemplate);
219 Map<String, LegacyGuardPolicyOutput> updatedLegacyGuardPolicyMap =
220 new LegacyGuardPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
222 LOGGER.debug("<-updateGuardPolicy: updatedLegacyGuardPolicyMap={}", updatedLegacyGuardPolicyMap);
223 return updatedLegacyGuardPolicyMap;
227 * Delete legacy guard policy.
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
235 public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final PfDao dao,
236 @NonNull final String policyId, @NonNull final String policyVersion) throws PfModelException {
238 LOGGER.debug("->deleteGuardPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
239 Map<String, LegacyGuardPolicyOutput> legacyGuardPolicyMap = new LegacyGuardPolicyMapper()
240 .fromToscaServiceTemplate(deleteLegacyPolicy(dao, policyId, policyVersion));
242 LOGGER.debug("<-deleteGuardPolicy: policyId={}, policyVersion={}, legacyGuardPolicyMap={}", policyId,
243 policyVersion, legacyGuardPolicyMap);
244 return legacyGuardPolicyMap;
248 * Get the JPA Policy for a policy ID and version.
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
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);
262 foundPolicy = dao.get(JpaToscaPolicy.class,
263 new PfConceptKey(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
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);
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);
278 return serviceTemplate;
282 * Delete a legacy policy.
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
289 private JpaToscaServiceTemplate deleteLegacyPolicy(final PfDao dao, final String policyId,
290 final String policyVersion) {
292 final JpaToscaPolicy deletePolicy =
293 dao.get(JpaToscaPolicy.class, new PfConceptKey(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
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);
302 dao.delete(deletePolicy);
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);
310 return serviceTemplate;
314 * Get the latest policy for a policy ID.
316 * @param dao The DAO to read from
317 * @param policyId the ID of the policy
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);
324 // Find the latest policy that matches the ID
325 JpaToscaPolicy newestPolicy = null;
327 for (JpaToscaPolicy policy : policyList) {
328 if (!policyId.equals(policy.getKey().getName())) {
332 // We found a matching policy
333 if (newestPolicy == null || policy.getKey().isNewerThan(newestPolicy.getKey())) {
334 // First policy found
335 newestPolicy = policy;