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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.rest;
24 import java.time.Instant;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
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.pdp.persistence.provider.PdpFilterParameters;
35 import org.onap.policy.models.provider.PolicyModelsProvider;
36 import org.onap.policy.pap.main.PapConstants;
37 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
38 import org.onap.policy.pap.main.startstop.PapActivator;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Class to fetch statistics of pap component.
45 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
47 public class StatisticsRestProvider {
48 private static final Logger LOGGER = LoggerFactory.getLogger(StatisticsRestProvider.class);
49 private static final String GET_STATISTICS_ERR_MSG = "fetch database failed";
52 * Returns the current statistics of pap component.
54 * @return Report containing statistics of pap component
56 public StatisticsReport fetchCurrentStatistics() {
57 final var report = new StatisticsReport();
58 report.setCode(Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class).isAlive() ? 200 : 500);
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());
74 * Returns statistics of pdp component from database.
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
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 Instant startTime = null;
90 Instant endTime = null;
92 if (groupName == null) {
93 pdpStatisticsMap = generatePdpStatistics(databaseProvider.getPdpStatistics(pdpName, startTime));
95 pdpStatisticsMap = generatePdpStatistics(databaseProvider.getFilteredPdpStatistics(
96 PdpFilterParameters.builder().name(pdpName).group(groupName)
97 .subGroup(subType).startTime(startTime).endTime(endTime)
98 .recordNum(recordCount).build()));
100 } catch (final PfModelException exp) {
101 String errorMessage = GET_STATISTICS_ERR_MSG + "groupName:" + groupName + "subType:" + subType + "pdpName:"
102 + pdpName + exp.getMessage();
103 LOGGER.debug(errorMessage, exp);
104 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
106 return pdpStatisticsMap;
110 * generate the statistics of pap component by group/subgroup.
113 private Map<String, Map<String, List<PdpStatistics>>> generatePdpStatistics(List<PdpStatistics> pdpStatisticsList) {
114 Map<String, Map<String, List<PdpStatistics>>> groupMap = new HashMap<>();
115 if (pdpStatisticsList != null) {
116 pdpStatisticsList.stream().forEach(s -> {
117 String curGroup = s.getPdpGroupName();
118 String curSubGroup = s.getPdpSubGroupName();
119 groupMap.computeIfAbsent(curGroup, curGroupMap -> new HashMap<>())
120 .computeIfAbsent(curSubGroup, curSubGroupList -> new ArrayList<>()).add(s);