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.ArrayList;
24 import java.util.List;
26 import javax.ws.rs.core.Response;
28 import lombok.NonNull;
30 import org.onap.policy.models.base.PfModelException;
31 import org.onap.policy.models.base.PfModelRuntimeException;
32 import org.onap.policy.models.dao.PfDao;
33 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy;
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.ToscaPolicies;
37 import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy;
38 import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
39 import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate;
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 private static final String FIRST_POLICY_VERSION = "1";
54 // Recurring constants
55 private static final String NO_POLICY_FOUND_FOR_POLICY_ID = "no policy found for policy ID: ";
58 * Get legacy operational policy.
60 * @param dao the DAO to use to access the database
61 * @param policyId ID 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 throws PfModelException {
68 ToscaPolicy newestPolicy = getLatestPolicy(dao, policyId);
70 if (newestPolicy == null) {
71 String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + policyId;
72 LOGGER.warn(errorMessage);
73 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
76 // Create the structure of the TOSCA service template to contain the policy type
77 ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
78 serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate());
79 serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies());
80 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(newestPolicy.getKey(), newestPolicy);
82 return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
86 * Create legacy operational policy.
88 * @param dao the DAO to use to access the database
89 * @param legacyOperationalPolicy the definition of the policy to be created.
90 * @return the created policy
91 * @throws PfModelException on errors creating policies
93 public LegacyOperationalPolicy createOperationalPolicy(@NonNull final PfDao dao,
94 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
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 ToscaPolicy 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 ToscaServiceTemplate incomingServiceTemplate =
107 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
108 ToscaServiceTemplate outgoingingServiceTemplate =
109 new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate);
111 return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
115 * Update legacy operational policy.
117 * @param dao the DAO to use to access the database
118 * @param legacyOperationalPolicy the definition of the policy to be updated
119 * @return the updated policy
120 * @throws PfModelException on errors updating policies
122 public LegacyOperationalPolicy updateOperationalPolicy(@NonNull final PfDao dao,
123 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
125 // We need to find the latest policy and use the major version, if there is no policy with this ID, then
127 ToscaPolicy newestPolicy = getLatestPolicy(dao, legacyOperationalPolicy.getPolicyId());
129 if (newestPolicy == null) {
130 String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + legacyOperationalPolicy.getPolicyId();
131 LOGGER.warn(errorMessage);
132 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
134 legacyOperationalPolicy.setPolicyVersion(Integer.toString(newestPolicy.getKey().getMajorVersion()));
137 ToscaServiceTemplate incomingServiceTemplate =
138 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
139 ToscaServiceTemplate outgoingingServiceTemplate =
140 new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate);
142 return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
146 * Delete legacy operational policy.
148 * @param dao the DAO to use to access the database
149 * @param policyId ID of the policy.
150 * @return the deleted policy
151 * @throws PfModelException on errors deleting policies
153 public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId)
154 throws PfModelException {
156 // Get all the policies in the database and check the policy ID against the policies returned
157 List<ToscaPolicy> policyList = dao.getAll(ToscaPolicy.class);
159 // Find the latest policy that matches the ID
160 List<ToscaPolicy> policyDeleteList = new ArrayList<>();
162 for (ToscaPolicy policy : policyList) {
163 if (policyId.equals(policy.getKey().getName())) {
164 policyDeleteList.add(policy);
168 if (policyDeleteList.isEmpty()) {
169 String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + policyId;
170 LOGGER.warn(errorMessage);
171 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
174 // Create the structure of the TOSCA service template to contain the policy type
175 ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
176 serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate());
177 serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies());
179 for (ToscaPolicy deletePolicy : policyDeleteList) {
180 dao.delete(deletePolicy);
181 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(deletePolicy.getKey(),
185 return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
189 * Get legacy guard policy.
191 * @param dao the DAO to use to access the database
192 * @param policyId ID of the policy.
193 * @return the policies found
194 * @throws PfModelException on errors getting policies
196 public LegacyGuardPolicy getGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId)
197 throws PfModelException {
202 * Create legacy guard policy.
204 * @param dao the DAO to use to access the database
205 * @param legacyGuardPolicy the definition of the policy to be created.
206 * @return the created policy
207 * @throws PfModelException on errors creating policies
209 public LegacyGuardPolicy createGuardPolicy(@NonNull final PfDao dao,
210 @NonNull final LegacyGuardPolicy legacyGuardPolicy) throws PfModelException {
215 * Update legacy guard policy.
217 * @param dao the DAO to use to access the database
218 * @param legacyGuardPolicy the definition of the policy to be updated
219 * @return the updated policy
220 * @throws PfModelException on errors updating policies
222 public LegacyGuardPolicy updateGuardPolicy(@NonNull final PfDao dao,
223 @NonNull final LegacyGuardPolicy legacyGuardPolicy) throws PfModelException {
229 * Delete legacy guard policy.
231 * @param dao the DAO to use to access the database
232 * @param policyId ID of the policy.
233 * @return the deleted policy
234 * @throws PfModelException on errors deleting policies
236 public LegacyGuardPolicy deleteGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId)
237 throws PfModelException {
242 * Get the latest policy for a policy ID.
244 * @param dao The DAO to read from
245 * @param policyId the ID of the policy
248 private ToscaPolicy getLatestPolicy(final PfDao dao, final String policyId) {
249 // Get all the policies in the database and check the policy ID against the policies returned
250 List<ToscaPolicy> policyList = dao.getAll(ToscaPolicy.class);
252 // Find the latest policy that matches the ID
253 ToscaPolicy newestPolicy = null;
255 for (ToscaPolicy policy : policyList) {
256 if (!policyId.equals(policy.getKey().getName())) {
260 // We found a matching policy
261 if (newestPolicy == null || policy.getKey().isNewerThan(newestPolicy.getKey())) {
262 // First policy found
263 newestPolicy = policy;