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;
27 import javax.ws.rs.core.Response;
29 import lombok.NonNull;
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";
57 // Recurring constants
58 private static final String NO_POLICY_FOUND_FOR_POLICY_ID = "no policy found for policy ID: ";
61 * Get legacy operational policy.
63 * @param dao the DAO to use to access the database
64 * @param policyId ID of the policy.
65 * @return the policies found
66 * @throws PfModelException on errors getting policies
68 public LegacyOperationalPolicy getOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId)
69 throws PfModelException {
71 JpaToscaPolicy newestPolicy = getLatestPolicy(dao, policyId);
73 if (newestPolicy == null) {
74 String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + policyId;
75 LOGGER.warn(errorMessage);
76 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
79 // Create the structure of the TOSCA service template to contain the policy type
80 JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
81 serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
82 serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
83 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(newestPolicy.getKey(), newestPolicy);
85 return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
89 * Create legacy operational policy.
91 * @param dao the DAO to use to access the database
92 * @param legacyOperationalPolicy the definition of the policy to be created.
93 * @return the created policy
94 * @throws PfModelException on errors creating policies
96 public LegacyOperationalPolicy createOperationalPolicy(@NonNull final PfDao dao,
97 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
99 // We need to find the latest policy and update the major version, if there is no policy with this ID, then
100 // we set it to the first version
101 JpaToscaPolicy newestPolicy = getLatestPolicy(dao, legacyOperationalPolicy.getPolicyId());
103 if (newestPolicy == null) {
104 legacyOperationalPolicy.setPolicyVersion(FIRST_POLICY_VERSION);
106 legacyOperationalPolicy.setPolicyVersion(Integer.toString(newestPolicy.getKey().getMajorVersion() + 1));
109 JpaToscaServiceTemplate incomingServiceTemplate =
110 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
111 JpaToscaServiceTemplate outgoingingServiceTemplate =
112 new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate);
114 return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
118 * Update legacy operational policy.
120 * @param dao the DAO to use to access the database
121 * @param legacyOperationalPolicy the definition of the policy to be updated
122 * @return the updated policy
123 * @throws PfModelException on errors updating policies
125 public LegacyOperationalPolicy updateOperationalPolicy(@NonNull final PfDao dao,
126 @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
128 // We need to find the latest policy and use the major version, if there is no policy with this ID, then
130 JpaToscaPolicy newestPolicy = getLatestPolicy(dao, legacyOperationalPolicy.getPolicyId());
132 if (newestPolicy == null) {
133 String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + legacyOperationalPolicy.getPolicyId();
134 LOGGER.warn(errorMessage);
135 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
137 legacyOperationalPolicy.setPolicyVersion(Integer.toString(newestPolicy.getKey().getMajorVersion()));
140 JpaToscaServiceTemplate incomingServiceTemplate =
141 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
142 JpaToscaServiceTemplate outgoingingServiceTemplate =
143 new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate);
145 return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
149 * Delete legacy operational policy.
151 * @param dao the DAO to use to access the database
152 * @param policyId ID of the policy.
153 * @return the deleted policy
154 * @throws PfModelException on errors deleting policies
156 public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId)
157 throws PfModelException {
159 // Get all the policies in the database and check the policy ID against the policies returned
160 List<JpaToscaPolicy> policyList = dao.getAll(JpaToscaPolicy.class);
162 // Find the latest policy that matches the ID
163 List<JpaToscaPolicy> policyDeleteList = new ArrayList<>();
165 for (JpaToscaPolicy policy : policyList) {
166 if (policyId.equals(policy.getKey().getName())) {
167 policyDeleteList.add(policy);
171 if (policyDeleteList.isEmpty()) {
172 String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + policyId;
173 LOGGER.warn(errorMessage);
174 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
177 // Create the structure of the TOSCA service template to contain the policy type
178 JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
179 serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
180 serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
182 for (JpaToscaPolicy deletePolicy : policyDeleteList) {
183 dao.delete(deletePolicy);
184 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(deletePolicy.getKey(),
188 return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
192 * Get legacy guard policy.
194 * @param dao the DAO to use to access the database
195 * @param policyId ID of the policy.
196 * @return the policies found
197 * @throws PfModelException on errors getting policies
199 public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId)
200 throws PfModelException {
202 JpaToscaPolicy newestPolicy = getLatestPolicy(dao, policyId);
204 if (newestPolicy == null) {
205 String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + policyId;
206 LOGGER.warn(errorMessage);
207 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
210 // Create the structure of the TOSCA service template to contain the policy type
211 JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
212 serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
213 serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
214 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(newestPolicy.getKey(), newestPolicy);
216 return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
220 * Create legacy guard policy.
222 * @param dao the DAO to use to access the database
223 * @param legacyGuardPolicy the definition of the policy to be created.
224 * @return the created policy
225 * @throws PfModelException on errors creating policies
227 public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(@NonNull final PfDao dao,
228 @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
230 JpaToscaServiceTemplate incomingServiceTemplate =
231 new LegacyGuardPolicyMapper().toToscaServiceTemplate(legacyGuardPolicy);
232 JpaToscaServiceTemplate outgoingingServiceTemplate =
233 new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate);
235 return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
239 * Update legacy guard policy.
241 * @param dao the DAO to use to access the database
242 * @param legacyGuardPolicy the definition of the policy to be updated
243 * @return the updated policy
244 * @throws PfModelException on errors updating policies
246 public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy(@NonNull final PfDao dao,
247 @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException {
249 JpaToscaServiceTemplate incomingServiceTemplate =
250 new LegacyGuardPolicyMapper().toToscaServiceTemplate(legacyGuardPolicy);
251 JpaToscaServiceTemplate outgoingingServiceTemplate =
252 new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate);
254 return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
259 * Delete legacy guard policy.
261 * @param dao the DAO to use to access the database
262 * @param policyId ID of the policy.
263 * @return the deleted policy
264 * @throws PfModelException on errors deleting policies
266 public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final PfDao dao,
267 @NonNull final String policyId) throws PfModelException {
269 // Get all the policies in the database and check the policy ID against the policies returned
270 List<JpaToscaPolicy> policyList = dao.getAll(JpaToscaPolicy.class);
272 // Find the latest policy that matches the ID
273 List<JpaToscaPolicy> policyDeleteList = new ArrayList<>();
275 for (JpaToscaPolicy policy : policyList) {
276 if (policyId.equals(policy.getKey().getName())) {
277 policyDeleteList.add(policy);
281 if (policyDeleteList.isEmpty()) {
282 String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + policyId;
283 LOGGER.warn(errorMessage);
284 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
287 // Create the structure of the TOSCA service template to contain the policy type
288 JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate();
289 serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate());
290 serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies());
292 for (JpaToscaPolicy deletePolicy : policyDeleteList) {
293 dao.delete(deletePolicy);
294 serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(deletePolicy.getKey(),
298 return new LegacyGuardPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
302 * Get the latest policy for a policy ID.
304 * @param dao The DAO to read from
305 * @param policyId the ID of the policy
308 private JpaToscaPolicy getLatestPolicy(final PfDao dao, final String policyId) {
309 // Get all the policies in the database and check the policy ID against the policies returned
310 List<JpaToscaPolicy> policyList = dao.getAll(JpaToscaPolicy.class);
312 // Find the latest policy that matches the ID
313 JpaToscaPolicy newestPolicy = null;
315 for (JpaToscaPolicy policy : policyList) {
316 if (!policyId.equals(policy.getKey().getName())) {
320 // We found a matching policy
321 if (newestPolicy == null || policy.getKey().isNewerThan(newestPolicy.getKey())) {
322 // First policy found
323 newestPolicy = policy;