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 * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
\r
8 * ================================================================================
\r
9 * Licensed under the Apache License, Version 2.0 (the "License");
\r
10 * you may not use this file except in compliance with the License.
\r
11 * You may obtain a copy of the License at
\r
13 * http://www.apache.org/licenses/LICENSE-2.0
\r
15 * Unless required by applicable law or agreed to in writing, software
\r
16 * distributed under the License is distributed on an "AS IS" BASIS,
\r
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
18 * See the License for the specific language governing permissions and
\r
19 * limitations under the License.
\r
21 * SPDX-License-Identifier: Apache-2.0
\r
22 * ============LICENSE_END=========================================================
\r
25 package org.onap.policy.api.main.rest.provider;
\r
27 import java.util.ArrayList;
\r
28 import java.util.HashMap;
\r
29 import java.util.List;
\r
30 import java.util.Map;
\r
31 import java.util.function.BiConsumer;
\r
32 import javax.ws.rs.core.Response;
\r
33 import org.apache.commons.lang3.tuple.Pair;
\r
34 import org.onap.policy.models.base.PfConceptKey;
\r
35 import org.onap.policy.models.base.PfModelException;
\r
36 import org.onap.policy.models.pdp.concepts.PdpGroup;
\r
37 import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
\r
38 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
\r
39 import org.onap.policy.models.pdp.enums.PdpState;
\r
40 import org.onap.policy.models.provider.PolicyModelsProvider;
\r
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
\r
44 * Super class for providers that use a model provider.
\r
46 public class CommonModelProvider {
\r
48 protected final PolicyModelsProvider modelsProvider;
\r
51 * Constructs the object, populating {@link #modelsProvider}.
\r
54 public CommonModelProvider(PolicyModelsProvider modelsProvider) {
\r
55 this.modelsProvider = modelsProvider;
\r
59 * Collects all deployed versions of specified policy in all pdp groups.
\r
61 * @param policyId the ID of policy
\r
62 * @param policyType the concept key of policy type
\r
63 * @param getter the custom generic getter Bifunction
\r
64 * @param consumer the BiConsumer
\r
65 * @param data the data structure storing retrieved deployed policies
\r
67 * @return a map between pdp group and deployed versions of specified policy in that group
\r
69 * @throws PfModelException the PfModel parsing exception
\r
71 protected <T, R> Map<Pair<String, String>, T> collectDeployedPolicies(String policyId, PfConceptKey policyType,
\r
72 BiFunctionWithEx<String, String, R> getter, BiConsumer<T, R> consumer, T data) throws PfModelException {
\r
74 List<PdpGroup> pdpGroups = getPolicyTypeFilteredPdpGroups(policyType);
\r
75 hasActivePdpGroup(pdpGroups, policyType, policyId);
\r
76 return constructDeployedPolicyMap(pdpGroups, policyId, policyType, getter, consumer, data);
\r
79 @FunctionalInterface
\r
80 protected interface BiFunctionWithEx<T, U, R> {
\r
81 public R apply(T value1, U value2) throws PfModelException;
\r
85 * Checks if the list of pdp groups is empty. If so, throws exception saying specified policy deployment is not
\r
86 * found in all existing pdp groups.
\r
88 * @param pdpGroups the list of pdp groups to check against
\r
89 * @param policyType the concept key of policy type
\r
90 * @param policyId the ID of policy
\r
92 * @throws PfModelException the PfModel parsing exception
\r
94 private void hasActivePdpGroup(List<PdpGroup> pdpGroups, PfConceptKey policyType, String policyId)
\r
95 throws PfModelException {
\r
97 if (pdpGroups.isEmpty()) {
\r
98 throw new PfModelException(Response.Status.NOT_FOUND,
\r
99 constructDeploymentNotFoundMessage(policyType, policyId));
\r
104 * Retrieves all pdp groups supporting specified policy type.
\r
106 * @param policyType the policy type
\r
108 * @return a list of pdp groups supporting specified policy type
\r
110 * @throws PfModelException the PfModel parsing exception
\r
112 private List<PdpGroup> getPolicyTypeFilteredPdpGroups(PfConceptKey policyType) throws PfModelException {
\r
114 List<ToscaConceptIdentifier> policyTypes = new ArrayList<>();
\r
115 policyTypes.add(new ToscaConceptIdentifier(policyType.getName(), policyType.getVersion()));
\r
116 var pdpGroupFilter = PdpGroupFilter.builder().policyTypeList(policyTypes).groupState(PdpState.ACTIVE)
\r
117 .pdpState(PdpState.ACTIVE).build();
\r
118 return modelsProvider.getFilteredPdpGroups(pdpGroupFilter);
\r
122 * Constructs the map of deployed pdp groups and deployed policies.
\r
124 * @param pdpGroups the list of pdp groups that contain the specified policy
\r
125 * @param policyId the ID of policy
\r
126 * @param policyType the concept key of policy type
\r
127 * @param getter the custom generic getter BiFunction
\r
128 * @param consumer the BiConsumer
\r
129 * @param data the data structure storing retrieved deployed policies
\r
131 * @return the constructed map of pdp groups and deployed policies
\r
133 * @throws PfModelException the PfModel parsing exception
\r
135 private <T, R> Map<Pair<String, String>, T> constructDeployedPolicyMap(List<PdpGroup> pdpGroups, String policyId,
\r
136 PfConceptKey policyType, BiFunctionWithEx<String, String, R> getter, BiConsumer<T, R> consumer, T data)
\r
137 throws PfModelException {
\r
139 Map<Pair<String, String>, T> deployedPolicyMap = new HashMap<>();
\r
140 for (PdpGroup pdpGroup : pdpGroups) {
\r
141 List<ToscaConceptIdentifier> policyIdentifiers = extractPolicyIdentifiers(policyId, pdpGroup, policyType);
\r
142 var deployedPolicies = getDeployedPolicies(policyIdentifiers, policyType, getter, consumer, data);
\r
143 deployedPolicyMap.put(Pair.of(pdpGroup.getName(), pdpGroup.getVersion()), deployedPolicies);
\r
145 return deployedPolicyMap;
\r
149 * Extracts policy identifiers matching specified policy ID from specified pdp group.
\r
151 * @param policyId the ID of policy to match
\r
152 * @param pdpGroup the target pdp group to search
\r
153 * @param policyType the concept key of policy type
\r
155 * @return the list of policy identifiers
\r
157 * @throws PfModelException the PfModel parsing exception
\r
159 private List<ToscaConceptIdentifier> extractPolicyIdentifiers(String policyId, PdpGroup pdpGroup,
\r
160 PfConceptKey policyType) throws PfModelException {
\r
162 List<ToscaConceptIdentifier> policyIdentifiers = new ArrayList<>();
\r
163 for (PdpSubGroup pdpSubGroup : pdpGroup.getPdpSubgroups()) {
\r
164 for (ToscaConceptIdentifier policyIdentifier : pdpSubGroup.getPolicies()) {
\r
165 if (policyId.equalsIgnoreCase(policyIdentifier.getName())) {
\r
166 policyIdentifiers.add(policyIdentifier);
\r
170 if (policyIdentifiers.isEmpty()) {
\r
171 throw new PfModelException(Response.Status.NOT_FOUND,
\r
172 constructDeploymentNotFoundMessage(policyType, policyId));
\r
174 return policyIdentifiers;
\r
178 * Retrieves deployed policies in a generic way.
\r
180 * @param policyIdentifiers the identifiers of the policies to return
\r
181 * @param policyType the concept key of current policy type
\r
182 * @param getter the method reference of getting deployed policies
\r
183 * @param consumer the method reference of consuming the returned policies
\r
184 * @param data the data structure of deployed policies to return
\r
186 * @return the generic type of policy data structure to return
\r
188 * @throws PfModelException the PfModel parsing exception
\r
190 private <T, R> T getDeployedPolicies(List<ToscaConceptIdentifier> policyIdentifiers, PfConceptKey policyType,
\r
191 BiFunctionWithEx<String, String, R> getter, BiConsumer<T, R> consumer, T data) throws PfModelException {
\r
193 for (ToscaConceptIdentifier policyIdentifier : policyIdentifiers) {
\r
194 var result = getter.apply(policyIdentifier.getName(),
\r
195 getTrimedVersionForLegacyType(policyIdentifier.getVersion(), policyType));
\r
196 consumer.accept(data, result);
\r
202 * Trims the version for legacy policies.
\r
204 * @param fullVersion the full version format with major, minor, patch
\r
205 * @param policyType the concept key of policy type
\r
207 * @return the trimmed version
\r
209 private String getTrimedVersionForLegacyType(String fullVersion, PfConceptKey policyType) {
\r
210 return (policyType.getName().contains("guard") || policyType.getName().contains("Operational"))
\r
211 ? fullVersion.split("\\.")[0]
\r
216 * Constructs returned message for not found policy deployment.
\r
218 * @param policyType the concept key of policy type
\r
219 * @param policyId the ID of policy
\r
221 * @return constructed message
\r
223 private String constructDeploymentNotFoundMessage(PfConceptKey policyType, String policyId) {
\r
225 return "could not find policy with ID " + policyId + " and type " + policyType.getName() + ":"
\r
226 + policyType.getVersion() + " deployed in any pdp group";
\r