2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
\r
6 * Modifications Copyright (C) 2019-2021 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.HashMap;
\r
28 import java.util.List;
\r
29 import java.util.Map;
\r
30 import java.util.function.BiConsumer;
\r
31 import javax.ws.rs.core.Response;
\r
32 import org.apache.commons.lang3.tuple.Pair;
\r
33 import org.onap.policy.api.main.parameters.ApiParameterGroup;
\r
34 import org.onap.policy.common.parameters.ParameterService;
\r
35 import org.onap.policy.models.base.PfConceptKey;
\r
36 import org.onap.policy.models.base.PfModelException;
\r
37 import org.onap.policy.models.pdp.concepts.PdpGroup;
\r
38 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
\r
39 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
\r
40 import org.onap.policy.models.pdp.enums.PdpState;
\r
41 import org.onap.policy.models.provider.PolicyModelsProvider;
\r
42 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
\r
43 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
\r
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
\r
47 * Super class for providers that use a model provider.
\r
49 public class CommonModelProvider implements AutoCloseable {
\r
51 protected final PolicyModelsProvider modelsProvider;
\r
54 * Constructs the object, populating {@link #modelsProvider}.
\r
56 * @throws PfModelException if an error occurs
\r
58 public CommonModelProvider() throws PfModelException {
\r
60 ApiParameterGroup parameterGroup = ParameterService.get("ApiGroup");
\r
61 PolicyModelsProviderParameters providerParameters = parameterGroup.getDatabaseProviderParameters();
\r
62 modelsProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(providerParameters);
\r
66 * Closes the connection to database.
\r
68 * @throws PfModelException the PfModel parsing exception
\r
71 public void close() throws PfModelException {
\r
73 modelsProvider.close();
\r
77 * Collects all deployed versions of specified policy in all pdp groups.
\r
79 * @param policyId the ID of policy
\r
80 * @param policyType the concept key of policy type
\r
81 * @param getter the custom generic getter Bifunction
\r
82 * @param consumer the BiConsumer
\r
83 * @param data the data structure storing retrieved deployed policies
\r
85 * @return a map between pdp group and deployed versions of specified policy in that group
\r
87 * @throws PfModelException the PfModel parsing exception
\r
89 protected <T, R> Map<Pair<String, String>, T> collectDeployedPolicies(String policyId, PfConceptKey policyType,
\r
90 BiFunctionWithEx<String, String, R> getter, BiConsumer<T, R> consumer, T data) throws PfModelException {
\r
92 List<PdpGroup> pdpGroups = getPolicyTypeFilteredPdpGroups(policyType);
\r
93 hasActivePdpGroup(pdpGroups, policyType, policyId);
\r
94 return constructDeployedPolicyMap(pdpGroups, policyId, policyType, getter, consumer, data);
\r
97 @FunctionalInterface
\r
98 protected interface BiFunctionWithEx<T, U, R> {
\r
99 public R apply(T value1, U value2) throws PfModelException;
\r
103 * Checks if the list of pdp groups is empty. If so, throws exception saying specified policy deployment is not
\r
104 * found in all existing pdp groups.
\r
106 * @param pdpGroups the list of pdp groups to check against
\r
107 * @param policyType the concept key of policy type
\r
108 * @param policyId the ID of policy
\r
110 * @throws PfModelException the PfModel parsing exception
\r
112 private void hasActivePdpGroup(List<PdpGroup> pdpGroups, PfConceptKey policyType, String policyId)
\r
113 throws PfModelException {
\r
115 if (pdpGroups.isEmpty()) {
\r
116 throw new PfModelException(Response.Status.NOT_FOUND,
\r
117 constructDeploymentNotFoundMessage(policyType, policyId));
\r
122 * Retrieves all pdp groups supporting specified policy type.
\r
124 * @param policyTypeId the ID of policy type
\r
125 * @param policyTypeVersion the version of policy type
\r
127 * @return a list of pdp groups supporting specified policy type
\r
129 * @throws PfModelException the PfModel parsing exception
\r
131 private List<PdpGroup> getPolicyTypeFilteredPdpGroups(PfConceptKey policyType) throws PfModelException {
\r
133 List<ToscaConceptIdentifier> policyTypes = new ArrayList<>();
\r
134 policyTypes.add(new ToscaConceptIdentifier(policyType.getName(), policyType.getVersion()));
\r
135 var pdpGroupFilter = PdpGroupFilter.builder().policyTypeList(policyTypes).groupState(PdpState.ACTIVE)
\r
136 .pdpState(PdpState.ACTIVE).build();
\r
137 return modelsProvider.getFilteredPdpGroups(pdpGroupFilter);
\r
141 * Constructs the map of deployed pdp groups and deployed policies.
\r
143 * @param pdpGroups the list of pdp groups that contain the specified policy
\r
144 * @param policyId the ID of policy
\r
145 * @param policyType the concept key of policy type
\r
146 * @param getter the custom generic getter BiFunction
\r
147 * @param consumer the BiConsumer
\r
148 * @param data the data structure storing retrieved deployed policies
\r
150 * @return the constructed map of pdp groups and deployed policies
\r
152 * @throws PfModelException the PfModel parsing exception
\r
154 private <T, R> Map<Pair<String, String>, T> constructDeployedPolicyMap(List<PdpGroup> pdpGroups, String policyId,
\r
155 PfConceptKey policyType, BiFunctionWithEx<String, String, R> getter, BiConsumer<T, R> consumer, T data)
\r
156 throws PfModelException {
\r
158 Map<Pair<String, String>, T> deployedPolicyMap = new HashMap<>();
\r
159 for (PdpGroup pdpGroup : pdpGroups) {
\r
160 List<ToscaConceptIdentifier> policyIdentifiers = extractPolicyIdentifiers(policyId, pdpGroup, policyType);
\r
161 var deployedPolicies = getDeployedPolicies(policyIdentifiers, policyType, getter, consumer, data);
\r
162 deployedPolicyMap.put(Pair.of(pdpGroup.getName(), pdpGroup.getVersion()), deployedPolicies);
\r
164 return deployedPolicyMap;
\r
168 * Extracts policy identifiers matching specified policy ID from specified pdp group.
\r
170 * @param policyId the ID of policy to match
\r
171 * @param pdpGroup the target pdp group to search
\r
172 * @param policyType the concept key of policy type
\r
174 * @return the list of policy identifiers
\r
176 * @throws PfModelException the PfModel parsing exception
\r
178 private List<ToscaConceptIdentifier> extractPolicyIdentifiers(String policyId, PdpGroup pdpGroup,
\r
179 PfConceptKey policyType) throws PfModelException {
\r
181 List<ToscaConceptIdentifier> policyIdentifiers = new ArrayList<>();
\r
182 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
\r
183 for (ToscaConceptIdentifier policyIdentifier : pdpSubGroup.getPolicies()) {
\r
184 if (policyId.equalsIgnoreCase(policyIdentifier.getName())) {
\r
185 policyIdentifiers.add(policyIdentifier);
\r
189 if (policyIdentifiers.isEmpty()) {
\r
190 throw new PfModelException(Response.Status.NOT_FOUND,
\r
191 constructDeploymentNotFoundMessage(policyType, policyId));
\r
193 return policyIdentifiers;
\r
197 * Retrieves deployed policies in a generic way.
\r
199 * @param policyIdentifiers the identifiers of the policies to return
\r
200 * @param policyType the concept key of current policy type
\r
201 * @param getter the method reference of getting deployed policies
\r
202 * @param consumer the method reference of consuming the returned policies
\r
203 * @param data the data structure of deployed policies to return
\r
205 * @return the generic type of policy data structure to return
\r
207 * @throws PfModelException the PfModel parsing exception
\r
209 private <T, R> T getDeployedPolicies(List<ToscaConceptIdentifier> policyIdentifiers, PfConceptKey policyType,
\r
210 BiFunctionWithEx<String, String, R> getter, BiConsumer<T, R> consumer, T data) throws PfModelException {
\r
212 for (ToscaConceptIdentifier policyIdentifier : policyIdentifiers) {
\r
213 var result = getter.apply(policyIdentifier.getName(),
\r
214 getTrimedVersionForLegacyType(policyIdentifier.getVersion(), policyType));
\r
215 consumer.accept(data, result);
\r
221 * Trims the version for legacy policies.
\r
223 * @param fullVersion the full version format with major, minor, patch
\r
224 * @param policyType the concept key of policy type
\r
226 * @return the trimmed version
\r
228 private String getTrimedVersionForLegacyType(String fullVersion, PfConceptKey policyType) {
\r
229 return (policyType.getName().contains("guard") || policyType.getName().contains("Operational"))
\r
230 ? fullVersion.split("\\.")[0]
\r
235 * Constructs returned message for not found policy deployment.
\r
237 * @param policyType the concept key of policy type
\r
238 * @param policyId the ID of policy
\r
240 * @return constructed message
\r
242 private String constructDeploymentNotFoundMessage(PfConceptKey policyType, String policyId) {
\r
244 return "could not find policy with ID " + policyId + " and type " + policyType.getName() + ":"
\r
245 + policyType.getVersion() + " deployed in any pdp group";
\r