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;
25 import javax.ws.rs.core.Response;
26 import lombok.NonNull;
27 import org.onap.policy.models.base.PfConceptKey;
28 import org.onap.policy.models.base.PfModelException;
29 import org.onap.policy.models.base.PfModelRuntimeException;
30 import org.onap.policy.models.dao.PfDao;
31 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
32 import org.onap.policy.models.tosca.legacy.mapping.LegacyOperationalPolicyMapper;
33 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
34 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
35 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
36 import org.onap.policy.models.tosca.utils.ToscaUtils;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * This class provides the provision of information on TOSCA concepts in the database to callers in legacy formats.
43 * @author Liam Fallon (liam.fallon@est.tech)
45 public class LegacyProvider {
46 private static final Logger LOGGER = LoggerFactory.getLogger(LegacyProvider.class);
48 public static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";
50 // Recurring constants
51 private static final String NO_POLICY_FOUND_FOR_POLICY = "no policy found for policy: ";
54 * Get legacy operational policy.
56 * @param dao the DAO to use to access the database
57 * @param policyId ID of the policy.
58 * @param policyVersion version of the policy.
59 * @return the policies found
60 * @throws PfModelException on errors getting policies
62 public LegacyOperationalPolicy getOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
63 final String policyVersion) throws PfModelException {
65 LOGGER.debug("->getOperationalPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
67 LegacyOperationalPolicy legacyOperationalPolicy =
68 new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(getLegacyPolicy(dao, policyId, policyVersion));
70 LOGGER.debug("<-getOperationalPolicy: policyId={}, policyVersion={}, legacyOperationalPolicy={}", policyId,
71 policyVersion, legacyOperationalPolicy);
72 return legacyOperationalPolicy;
76 * Create legacy operational policy.
78 * @param dao the DAO to use to access the database
79 * @param legacyOperationalPolicy the definition of the policy to be created.
80 * @return the created policy
81 * @throws PfModelException on errors creating policies
83 public LegacyOperationalPolicy createOperationalPolicy(@NonNull final PfDao dao,
84 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
86 LOGGER.debug("->createOperationalPolicy: legacyOperationalPolicy={}", legacyOperationalPolicy);
88 JpaToscaServiceTemplate legacyOperationalServiceTemplate =
89 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
91 new SimpleToscaProvider().createPolicies(dao, legacyOperationalServiceTemplate);
93 LegacyOperationalPolicy createdLegacyOperationalPolicy =
94 new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(legacyOperationalServiceTemplate);
96 LOGGER.debug("<-createOperationalPolicy: createdLegacyOperationalPolicy={}", createdLegacyOperationalPolicy);
97 return createdLegacyOperationalPolicy;
101 * Update legacy operational policy.
103 * @param dao the DAO to use to access the database
104 * @param legacyOperationalPolicy the definition of the policy to be updated
105 * @return the updated policy
106 * @throws PfModelException on errors updating policies
108 public LegacyOperationalPolicy updateOperationalPolicy(@NonNull final PfDao dao,
109 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
111 LOGGER.debug("->updateOperationalPolicy: legacyOperationalPolicy={}", legacyOperationalPolicy);
112 JpaToscaServiceTemplate incomingServiceTemplate =
113 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
114 JpaToscaServiceTemplate outgoingingServiceTemplate =
115 new SimpleToscaProvider().updatePolicies(dao, incomingServiceTemplate);
117 LegacyOperationalPolicy updatedLegacyOperationalPolicy =
118 new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
120 LOGGER.debug("<-updateOperationalPolicy: updatedLegacyOperationalPolicy={}", updatedLegacyOperationalPolicy);
121 return updatedLegacyOperationalPolicy;
125 * Delete legacy operational policy.
127 * @param dao the DAO to use to access the database
128 * @param policyId ID of the policy.
129 * @param policyVersion version of the policy.
130 * @return the deleted policy
131 * @throws PfModelException on errors deleting policies
133 public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
134 @NonNull final String policyVersion) throws PfModelException {
136 LOGGER.debug("->deleteOperationalPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
138 JpaToscaServiceTemplate deleteServiceTemplate = new SimpleToscaProvider().deletePolicy(dao,
139 new PfConceptKey(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
140 LegacyOperationalPolicy legacyOperationalPolicy =
141 new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(deleteServiceTemplate);
143 LOGGER.debug("<-deleteOperationalPolicy: policyId={}, policyVersion={}, legacyOperationalPolicy={}", policyId,
144 policyVersion, legacyOperationalPolicy);
145 return legacyOperationalPolicy;
149 * Get the JPA Policy for a policy ID and version.
151 * @param dao The DAO to search
152 * @param policyId the policy ID to search for
153 * @param policyVersion the policy version to search for
154 * @return the JPA policy found
155 * @throws PfModelException if a policy is not found
157 private JpaToscaServiceTemplate getLegacyPolicy(final PfDao dao, final String policyId, final String policyVersion)
158 throws PfModelException {
159 JpaToscaServiceTemplate foundPolicyServiceTemplate = null;
160 if (policyVersion == null) {
161 foundPolicyServiceTemplate = getLatestPolicy(dao, policyId);
163 foundPolicyServiceTemplate =
164 new SimpleToscaProvider().getPolicies(dao, policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX);
167 if (foundPolicyServiceTemplate == null) {
168 String errorMessage = NO_POLICY_FOUND_FOR_POLICY + policyId + ':' + policyVersion;
169 LOGGER.warn(errorMessage);
170 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
173 return foundPolicyServiceTemplate;
177 * Get the latest policy for a policy ID.
179 * @param dao The DAO to read from
180 * @param policyId the ID of the policy
182 * @throws PfModelException on exceptions getting the policies
184 private JpaToscaServiceTemplate getLatestPolicy(final PfDao dao, final String policyId) throws PfModelException {
185 // Get all the policies in the database and check the policy ID against the policies returned
186 JpaToscaServiceTemplate serviceTemplate = new SimpleToscaProvider().getPolicies(dao, policyId, null);
188 if (!ToscaUtils.doPoliciesExist(serviceTemplate)) {
192 // Find the latest policy that matches the ID
193 final Map<PfConceptKey, JpaToscaPolicy> policyMap =
194 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
195 PfConceptKey newestPolicyKey = null;
197 for (JpaToscaPolicy policy : policyMap.values()) {
198 if (!policyId.equals(policy.getKey().getName())) {
202 // We found a matching policy
203 if (newestPolicyKey == null || policy.getKey().isNewerThan(newestPolicyKey)) {
204 // First policy found
205 newestPolicyKey = policy.getKey();
209 final PfConceptKey newestPolicyFinalKey = newestPolicyKey;
210 policyMap.keySet().removeIf(key -> !key.equals(newestPolicyFinalKey));
212 return serviceTemplate;