06c7a11f8203cd7d6856253655d1d9d40322ab9f
[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.List;
25 import java.util.Map;
26 import java.util.stream.Collectors;
27 import javax.ws.rs.core.Response.Status;
28 import lombok.AllArgsConstructor;
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.clamp.controlloop.models.controlloop.persistence.repository.ParticipantStatisticsRepository;
33 import org.onap.policy.models.base.PfModelException;
34 import org.onap.policy.models.base.PfTimestampKey;
35 import org.onap.policy.models.dao.PfFilterParameters;
36 import org.springframework.stereotype.Component;
37 import org.springframework.transaction.annotation.Transactional;
38
39 /**
40  * This class provides the provision of information on participant statistics in the database to callers.
41  */
42 @Component
43 @Transactional
44 @AllArgsConstructor
45 public class ParticipantStatisticsProvider {
46
47     private ParticipantStatisticsRepository participantStatisticsRepository;
48
49     /**
50      * Get Participant statistics.
51      *
52      * @param name the name of the participant statistics to get, null to get all stats
53      * @param version the version of the participant statistics to get, null to get all stats for a name
54      * @param timestamp the time stamp for the stats to get
55      * @return the participant statistics found
56      */
57     @Transactional(readOnly = true)
58     public List<ParticipantStatistics> getParticipantStatistics(final String name, final String version,
59             final Instant timestamp) {
60
61         if (name != null && version != null && timestamp != null) {
62             return asParticipantStatisticsList(
63                     participantStatisticsRepository.findAllById(List.of(new PfTimestampKey(name, version, timestamp))));
64         } else if (name != null) {
65             return getFilteredParticipantStatistics(name, version, timestamp, null, null, "DESC", 0);
66         } else {
67             return asParticipantStatisticsList(participantStatisticsRepository.findAll());
68         }
69     }
70
71     /**
72      * Get filtered participant statistics.
73      *
74      * @param name the participant name for the statistics to get
75      * @param version the participant version for the statistics to get
76      * @param startTimeStamp startTimeStamp to filter statistics
77      * @param endTimeStamp endTimeStamp to filter statistics
78      * @param sortOrder sortOrder to query database
79      * @param getRecordNum Total query count from database
80      * @param filterMap the filters to apply to the get operation
81      * @return the participant statistics found
82      */
83     @Transactional(readOnly = true)
84     public List<ParticipantStatistics> getFilteredParticipantStatistics(final String name, final String version,
85             final Instant startTimeStamp, final Instant endTimeStamp, Map<String, Object> filterMap,
86             final String sortOrder, final int getRecordNum) {
87
88         // @formatter:off
89         PfFilterParameters filterParams = PfFilterParameters
90                 .builder()
91                 .name(name)
92                 .version(version)
93                 .startTime(startTimeStamp)
94                 .endTime(endTimeStamp)
95                 .filterMap(filterMap)
96                 .sortOrder(sortOrder)
97                 .recordNum(getRecordNum)
98                 .build();
99         // @formatter:on
100
101         return asParticipantStatisticsList(
102                 participantStatisticsRepository.getFiltered(JpaParticipantStatistics.class, filterParams));
103     }
104
105     /**
106      * Creates Participant statistics.
107      *
108      * @param participantStatisticsList a specification of the CL statistics to create
109      * @return the participant statistics created
110      * @throws PfModelException on errors creating participant statistics
111      */
112     public List<ParticipantStatistics> createParticipantStatistics(
113             @NonNull final List<ParticipantStatistics> participantStatisticsList) throws PfModelException {
114
115         try {
116             var jpaParticipantStatisticsList = ProviderUtils.getJpaAndValidate(participantStatisticsList,
117                     JpaParticipantStatistics::new, "Participant Statistics");
118
119             var jpaParticipantStatisticsSaved = participantStatisticsRepository.saveAll(jpaParticipantStatisticsList);
120
121             // Return the saved participant statistics
122             return asParticipantStatisticsList(jpaParticipantStatisticsSaved);
123         } catch (IllegalArgumentException e) {
124             throw new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error in save participant statistics", e);
125         }
126     }
127
128     /**
129      * Convert JPA participant statistics list to participant statistics list.
130      *
131      * @param jpaParticipantStatisticsList the list to convert
132      * @return the participant statistics list
133      */
134     private List<ParticipantStatistics> asParticipantStatisticsList(
135             List<JpaParticipantStatistics> jpaParticipantStatisticsList) {
136
137         return jpaParticipantStatisticsList.stream().map(JpaParticipantStatistics::toAuthorative)
138                 .collect(Collectors.toList());
139     }
140 }