2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
\r
6 * Modifications Copyright (C) 2020 Nordix Foundation.
\r
7 * ================================================================================
\r
8 * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * you may not use this file except in compliance with the License.
\r
10 * You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * Unless required by applicable law or agreed to in writing, software
\r
15 * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * See the License for the specific language governing permissions and
\r
18 * limitations under the License.
\r
20 * SPDX-License-Identifier: Apache-2.0
\r
21 * ============LICENSE_END=========================================================
\r
24 package org.onap.policy.api.main.rest.provider;
\r
26 import java.util.ArrayList;
\r
27 import java.util.List;
\r
28 import java.util.Map;
\r
30 import org.apache.commons.lang3.tuple.Pair;
\r
31 import org.onap.policy.models.base.PfConceptKey;
\r
32 import org.onap.policy.models.base.PfModelException;
\r
33 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
\r
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter;
\r
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
\r
38 * Class to provide all kinds of policy operations.
\r
40 * @author Chenfei Gao (cgao@research.att.com)
\r
42 public class PolicyProvider extends CommonModelProvider {
\r
45 * Default constructor.
\r
47 public PolicyProvider() throws PfModelException {
\r
52 * Retrieves a list of policies matching specified ID and version of both policy type and policy.
\r
54 * @param policyTypeId the ID of policy type
\r
55 * @param policyTypeVersion the version of policy type
\r
56 * @param policyId the ID of policy
\r
57 * @param policyVersion the version of policy
\r
59 * @return the ToscaServiceTemplate object
\r
61 * @throws PfModelException the PfModel parsing exception
\r
63 public ToscaServiceTemplate fetchPolicies(String policyTypeId, String policyTypeVersion, String policyId,
\r
64 String policyVersion) throws PfModelException {
\r
66 return getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, policyVersion);
\r
70 * Retrieves a list of policies with the latest versions that match specified policy type id and version.
\r
72 * @param policyTypeId the ID of policy type
\r
73 * @param policyTypeVersion the version of policy type
\r
74 * @param policyId the ID of the policy
\r
76 * @return the ToscaServiceTemplate object
\r
78 * @throws PfModelException the PfModel parsing exception
\r
80 public ToscaServiceTemplate fetchLatestPolicies(String policyTypeId, String policyTypeVersion, String policyId)
\r
81 throws PfModelException {
\r
83 return getFilteredPolicies(policyTypeId, policyTypeVersion, policyId, ToscaPolicyFilter.LATEST_VERSION);
\r
87 * Retrieves a list of deployed policies in each pdp group.
\r
89 * @param policyTypeId the ID of policy type
\r
90 * @param policyTypeVersion the version of policy type
\r
91 * @param policyId the ID of the policy
\r
93 * @return a list of deployed policies in each pdp group
\r
95 * @throws PfModelException the PfModel parsing exception
\r
97 public Map<Pair<String, String>, List<ToscaPolicy>> fetchDeployedPolicies(String policyTypeId,
\r
98 String policyTypeVersion, String policyId) throws PfModelException {
\r
100 return collectDeployedPolicies(policyId, new PfConceptKey(policyTypeId, policyTypeVersion),
\r
101 modelsProvider::getPolicyList, List::addAll, new ArrayList<>(5));
\r
105 * Creates one or more new policies for the same policy type ID and version.
\r
107 * @param policyTypeId the ID of policy type
\r
108 * @param policyTypeVersion the version of policy type
\r
109 * @param body the entity body of polic(ies)
\r
111 * @return the ToscaServiceTemplate object
\r
113 * @throws PfModelException the PfModel parsing exception
\r
115 public ToscaServiceTemplate createPolicy(String policyTypeId, String policyTypeVersion, ToscaServiceTemplate body)
\r
116 throws PfModelException {
\r
118 return modelsProvider.createPolicies(body);
\r
122 * Creates one or more new policies.
\r
124 * @param body the entity body of policy
\r
126 * @return the ToscaServiceTemplate object
\r
128 * @throws PfModelException the PfModel parsing exception
\r
130 public ToscaServiceTemplate createPolicies(ToscaServiceTemplate body) throws PfModelException {
\r
131 return modelsProvider.createPolicies(body);
\r
135 * Deletes the policy matching specified ID and version of both policy type and policy.
\r
137 * @param policyTypeId the ID of policy type
\r
138 * @param policyTypeVersion the version of policy type
\r
139 * @param policyId the ID of policy
\r
140 * @param policyVersion the version of policy
\r
142 * @return the ToscaServiceTemplate object
\r
144 * @throws PfModelException the PfModel parsing exception
\r
146 public ToscaServiceTemplate deletePolicy(String policyTypeId, String policyTypeVersion, String policyId,
\r
147 String policyVersion) throws PfModelException {
\r
149 return modelsProvider.deletePolicy(policyId, policyVersion);
\r
153 * Retrieves the specified version of the policy.
\r
155 * @param policyTypeName the name of the policy type
\r
156 * @param policyTypeVersion the version of the policy type
\r
157 * @param policyName the name of the policy
\r
158 * @param policyVersion the version of the policy
\r
160 * @return the TOSCA service template containing the specified version of the policy
\r
162 * @throws PfModelException the PfModel parsing exception
\r
164 private ToscaServiceTemplate getFilteredPolicies(String policyTypeName, String policyTypeVersion, String policyName,
\r
165 String policyVersion) throws PfModelException {
\r
167 ToscaPolicyFilter policyFilter = ToscaPolicyFilter.builder().name(policyName).version(policyVersion)
\r
168 .type(policyTypeName).typeVersion(policyTypeVersion).build();
\r
169 return modelsProvider.getFilteredPolicies(policyFilter);
\r