2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.api.main.rest.provider;
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;
37 * Class to provide all kinds of legacy guard policy operations.
39 * @author Chenfei Gao (cgao@research.att.com)
41 public class LegacyGuardPolicyProvider {
43 private PolicyModelsProvider modelsProvider;
46 * Default constructor.
48 public LegacyGuardPolicyProvider() throws PfModelException {
50 ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");
51 PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();
52 modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);
56 * Retrieves a list of guard policies matching specified ID and version.
58 * @param policyId the ID of policy
59 * @param policyVersion the version of policy
61 * @return the map of LegacyGuardPolicyOutput objects
63 public Map<String, LegacyGuardPolicyOutput> fetchGuardPolicy(String policyId, String policyVersion)
64 throws PfModelException {
66 Map<String, LegacyGuardPolicyOutput> guardPolicies = modelsProvider.getGuardPolicy(policyId);
73 * Creates a new guard policy.
75 * @param body the entity body of policy
77 * @return the map of LegacyGuardPolicyOutput objectst
79 public Map<String, LegacyGuardPolicyOutput> createGuardPolicy(LegacyGuardPolicyInput body) throws PfModelException {
81 Map<String, LegacyGuardPolicyOutput> guardPolicies = modelsProvider.createGuardPolicy(body);
88 * Deletes the guard policies matching specified ID and version.
90 * @param policyId the ID of policy
91 * @param policyVersion the version of policy
93 * @return the map of LegacyGuardPolicyOutput objects
95 public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(String policyId, String policyVersion)
96 throws PfModelException {
98 Map<String, LegacyGuardPolicyOutput> guardPolicies = modelsProvider.deleteGuardPolicy(policyId);
101 return guardPolicies;
105 * Closes the connection to database.
107 * @throws PfModelException the PfModel parsing exception
109 private void close() throws PfModelException {
111 modelsProvider.close();
112 } catch (Exception e) {
113 throw new PfModelException(
114 Response.Status.INTERNAL_SERVER_ERROR, "error closing connection to database", e);