Changes for Checkstyle 8.32
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / legacy / provider / LegacyProvider.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.tosca.legacy.provider;
23
24 import java.util.Map;
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;
39
40 /**
41  * This class provides the provision of information on TOSCA concepts in the database to callers in legacy formats.
42  *
43  * @author Liam Fallon (liam.fallon@est.tech)
44  */
45 public class LegacyProvider {
46     private static final Logger LOGGER = LoggerFactory.getLogger(LegacyProvider.class);
47
48     public static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";
49
50     // Recurring constants
51     private static final String NO_POLICY_FOUND_FOR_POLICY = "no policy found for policy: ";
52
53     /**
54      * Get legacy operational policy.
55      *
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
61      */
62     public LegacyOperationalPolicy getOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
63         final String policyVersion) throws PfModelException {
64
65         LOGGER.debug("->getOperationalPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
66
67         LegacyOperationalPolicy legacyOperationalPolicy =
68             new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(getLegacyPolicy(dao, policyId, policyVersion));
69
70         LOGGER.debug("<-getOperationalPolicy: policyId={}, policyVersion={}, legacyOperationalPolicy={}", policyId,
71             policyVersion, legacyOperationalPolicy);
72         return legacyOperationalPolicy;
73     }
74
75     /**
76      * Create legacy operational policy.
77      *
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
82      */
83     public LegacyOperationalPolicy createOperationalPolicy(@NonNull final PfDao dao,
84         @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
85
86         LOGGER.debug("->createOperationalPolicy: legacyOperationalPolicy={}", legacyOperationalPolicy);
87
88         JpaToscaServiceTemplate legacyOperationalServiceTemplate =
89             new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
90
91         new SimpleToscaProvider().createPolicies(dao, legacyOperationalServiceTemplate);
92
93         LegacyOperationalPolicy createdLegacyOperationalPolicy =
94             new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(legacyOperationalServiceTemplate);
95
96         LOGGER.debug("<-createOperationalPolicy: createdLegacyOperationalPolicy={}", createdLegacyOperationalPolicy);
97         return createdLegacyOperationalPolicy;
98     }
99
100     /**
101      * Update legacy operational policy.
102      *
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
107      */
108     public LegacyOperationalPolicy updateOperationalPolicy(@NonNull final PfDao dao,
109         @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
110
111         LOGGER.debug("->updateOperationalPolicy: legacyOperationalPolicy={}", legacyOperationalPolicy);
112         JpaToscaServiceTemplate incomingServiceTemplate =
113             new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
114         JpaToscaServiceTemplate outgoingingServiceTemplate =
115             new SimpleToscaProvider().updatePolicies(dao, incomingServiceTemplate);
116
117         LegacyOperationalPolicy updatedLegacyOperationalPolicy =
118             new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
119
120         LOGGER.debug("<-updateOperationalPolicy: updatedLegacyOperationalPolicy={}", updatedLegacyOperationalPolicy);
121         return updatedLegacyOperationalPolicy;
122     }
123
124     /**
125      * Delete legacy operational policy.
126      *
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
132      */
133     public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
134         @NonNull final String policyVersion) throws PfModelException {
135
136         LOGGER.debug("->deleteOperationalPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
137
138         JpaToscaServiceTemplate deleteServiceTemplate = new SimpleToscaProvider().deletePolicy(dao,
139             new PfConceptKey(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
140         LegacyOperationalPolicy legacyOperationalPolicy =
141             new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(deleteServiceTemplate);
142
143         LOGGER.debug("<-deleteOperationalPolicy: policyId={}, policyVersion={}, legacyOperationalPolicy={}", policyId,
144             policyVersion, legacyOperationalPolicy);
145         return legacyOperationalPolicy;
146     }
147
148     /**
149      * Get the JPA Policy for a policy ID and version.
150      *
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
156      */
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);
162         } else {
163             foundPolicyServiceTemplate =
164                 new SimpleToscaProvider().getPolicies(dao, policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX);
165         }
166
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);
171         }
172
173         return foundPolicyServiceTemplate;
174     }
175
176     /**
177      * Get the latest policy for a policy ID.
178      *
179      * @param dao The DAO to read from
180      * @param policyId the ID of the policy
181      * @return the policy
182      * @throws PfModelException on exceptions getting the policies
183      */
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);
187
188         if (!ToscaUtils.doPoliciesExist(serviceTemplate)) {
189             return null;
190         }
191
192         // Find the latest policy that matches the ID
193         final Map<PfConceptKey, JpaToscaPolicy> policyMap =
194             serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
195         PfConceptKey newestPolicyKey = null;
196
197         for (JpaToscaPolicy policy : policyMap.values()) {
198             if (!policyId.equals(policy.getKey().getName())) {
199                 continue;
200             }
201
202             // We found a matching policy
203             if (newestPolicyKey == null || policy.getKey().isNewerThan(newestPolicyKey)) {
204                 // First policy found
205                 newestPolicyKey = policy.getKey();
206             }
207         }
208
209         final PfConceptKey newestPolicyFinalKey = newestPolicyKey;
210         policyMap.keySet().removeIf(key -> !key.equals(newestPolicyFinalKey));
211
212         return serviceTemplate;
213     }
214 }