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