Merge "Fix VF Module Create in guard"
[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 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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.legacy.provider;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import javax.ws.rs.core.Response;
27
28 import lombok.NonNull;
29
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;
43
44 /**
45  * This class provides the provision of information on TOSCA concepts in the database to callers in legacy formats.
46  *
47  * @author Liam Fallon (liam.fallon@est.tech)
48  */
49 public class LegacyProvider {
50     private static final Logger LOGGER = LoggerFactory.getLogger(LegacyProvider.class);
51
52     private static final String FIRST_POLICY_VERSION = "1";
53
54     // Recurring constants
55     private static final String NO_POLICY_FOUND_FOR_POLICY_ID = "no policy found for policy ID: ";
56
57     /**
58      * Get legacy operational policy.
59      *
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
64      */
65     public LegacyOperationalPolicy getOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId)
66             throws PfModelException {
67
68         ToscaPolicy newestPolicy = getLatestPolicy(dao, policyId);
69
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);
74         }
75
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);
81
82         return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
83     }
84
85     /**
86      * Create legacy operational policy.
87      *
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
92      */
93     public LegacyOperationalPolicy createOperationalPolicy(@NonNull final PfDao dao,
94             @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
95
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());
99
100         if (newestPolicy == null) {
101             legacyOperationalPolicy.setPolicyVersion(FIRST_POLICY_VERSION);
102         } else {
103             legacyOperationalPolicy.setPolicyVersion(Integer.toString(newestPolicy.getKey().getMajorVersion() + 1));
104         }
105
106         ToscaServiceTemplate incomingServiceTemplate =
107                 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
108         ToscaServiceTemplate outgoingingServiceTemplate =
109                 new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate);
110
111         return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
112     }
113
114     /**
115      * Update legacy operational policy.
116      *
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
121      */
122     public LegacyOperationalPolicy updateOperationalPolicy(@NonNull final PfDao dao,
123             @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
124
125         // We need to find the latest policy and use the major version, if there is no policy with this ID, then
126         // we have an error
127         ToscaPolicy newestPolicy = getLatestPolicy(dao, legacyOperationalPolicy.getPolicyId());
128
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);
133         } else {
134             legacyOperationalPolicy.setPolicyVersion(Integer.toString(newestPolicy.getKey().getMajorVersion()));
135         }
136
137         ToscaServiceTemplate incomingServiceTemplate =
138                 new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
139         ToscaServiceTemplate outgoingingServiceTemplate =
140                 new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate);
141
142         return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
143     }
144
145     /**
146      * Delete legacy operational policy.
147      *
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
152      */
153     public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId)
154             throws PfModelException {
155
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);
158
159         // Find the latest policy that matches the ID
160         List<ToscaPolicy> policyDeleteList = new ArrayList<>();
161
162         for (ToscaPolicy policy : policyList) {
163             if (policyId.equals(policy.getKey().getName())) {
164                 policyDeleteList.add(policy);
165             }
166         }
167
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);
172         }
173
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());
178
179         for (ToscaPolicy deletePolicy : policyDeleteList) {
180             dao.delete(deletePolicy);
181             serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(deletePolicy.getKey(),
182                     deletePolicy);
183         }
184
185         return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
186     }
187
188     /**
189      * Get legacy guard policy.
190      *
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
195      */
196     public LegacyGuardPolicy getGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId)
197             throws PfModelException {
198         return null;
199     }
200
201     /**
202      * Create legacy guard policy.
203      *
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
208      */
209     public LegacyGuardPolicy createGuardPolicy(@NonNull final PfDao dao,
210             @NonNull final LegacyGuardPolicy legacyGuardPolicy) throws PfModelException {
211         return null;
212     }
213
214     /**
215      * Update legacy guard policy.
216      *
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
221      */
222     public LegacyGuardPolicy updateGuardPolicy(@NonNull final PfDao dao,
223             @NonNull final LegacyGuardPolicy legacyGuardPolicy) throws PfModelException {
224         return null;
225     }
226
227
228     /**
229      * Delete legacy guard policy.
230      *
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
235      */
236     public LegacyGuardPolicy deleteGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId)
237             throws PfModelException {
238         return null;
239     }
240
241     /**
242      * Get the latest policy for a policy ID.
243      *
244      * @param dao The DAO to read from
245      * @param policyId the ID of the policy
246      * @return the policy
247      */
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);
251
252         // Find the latest policy that matches the ID
253         ToscaPolicy newestPolicy = null;
254
255         for (ToscaPolicy policy : policyList) {
256             if (!policyId.equals(policy.getKey().getName())) {
257                 continue;
258             }
259
260             // We found a matching policy
261             if (newestPolicy == null || policy.getKey().isNewerThan(newestPolicy.getKey())) {
262                 // First policy found
263                 newestPolicy = policy;
264             }
265         }
266         return newestPolicy;
267     }
268 }