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