137af3a5e63330c8b1c49b1d2d4c6e8ba0a61309
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider;
22
23 import java.time.Instant;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map;
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;
36
37 /**
38  * This class provides the provision of information on participant statistics in the database to callers.
39  */
40 public class ParticipantStatisticsProvider extends AbstractModelsProvider {
41
42     /**
43      * Create a provider for control loops statistics.
44      *
45      * @param parameters the parameters for database access
46      * @throws PfModelException on initiation errors
47      */
48     public ParticipantStatisticsProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException {
49         super(parameters);
50         this.init();
51     }
52
53     /**
54      * Get Participant statistics.
55      *
56      * @param name the name of the participant statistics to get, null to get all stats
57      * @return the participant statistics found
58      * @throws PfModelException on errors getting participant statistics
59      */
60     public List<ParticipantStatistics> getParticipantStatistics(final String name, final String version,
61             final Instant timestamp) throws PfModelException {
62
63         if (name != null && version != null && timestamp != null) {
64             List<ParticipantStatistics> participantStatistics = new ArrayList<>(1);
65             participantStatistics.add(getPfDao()
66                     .get(JpaParticipantStatistics.class, new PfTimestampKey(name, version, timestamp)).toAuthorative());
67             return participantStatistics;
68         } else if (name != null) {
69             return getFilteredParticipantStatistics(name, version, timestamp, null, null, "DESC", 0);
70         } else {
71             return asParticipantStatisticsList(getPfDao().getAll(JpaParticipantStatistics.class));
72         }
73     }
74
75
76     /**
77      * Get filtered participant statistics.
78      *
79      * @param name the participant name for the statistics to get
80      * @param startTimeStamp startTimeStamp to filter statistics
81      * @param endTimeStamp endTimeStamp to filter statistics
82      * @param sortOrder sortOrder to query database
83      * @param getRecordNum Total query count from database
84      * @return the participant statistics found
85      * @throws PfModelException on errors getting policies
86      */
87     public List<ParticipantStatistics> getFilteredParticipantStatistics(final String name, final String version,
88             final Instant startTimeStamp, final Instant endTimeStamp, Map<String, Object> filterMap,
89             final String sortOrder, final int getRecordNum) {
90
91         // @formatter:off
92         PfFilterParameters filterParams = PfFilterParameters
93                 .builder()
94                 .name(name)
95                 .version(version)
96                 .startTime(startTimeStamp)
97                 .endTime(endTimeStamp)
98                 .filterMap(filterMap)
99                 .sortOrder(sortOrder)
100                 .recordNum(getRecordNum)
101                 .build();
102         // @formatter:on
103
104         return asParticipantStatisticsList(getPfDao().getFiltered(JpaParticipantStatistics.class, filterParams));
105     }
106
107
108     /**
109      * Creates Participant statistics.
110      *
111      * @param participantStatisticsList a specification of the CL statistics to create
112      * @return the participant statistics created
113      * @throws PfModelException on errors creating participant statistics
114      */
115     public List<ParticipantStatistics> createParticipantStatistics(
116             @NonNull final List<ParticipantStatistics> participantStatisticsList) throws PfModelException {
117
118         List<JpaParticipantStatistics> jpaParticipantStatisticsList = ProviderUtils
119                 .getJpaAndValidate(participantStatisticsList, JpaParticipantStatistics::new, "Participant Statistics");
120
121         jpaParticipantStatisticsList.forEach(jpaParticipantStatistics -> getPfDao().update(jpaParticipantStatistics));
122
123         // Return the created participant statistics
124         List<ParticipantStatistics> participantStatistics = new ArrayList<>(participantStatisticsList.size());
125
126         for (ParticipantStatistics participantStatisticsItem : participantStatisticsList) {
127             var jpaParticipantStatistics = getPfDao().get(JpaParticipantStatistics.class,
128                     new PfTimestampKey(participantStatisticsItem.getParticipantId().getName(),
129                             participantStatisticsItem.getParticipantId().getVersion(),
130                             participantStatisticsItem.getTimeStamp()));
131             participantStatistics.add(jpaParticipantStatistics.toAuthorative());
132         }
133
134         return participantStatistics;
135     }
136
137
138
139     /**
140      * Convert JPA participant statistics list to participant statistics list.
141      *
142      * @param jpaParticipantStatisticsList the list to convert
143      * @return the participant statistics list
144      */
145     private List<ParticipantStatistics> asParticipantStatisticsList(
146             List<JpaParticipantStatistics> jpaParticipantStatisticsList) {
147
148         return jpaParticipantStatisticsList.stream().map(JpaParticipantStatistics::toAuthorative)
149                 .collect(Collectors.toList());
150     }
151 }