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 return new LegacyOperationalPolicyMapper()
74 .fromToscaServiceTemplate(getLegacyPolicy(dao, policyId, policyVersion));
78 * Create legacy operational policy.
80 * @param dao the DAO to use to access the database
81 * @param legacyOperationalPolicy the definition of the policy to be created.
82 * @return the created policy
83 * @throws PfModelException on errors creating policies
85 public LegacyOperationalPolicy createOperationalPolicy(@NonNull final PfDao dao,
86 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
88 // We need to find the latest policy and update the major version, if there is no policy with this ID, then
89 // we set it to the first version
90 JpaToscaPolicy newestPolicy = getLatestPolicy(dao, legacyOperationalPolicy.getPolicyId());
92 if (newestPolicy == null) {
93 legacyOperationalPolicy.setPolicyVersion(FIRST_POLICY_VERSION);
95 legacyOperationalPolicy.setPolicyVersion(Integer.toString(newestPolicy.getKey().getMajorVersion() + 1));
98 JpaToscaServiceTemplate incomingServiceTemplate =
99 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
100 JpaToscaServiceTemplate outgoingingServiceTemplate =
101 new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate);
103 return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
107 * Update legacy operational policy.
109 * @param dao the DAO to use to access the database
110 * @param legacyOperationalPolicy the definition of the policy to be updated
111 * @return the updated policy
112 * @throws PfModelException on errors updating policies
114 public LegacyOperationalPolicy updateOperationalPolicy(@NonNull final PfDao dao,
115 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
117 JpaToscaServiceTemplate incomingServiceTemplate =
118 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
119 JpaToscaServiceTemplate outgoingingServiceTemplate =
120 new SimpleToscaProvider().updatePolicies(dao, incomingServiceTemplate);
122 return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
126 * Delete legacy operational policy.
128 * @param dao the DAO to use to access the database
129 * @param policyId ID of the policy.
130 * @param policyVersion version of the policy.
131 * @return the deleted policy
132 * @throws PfModelException on errors deleting policies
134 public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
135 @NonNull final String policyVersion) throws PfModelException {
137 return new LegacyOperationalPolicyMapper()
138 .fromToscaServiceTemplate(deleteLegacyPolicy(dao, policyId, policyVersion));
142 * Get legacy guard policy.
144 * @param dao the DAO to use to access the database
145 * @param policyId ID of the policy.
146 * @param policyVersion version of the policy.
147 * @return the policies found
148 * @throws PfModelException on errors getting policies
150 public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
151 final String policyVersion) throws PfModelException {
153 return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(getLegacyPolicy(dao, policyId, policyVersion));
157 * Create legacy guard policy.
159 * @param dao the DAO to use to access the database
160 * @param legacyGuardPolicy the definition of the policy to be created.
161 * @return the created policy
162 * @throws PfModelException on errors creating policies
164 public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(@NonNull final PfDao dao,
165 @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
167 JpaToscaServiceTemplate incomingServiceTemplate =
168 new LegacyGuardPolicyMapper().toToscaServiceTemplate(legacyGuardPolicy);
169 JpaToscaServiceTemplate outgoingingServiceTemplate =
170 new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate);
172 return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
176 * Update legacy guard policy.
178 * @param dao the DAO to use to access the database
179 * @param legacyGuardPolicy the definition of the policy to be updated
180 * @return the updated policy
181 * @throws PfModelException on errors updating policies
183 public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(@NonNull final PfDao dao,
184 @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
186 JpaToscaServiceTemplate incomingServiceTemplate =
187 new LegacyGuardPolicyMapper().toToscaServiceTemplate(legacyGuardPolicy);
188 JpaToscaServiceTemplate outgoingingServiceTemplate =
189 new SimpleToscaProvider().updatePolicies(dao, incomingServiceTemplate);
191 return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
196 * Delete legacy guard policy.
198 * @param dao the DAO to use to access the database
199 * @param policyId ID of the policy.
200 * @param policyVersion version of the policy.
201 * @return the deleted policy
202 * @throws PfModelException on errors deleting policies
204 public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final PfDao dao,
205 @NonNull final String policyId, @NonNull final String policyVersion) throws PfModelException {
207 return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(deleteLegacyPolicy(dao, policyId, policyVersion));
211 * Get the JPA Policy for a policy ID and version.
213 * @param dao The DAO to search
214 * @param policyId the policy ID to search for
215 * @param policyVersion the policy version to search for
216 * @return the JPA policy found
217 * @throws PfModelRuntimeException if a policy is not found
219 private JpaToscaServiceTemplate getLegacyPolicy(final PfDao dao, final String policyId,
220 final String policyVersion) {
221 JpaToscaPolicy foundPolicy = null;
222 if (policyVersion == null) {
223 foundPolicy = getLatestPolicy(dao, policyId);
225 foundPolicy = dao.get(JpaToscaPolicy.class,
226 new PfConceptKey(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
229 if (foundPolicy == null) {
230 String errorMessage = NO_POLICY_FOUND_FOR_POLICY + policyId + ':' + policyVersion;
231 LOGGER.warn(errorMessage);
232 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
235 // Create the structure of the TOSCA service template to contain the policy type
236 JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
237 serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
238 serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
239 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(foundPolicy.getKey(), foundPolicy);
241 return serviceTemplate;
245 * Delete a legacy policy.
247 * @param dao the DAO to use for the deletion
248 * @param policyId the policy ID
249 * @param policyVersion the policy version
250 * @return a service template containing the policy that has been deleted
252 private JpaToscaServiceTemplate deleteLegacyPolicy(final PfDao dao, final String policyId,
253 final String policyVersion) {
255 final JpaToscaPolicy deletePolicy =
256 dao.get(JpaToscaPolicy.class, new PfConceptKey(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
258 if (deletePolicy == null) {
259 String errorMessage = NO_POLICY_FOUND_FOR_POLICY + policyId + ':' + policyVersion;
260 LOGGER.warn(errorMessage);
261 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
265 dao.delete(deletePolicy);
267 // Create the structure of the TOSCA service template to contain the policy type
268 JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
269 serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
270 serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
271 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(deletePolicy.getKey(), deletePolicy);
273 return serviceTemplate;
277 * Get the latest policy for a policy ID.
279 * @param dao The DAO to read from
280 * @param policyId the ID of the policy
283 private JpaToscaPolicy getLatestPolicy(final PfDao dao, final String policyId) {
284 // Get all the policies in the database and check the policy ID against the policies returned
285 List<JpaToscaPolicy> policyList = dao.getAll(JpaToscaPolicy.class);
287 // Find the latest policy that matches the ID
288 JpaToscaPolicy newestPolicy = null;
290 for (JpaToscaPolicy policy : policyList) {
291 if (!policyId.equals(policy.getKey().getName())) {
295 // We found a matching policy
296 if (newestPolicy == null || policy.getKey().isNewerThan(newestPolicy.getKey())) {
297 // First policy found
298 newestPolicy = policy;