2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
7 * Modifications Copyright (C) 2021 Nordix Foundation.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pap.main.rest;
25 import com.google.re2j.Pattern;
26 import java.util.Collection;
27 import java.util.List;
28 import java.util.stream.Collectors;
29 import org.onap.policy.common.utils.services.Registry;
30 import org.onap.policy.models.base.PfModelException;
31 import org.onap.policy.models.pap.concepts.PolicyStatus;
32 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
33 import org.onap.policy.models.provider.PolicyModelsProvider;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
35 import org.onap.policy.pap.main.PapConstants;
36 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
37 import org.onap.policy.pap.main.notification.DeploymentTracker;
40 * Provider for PAP component to query policy deployment status.
42 public class PolicyStatusProvider {
45 * Factory for PAP DAO.
47 private final PolicyModelsProviderFactoryWrapper daoFactory;
51 * Constructs the object. Loads all deployed policies into the internal cache.
53 public PolicyStatusProvider() {
54 this.daoFactory = Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
58 * Gets the deployment status of all policies.
60 * @return the deployment status of all policies
61 * @throws PfModelException if a DB error occurs
63 public Collection<PolicyStatus> getStatus() throws PfModelException {
64 try (PolicyModelsProvider dao = daoFactory.create()) {
65 return accumulate(dao.getAllPolicyStatus());
70 * Gets the deployment status of a policy.
72 * @param policy policy of interest
73 * @return the deployment status of all policies
74 * @throws PfModelException if a DB error occurs
76 public Collection<PolicyStatus> getStatus(ToscaConceptIdentifierOptVersion policy) throws PfModelException {
77 try (PolicyModelsProvider dao = daoFactory.create()) {
78 return accumulate(dao.getAllPolicyStatus(policy));
83 * Gets the deployment status of a policy, returns only statuses, which matches the regex.
85 * @param patternString policy of interest
86 * @return the deployment status of all policies
87 * @throws PfModelException if a DB error occurs
89 public Collection<PolicyStatus> getByRegex(String patternString) throws PfModelException {
90 // try to make pattern out of regex
91 final var pattern = Pattern.compile(patternString);
92 // get all the statuses
93 final List<PdpPolicyStatus> policyStatuses;
94 try (PolicyModelsProvider dao = daoFactory.create()) {
95 policyStatuses = dao.getAllPolicyStatus();
97 // filter out statuses with the wrong name
98 final Collection<PdpPolicyStatus> pdpPolicyStatuses = filterWithPattern(pattern, policyStatuses);
100 return accumulate(pdpPolicyStatuses);
105 * Accumulates the deployment status of individual PDP/policy pairs into a status for
108 * @param source PDP/policy pairs
109 * @return the deployment status of the policies
111 private Collection<PolicyStatus> accumulate(Collection<PdpPolicyStatus> source) {
112 var tracker = new DeploymentTracker();
114 for (PdpPolicyStatus status : source) {
115 if (status.isDeploy()) {
120 return tracker.getDeploymentStatus();
125 * Gets the status of all policies.
127 * @return the status of all policies
128 * @throws PfModelException if a DB error occurs
130 public Collection<PdpPolicyStatus> getPolicyStatus() throws PfModelException {
131 try (PolicyModelsProvider dao = daoFactory.create()) {
132 return dao.getAllPolicyStatus();
137 * Gets the status of policies in a PdpGroup.
139 * @param pdpGroupName the pdp group
140 * @return the deployment status of policies
141 * @throws PfModelException if a DB error occurs
143 public Collection<PdpPolicyStatus> getPolicyStatus(String pdpGroupName) throws PfModelException {
144 try (PolicyModelsProvider dao = daoFactory.create()) {
145 return dao.getGroupPolicyStatus(pdpGroupName);
150 * Gets the status of a policy in a PdpGroup.
152 * @param pdpGroupName the pdp group
153 * @param policy the policy
154 * @return the deployment status of the policy
155 * @throws PfModelException if a DB error occurs
157 public Collection<PdpPolicyStatus> getPolicyStatus(String pdpGroupName, ToscaConceptIdentifierOptVersion policy)
158 throws PfModelException {
159 try (PolicyModelsProvider dao = daoFactory.create()) {
160 return dao.getAllPolicyStatus(policy).stream().filter(p -> p.getPdpGroup().equals(pdpGroupName))
161 .collect(Collectors.toList());
166 * Gets the status of policies in a PdpGroup that match the given regex.
168 * @param pdpGroupName the pdp group
169 * @param patternString regex
170 * @return the deployment status of policies
171 * @throws PfModelException if a DB error occurs
173 public Collection<PdpPolicyStatus> getPolicyStatusByRegex(String pdpGroupName, String patternString)
174 throws PfModelException {
175 final var pattern = Pattern.compile(patternString);
176 // get all the statuses
177 final Collection<PdpPolicyStatus> policyStatuses = getPolicyStatus(pdpGroupName);
178 // filter out statuses with the wrong name
179 return filterWithPattern(pattern, policyStatuses);
182 private Collection<PdpPolicyStatus> filterWithPattern(Pattern pattern, Collection<PdpPolicyStatus> policyStatuses) {
183 return policyStatuses
185 .filter(policyStatus -> {
187 final String policyName = policyStatus
190 return pattern.matcher(policyName).matches();
192 .collect(Collectors.toList());