3a7fa9c874de2879e98340b52f21461d173669c1
[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.ClElementStatistics;
31 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaClElementStatistics;
32 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ClElementStatisticsRepository;
33 import org.onap.policy.models.base.PfModelException;
34 import org.onap.policy.models.base.PfReferenceTimestampKey;
35 import org.onap.policy.models.dao.PfFilterParameters;
36 import org.springframework.stereotype.Service;
37 import org.springframework.transaction.annotation.Transactional;
38
39 /**
40  * This class provides the provision of information on control loop element statistics in the database to callers.
41  *
42  * @author Ramesh Murugan Iyer (ramesh.murugan.iyer@est.tech)
43  */
44 @Service
45 @Transactional
46 @AllArgsConstructor
47 public class ClElementStatisticsProvider {
48
49     private ClElementStatisticsRepository clElementStatisticsRepository;
50
51     /**
52      * Creates control loop element statistics.
53      *
54      * @param clElementStatisticsList a specification of the CL element statistics to create
55      * @return the clElement statistics created
56      * @throws PfModelException on initiation errors
57      */
58     public List<ClElementStatistics> createClElementStatistics(
59             @NonNull final List<ClElementStatistics> clElementStatisticsList) throws PfModelException {
60
61         try {
62             var jpaClElementStatisticsList = ProviderUtils.getJpaAndValidateList(clElementStatisticsList,
63                     JpaClElementStatistics::new, "control loop element statistics");
64
65             var jpaClElementStatisticsSaved = clElementStatisticsRepository.saveAll(jpaClElementStatisticsList);
66
67             // Return the saved control loop element statistics
68             return asClElementStatisticsList(jpaClElementStatisticsSaved);
69         } catch (IllegalArgumentException e) {
70             throw new PfModelException(Status.BAD_REQUEST, "Error in save control loop element statistics", e);
71         }
72     }
73
74     /**
75      * Convert JPA clElement statistics list to clElement statistics list.
76      *
77      * @param jpaClElementStatistics the list to convert
78      * @return the clElement statistics list
79      */
80     private List<ClElementStatistics> asClElementStatisticsList(List<JpaClElementStatistics> jpaClElementStatistics) {
81         return jpaClElementStatistics.stream().map(JpaClElementStatistics::toAuthorative).collect(Collectors.toList());
82     }
83
84     /**
85      * Get clElement statistics.
86      *
87      * @param name the name of the participant
88      * @param version version of the participant
89      * @param id of the control loop element
90      * @param timestamp timestamp of the statistics
91      * @return the clElement statistics found
92      */
93     @Transactional(readOnly = true)
94     public List<ClElementStatistics> getClElementStatistics(final String name, final String version, final String id,
95             final Instant timestamp) {
96         if (name != null && version != null && timestamp != null && id != null) {
97             return asClElementStatisticsList(clElementStatisticsRepository
98                     .findAllById(List.of(new PfReferenceTimestampKey(name, version, id, timestamp))));
99         } else if (name != null) {
100             return getFilteredClElementStatistics(name, version, null, null, null, "DESC", 0);
101         }
102         return asClElementStatisticsList(clElementStatisticsRepository.findAll());
103     }
104
105     /**
106      * Get filtered clElement statistics.
107      *
108      * @param name the clElement name for the statistics to get
109      * @param version the clElement version for the statistics to get
110      * @param startTimeStamp startTimeStamp to filter statistics
111      * @param endTimeStamp endTimeStamp to filter statistics
112      * @param sortOrder sortOrder to query database
113      * @param getRecordNum Total query count from database
114      * @param filterMap the filters to apply to the get operation
115      * @return the clElement statistics found
116      */
117     @Transactional(readOnly = true)
118     public List<ClElementStatistics> getFilteredClElementStatistics(final String name, final String version,
119             final Instant startTimeStamp, final Instant endTimeStamp, Map<String, Object> filterMap,
120             final String sortOrder, final int getRecordNum) {
121
122         // @formatter:off
123         PfFilterParameters filterParams = PfFilterParameters
124                 .builder()
125                 .name(name)
126                 .version(version)
127                 .startTime(startTimeStamp)
128                 .endTime(endTimeStamp)
129                 .filterMap(filterMap)
130                 .sortOrder(sortOrder)
131                 .recordNum(getRecordNum)
132                 .build();
133         // @formatter:on
134         return asClElementStatisticsList(
135                 clElementStatisticsRepository.getFiltered(JpaClElementStatistics.class, filterParams));
136     }
137 }