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;
38 import org.springframework.boot.context.event.ApplicationReadyEvent;
39 import org.springframework.context.event.EventListener;
40 import org.springframework.stereotype.Service;
43 * Provider for PAP component to query policy deployment status.
46 public class PolicyStatusProvider {
49 * Factory for PAP DAO.
51 private PolicyModelsProviderFactoryWrapper daoFactory;
54 * Constructs the object. Loads all deployed policies into the internal cache.
56 @EventListener(ApplicationReadyEvent.class)
57 public void initialize() {
58 this.daoFactory = Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
62 * Gets the deployment status of all policies.
64 * @return the deployment status of all policies
65 * @throws PfModelException if a DB error occurs
67 public Collection<PolicyStatus> getStatus() throws PfModelException {
68 try (PolicyModelsProvider dao = daoFactory.create()) {
69 return accumulate(dao.getAllPolicyStatus());
74 * Gets the deployment status of a policy.
76 * @param policy policy of interest
77 * @return the deployment status of all policies
78 * @throws PfModelException if a DB error occurs
80 public Collection<PolicyStatus> getStatus(ToscaConceptIdentifierOptVersion policy) throws PfModelException {
81 try (PolicyModelsProvider dao = daoFactory.create()) {
82 return accumulate(dao.getAllPolicyStatus(policy));
87 * Gets the deployment status of a policy, returns only statuses, which matches the regex.
89 * @param patternString policy of interest
90 * @return the deployment status of all policies
91 * @throws PfModelException if a DB error occurs
93 public Collection<PolicyStatus> getByRegex(String patternString) throws PfModelException {
94 // try to make pattern out of regex
95 final var pattern = Pattern.compile(patternString);
96 // get all the statuses
97 final List<PdpPolicyStatus> policyStatuses;
98 try (PolicyModelsProvider dao = daoFactory.create()) {
99 policyStatuses = dao.getAllPolicyStatus();
101 // filter out statuses with the wrong name
102 final Collection<PdpPolicyStatus> pdpPolicyStatuses = filterWithPattern(pattern, policyStatuses);
104 return accumulate(pdpPolicyStatuses);
109 * Accumulates the deployment status of individual PDP/policy pairs into a status for
112 * @param source PDP/policy pairs
113 * @return the deployment status of the policies
115 private Collection<PolicyStatus> accumulate(Collection<PdpPolicyStatus> source) {
116 var tracker = new DeploymentTracker();
118 for (PdpPolicyStatus status : source) {
119 if (status.isDeploy()) {
124 return tracker.getDeploymentStatus();
129 * Gets the status of all policies.
131 * @return the status of all policies
132 * @throws PfModelException if a DB error occurs
134 public Collection<PdpPolicyStatus> getPolicyStatus() throws PfModelException {
135 try (PolicyModelsProvider dao = daoFactory.create()) {
136 return dao.getAllPolicyStatus();
141 * Gets the status of policies in a PdpGroup.
143 * @param pdpGroupName the pdp group
144 * @return the deployment status of policies
145 * @throws PfModelException if a DB error occurs
147 public Collection<PdpPolicyStatus> getPolicyStatus(String pdpGroupName) throws PfModelException {
148 try (PolicyModelsProvider dao = daoFactory.create()) {
149 return dao.getGroupPolicyStatus(pdpGroupName);
154 * Gets the status of a policy in a PdpGroup.
156 * @param pdpGroupName the pdp group
157 * @param policy the policy
158 * @return the deployment status of the policy
159 * @throws PfModelException if a DB error occurs
161 public Collection<PdpPolicyStatus> getPolicyStatus(String pdpGroupName, ToscaConceptIdentifierOptVersion policy)
162 throws PfModelException {
163 try (PolicyModelsProvider dao = daoFactory.create()) {
164 return dao.getAllPolicyStatus(policy).stream().filter(p -> p.getPdpGroup().equals(pdpGroupName))
165 .collect(Collectors.toList());
170 * Gets the status of policies in a PdpGroup that match the given regex.
172 * @param pdpGroupName the pdp group
173 * @param patternString regex
174 * @return the deployment status of policies
175 * @throws PfModelException if a DB error occurs
177 public Collection<PdpPolicyStatus> getPolicyStatusByRegex(String pdpGroupName, String patternString)
178 throws PfModelException {
179 final var pattern = Pattern.compile(patternString);
180 // get all the statuses
181 final Collection<PdpPolicyStatus> policyStatuses = getPolicyStatus(pdpGroupName);
182 // filter out statuses with the wrong name
183 return filterWithPattern(pattern, policyStatuses);
186 private Collection<PdpPolicyStatus> filterWithPattern(Pattern pattern, Collection<PdpPolicyStatus> policyStatuses) {
187 return policyStatuses
189 .filter(policyStatus -> {
191 final String policyName = policyStatus
194 return pattern.matcher(policyName).matches();
196 .collect(Collectors.toList());