Incorporate filters in provider functions
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / provider / LegacyOperationalPolicyProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.api.main.rest.provider;
24
25 import javax.ws.rs.core.Response;
26 import org.onap.policy.api.main.parameters.ApiParameterGroup;
27 import org.onap.policy.common.parameters.ParameterService;
28 import org.onap.policy.models.base.PfModelException;
29 import org.onap.policy.models.provider.PolicyModelsProvider;
30 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
31 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
32 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
33
34 /**
35  * Class to provide all kinds of legacy operational policy operations.
36  *
37  * @author Chenfei Gao (cgao@research.att.com)
38  */
39 public class LegacyOperationalPolicyProvider {
40
41     private PolicyModelsProvider modelsProvider;
42
43     /**
44      * Default constructor.
45      */
46     public LegacyOperationalPolicyProvider() throws PfModelException {
47
48         ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");
49         PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();
50         modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);
51     }
52
53     /**
54      * Retrieves a list of operational policies matching specified ID and version.
55      *
56      * @param policyId the ID of policy
57      * @param policyVersion the version of policy
58      *
59      * @return the LegacyOperationalPolicy object
60      */
61     public LegacyOperationalPolicy fetchOperationalPolicy(String policyId, String policyVersion)
62             throws PfModelException {
63
64         LegacyOperationalPolicy operationalPolicy = modelsProvider.getOperationalPolicy(policyId);
65
66         close();
67         return operationalPolicy;
68     }
69
70     /**
71      * Creates a new operational policy.
72      *
73      * @param body the entity body of policy
74      *
75      * @return the LegacyOperationalPolicy object
76      */
77     public LegacyOperationalPolicy createOperationalPolicy(LegacyOperationalPolicy body) throws PfModelException {
78
79         LegacyOperationalPolicy operationalPolicy = modelsProvider.createOperationalPolicy(body);
80
81         close();
82         return operationalPolicy;
83     }
84
85     /**
86      * Deletes the operational policies matching specified ID and version.
87      *
88      * @param policyId the ID of policy
89      * @param policyVersion the version of policy
90      *
91      * @return the LegacyOperationalPolicy object
92      */
93     public LegacyOperationalPolicy deleteOperationalPolicy(String policyId, String policyVersion)
94             throws PfModelException {
95
96         LegacyOperationalPolicy operationalPolicy = modelsProvider.deleteOperationalPolicy(policyId);
97
98         close();
99         return operationalPolicy;
100     }
101
102     /**
103      * Closes the connection to database.
104      *
105      * @throws PfModelException the PfModel parsing exception
106      */
107     private void close() throws PfModelException {
108         try {
109             modelsProvider.close();
110         } catch (Exception e) {
111             throw new PfModelException(
112                     Response.Status.INTERNAL_SERVER_ERROR, "error closing connection to database", e);
113         }
114     }
115 }