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