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.time.Instant;
24 import java.util.List;
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;
40 * This class provides the provision of information on control loop element statistics in the database to callers.
42 * @author Ramesh Murugan Iyer (ramesh.murugan.iyer@est.tech)
47 public class ClElementStatisticsProvider {
49 private ClElementStatisticsRepository clElementStatisticsRepository;
52 * Creates control loop element statistics.
54 * @param clElementStatisticsList a specification of the CL element statistics to create
55 * @return the clElement statistics created
56 * @throws PfModelException on initiation errors
58 public List<ClElementStatistics> createClElementStatistics(
59 @NonNull final List<ClElementStatistics> clElementStatisticsList) throws PfModelException {
62 var jpaClElementStatisticsList = ProviderUtils.getJpaAndValidateList(clElementStatisticsList,
63 JpaClElementStatistics::new, "control loop element statistics");
65 var jpaClElementStatisticsSaved = clElementStatisticsRepository.saveAll(jpaClElementStatisticsList);
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);
75 * Convert JPA clElement statistics list to clElement statistics list.
77 * @param jpaClElementStatistics the list to convert
78 * @return the clElement statistics list
80 private List<ClElementStatistics> asClElementStatisticsList(List<JpaClElementStatistics> jpaClElementStatistics) {
81 return jpaClElementStatistics.stream().map(JpaClElementStatistics::toAuthorative).collect(Collectors.toList());
85 * Get clElement statistics.
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
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);
102 return asClElementStatisticsList(clElementStatisticsRepository.findAll());
106 * Get filtered clElement statistics.
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
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) {
123 PfFilterParameters filterParams = PfFilterParameters
127 .startTime(startTimeStamp)
128 .endTime(endTimeStamp)
129 .filterMap(filterMap)
130 .sortOrder(sortOrder)
131 .recordNum(getRecordNum)
134 return asClElementStatisticsList(
135 clElementStatisticsRepository.getFiltered(JpaClElementStatistics.class, filterParams));