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.util.ArrayList;
24 import java.util.Date;
25 import java.util.List;
27 import java.util.stream.Collectors;
28 import javax.ws.rs.core.Response;
29 import lombok.NonNull;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics;
31 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaParticipantStatistics;
32 import org.onap.policy.common.parameters.BeanValidationResult;
33 import org.onap.policy.models.base.PfModelException;
34 import org.onap.policy.models.base.PfModelRuntimeException;
35 import org.onap.policy.models.base.PfTimestampKey;
36 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
37 import org.onap.policy.models.provider.impl.AbstractModelsProvider;
40 * This class provides the provision of information on participant statistics in the database to callers.
42 * @author Ramesh Murugan Iyer (ramesh.murugan.iyer@est.tech)
44 public class ParticipantStatisticsProvider extends AbstractModelsProvider {
47 * Create a provider for control loops statistics.
49 * @param parameters the parameters for database access
50 * @throws PfModelException on initiation errors
52 public ParticipantStatisticsProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException {
58 * Get Participant statistics.
60 * @param name the name of the participant statistics to get, null to get all stats
61 * @return the participant statistics found
62 * @throws PfModelException on errors getting participant statistics
64 public List<ParticipantStatistics> getParticipantStatistics(final String name, final String version,
65 final Date timestamp) throws PfModelException {
67 if (name != null && version != null && timestamp != null) {
68 List<ParticipantStatistics> participantStatistics = new ArrayList<>(1);
69 participantStatistics.add(getPfDao()
70 .get(JpaParticipantStatistics.class, new PfTimestampKey(name, version, timestamp)).toAuthorative());
71 return participantStatistics;
73 return asParticipantStatisticsList(getPfDao().getAll(JpaParticipantStatistics.class));
79 * Get filtered participant statistics.
81 * @param name the participant name 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 * @return the participant statistics found
87 * @throws PfModelException on errors getting policies
89 public List<ParticipantStatistics> getFilteredParticipantStatistics(final String name, final String version,
90 final Date startTimeStamp, final Date endTimeStamp, Map<String, Object> filterMap, final String sortOrder,
91 final int getRecordNum) {
93 return asParticipantStatisticsList(getPfDao().getFiltered(JpaParticipantStatistics.class, name, version,
94 startTimeStamp, endTimeStamp, filterMap, sortOrder, getRecordNum));
99 * Creates Participant statistics.
101 * @param participantStatisticsList a specification of the CL statistics to create
102 * @return the participant statistics created
103 * @throws PfModelException on errors creating participant statistics
105 public List<ParticipantStatistics> createParticipantStatistics(
106 @NonNull final List<ParticipantStatistics> participantStatisticsList) throws PfModelException {
108 BeanValidationResult validationResult =
109 new BeanValidationResult("participant statistics List", participantStatisticsList);
111 for (ParticipantStatistics participantStatistics : participantStatisticsList) {
112 JpaParticipantStatistics jpaParticipantStatistics = new JpaParticipantStatistics();
113 jpaParticipantStatistics.fromAuthorative(participantStatistics);
115 validationResult.addResult(jpaParticipantStatistics.validate("participant statistics"));
118 if (!validationResult.isValid()) {
119 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
122 for (ParticipantStatistics participantStatistics : participantStatisticsList) {
123 JpaParticipantStatistics jpaParticipantStatistics = new JpaParticipantStatistics();
124 jpaParticipantStatistics.fromAuthorative(participantStatistics);
126 getPfDao().create(jpaParticipantStatistics);
129 // Return the created participant statistics
130 List<ParticipantStatistics> participantStatistics = new ArrayList<>(participantStatisticsList.size());
132 for (ParticipantStatistics participantStatisticsItem : participantStatisticsList) {
133 JpaParticipantStatistics jpaParticipantStatistics = getPfDao().get(JpaParticipantStatistics.class,
134 new PfTimestampKey(participantStatisticsItem.getParticipantId().getName(),
135 participantStatisticsItem.getParticipantId().getVersion(),
136 participantStatisticsItem.getTimeStamp()));
137 participantStatistics.add(jpaParticipantStatistics.toAuthorative());
140 return participantStatistics;
145 * Convert JPA participant statistics list to participant statistics list.
147 * @param jpaParticipantStatisticsList the list to convert
148 * @return the participant statistics list
150 private List<ParticipantStatistics> asParticipantStatisticsList(
151 List<JpaParticipantStatistics> jpaParticipantStatisticsList) {
153 return jpaParticipantStatisticsList.stream().map(JpaParticipantStatistics::toAuthorative)
154 .collect(Collectors.toList());