2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider;
23 import java.time.Instant;
24 import java.util.ArrayList;
25 import java.util.List;
27 import java.util.stream.Collectors;
28 import lombok.NonNull;
29 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics;
30 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaParticipantStatistics;
31 import org.onap.policy.models.base.PfModelException;
32 import org.onap.policy.models.base.PfTimestampKey;
33 import org.onap.policy.models.dao.PfFilterParameters;
34 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
35 import org.onap.policy.models.provider.impl.AbstractModelsProvider;
38 * This class provides the provision of information on participant statistics in the database to callers.
40 public class ParticipantStatisticsProvider extends AbstractModelsProvider {
43 * Create a provider for control loops statistics.
45 * @param parameters the parameters for database access
46 * @throws PfModelException on initiation errors
48 public ParticipantStatisticsProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException {
54 * Get Participant statistics.
56 * @param name the name of the participant statistics to get, null to get all stats
57 * @param version the version of the participant statistics to get, null to get all stats for a name
58 * @param timestamp the time stamp for the stats to get
59 * @return the participant statistics found
60 * @throws PfModelException on errors getting participant statistics
62 public List<ParticipantStatistics> getParticipantStatistics(final String name, final String version,
63 final Instant timestamp) throws PfModelException {
65 if (name != null && version != null && timestamp != null) {
66 List<ParticipantStatistics> participantStatistics = new ArrayList<>(1);
67 participantStatistics.add(getPfDao()
68 .get(JpaParticipantStatistics.class, new PfTimestampKey(name, version, timestamp)).toAuthorative());
69 return participantStatistics;
70 } else if (name != null) {
71 return getFilteredParticipantStatistics(name, version, timestamp, null, null, "DESC", 0);
73 return asParticipantStatisticsList(getPfDao().getAll(JpaParticipantStatistics.class));
78 * Get filtered participant statistics.
80 * @param name the participant name for the statistics to get
81 * @param version the participant version for the statistics to get
82 * @param startTimeStamp startTimeStamp to filter statistics
83 * @param endTimeStamp endTimeStamp to filter statistics
84 * @param sortOrder sortOrder to query database
85 * @param getRecordNum Total query count from database
86 * @param filterMap the filters to apply to the get operation
87 * @return the participant statistics found
88 * @throws PfModelException on errors getting policies
90 public List<ParticipantStatistics> getFilteredParticipantStatistics(final String name, final String version,
91 final Instant startTimeStamp, final Instant endTimeStamp, Map<String, Object> filterMap,
92 final String sortOrder, final int getRecordNum) {
95 PfFilterParameters filterParams = PfFilterParameters
99 .startTime(startTimeStamp)
100 .endTime(endTimeStamp)
101 .filterMap(filterMap)
102 .sortOrder(sortOrder)
103 .recordNum(getRecordNum)
107 return asParticipantStatisticsList(getPfDao().getFiltered(JpaParticipantStatistics.class, filterParams));
111 * Creates Participant statistics.
113 * @param participantStatisticsList a specification of the CL statistics to create
114 * @return the participant statistics created
115 * @throws PfModelException on errors creating participant statistics
117 public List<ParticipantStatistics> createParticipantStatistics(
118 @NonNull final List<ParticipantStatistics> participantStatisticsList) throws PfModelException {
120 List<JpaParticipantStatistics> jpaParticipantStatisticsList = ProviderUtils
121 .getJpaAndValidate(participantStatisticsList, JpaParticipantStatistics::new, "Participant Statistics");
123 jpaParticipantStatisticsList.forEach(jpaParticipantStatistics -> getPfDao().update(jpaParticipantStatistics));
125 // Return the created participant statistics
126 List<ParticipantStatistics> participantStatistics = new ArrayList<>(participantStatisticsList.size());
128 for (ParticipantStatistics participantStatisticsItem : participantStatisticsList) {
129 var jpaParticipantStatistics = getPfDao().get(JpaParticipantStatistics.class,
130 new PfTimestampKey(participantStatisticsItem.getParticipantId().getName(),
131 participantStatisticsItem.getParticipantId().getVersion(),
132 participantStatisticsItem.getTimeStamp()));
133 participantStatistics.add(jpaParticipantStatistics.toAuthorative());
136 return participantStatistics;
140 * Convert JPA participant statistics list to participant statistics list.
142 * @param jpaParticipantStatisticsList the list to convert
143 * @return the participant statistics list
145 private List<ParticipantStatistics> asParticipantStatisticsList(
146 List<JpaParticipantStatistics> jpaParticipantStatisticsList) {
148 return jpaParticipantStatisticsList.stream().map(JpaParticipantStatistics::toAuthorative)
149 .collect(Collectors.toList());