4ea76ff4b81192a1c0500edcfc888c3a49c92b45
[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.ParticipantStatistics;
30 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaParticipantStatistics;
31 import org.onap.policy.models.base.PfModelException;
32 import org.onap.policy.models.base.PfTimestampKey;
33 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
34 import org.onap.policy.models.provider.impl.AbstractModelsProvider;
35
36 /**
37  * This class provides the provision of information on participant statistics in the database to callers.
38  */
39 public class ParticipantStatisticsProvider extends AbstractModelsProvider {
40
41     /**
42      * Create a provider for control loops statistics.
43      *
44      * @param parameters the parameters for database access
45      * @throws PfModelException on initiation errors
46      */
47     public ParticipantStatisticsProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException {
48         super(parameters);
49         this.init();
50     }
51
52     /**
53      * Get Participant statistics.
54      *
55      * @param name the name of the participant statistics to get, null to get all stats
56      * @return the participant statistics found
57      * @throws PfModelException on errors getting participant statistics
58      */
59     public List<ParticipantStatistics> getParticipantStatistics(final String name, final String version,
60             final Instant timestamp) throws PfModelException {
61
62         if (name != null && version != null && timestamp != null) {
63             List<ParticipantStatistics> participantStatistics = new ArrayList<>(1);
64             participantStatistics.add(getPfDao()
65                     .get(JpaParticipantStatistics.class, new PfTimestampKey(name, version, timestamp)).toAuthorative());
66             return participantStatistics;
67         } else if (name != null) {
68             return getFilteredParticipantStatistics(name, version, timestamp, null, null, "DESC", 0);
69         } else {
70             return asParticipantStatisticsList(getPfDao().getAll(JpaParticipantStatistics.class));
71         }
72     }
73
74
75     /**
76      * Get filtered participant statistics.
77      *
78      * @param name the participant name for the statistics to get
79      * @param startTimeStamp startTimeStamp to filter statistics
80      * @param endTimeStamp endTimeStamp to filter statistics
81      * @param sortOrder sortOrder to query database
82      * @param getRecordNum Total query count from database
83      * @return the participant statistics found
84      * @throws PfModelException on errors getting policies
85      */
86     public List<ParticipantStatistics> getFilteredParticipantStatistics(final String name, final String version,
87             final Instant startTimeStamp, final Instant endTimeStamp, Map<String, Object> filterMap,
88             final String sortOrder, final int getRecordNum) {
89
90         return asParticipantStatisticsList(getPfDao().getFiltered(JpaParticipantStatistics.class, name, version,
91                 startTimeStamp, endTimeStamp, filterMap, sortOrder, getRecordNum));
92     }
93
94
95     /**
96      * Creates Participant statistics.
97      *
98      * @param participantStatisticsList a specification of the CL statistics to create
99      * @return the participant statistics created
100      * @throws PfModelException on errors creating participant statistics
101      */
102     public List<ParticipantStatistics> createParticipantStatistics(
103             @NonNull final List<ParticipantStatistics> participantStatisticsList) throws PfModelException {
104
105         List<JpaParticipantStatistics> jpaParticipantStatisticsList = ProviderUtils
106                 .getJpaAndValidate(participantStatisticsList, JpaParticipantStatistics::new, "Participant Statistics");
107
108         jpaParticipantStatisticsList.forEach(jpaParticipantStatistics -> getPfDao().update(jpaParticipantStatistics));
109
110         // Return the created participant statistics
111         List<ParticipantStatistics> participantStatistics = new ArrayList<>(participantStatisticsList.size());
112
113         for (ParticipantStatistics participantStatisticsItem : participantStatisticsList) {
114             var jpaParticipantStatistics = getPfDao().get(JpaParticipantStatistics.class,
115                     new PfTimestampKey(participantStatisticsItem.getParticipantId().getName(),
116                             participantStatisticsItem.getParticipantId().getVersion(),
117                             participantStatisticsItem.getTimeStamp()));
118             participantStatistics.add(jpaParticipantStatistics.toAuthorative());
119         }
120
121         return participantStatistics;
122     }
123
124
125
126     /**
127      * Convert JPA participant statistics list to participant statistics list.
128      *
129      * @param jpaParticipantStatisticsList the list to convert
130      * @return the participant statistics list
131      */
132     private List<ParticipantStatistics> asParticipantStatisticsList(
133             List<JpaParticipantStatistics> jpaParticipantStatisticsList) {
134
135         return jpaParticipantStatisticsList.stream().map(JpaParticipantStatistics::toAuthorative)
136                 .collect(Collectors.toList());
137     }
138 }