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