2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 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;
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.JpaToscaPolicy;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
38 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
39 import org.onap.policy.models.tosca.utils.ToscaUtils;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * This class provides the provision of information on TOSCA concepts in the database to callers in legacy formats.
46 * @author Liam Fallon (liam.fallon@est.tech)
48 public class LegacyProvider {
49 private static final Logger LOGGER = LoggerFactory.getLogger(LegacyProvider.class);
51 public static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";
53 // Recurring constants
54 private static final String NO_POLICY_FOUND_FOR_POLICY = "no policy found for policy: ";
57 * Get legacy operational policy.
59 * @param dao the DAO to use to access the database
60 * @param policyId ID of the policy.
61 * @param policyVersion version of the policy.
62 * @return the policies found
63 * @throws PfModelException on errors getting policies
65 public LegacyOperationalPolicy getOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
66 final String policyVersion) throws PfModelException {
68 LOGGER.debug("->getOperationalPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
70 LegacyOperationalPolicy legacyOperationalPolicy =
71 new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(getLegacyPolicy(dao, policyId, policyVersion));
73 LOGGER.debug("<-getOperationalPolicy: policyId={}, policyVersion={}, legacyOperationalPolicy={}", policyId,
74 policyVersion, legacyOperationalPolicy);
75 return legacyOperationalPolicy;
79 * Create legacy operational policy.
81 * @param dao the DAO to use to access the database
82 * @param legacyOperationalPolicy the definition of the policy to be created.
83 * @return the created policy
84 * @throws PfModelException on errors creating policies
86 public LegacyOperationalPolicy createOperationalPolicy(@NonNull final PfDao dao,
87 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
89 LOGGER.debug("->createOperationalPolicy: legacyOperationalPolicy={}", legacyOperationalPolicy);
91 JpaToscaServiceTemplate legacyOperationalServiceTemplate =
92 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
94 new SimpleToscaProvider().createPolicies(dao, legacyOperationalServiceTemplate);
96 LegacyOperationalPolicy createdLegacyOperationalPolicy =
97 new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(legacyOperationalServiceTemplate);
99 LOGGER.debug("<-createOperationalPolicy: createdLegacyOperationalPolicy={}", createdLegacyOperationalPolicy);
100 return createdLegacyOperationalPolicy;
104 * Update legacy operational policy.
106 * @param dao the DAO to use to access the database
107 * @param legacyOperationalPolicy the definition of the policy to be updated
108 * @return the updated policy
109 * @throws PfModelException on errors updating policies
111 public LegacyOperationalPolicy updateOperationalPolicy(@NonNull final PfDao dao,
112 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
114 LOGGER.debug("->updateOperationalPolicy: legacyOperationalPolicy={}", legacyOperationalPolicy);
115 JpaToscaServiceTemplate incomingServiceTemplate =
116 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
117 JpaToscaServiceTemplate outgoingingServiceTemplate =
118 new SimpleToscaProvider().updatePolicies(dao, incomingServiceTemplate);
120 LegacyOperationalPolicy updatedLegacyOperationalPolicy =
121 new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
123 LOGGER.debug("<-updateOperationalPolicy: updatedLegacyOperationalPolicy={}", updatedLegacyOperationalPolicy);
124 return updatedLegacyOperationalPolicy;
128 * Delete legacy operational policy.
130 * @param dao the DAO to use to access the database
131 * @param policyId ID of the policy.
132 * @param policyVersion version of the policy.
133 * @return the deleted policy
134 * @throws PfModelException on errors deleting policies
136 public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
137 @NonNull final String policyVersion) throws PfModelException {
139 LOGGER.debug("->deleteOperationalPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
141 JpaToscaServiceTemplate deleteServiceTemplate = new SimpleToscaProvider().deletePolicy(dao,
142 new PfConceptKey(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
143 LegacyOperationalPolicy legacyOperationalPolicy =
144 new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(deleteServiceTemplate);
146 LOGGER.debug("<-deleteOperationalPolicy: policyId={}, policyVersion={}, legacyOperationalPolicy={}", policyId,
147 policyVersion, legacyOperationalPolicy);
148 return legacyOperationalPolicy;
152 * Get the JPA Policy for a policy ID and version.
154 * @param dao The DAO to search
155 * @param policyId the policy ID to search for
156 * @param policyVersion the policy version to search for
157 * @return the JPA policy found
158 * @throws PfModelException if a policy is not found
160 private JpaToscaServiceTemplate getLegacyPolicy(final PfDao dao, final String policyId, final String policyVersion)
161 throws PfModelException {
162 JpaToscaServiceTemplate foundPolicyServiceTemplate = null;
163 if (policyVersion == null) {
164 foundPolicyServiceTemplate = getLatestPolicy(dao, policyId);
166 foundPolicyServiceTemplate =
167 new SimpleToscaProvider().getPolicies(dao, policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX);
170 if (foundPolicyServiceTemplate == null) {
171 String errorMessage = NO_POLICY_FOUND_FOR_POLICY + policyId + ':' + policyVersion;
172 LOGGER.warn(errorMessage);
173 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
176 return foundPolicyServiceTemplate;
180 * Get the latest policy for a policy ID.
182 * @param dao The DAO to read from
183 * @param policyId the ID of the policy
185 * @throws PfModelException on exceptions getting the policies
187 private JpaToscaServiceTemplate getLatestPolicy(final PfDao dao, final String policyId) throws PfModelException {
188 // Get all the policies in the database and check the policy ID against the policies returned
189 JpaToscaServiceTemplate serviceTemplate = new SimpleToscaProvider().getPolicies(dao, policyId, null);
191 if (!ToscaUtils.doPoliciesExist(serviceTemplate)) {
195 // Find the latest policy that matches the ID
196 final Map<PfConceptKey, JpaToscaPolicy> policyMap =
197 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
198 PfConceptKey newestPolicyKey = null;
200 for (JpaToscaPolicy policy : policyMap.values()) {
201 if (!policyId.equals(policy.getKey().getName())) {
205 // We found a matching policy
206 if (newestPolicyKey == null || policy.getKey().isNewerThan(newestPolicyKey)) {
207 // First policy found
208 newestPolicyKey = policy.getKey();
212 final PfConceptKey newestPolicyFinalKey = newestPolicyKey;
213 policyMap.keySet().removeIf(key -> !key.equals(newestPolicyFinalKey));
215 return serviceTemplate;