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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.legacy.provider;
23 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.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;
48 * This class provides the provision of information on TOSCA concepts in the database to callers in legacy formats.
50 * @author Liam Fallon (liam.fallon@est.tech)
52 public class LegacyProvider {
53 private static final Logger LOGGER = LoggerFactory.getLogger(LegacyProvider.class);
55 private static final String FIRST_POLICY_VERSION = "1";
56 private 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 // We need to find the latest policy and update the major version, if there is no policy with this ID, then
97 // we set it to the first version
98 JpaToscaPolicy newestPolicy = getLatestPolicy(dao, legacyOperationalPolicy.getPolicyId());
100 if (newestPolicy == null) {
101 legacyOperationalPolicy.setPolicyVersion(FIRST_POLICY_VERSION);
103 legacyOperationalPolicy.setPolicyVersion(Integer.toString(newestPolicy.getKey().getMajorVersion() + 1));
106 JpaToscaServiceTemplate incomingServiceTemplate =
107 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
108 JpaToscaServiceTemplate outgoingingServiceTemplate =
109 new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate);
111 LegacyOperationalPolicy createdLegacyOperationalPolicy =
112 new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
114 LOGGER.debug("<-createOperationalPolicy: createdLegacyOperationalPolicy={}", createdLegacyOperationalPolicy);
115 return createdLegacyOperationalPolicy;
119 * Update legacy operational policy.
121 * @param dao the DAO to use to access the database
122 * @param legacyOperationalPolicy the definition of the policy to be updated
123 * @return the updated policy
124 * @throws PfModelException on errors updating policies
126 public LegacyOperationalPolicy updateOperationalPolicy(@NonNull final PfDao dao,
127 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
129 LOGGER.debug("->updateOperationalPolicy: legacyOperationalPolicy={}", legacyOperationalPolicy);
130 JpaToscaServiceTemplate incomingServiceTemplate =
131 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
132 JpaToscaServiceTemplate outgoingingServiceTemplate =
133 new SimpleToscaProvider().updatePolicies(dao, incomingServiceTemplate);
135 LegacyOperationalPolicy updatedLegacyOperationalPolicy =
136 new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
138 LOGGER.debug("<-updateOperationalPolicy: updatedLegacyOperationalPolicy={}", updatedLegacyOperationalPolicy);
139 return updatedLegacyOperationalPolicy;
143 * Delete legacy operational policy.
145 * @param dao the DAO to use to access the database
146 * @param policyId ID of the policy.
147 * @param policyVersion version of the policy.
148 * @return the deleted policy
149 * @throws PfModelException on errors deleting policies
151 public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
152 @NonNull final String policyVersion) throws PfModelException {
154 LOGGER.debug("->deleteOperationalPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
156 LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicyMapper()
157 .fromToscaServiceTemplate(deleteLegacyPolicy(dao, policyId, policyVersion));
159 LOGGER.debug("<-deleteOperationalPolicy: policyId={}, policyVersion={}, legacyOperationalPolicy={}", policyId,
160 policyVersion, legacyOperationalPolicy);
161 return legacyOperationalPolicy;
165 * Get legacy guard policy.
167 * @param dao the DAO to use to access the database
168 * @param policyId ID of the policy.
169 * @param policyVersion version of the policy.
170 * @return the policies found
171 * @throws PfModelException on errors getting policies
173 public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
174 final String policyVersion) throws PfModelException {
176 LOGGER.debug("->getGuardPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
178 Map<String, LegacyGuardPolicyOutput> legacyGuardPolicyMap =
179 new LegacyGuardPolicyMapper().fromToscaServiceTemplate(getLegacyPolicy(dao, policyId, policyVersion));
181 LOGGER.debug("<-getGuardPolicy: policyId={}, policyVersion={}, legacyGuardPolicyMap={}", policyId,
182 policyVersion, legacyGuardPolicyMap);
183 return legacyGuardPolicyMap;
187 * Create legacy guard policy.
189 * @param dao the DAO to use to access the database
190 * @param legacyGuardPolicy the definition of the policy to be created.
191 * @return the created policy
192 * @throws PfModelException on errors creating policies
194 public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(@NonNull final PfDao dao,
195 @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
197 LOGGER.debug("->createGuardPolicy: legacyGuardPolicy={}", legacyGuardPolicy);
199 JpaToscaServiceTemplate incomingServiceTemplate =
200 new LegacyGuardPolicyMapper().toToscaServiceTemplate(legacyGuardPolicy);
201 JpaToscaServiceTemplate outgoingingServiceTemplate =
202 new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate);
204 Map<String, LegacyGuardPolicyOutput> createdLegacyGuardPolicyMap =
205 new LegacyGuardPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
207 LOGGER.debug("<-createGuardPolicy: createdLegacyGuardPolicyMap={}", createdLegacyGuardPolicyMap);
208 return createdLegacyGuardPolicyMap;
212 * Update legacy guard policy.
214 * @param dao the DAO to use to access the database
215 * @param legacyGuardPolicy the definition of the policy to be updated
216 * @return the updated policy
217 * @throws PfModelException on errors updating policies
219 public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(@NonNull final PfDao dao,
220 @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
222 LOGGER.debug("->updateGuardPolicy: legacyGuardPolicy={}", legacyGuardPolicy);
224 JpaToscaServiceTemplate incomingServiceTemplate =
225 new LegacyGuardPolicyMapper().toToscaServiceTemplate(legacyGuardPolicy);
226 JpaToscaServiceTemplate outgoingingServiceTemplate =
227 new SimpleToscaProvider().updatePolicies(dao, incomingServiceTemplate);
229 Map<String, LegacyGuardPolicyOutput> updatedLegacyGuardPolicyMap =
230 new LegacyGuardPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
232 LOGGER.debug("<-updateGuardPolicy: updatedLegacyGuardPolicyMap={}", updatedLegacyGuardPolicyMap);
233 return updatedLegacyGuardPolicyMap;
238 * Delete legacy guard policy.
240 * @param dao the DAO to use to access the database
241 * @param policyId ID of the policy.
242 * @param policyVersion version of the policy.
243 * @return the deleted policy
244 * @throws PfModelException on errors deleting policies
246 public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final PfDao dao,
247 @NonNull final String policyId, @NonNull final String policyVersion) throws PfModelException {
249 LOGGER.debug("->deleteGuardPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
250 Map<String, LegacyGuardPolicyOutput> legacyGuardPolicyMap = new LegacyGuardPolicyMapper()
251 .fromToscaServiceTemplate(deleteLegacyPolicy(dao, policyId, policyVersion));
253 LOGGER.debug("<-deleteGuardPolicy: policyId={}, policyVersion={}, legacyGuardPolicyMap={}", policyId,
254 policyVersion, legacyGuardPolicyMap);
255 return legacyGuardPolicyMap;
259 * Get the JPA Policy for a policy ID and version.
261 * @param dao The DAO to search
262 * @param policyId the policy ID to search for
263 * @param policyVersion the policy version to search for
264 * @return the JPA policy found
265 * @throws PfModelRuntimeException if a policy is not found
267 private JpaToscaServiceTemplate getLegacyPolicy(final PfDao dao, final String policyId,
268 final String policyVersion) {
269 JpaToscaPolicy foundPolicy = null;
270 if (policyVersion == null) {
271 foundPolicy = getLatestPolicy(dao, policyId);
273 foundPolicy = dao.get(JpaToscaPolicy.class,
274 new PfConceptKey(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
277 if (foundPolicy == null) {
278 String errorMessage = NO_POLICY_FOUND_FOR_POLICY + policyId + ':' + policyVersion;
279 LOGGER.warn(errorMessage);
280 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
283 // Create the structure of the TOSCA service template to contain the policy type
284 JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
285 serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
286 serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
287 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(foundPolicy.getKey(), foundPolicy);
289 return serviceTemplate;
293 * Delete a legacy policy.
295 * @param dao the DAO to use for the deletion
296 * @param policyId the policy ID
297 * @param policyVersion the policy version
298 * @return a service template containing the policy that has been deleted
300 private JpaToscaServiceTemplate deleteLegacyPolicy(final PfDao dao, final String policyId,
301 final String policyVersion) {
303 final JpaToscaPolicy deletePolicy =
304 dao.get(JpaToscaPolicy.class, new PfConceptKey(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
306 if (deletePolicy == null) {
307 String errorMessage = NO_POLICY_FOUND_FOR_POLICY + policyId + ':' + policyVersion;
308 LOGGER.warn(errorMessage);
309 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
313 dao.delete(deletePolicy);
315 // Create the structure of the TOSCA service template to contain the policy type
316 JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
317 serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
318 serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
319 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(deletePolicy.getKey(), deletePolicy);
321 return serviceTemplate;
325 * Get the latest policy for a policy ID.
327 * @param dao The DAO to read from
328 * @param policyId the ID of the policy
331 private JpaToscaPolicy getLatestPolicy(final PfDao dao, final String policyId) {
332 // Get all the policies in the database and check the policy ID against the policies returned
333 List<JpaToscaPolicy> policyList = dao.getAll(JpaToscaPolicy.class);
335 // Find the latest policy that matches the ID
336 JpaToscaPolicy newestPolicy = null;
338 for (JpaToscaPolicy policy : policyList) {
339 if (!policyId.equals(policy.getKey().getName())) {
343 // We found a matching policy
344 if (newestPolicy == null || policy.getKey().isNewerThan(newestPolicy.getKey())) {
345 // First policy found
346 newestPolicy = policy;