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;
26 import javax.ws.rs.core.Response;
28 import lombok.NonNull;
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;
45 * This class provides the provision of information on TOSCA concepts in the database to callers in legacy formats.
47 * @author Liam Fallon (liam.fallon@est.tech)
49 public class LegacyProvider {
50 private static final Logger LOGGER = LoggerFactory.getLogger(LegacyProvider.class);
52 public static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";
54 // Recurring constants
55 private static final String NO_POLICY_FOUND_FOR_POLICY = "no policy found for policy: ";
58 * Get legacy operational policy.
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
66 public LegacyOperationalPolicy getOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
67 final String policyVersion) throws PfModelException {
69 LOGGER.debug("->getOperationalPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
71 LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicyMapper()
72 .fromToscaServiceTemplate(getLegacyPolicy(dao, policyId, policyVersion));
74 LOGGER.debug("<-getOperationalPolicy: policyId={}, policyVersion={}, legacyOperationalPolicy={}", policyId,
75 policyVersion, legacyOperationalPolicy);
76 return legacyOperationalPolicy;
80 * Create legacy operational policy.
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
87 public LegacyOperationalPolicy createOperationalPolicy(@NonNull final PfDao dao,
88 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
90 LOGGER.debug("->createOperationalPolicy: legacyOperationalPolicy={}", legacyOperationalPolicy);
92 JpaToscaServiceTemplate legacyOperationalServiceTemplate =
93 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
95 new SimpleToscaProvider().createPolicies(dao, legacyOperationalServiceTemplate);
97 LegacyOperationalPolicy createdLegacyOperationalPolicy =
98 new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(legacyOperationalServiceTemplate);
100 LOGGER.debug("<-createOperationalPolicy: createdLegacyOperationalPolicy={}", createdLegacyOperationalPolicy);
101 return createdLegacyOperationalPolicy;
105 * Update legacy operational policy.
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
112 public LegacyOperationalPolicy updateOperationalPolicy(@NonNull final PfDao dao,
113 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
115 LOGGER.debug("->updateOperationalPolicy: legacyOperationalPolicy={}", legacyOperationalPolicy);
116 JpaToscaServiceTemplate incomingServiceTemplate =
117 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
118 JpaToscaServiceTemplate outgoingingServiceTemplate =
119 new SimpleToscaProvider().updatePolicies(dao, incomingServiceTemplate);
121 LegacyOperationalPolicy updatedLegacyOperationalPolicy =
122 new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
124 LOGGER.debug("<-updateOperationalPolicy: updatedLegacyOperationalPolicy={}", updatedLegacyOperationalPolicy);
125 return updatedLegacyOperationalPolicy;
129 * Delete legacy operational policy.
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
137 public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
138 @NonNull final String policyVersion) throws PfModelException {
140 LOGGER.debug("->deleteOperationalPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
142 LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicyMapper()
143 .fromToscaServiceTemplate(deleteLegacyPolicy(dao, policyId, policyVersion));
145 LOGGER.debug("<-deleteOperationalPolicy: policyId={}, policyVersion={}, legacyOperationalPolicy={}", policyId,
146 policyVersion, legacyOperationalPolicy);
147 return legacyOperationalPolicy;
151 * Get the JPA Policy for a policy ID and version.
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
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);
165 foundPolicy = dao.get(JpaToscaPolicy.class,
166 new PfConceptKey(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
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);
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);
181 return serviceTemplate;
185 * Delete a legacy policy.
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
192 private JpaToscaServiceTemplate deleteLegacyPolicy(final PfDao dao, final String policyId,
193 final String policyVersion) {
195 final JpaToscaPolicy deletePolicy =
196 dao.get(JpaToscaPolicy.class, new PfConceptKey(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
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);
205 dao.delete(deletePolicy);
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);
213 return serviceTemplate;
217 * Get the latest policy for a policy ID.
219 * @param dao The DAO to read from
220 * @param policyId the ID of the policy
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);
227 // Find the latest policy that matches the ID
228 JpaToscaPolicy newestPolicy = null;
230 for (JpaToscaPolicy policy : policyList) {
231 if (!policyId.equals(policy.getKey().getName())) {
235 // We found a matching policy
236 if (newestPolicy == null || policy.getKey().isNewerThan(newestPolicy.getKey())) {
237 // First policy found
238 newestPolicy = policy;