44c086dc414bf4fba63c34d9ba479d69f486a4c2
[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
26 import javax.ws.rs.core.Response;
27
28 import lombok.NonNull;
29
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.LegacyOperationalPolicy;
35 import org.onap.policy.models.tosca.legacy.mapping.LegacyOperationalPolicyMapper;
36 import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
38 import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
39 import org.onap.policy.models.tosca.utils.ToscaUtils;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * This class provides the provision of information on TOSCA concepts in the database to callers in legacy formats.
45  *
46  * @author Liam Fallon (liam.fallon@est.tech)
47  */
48 public class LegacyProvider {
49     private static final Logger LOGGER = LoggerFactory.getLogger(LegacyProvider.class);
50
51     public static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0";
52
53     // Recurring constants
54     private static final String NO_POLICY_FOUND_FOR_POLICY = "no policy found for policy: ";
55
56     /**
57      * Get legacy operational policy.
58      *
59      * @param dao the DAO to use to access the database
60      * @param policyId ID of the policy.
61      * @param policyVersion version of the policy.
62      * @return the policies found
63      * @throws PfModelException on errors getting policies
64      */
65     public LegacyOperationalPolicy getOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
66         final String policyVersion) throws PfModelException {
67
68         LOGGER.debug("->getOperationalPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
69
70         LegacyOperationalPolicy legacyOperationalPolicy =
71             new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(getLegacyPolicy(dao, policyId, policyVersion));
72
73         LOGGER.debug("<-getOperationalPolicy: policyId={}, policyVersion={}, legacyOperationalPolicy={}", policyId,
74             policyVersion, legacyOperationalPolicy);
75         return legacyOperationalPolicy;
76     }
77
78     /**
79      * Create legacy operational policy.
80      *
81      * @param dao the DAO to use to access the database
82      * @param legacyOperationalPolicy the definition of the policy to be created.
83      * @return the created policy
84      * @throws PfModelException on errors creating policies
85      */
86     public LegacyOperationalPolicy createOperationalPolicy(@NonNull final PfDao dao,
87         @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
88
89         LOGGER.debug("->createOperationalPolicy: legacyOperationalPolicy={}", legacyOperationalPolicy);
90
91         JpaToscaServiceTemplate legacyOperationalServiceTemplate =
92             new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
93
94         new SimpleToscaProvider().createPolicies(dao, legacyOperationalServiceTemplate);
95
96         LegacyOperationalPolicy createdLegacyOperationalPolicy =
97             new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(legacyOperationalServiceTemplate);
98
99         LOGGER.debug("<-createOperationalPolicy: createdLegacyOperationalPolicy={}", createdLegacyOperationalPolicy);
100         return createdLegacyOperationalPolicy;
101     }
102
103     /**
104      * Update legacy operational policy.
105      *
106      * @param dao the DAO to use to access the database
107      * @param legacyOperationalPolicy the definition of the policy to be updated
108      * @return the updated policy
109      * @throws PfModelException on errors updating policies
110      */
111     public LegacyOperationalPolicy updateOperationalPolicy(@NonNull final PfDao dao,
112         @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
113
114         LOGGER.debug("->updateOperationalPolicy: legacyOperationalPolicy={}", legacyOperationalPolicy);
115         JpaToscaServiceTemplate incomingServiceTemplate =
116             new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
117         JpaToscaServiceTemplate outgoingingServiceTemplate =
118             new SimpleToscaProvider().updatePolicies(dao, incomingServiceTemplate);
119
120         LegacyOperationalPolicy updatedLegacyOperationalPolicy =
121             new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
122
123         LOGGER.debug("<-updateOperationalPolicy: updatedLegacyOperationalPolicy={}", updatedLegacyOperationalPolicy);
124         return updatedLegacyOperationalPolicy;
125     }
126
127     /**
128      * Delete legacy operational policy.
129      *
130      * @param dao the DAO to use to access the database
131      * @param policyId ID of the policy.
132      * @param policyVersion version of the policy.
133      * @return the deleted policy
134      * @throws PfModelException on errors deleting policies
135      */
136     public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId,
137         @NonNull final String policyVersion) throws PfModelException {
138
139         LOGGER.debug("->deleteOperationalPolicy: policyId={}, policyVersion={}", policyId, policyVersion);
140
141         JpaToscaServiceTemplate deleteServiceTemplate = new SimpleToscaProvider().deletePolicy(dao,
142             new PfConceptKey(policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX));
143         LegacyOperationalPolicy legacyOperationalPolicy =
144             new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(deleteServiceTemplate);
145
146         LOGGER.debug("<-deleteOperationalPolicy: policyId={}, policyVersion={}, legacyOperationalPolicy={}", policyId,
147             policyVersion, legacyOperationalPolicy);
148         return legacyOperationalPolicy;
149     }
150
151     /**
152      * Get the JPA Policy for a policy ID and version.
153      *
154      * @param dao The DAO to search
155      * @param policyId the policy ID to search for
156      * @param policyVersion the policy version to search for
157      * @return the JPA policy found
158      * @throws PfModelException if a policy is not found
159      */
160     private JpaToscaServiceTemplate getLegacyPolicy(final PfDao dao, final String policyId, final String policyVersion)
161         throws PfModelException {
162         JpaToscaServiceTemplate foundPolicyServiceTemplate = null;
163         if (policyVersion == null) {
164             foundPolicyServiceTemplate = getLatestPolicy(dao, policyId);
165         } else {
166             foundPolicyServiceTemplate =
167                 new SimpleToscaProvider().getPolicies(dao, policyId, policyVersion + LEGACY_MINOR_PATCH_SUFFIX);
168         }
169
170         if (foundPolicyServiceTemplate == null) {
171             String errorMessage = NO_POLICY_FOUND_FOR_POLICY + policyId + ':' + policyVersion;
172             LOGGER.warn(errorMessage);
173             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
174         }
175
176         return foundPolicyServiceTemplate;
177     }
178
179     /**
180      * Get the latest policy for a policy ID.
181      *
182      * @param dao The DAO to read from
183      * @param policyId the ID of the policy
184      * @return the policy
185      * @throws PfModelException on exceptions getting the policies
186      */
187     private JpaToscaServiceTemplate getLatestPolicy(final PfDao dao, final String policyId) throws PfModelException {
188         // Get all the policies in the database and check the policy ID against the policies returned
189         JpaToscaServiceTemplate serviceTemplate = new SimpleToscaProvider().getPolicies(dao, policyId, null);
190
191         if (!ToscaUtils.doPoliciesExist(serviceTemplate)) {
192             return null;
193         }
194
195         // Find the latest policy that matches the ID
196         final Map<PfConceptKey, JpaToscaPolicy> policyMap =
197             serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap();
198         PfConceptKey newestPolicyKey = null;
199
200         for (JpaToscaPolicy policy : policyMap.values()) {
201             if (!policyId.equals(policy.getKey().getName())) {
202                 continue;
203             }
204
205             // We found a matching policy
206             if (newestPolicyKey == null || policy.getKey().isNewerThan(newestPolicyKey)) {
207                 // First policy found
208                 newestPolicyKey = policy.getKey();
209             }
210         }
211
212         final PfConceptKey newestPolicyFinalKey = newestPolicyKey;
213         policyMap.keySet().removeIf(key -> !key.equals(newestPolicyFinalKey));
214
215         return serviceTemplate;
216     }
217 }