00eab8f691557d97e1db5a4f6ceb79e329ed1ae7
[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.ClElementStatistics;
30 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaClElementStatistics;
31 import org.onap.policy.models.base.PfModelException;
32 import org.onap.policy.models.base.PfReferenceTimestampKey;
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
37 /**
38  * This class provides the provision of information on control loop element statistics in the database to callers.
39  *
40  * @author Ramesh Murugan Iyer (ramesh.murugan.iyer@est.tech)
41  */
42 public class ClElementStatisticsProvider extends AbstractModelsProvider {
43
44     /**
45      * Create a provider for control loop element statistics.
46      *
47      * @param parameters the parameters for database access
48      * @throws PfModelException on initiation errors
49      */
50     public ClElementStatisticsProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException {
51         super(parameters);
52         this.init();
53     }
54
55     /**
56      * Creates control loop element statistics.
57      *
58      * @param clElementStatisticsList a specification of the CL element statistics to create
59      * @return the clElement statistics created
60      * @throws PfModelException on errors creating clElement statistics
61      */
62     public List<ClElementStatistics> createClElementStatistics(
63             @NonNull final List<ClElementStatistics> clElementStatisticsList) throws PfModelException {
64
65         List<JpaClElementStatistics> jpaClElementStatisticsList = ProviderUtils.getJpaAndValidate(
66                 clElementStatisticsList, JpaClElementStatistics::new, "control loop element statistics");
67
68         jpaClElementStatisticsList.forEach(jpaClElementStatistics -> getPfDao().create(jpaClElementStatistics));
69
70         // Return the created control loop element statistics
71         List<ClElementStatistics> elementStatistics = new ArrayList<>(clElementStatisticsList.size());
72
73         for (ClElementStatistics clElementStat : clElementStatisticsList) {
74             var jpaClElementStatistics = getPfDao().get(JpaClElementStatistics.class,
75                     new PfReferenceTimestampKey(clElementStat.getParticipantId().getName(),
76                             clElementStat.getParticipantId().getVersion(), clElementStat.getId().toString(),
77                             clElementStat.getTimeStamp()));
78             elementStatistics.add(jpaClElementStatistics.toAuthorative());
79         }
80
81         return elementStatistics;
82     }
83
84     /**
85      * Convert JPA clElement statistics list to clElement statistics list.
86      *
87      * @param jpaClElementStatistics the list to convert
88      * @return the clElement statistics list
89      */
90     private List<ClElementStatistics> asClElementStatisticsList(List<JpaClElementStatistics> jpaClElementStatistics) {
91         return jpaClElementStatistics.stream().map(JpaClElementStatistics::toAuthorative).collect(Collectors.toList());
92     }
93
94     /**
95      * Get clElement statistics.
96      *
97      * @param name the name of the participant
98      * @param version version of the participant
99      * @param id of the control loop element
100      * @param timestamp timestamp of the statistics
101      * @return the clElement statistics found
102      * @throws PfModelException on errors getting clElement statistics
103      */
104     public List<ClElementStatistics> getClElementStatistics(final String name, final String version, final String id,
105             final Instant timestamp) throws PfModelException {
106         List<ClElementStatistics> clElementStatistics = new ArrayList<>(1);
107         if (name != null && version != null && timestamp != null && id != null) {
108             clElementStatistics.add(getPfDao()
109                     .get(JpaClElementStatistics.class, new PfReferenceTimestampKey(name, version, id, timestamp))
110                     .toAuthorative());
111             return clElementStatistics;
112         } else if (name != null) {
113             clElementStatistics.addAll(getFilteredClElementStatistics(name, version, null, null, null, "DESC", 0));
114         } else {
115             clElementStatistics.addAll(asClElementStatisticsList(getPfDao().getAll(JpaClElementStatistics.class)));
116         }
117         return clElementStatistics;
118     }
119
120     /**
121      * Get filtered clElement statistics.
122      *
123      * @param name the clElement name for the statistics to get
124      * @param startTimeStamp startTimeStamp to filter statistics
125      * @param endTimeStamp endTimeStamp to filter statistics
126      * @param sortOrder sortOrder to query database
127      * @param getRecordNum Total query count from database
128      * @return the clElement statistics found
129      * @throws PfModelException on errors getting policies
130      */
131     public List<ClElementStatistics> getFilteredClElementStatistics(final String name, final String version,
132             final Instant startTimeStamp, final Instant endTimeStamp, Map<String, Object> filterMap,
133             final String sortOrder, final int getRecordNum) {
134
135         // @formatter:off
136         PfFilterParameters filterParams = PfFilterParameters
137                 .builder()
138                 .name(name)
139                 .version(version)
140                 .startTime(startTimeStamp)
141                 .endTime(endTimeStamp)
142                 .filterMap(filterMap)
143                 .sortOrder(sortOrder)
144                 .recordNum(getRecordNum)
145                 .build();
146         // @formatter:on
147         return asClElementStatisticsList(getPfDao().getFiltered(JpaClElementStatistics.class, filterParams));
148     }
149 }