2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2021 Nordix Foundation.
4 * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property.
5 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pap.main.rest;
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.springframework.stereotype.Service;
42 * Class to fetch statistics of pap component.
44 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
47 public class StatisticsRestProvider {
48 private static final String GET_STATISTICS_ERR_MSG = "database query failed";
51 * Returns the current statistics of pap component.
53 * @return Report containing statistics of pap component
55 public StatisticsReport fetchCurrentStatistics() {
56 final var report = new StatisticsReport();
57 report.setCode(Registry.get(PapConstants.REG_PAP_ACTIVATOR, PapActivator.class).isAlive() ? 200 : 500);
59 PapStatisticsManager mgr = Registry.get(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class);
60 report.setTotalPdpCount(mgr.getTotalPdpCount());
61 report.setTotalPdpGroupCount(mgr.getTotalPdpGroupCount());
62 report.setTotalPolicyDownloadCount(mgr.getTotalPolicyDownloadCount());
63 report.setPolicyDownloadSuccessCount(mgr.getPolicyDownloadSuccessCount());
64 report.setPolicyDownloadFailureCount(mgr.getPolicyDownloadFailureCount());
65 report.setTotalPolicyDeployCount(mgr.getTotalPolicyDeployCount());
66 report.setPolicyDeploySuccessCount(mgr.getPolicyDeploySuccessCount());
67 report.setPolicyDeployFailureCount(mgr.getPolicyDeployFailureCount());
73 * Returns statistics of pdp component from database.
75 * @param filter record filter
76 * @return Report containing statistics of pdp component
77 * @throws PfModelException when database can not found
79 public Map<String, Map<String, List<PdpStatistics>>> fetchDatabaseStatistics(PdpFilterParameters filter)
80 throws PfModelException {
81 final PolicyModelsProviderFactoryWrapper modelProviderWrapper =
82 Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
83 try (PolicyModelsProvider databaseProvider = modelProviderWrapper.create()) {
84 return generatePdpStatistics(databaseProvider.getFilteredPdpStatistics(filter));
86 } catch (final PfModelException exp) {
87 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, GET_STATISTICS_ERR_MSG);
92 * generate the statistics of pap component by group/subgroup.
95 private Map<String, Map<String, List<PdpStatistics>>> generatePdpStatistics(List<PdpStatistics> pdpStatisticsList) {
96 Map<String, Map<String, List<PdpStatistics>>> groupMap = new HashMap<>();
97 if (pdpStatisticsList != null) {
98 pdpStatisticsList.stream().forEach(s -> {
99 String curGroup = s.getPdpGroupName();
100 String curSubGroup = s.getPdpSubGroupName();
101 groupMap.computeIfAbsent(curGroup, curGroupMap -> new HashMap<>())
102 .computeIfAbsent(curSubGroup, curSubGroupList -> new ArrayList<>()).add(s);