Implement policy 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         modelsProvider.init();
54     }
55
56     /**
57      * Retrieves a list of guard policies matching specified ID and version.
58      *
59      * @param policyId the ID of policy
60      * @param policyVersion the version of policy
61      *
62      * @return the map of LegacyGuardPolicyOutput objects
63      */
64     public Map<String, LegacyGuardPolicyOutput> fetchGuardPolicies(String policyId, String policyVersion)
65             throws PfModelException {
66
67         Map<String, LegacyGuardPolicyOutput> guardPolicies = modelsProvider.getGuardPolicy(policyId);
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         Map<String, LegacyGuardPolicyOutput> guardPolicies = modelsProvider.createGuardPolicy(body);
81         close();
82         return guardPolicies;
83     }
84
85     /**
86      * Deletes the guard 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 map of LegacyGuardPolicyOutput objects
92      */
93     public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicies(String policyId, String policyVersion)
94             throws PfModelException {
95
96         Map<String, LegacyGuardPolicyOutput> guardPolicies = modelsProvider.deleteGuardPolicy(policyId);
97         close();
98         return guardPolicies;
99     }
100
101     /**
102      * Closes the connection to database.
103      *
104      * @throws PfModelException the PfModel parsing exception
105      */
106     private void close() throws PfModelException {
107         try {
108             modelsProvider.close();
109         } catch (Exception e) {
110             throw new PfModelException(
111                     Response.Status.INTERNAL_SERVER_ERROR, "error closing connection to database", e);
112         }
113     }
114 }