Migrate pap startup & controllers to spring boot
[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 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 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;
41
42 /**
43  * Provider for PAP component to query policy deployment status.
44  */
45 @Service
46 public class PolicyStatusProvider {
47
48     /**
49      * Factory for PAP DAO.
50      */
51     private PolicyModelsProviderFactoryWrapper daoFactory;
52
53     /**
54      * Constructs the object. Loads all deployed policies into the internal cache.
55      */
56     @EventListener(ApplicationReadyEvent.class)
57     public void initialize() {
58         this.daoFactory = Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
59     }
60
61     /**
62      * Gets the deployment status of all policies.
63      *
64      * @return the deployment status of all policies
65      * @throws PfModelException if a DB error occurs
66      */
67     public Collection<PolicyStatus> getStatus() throws PfModelException {
68         try (PolicyModelsProvider dao = daoFactory.create()) {
69             return accumulate(dao.getAllPolicyStatus());
70         }
71     }
72
73     /**
74      * Gets the deployment status of a policy.
75      *
76      * @param policy policy of interest
77      * @return the deployment status of all policies
78      * @throws PfModelException if a DB error occurs
79      */
80     public Collection<PolicyStatus> getStatus(ToscaConceptIdentifierOptVersion policy) throws PfModelException {
81         try (PolicyModelsProvider dao = daoFactory.create()) {
82             return accumulate(dao.getAllPolicyStatus(policy));
83         }
84     }
85
86     /**
87      * Gets the deployment status of a policy, returns only statuses, which matches the regex.
88      *
89      * @param patternString policy of interest
90      * @return the deployment status of all policies
91      * @throws PfModelException if a DB error occurs
92      */
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();
100         }
101         // filter out statuses with the wrong name
102         final Collection<PdpPolicyStatus> pdpPolicyStatuses = filterWithPattern(pattern, policyStatuses);
103
104         return accumulate(pdpPolicyStatuses);
105     }
106
107
108     /**
109      * Accumulates the deployment status of individual PDP/policy pairs into a status for
110      * a policy.
111      *
112      * @param source PDP/policy pairs
113      * @return the deployment status of the policies
114      */
115     private Collection<PolicyStatus> accumulate(Collection<PdpPolicyStatus> source) {
116         var tracker = new DeploymentTracker();
117
118         for (PdpPolicyStatus status : source) {
119             if (status.isDeploy()) {
120                 tracker.add(status);
121             }
122         }
123
124         return tracker.getDeploymentStatus();
125     }
126
127
128     /**
129      * Gets the status of all policies.
130      *
131      * @return the status of all policies
132      * @throws PfModelException if a DB error occurs
133      */
134     public Collection<PdpPolicyStatus> getPolicyStatus() throws PfModelException {
135         try (PolicyModelsProvider dao = daoFactory.create()) {
136             return dao.getAllPolicyStatus();
137         }
138     }
139
140     /**
141      * Gets the status of policies in a PdpGroup.
142      *
143      * @param pdpGroupName the pdp group
144      * @return the deployment status of policies
145      * @throws PfModelException if a DB error occurs
146      */
147     public Collection<PdpPolicyStatus> getPolicyStatus(String pdpGroupName) throws PfModelException {
148         try (PolicyModelsProvider dao = daoFactory.create()) {
149             return dao.getGroupPolicyStatus(pdpGroupName);
150         }
151     }
152
153     /**
154      * Gets the status of a policy in a PdpGroup.
155      *
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
160      */
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());
166         }
167     }
168
169     /**
170      * Gets the status of policies in a PdpGroup that match the given regex.
171      *
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
176      */
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);
184     }
185
186     private Collection<PdpPolicyStatus> filterWithPattern(Pattern pattern, Collection<PdpPolicyStatus> policyStatuses) {
187         return policyStatuses
188             .stream()
189             .filter(policyStatus -> {
190                 // Check policy name
191                 final String policyName = policyStatus
192                     .getPolicy()
193                     .getName();
194                 return pattern.matcher(policyName).matches();
195             })
196             .collect(Collectors.toList());
197     }
198 }