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;
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;
35 * Class to provide all kinds of legacy operational policy operations.
37 * @author Chenfei Gao (cgao@research.att.com)
39 public class LegacyOperationalPolicyProvider {
41 private PolicyModelsProvider modelsProvider;
44 * Default constructor.
46 public LegacyOperationalPolicyProvider() throws PfModelException {
48 ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");
49 PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();
50 modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);
54 * Retrieves a list of operational policies matching specified ID and version.
56 * @param policyId the ID of policy
57 * @param policyVersion the version of policy
59 * @return the LegacyOperationalPolicy object
61 public LegacyOperationalPolicy fetchOperationalPolicy(String policyId, String policyVersion)
62 throws PfModelException {
64 LegacyOperationalPolicy operationalPolicy = modelsProvider.getOperationalPolicy(policyId);
67 return operationalPolicy;
71 * Creates a new operational policy.
73 * @param body the entity body of policy
75 * @return the LegacyOperationalPolicy object
77 public LegacyOperationalPolicy createOperationalPolicy(LegacyOperationalPolicy body) throws PfModelException {
79 LegacyOperationalPolicy operationalPolicy = modelsProvider.createOperationalPolicy(body);
82 return operationalPolicy;
86 * Deletes the operational policies matching specified ID and version.
88 * @param policyId the ID of policy
89 * @param policyVersion the version of policy
91 * @return the LegacyOperationalPolicy object
93 public LegacyOperationalPolicy deleteOperationalPolicy(String policyId, String policyVersion)
94 throws PfModelException {
96 LegacyOperationalPolicy operationalPolicy = modelsProvider.deleteOperationalPolicy(policyId);
99 return operationalPolicy;
103 * Closes the connection to database.
105 * @throws PfModelException the PfModel parsing exception
107 private void close() throws PfModelException {
109 modelsProvider.close();
110 } catch (Exception e) {
111 throw new PfModelException(
112 Response.Status.INTERNAL_SERVER_ERROR, "error closing connection to database", e);