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