Merge "validate and save PdpStatistisc"
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / rest / StatisticsRestProvider.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 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.Date;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import javax.ws.rs.core.Response;
30 import org.onap.policy.common.utils.services.Registry;
31 import org.onap.policy.models.base.PfModelException;
32 import org.onap.policy.models.base.PfModelRuntimeException;
33 import org.onap.policy.models.pdp.concepts.PdpStatistics;
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 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * Class to fetch statistics of pap component.
43  *
44  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
45  */
46 public class StatisticsRestProvider {
47     private static final Logger LOGGER = LoggerFactory.getLogger(StatisticsRestProvider.class);
48     private static final String GET_STATISTICS_ERR_MSG = "fetch database failed";
49     private static final String DESC_ORDER = "DESC";
50
51     /**
52      * Returns the current statistics of pap component.
53      *
54      * @return Report containing statistics of pap component
55      */
56     public StatisticsReport fetchCurrentStatistics() {
57         final StatisticsReport report = new StatisticsReport();
58         report.setCode(Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class).isAlive() ? 200 : 500);
59
60         PapStatisticsManager mgr = Registry.get(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class);
61         report.setTotalPdpCount(mgr.getTotalPdpCount());
62         report.setTotalPdpGroupCount(mgr.getTotalPdpGroupCount());
63         report.setTotalPolicyDownloadCount(mgr.getTotalPolicyDownloadCount());
64         report.setPolicyDownloadSuccessCount(mgr.getPolicyDownloadSuccessCount());
65         report.setPolicyDownloadFailureCount(mgr.getPolicyDownloadFailureCount());
66         report.setTotalPolicyDeployCount(mgr.getTotalPolicyDeployCount());
67         report.setPolicyDeploySuccessCount(mgr.getPolicyDeploySuccessCount());
68         report.setPolicyDeployFailureCount(mgr.getPolicyDeployFailureCount());
69
70         return report;
71     }
72
73     /**
74      * Returns statistics of pdp component from database.
75      *
76      * @param groupName name of the PDP group
77      * @param subType type of the sub PDP group
78      * @param pdpName the name of the PDP
79      * @param recordCount the count to query from database
80      * @return Report containing statistics of pdp component
81      * @throws PfModelException when database can not found
82      */
83     public Map<String, Map<String, List<PdpStatistics>>> fetchDatabaseStatistics(String groupName, String subType,
84             String pdpName, int recordCount) throws PfModelException {
85         final PolicyModelsProviderFactoryWrapper modelProviderWrapper =
86                 Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
87         Map<String, Map<String, List<PdpStatistics>>> pdpStatisticsMap;
88         try (PolicyModelsProvider databaseProvider = modelProviderWrapper.create()) {
89             Date startTime = null;
90             Date endTime = null;
91
92             if (groupName == null) {
93                 pdpStatisticsMap = generatePdpStatistics(databaseProvider.getPdpStatistics(pdpName, startTime));
94             } else {
95                 pdpStatisticsMap = generatePdpStatistics(databaseProvider.getFilteredPdpStatistics(pdpName, groupName,
96                         subType, startTime, endTime, DESC_ORDER, recordCount));
97             }
98         } catch (final PfModelException exp) {
99             String errorMessage = GET_STATISTICS_ERR_MSG + "groupName:" + groupName + "subType:" + subType + "pdpName:"
100                     + pdpName + exp.getMessage();
101             LOGGER.debug(errorMessage, exp);
102             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
103         }
104         return pdpStatisticsMap;
105     }
106
107     /**
108      * generate the statistics of pap component by group/subgroup.
109      *
110      */
111     private Map<String, Map<String, List<PdpStatistics>>> generatePdpStatistics(List<PdpStatistics> pdpStatisticsList) {
112         Map<String, Map<String, List<PdpStatistics>>> groupMap = new HashMap<>();
113         if (pdpStatisticsList != null) {
114             pdpStatisticsList.stream().forEach(s -> {
115                 String curGroup = s.getPdpGroupName();
116                 String curSubGroup = s.getPdpSubGroupName();
117                 groupMap.computeIfAbsent(curGroup, curGroupMap -> new HashMap<>())
118                         .computeIfAbsent(curSubGroup, curSubGroupList -> new ArrayList<>()).add(s);
119             });
120         }
121         return groupMap;
122     }
123 }
124
125