2ec438bea060a7163eac5f00111460c1bcda956f
[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.runtime.monitoring;
22
23 import java.time.Instant;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import lombok.AllArgsConstructor;
29 import lombok.NonNull;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatisticsList;
35 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ClElementStatisticsProvider;
36 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
37 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantStatisticsProvider;
38 import org.onap.policy.models.base.PfModelException;
39 import org.onap.policy.models.base.PfModelRuntimeException;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41 import org.springframework.stereotype.Service;
42 import org.springframework.transaction.annotation.Transactional;
43
44 /**
45  * This class provides information about statistics data of CL elements and CL Participants in database to callers.
46  */
47 @Service
48 @Transactional
49 @AllArgsConstructor
50 public class MonitoringProvider {
51
52     private static final String DESC_ORDER = "DESC";
53     private final ParticipantStatisticsProvider participantStatisticsProvider;
54     private final ClElementStatisticsProvider clElementStatisticsProvider;
55     private final ControlLoopProvider controlLoopProvider;
56
57     /**
58      * Create participant statistics.
59      *
60      * @param participantStatistics the participant statistics
61      * @return the result of create operation
62      * @throws PfModelException on creation errors
63      */
64     public ParticipantStatisticsList createParticipantStatistics(List<ParticipantStatistics> participantStatistics)
65             throws PfModelException {
66         var participantStatisticsList = new ParticipantStatisticsList();
67         participantStatisticsList
68                 .setStatisticsList(participantStatisticsProvider.createParticipantStatistics(participantStatistics));
69
70         return participantStatisticsList;
71     }
72
73     /**
74      * Create clElement statistics.
75      *
76      * @param clElementStatisticsList the clElement statistics
77      * @return the result of create operation
78      * @throws PfModelException on creation errors
79      */
80     public ClElementStatisticsList createClElementStatistics(List<ClElementStatistics> clElementStatisticsList)
81             throws PfModelException {
82         var elementStatisticsList = new ClElementStatisticsList();
83         elementStatisticsList
84                 .setClElementStatistics(clElementStatisticsProvider.createClElementStatistics(clElementStatisticsList));
85
86         return elementStatisticsList;
87     }
88
89     /**
90      * Get participant statistics based on specific filters.
91      *
92      * @param name the name of the participant statistics to get, null to get all statistics
93      * @param version the version of the participant statistics to get, null to get all statistics
94      * @param recordCount number of records to be fetched.
95      * @param startTime start of the timestamp, from statistics to be filtered
96      * @param endTime end of the timestamp up to which statistics to be filtered
97      * @return the participant found
98      */
99     @Transactional(readOnly = true)
100     public ParticipantStatisticsList fetchFilteredParticipantStatistics(@NonNull final String name,
101             final String version, int recordCount, Instant startTime, Instant endTime) {
102         var participantStatisticsList = new ParticipantStatisticsList();
103
104         // Additional parameters can be added in filterMap for filtering data.
105         Map<String, Object> filterMap = null;
106         participantStatisticsList.setStatisticsList(participantStatisticsProvider.getFilteredParticipantStatistics(name,
107                 version, startTime, endTime, filterMap, DESC_ORDER, recordCount));
108
109         return participantStatisticsList;
110     }
111
112     /**
113      * Get all participant statistics records found for a specific control loop. *
114      *
115      * @param controlLoopName name of the control loop
116      * @param controlLoopVersion version of the control loop
117      * @return All the participant statistics found
118      * @throws PfModelRuntimeException on errors getting participant statistics
119      */
120     @Transactional(readOnly = true)
121     public ParticipantStatisticsList fetchParticipantStatsPerControlLoop(@NonNull final String controlLoopName,
122             @NonNull final String controlLoopVersion) {
123         var statisticsList = new ParticipantStatisticsList();
124         List<ParticipantStatistics> participantStatistics = new ArrayList<>();
125         try {
126             // Fetch all participantIds for a specific control loop
127             List<ToscaConceptIdentifier> participantIds =
128                     getAllParticipantIdsPerControlLoop(controlLoopName, controlLoopVersion);
129             for (ToscaConceptIdentifier id : participantIds) {
130                 participantStatistics.addAll(participantStatisticsProvider.getFilteredParticipantStatistics(
131                         id.getName(), id.getVersion(), null, null, null, DESC_ORDER, 0));
132             }
133             statisticsList.setStatisticsList(participantStatistics);
134         } catch (PfModelException e) {
135             throw new PfModelRuntimeException(e);
136         }
137         return statisticsList;
138     }
139
140     /**
141      * Get clElement statistics based on specific filters.
142      *
143      * @param name the name of the clElement statistics to get, null to get all statistics
144      * @param version the version of the clElement statistics to get, null to get all statistics
145      * @param id UUID of the control loop element
146      * @param startTime start of the timestamp, from statistics to be filtered
147      * @param endTime end of the timestamp up to which statistics to be filtered
148      * @param recordCount number of records to be fetched.
149      * @return the participant found
150      * @throws PfModelException on errors getting control loop statistics
151      */
152     @Transactional(readOnly = true)
153     public ClElementStatisticsList fetchFilteredClElementStatistics(@NonNull final String name, final String version,
154             final String id, Instant startTime, Instant endTime, int recordCount) throws PfModelException {
155         var clElementStatisticsList = new ClElementStatisticsList();
156         Map<String, Object> filterMap = new HashMap<>();
157         // Adding UUID in filter if present
158         if (id != null) {
159             filterMap.put("localName", id);
160         }
161         clElementStatisticsList.setClElementStatistics(clElementStatisticsProvider.getFilteredClElementStatistics(name,
162                 version, startTime, endTime, filterMap, DESC_ORDER, recordCount));
163
164         return clElementStatisticsList;
165     }
166
167     /**
168      * Get clElement statistics per control loop.
169      *
170      * @param name the name of the control loop
171      * @param version the version of the control loop
172      * @return the clElement statistics found
173      * @throws PfModelRuntimeException on errors getting control loop statistics
174      */
175     @Transactional(readOnly = true)
176     public ClElementStatisticsList fetchClElementStatsPerControlLoop(@NonNull final String name,
177             @NonNull final String version) {
178         var clElementStatisticsList = new ClElementStatisticsList();
179         List<ClElementStatistics> clElementStats = new ArrayList<>();
180         try {
181             List<ControlLoopElement> clElements = new ArrayList<>();
182             // Fetch all control loop elements for the control loop
183             var controlLoopOpt = controlLoopProvider.findControlLoop(new ToscaConceptIdentifier(name, version));
184             if (controlLoopOpt.isPresent()) {
185                 clElements.addAll(controlLoopOpt.get().getElements().values());
186                 // Collect control loop element statistics for each cl element.
187                 for (ControlLoopElement clElement : clElements) {
188                     clElementStats.addAll(fetchFilteredClElementStatistics(clElement.getParticipantId().getName(),
189                             clElement.getParticipantId().getVersion(), clElement.getId().toString(), null, null, 0)
190                                     .getClElementStatistics());
191                 }
192             }
193             clElementStatisticsList.setClElementStatistics(clElementStats);
194         } catch (PfModelException e) {
195             throw new PfModelRuntimeException(e);
196         }
197         return clElementStatisticsList;
198     }
199
200     /**
201      * If required, REST end point can be defined for this method to fetch associated participant Ids
202      * for a control loop.
203      *
204      * @param name the name of the control loop
205      * @param version the version of the control loop
206      * @return List of participant Id
207      * @throws PfModelException on errors
208      */
209     @Transactional(readOnly = true)
210     public List<ToscaConceptIdentifier> getAllParticipantIdsPerControlLoop(String name, String version)
211             throws PfModelException {
212         List<ToscaConceptIdentifier> participantIds = new ArrayList<>();
213         var controlLoopOpt = controlLoopProvider.findControlLoop(new ToscaConceptIdentifier(name, version));
214         if (controlLoopOpt.isPresent()) {
215             for (ControlLoopElement clElement : controlLoopOpt.get().getElements().values()) {
216                 participantIds.add(clElement.getParticipantId());
217             }
218         }
219         return participantIds;
220     }
221
222     /**
223      * If required, REST end point can be defined for this method to fetch associated control loop element Ids
224      * for a control loop.
225      *
226      * @param name the name of the control loop
227      * @param version the version of the control loop
228      * @return Map of control loop Id and participant details
229      * @throws PfModelException on errors
230      */
231     @Transactional(readOnly = true)
232     public Map<String, ToscaConceptIdentifier> getAllClElementsIdPerControlLoop(String name, String version)
233             throws PfModelException {
234         Map<String, ToscaConceptIdentifier> clElementId = new HashMap<>();
235         var controlLoopOpt = controlLoopProvider.findControlLoop(new ToscaConceptIdentifier(name, version));
236         if (controlLoopOpt.isPresent()) {
237             for (ControlLoopElement clElement : controlLoopOpt.get().getElements().values()) {
238                 clElementId.put(clElement.getId().toString(), clElement.getParticipantId());
239             }
240         }
241         return clElementId;
242     }
243 }