Merge "Add datetime format to audit api's"
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / StatisticsRestProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020-2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.rest;
23
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import javax.ws.rs.core.Response;
29 import org.onap.policy.common.utils.services.Registry;
30 import org.onap.policy.models.base.PfModelException;
31 import org.onap.policy.models.base.PfModelRuntimeException;
32 import org.onap.policy.models.pdp.concepts.PdpStatistics;
33 import org.onap.policy.models.pdp.persistence.provider.PdpFilterParameters;
34 import org.onap.policy.models.provider.PolicyModelsProvider;
35 import org.onap.policy.pap.main.PapConstants;
36 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
37 import org.onap.policy.pap.main.startstop.PapActivator;
38
39 /**
40  * Class to fetch statistics of pap component.
41  *
42  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
43  */
44 public class StatisticsRestProvider {
45     private static final String GET_STATISTICS_ERR_MSG = "database query failed";
46
47     /**
48      * Returns the current statistics of pap component.
49      *
50      * @return Report containing statistics of pap component
51      */
52     public StatisticsReport fetchCurrentStatistics() {
53         final var report = new StatisticsReport();
54         report.setCode(Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class).isAlive() ? 200 : 500);
55
56         PapStatisticsManager mgr = Registry.get(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class);
57         report.setTotalPdpCount(mgr.getTotalPdpCount());
58         report.setTotalPdpGroupCount(mgr.getTotalPdpGroupCount());
59         report.setTotalPolicyDownloadCount(mgr.getTotalPolicyDownloadCount());
60         report.setPolicyDownloadSuccessCount(mgr.getPolicyDownloadSuccessCount());
61         report.setPolicyDownloadFailureCount(mgr.getPolicyDownloadFailureCount());
62         report.setTotalPolicyDeployCount(mgr.getTotalPolicyDeployCount());
63         report.setPolicyDeploySuccessCount(mgr.getPolicyDeploySuccessCount());
64         report.setPolicyDeployFailureCount(mgr.getPolicyDeployFailureCount());
65
66         return report;
67     }
68
69     /**
70      * Returns statistics of pdp component from database.
71      *
72      * @param filter record filter
73      * @return Report containing statistics of pdp component
74      * @throws PfModelException when database can not found
75      */
76     public Map<String, Map<String, List<PdpStatistics>>> fetchDatabaseStatistics(PdpFilterParameters filter)
77                     throws PfModelException {
78         final PolicyModelsProviderFactoryWrapper modelProviderWrapper =
79                         Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
80         try (PolicyModelsProvider databaseProvider = modelProviderWrapper.create()) {
81             return generatePdpStatistics(databaseProvider.getFilteredPdpStatistics(filter));
82
83         } catch (final PfModelException exp) {
84             throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, GET_STATISTICS_ERR_MSG);
85         }
86     }
87
88     /**
89      * generate the statistics of pap component by group/subgroup.
90      *
91      */
92     private Map<String, Map<String, List<PdpStatistics>>> generatePdpStatistics(List<PdpStatistics> pdpStatisticsList) {
93         Map<String, Map<String, List<PdpStatistics>>> groupMap = new HashMap<>();
94         if (pdpStatisticsList != null) {
95             pdpStatisticsList.stream().forEach(s -> {
96                 String curGroup = s.getPdpGroupName();
97                 String curSubGroup = s.getPdpSubGroupName();
98                 groupMap.computeIfAbsent(curGroup, curGroupMap -> new HashMap<>())
99                         .computeIfAbsent(curSubGroup, curSubGroupList -> new ArrayList<>()).add(s);
100             });
101         }
102         return groupMap;
103     }
104 }