2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021-2022 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 lombok.RequiredArgsConstructor;
30 import org.onap.policy.models.pap.concepts.PolicyStatus;
31 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
33 import org.onap.policy.pap.main.notification.DeploymentTracker;
34 import org.onap.policy.pap.main.service.PolicyStatusService;
35 import org.springframework.stereotype.Service;
38 * Provider for PAP component to query policy deployment status.
41 @RequiredArgsConstructor
42 public class PolicyStatusProvider {
44 private final PolicyStatusService policyStatusService;
47 * Gets the deployment status of all policies.
49 * @return the deployment status of all policies
51 public Collection<PolicyStatus> getStatus() {
52 return accumulate(policyStatusService.getAllPolicyStatus());
56 * Gets the deployment status of a policy.
58 * @param policy policy of interest
59 * @return the deployment status of all policies
61 public Collection<PolicyStatus> getStatus(ToscaConceptIdentifierOptVersion policy) {
62 return accumulate(policyStatusService.getAllPolicyStatus(policy));
66 * Gets the deployment status of a policy, returns only statuses, which matches the regex.
68 * @param patternString policy of interest
69 * @return the deployment status of all policies
71 public Collection<PolicyStatus> getByRegex(String patternString) {
72 // try to make pattern out of regex
73 final var pattern = Pattern.compile(patternString);
74 // get all the statuses
75 final List<PdpPolicyStatus> policyStatuses = policyStatusService.getAllPolicyStatus();
76 // filter out statuses with the wrong name
77 final Collection<PdpPolicyStatus> pdpPolicyStatuses = filterWithPattern(pattern, policyStatuses);
79 return accumulate(pdpPolicyStatuses);
84 * Accumulates the deployment status of individual PDP/policy pairs into a status for
87 * @param source PDP/policy pairs
88 * @return the deployment status of the policies
90 private Collection<PolicyStatus> accumulate(Collection<PdpPolicyStatus> source) {
91 var tracker = new DeploymentTracker();
93 for (PdpPolicyStatus status : source) {
94 if (status.isDeploy()) {
99 return tracker.getDeploymentStatus();
104 * Gets the status of all policies.
106 * @return the status of all policies
108 public Collection<PdpPolicyStatus> getPolicyStatus() {
109 return policyStatusService.getAllPolicyStatus();
113 * Gets the status of policies in a PdpGroup.
115 * @param pdpGroupName the pdp group
117 public Collection<PdpPolicyStatus> getPolicyStatus(String pdpGroupName) {
118 return policyStatusService.getGroupPolicyStatus(pdpGroupName);
122 * Gets the status of a policy in a PdpGroup.
124 * @param pdpGroupName the pdp group
125 * @param policy the policy
126 * @return the deployment status of the policy
128 public Collection<PdpPolicyStatus> getPolicyStatus(String pdpGroupName, ToscaConceptIdentifierOptVersion policy) {
129 return policyStatusService.getAllPolicyStatus(policy).stream().filter(p -> p.getPdpGroup().equals(pdpGroupName))
130 .collect(Collectors.toList());
134 * Gets the status of policies in a PdpGroup that match the given regex.
136 * @param pdpGroupName the pdp group
137 * @param patternString regex
138 * @return the deployment status of policies
140 public Collection<PdpPolicyStatus> getPolicyStatusByRegex(String pdpGroupName, String patternString) {
141 final var pattern = Pattern.compile(patternString);
142 // get all the statuses
143 final Collection<PdpPolicyStatus> policyStatuses = getPolicyStatus(pdpGroupName);
144 // filter out statuses with the wrong name
145 return filterWithPattern(pattern, policyStatuses);
148 private Collection<PdpPolicyStatus> filterWithPattern(Pattern pattern, Collection<PdpPolicyStatus> policyStatuses) {
149 return policyStatuses
151 .filter(policyStatus -> {
153 final String policyName = policyStatus
156 return pattern.matcher(policyName).matches();
158 .collect(Collectors.toList());