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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.controlloop.runtime.monitoring;
23 import java.time.Instant;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
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;
45 * This class provides information about statistics data of CL elements and CL Participants in database to callers.
50 public class MonitoringProvider {
52 private static final String DESC_ORDER = "DESC";
53 private final ParticipantStatisticsProvider participantStatisticsProvider;
54 private final ClElementStatisticsProvider clElementStatisticsProvider;
55 private final ControlLoopProvider controlLoopProvider;
58 * Create participant statistics.
60 * @param participantStatistics the participant statistics
61 * @return the result of create operation
62 * @throws PfModelException on creation errors
64 public ParticipantStatisticsList createParticipantStatistics(List<ParticipantStatistics> participantStatistics)
65 throws PfModelException {
66 var participantStatisticsList = new ParticipantStatisticsList();
67 participantStatisticsList
68 .setStatisticsList(participantStatisticsProvider.createParticipantStatistics(participantStatistics));
70 return participantStatisticsList;
74 * Create clElement statistics.
76 * @param clElementStatisticsList the clElement statistics
77 * @return the result of create operation
78 * @throws PfModelException on creation errors
80 public ClElementStatisticsList createClElementStatistics(List<ClElementStatistics> clElementStatisticsList)
81 throws PfModelException {
82 var elementStatisticsList = new ClElementStatisticsList();
84 .setClElementStatistics(clElementStatisticsProvider.createClElementStatistics(clElementStatisticsList));
86 return elementStatisticsList;
90 * Get participant statistics based on specific filters.
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
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();
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));
109 return participantStatisticsList;
113 * Get all participant statistics records found for a specific control loop. *
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
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<>();
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));
133 statisticsList.setStatisticsList(participantStatistics);
134 } catch (PfModelException e) {
135 throw new PfModelRuntimeException(e);
137 return statisticsList;
141 * Get clElement statistics based on specific filters.
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
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
159 filterMap.put("localName", id);
161 clElementStatisticsList.setClElementStatistics(clElementStatisticsProvider.getFilteredClElementStatistics(name,
162 version, startTime, endTime, filterMap, DESC_ORDER, recordCount));
164 return clElementStatisticsList;
168 * Get clElement statistics per control loop.
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
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<>();
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());
193 clElementStatisticsList.setClElementStatistics(clElementStats);
194 } catch (PfModelException e) {
195 throw new PfModelRuntimeException(e);
197 return clElementStatisticsList;
201 * If required, REST end point can be defined for this method to fetch associated participant Ids
202 * for a control loop.
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
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());
219 return participantIds;
223 * If required, REST end point can be defined for this method to fetch associated control loop element Ids
224 * for a control loop.
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
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());