Incorporate filters in provider functions
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / rest / provider / LegacyGuardPolicyProvider.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 java.util.Map;
26 import javax.ws.rs.core.Response;
27 import org.onap.policy.api.main.parameters.ApiParameterGroup;
28 import org.onap.policy.common.parameters.ParameterService;
29 import org.onap.policy.models.base.PfModelException;
30 import org.onap.policy.models.provider.PolicyModelsProvider;
31 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
32 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
33 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput;
34 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyOutput;
35
36 /**
37  * Class to provide all kinds of legacy guard policy operations.
38  *
39  * @author Chenfei Gao (cgao@research.att.com)
40  */
41 public class LegacyGuardPolicyProvider {
42
43     private PolicyModelsProvider modelsProvider;
44
45     /**
46      * Default constructor.
47      */
48     public LegacyGuardPolicyProvider() throws PfModelException {
49
50         ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");
51         PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();
52         modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);
53     }
54
55     /**
56      * Retrieves a list of guard policies matching specified ID and version.
57      *
58      * @param policyId the ID of policy
59      * @param policyVersion the version of policy
60      *
61      * @return the map of LegacyGuardPolicyOutput objects
62      */
63     public Map<String, LegacyGuardPolicyOutput> fetchGuardPolicy(String policyId, String policyVersion)
64             throws PfModelException {
65
66         Map<String, LegacyGuardPolicyOutput> guardPolicies = modelsProvider.getGuardPolicy(policyId);
67
68         close();
69         return guardPolicies;
70     }
71
72     /**
73      * Creates a new guard policy.
74      *
75      * @param body the entity body of policy
76      *
77      * @return the map of LegacyGuardPolicyOutput objectst
78      */
79     public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(LegacyGuardPolicyInput body) throws PfModelException {
80
81         Map<String, LegacyGuardPolicyOutput> guardPolicies = modelsProvider.createGuardPolicy(body);
82
83         close();
84         return guardPolicies;
85     }
86
87     /**
88      * Deletes the guard policies matching specified ID and version.
89      *
90      * @param policyId the ID of policy
91      * @param policyVersion the version of policy
92      *
93      * @return the map of LegacyGuardPolicyOutput objects
94      */
95     public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(String policyId, String policyVersion)
96             throws PfModelException {
97
98         Map<String, LegacyGuardPolicyOutput> guardPolicies = modelsProvider.deleteGuardPolicy(policyId);
99
100         close();
101         return guardPolicies;
102     }
103
104     /**
105      * Closes the connection to database.
106      *
107      * @throws PfModelException the PfModel parsing exception
108      */
109     private void close() throws PfModelException {
110         try {
111             modelsProvider.close();
112         } catch (Exception e) {
113             throw new PfModelException(
114                     Response.Status.INTERNAL_SERVER_ERROR, "error closing connection to database", e);
115         }
116     }
117 }