2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
19 * SPDX-License-Identifier: Apache-2.0
\r
20 * ============LICENSE_END=========================================================
\r
23 package org.onap.policy.api.main.rest.provider;
\r
25 import java.util.List;
\r
26 import java.util.Map;
\r
27 import javax.ws.rs.core.Response;
\r
28 import org.apache.commons.lang3.tuple.Pair;
\r
29 import org.onap.policy.api.main.parameters.ApiParameterGroup;
\r
30 import org.onap.policy.common.parameters.ParameterService;
\r
31 import org.onap.policy.models.base.PfModelException;
\r
32 import org.onap.policy.models.provider.PolicyModelsProvider;
\r
33 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
\r
34 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
\r
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
\r
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
\r
39 * Class to provide all kinds of policy operations.
\r
41 * @author Chenfei Gao (cgao@research.att.com)
\r
43 public class PolicyProvider {
\r
45 private PolicyModelsProvider modelsProvider;
\r
48 * Default constructor.
\r
50 public PolicyProvider() throws PfModelException {
\r
52 ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");
\r
53 PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();
\r
54 modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);
\r
55 modelsProvider.init();
\r
59 * Retrieves a list of policies matching specified ID and version of both policy type and policy.
\r
61 * @param policyTypeId the ID of policy type
\r
62 * @param policyTypeVersion the version of policy type
\r
63 * @param policyId the ID of policy
\r
64 * @param policyVersion the version of policy
\r
66 * @return the ToscaServiceTemplate object
\r
68 * @throws PfModelException the PfModel parsing exception
\r
70 public ToscaServiceTemplate fetchPolicies(String policyTypeId, String policyTypeVersion,
\r
71 String policyId, String policyVersion) throws PfModelException {
\r
73 validatePathParam(policyTypeId, policyTypeVersion);
\r
74 ToscaServiceTemplate serviceTemplate;
\r
75 if (policyId == null) {
\r
76 serviceTemplate = modelsProvider.getPolicies4PolicyType(policyTypeId, policyTypeVersion);
\r
78 serviceTemplate = modelsProvider.getPolicies(policyId, policyVersion);
\r
81 return serviceTemplate;
\r
85 * Retrieves a list of policies with the latest versions that match specified policy type id and version.
\r
87 * @param policyTypeId the ID of policy type
\r
88 * @param policyTypeVersion the version of policy type
\r
89 * @param policyId the ID of the policy
\r
91 * @return the ToscaServiceTemplate object
\r
93 * @throws PfModelException the PfModel parsing exception
\r
95 public ToscaServiceTemplate fetchLatestPolicies(String policyTypeId, String policyTypeVersion,
\r
96 String policyId) throws PfModelException {
\r
98 validatePathParam(policyTypeId, policyTypeVersion);
\r
99 ToscaServiceTemplate serviceTemplate = modelsProvider.getLatestPolicies(policyId);
\r
101 return serviceTemplate;
\r
105 * Retrieves a list of deployed policies in each pdp group.
\r
107 * @param policyTypeId the ID of policy type
\r
108 * @param policyTypeVersion the version of policy type
\r
109 * @param policyId the ID of the policy
\r
111 * @return a list of deployed policies in each pdp group
\r
113 * @throws PfModelException the PfModel parsing exception
\r
115 public Map<Pair<String, String>, List<ToscaPolicy>> fetchDeployedPolicies(
\r
116 String policyTypeId, String policyTypeVersion, String policyId) throws PfModelException {
\r
118 validatePathParam(policyTypeId, policyTypeVersion);
\r
119 Map<Pair<String, String>, List<ToscaPolicy>> deployedPolicies = modelsProvider.getDeployedPolicyList(policyId);
\r
121 return deployedPolicies;
\r
125 * Creates a new policy for a policy type ID and version.
\r
127 * @param policyTypeId the ID of policy type
\r
128 * @param policyTypeVersion the version of policy type
\r
129 * @param body the entity body of policy
\r
131 * @return the ToscaServiceTemplate object
\r
133 * @throws PfModelException the PfModel parsing exception
\r
135 public ToscaServiceTemplate createPolicy(String policyTypeId, String policyTypeVersion,
\r
136 ToscaServiceTemplate body) throws PfModelException {
\r
138 validatePathParam(policyTypeId, policyTypeVersion);
\r
139 validatePolicyTypeMatch(policyTypeId, policyTypeVersion, body);
\r
140 ToscaServiceTemplate serviceTemplate = modelsProvider.createPolicies(body);
\r
142 return serviceTemplate;
\r
146 * Deletes the policy matching specified ID and version of both policy type and policy.
\r
148 * @param policyTypeId the ID of policy type
\r
149 * @param policyTypeVersion the version of policy type
\r
150 * @param policyId the ID of policy
\r
151 * @param policyVersion the version of policy
\r
153 * @return the ToscaServiceTemplate object
\r
155 * @throws PfModelException the PfModel parsing exception
\r
157 public ToscaServiceTemplate deletePolicy(String policyTypeId, String policyTypeVersion,
\r
158 String policyId, String policyVersion) throws PfModelException {
\r
160 validatePathParam(policyTypeId, policyTypeVersion);
\r
161 ToscaServiceTemplate serviceTemplate = modelsProvider.deletePolicy(policyId, policyVersion);
\r
163 return serviceTemplate;
\r
167 * Checks the validation of policy type info passed in as path param.
\r
169 * @param policyTypeId the ID of policy type
\r
170 * @param policyTypeVersion the version of policy type
\r
172 * @throws PfModelException the PfModel parsing exception
\r
174 private void validatePathParam(String policyTypeId, String policyTypeVersion) throws PfModelException {
\r
176 // Check policy type existence
\r
178 modelsProvider.getPolicyTypes(policyTypeId, policyTypeVersion);
\r
179 } catch (Exception e) {
\r
180 throw new PfModelException(Response.Status.NOT_FOUND, "specified policy type does not exist", e);
\r
185 * Validates the match between policy type specified in path param and the one specified in type of policy.
\r
187 * @param body the ToscaServiceTemplate to create
\r
189 * @throws PfModelException the PfModel parsing exception
\r
191 private void validatePolicyTypeMatch(String policyTypeId, String policyTypeVersion, ToscaServiceTemplate body)
\r
192 throws PfModelException {
\r
194 List<Map<String, ToscaPolicy>> policies = body.getToscaTopologyTemplate().getPolicies();
\r
195 for (Map<String, ToscaPolicy> policy : policies) {
\r
196 if (policy.size() != 1) {
\r
197 throw new PfModelException(Response.Status.BAD_REQUEST,
\r
198 "one policy block contains more than one policies");
\r
200 ToscaPolicy policyContent = policy.values().iterator().next();
\r
201 if (!policyTypeId.equalsIgnoreCase(policyContent.getType())
\r
202 || !policyTypeVersion.equalsIgnoreCase(policyContent.getVersion())) {
\r
203 throw new PfModelException(Response.Status.BAD_REQUEST, "policy type info does not match");
\r
209 * Closes the connection to database.
\r
211 * @throws PfModelException the PfModel parsing exception
\r
213 private void close() throws PfModelException {
\r
215 modelsProvider.close();
\r
216 } catch (Exception e) {
\r
217 throw new PfModelException(
\r
218 Response.Status.INTERNAL_SERVER_ERROR, "error closing connection to database", e);
\r