Move PAP database provider to spring boot default
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / PolicyStatusProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.policy.pap.main.rest;
24
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;
36
37 /**
38  * Provider for PAP component to query policy deployment status.
39  */
40 @Service
41 @RequiredArgsConstructor
42 public class PolicyStatusProvider {
43
44     private final PolicyStatusService policyStatusService;
45
46     /**
47      * Gets the deployment status of all policies.
48      *
49      * @return the deployment status of all policies
50      */
51     public Collection<PolicyStatus> getStatus() {
52         return accumulate(policyStatusService.getAllPolicyStatus());
53     }
54
55     /**
56      * Gets the deployment status of a policy.
57      *
58      * @param policy policy of interest
59      * @return the deployment status of all policies
60      */
61     public Collection<PolicyStatus> getStatus(ToscaConceptIdentifierOptVersion policy) {
62         return accumulate(policyStatusService.getAllPolicyStatus(policy));
63     }
64
65     /**
66      * Gets the deployment status of a policy, returns only statuses, which matches the regex.
67      *
68      * @param patternString policy of interest
69      * @return the deployment status of all policies
70      */
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);
78
79         return accumulate(pdpPolicyStatuses);
80     }
81
82
83     /**
84      * Accumulates the deployment status of individual PDP/policy pairs into a status for
85      * a policy.
86      *
87      * @param source PDP/policy pairs
88      * @return the deployment status of the policies
89      */
90     private Collection<PolicyStatus> accumulate(Collection<PdpPolicyStatus> source) {
91         var tracker = new DeploymentTracker();
92
93         for (PdpPolicyStatus status : source) {
94             if (status.isDeploy()) {
95                 tracker.add(status);
96             }
97         }
98
99         return tracker.getDeploymentStatus();
100     }
101
102
103     /**
104      * Gets the status of all policies.
105      *
106      * @return the status of all policies
107      */
108     public Collection<PdpPolicyStatus> getPolicyStatus() {
109         return policyStatusService.getAllPolicyStatus();
110     }
111
112     /**
113      * Gets the status of policies in a PdpGroup.
114      *
115      * @param pdpGroupName the pdp group
116      */
117     public Collection<PdpPolicyStatus> getPolicyStatus(String pdpGroupName) {
118         return policyStatusService.getGroupPolicyStatus(pdpGroupName);
119     }
120
121     /**
122      * Gets the status of a policy in a PdpGroup.
123      *
124      * @param pdpGroupName the pdp group
125      * @param policy the policy
126      * @return the deployment status of the policy
127      */
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());
131     }
132
133     /**
134      * Gets the status of policies in a PdpGroup that match the given regex.
135      *
136      * @param pdpGroupName  the pdp group
137      * @param patternString regex
138      * @return the deployment status of policies
139      */
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);
146     }
147
148     private Collection<PdpPolicyStatus> filterWithPattern(Pattern pattern, Collection<PdpPolicyStatus> policyStatuses) {
149         return policyStatuses
150             .stream()
151             .filter(policyStatus -> {
152                 // Check policy name
153                 final String policyName = policyStatus
154                     .getPolicy()
155                     .getName();
156                 return pattern.matcher(policyName).matches();
157             })
158             .collect(Collectors.toList());
159     }
160 }