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.Component;
44 * This class provides information about statistics data of CL elements and CL Participants in database to callers.
48 public class MonitoringProvider {
50 private static final String DESC_ORDER = "DESC";
51 private final ParticipantStatisticsProvider participantStatisticsProvider;
52 private final ClElementStatisticsProvider clElementStatisticsProvider;
53 private final ControlLoopProvider controlLoopProvider;
56 * Create participant statistics.
58 * @param participantStatistics the participant statistics
59 * @return the result of create operation
60 * @throws PfModelException on creation errors
62 public ParticipantStatisticsList createParticipantStatistics(List<ParticipantStatistics> participantStatistics)
63 throws PfModelException {
64 var participantStatisticsList = new ParticipantStatisticsList();
65 participantStatisticsList
66 .setStatisticsList(participantStatisticsProvider.createParticipantStatistics(participantStatistics));
68 return participantStatisticsList;
72 * Create clElement statistics.
74 * @param clElementStatisticsList the clElement statistics
75 * @return the result of create operation
76 * @throws PfModelException on creation errors
78 public ClElementStatisticsList createClElementStatistics(List<ClElementStatistics> clElementStatisticsList)
79 throws PfModelException {
80 var elementStatisticsList = new ClElementStatisticsList();
82 .setClElementStatistics(clElementStatisticsProvider.createClElementStatistics(clElementStatisticsList));
84 return elementStatisticsList;
88 * Get participant statistics based on specific filters.
90 * @param name the name of the participant statistics to get, null to get all statistics
91 * @param version the version of the participant statistics to get, null to get all statistics
92 * @param recordCount number of records to be fetched.
93 * @param startTime start of the timestamp, from statistics to be filtered
94 * @param endTime end of the timestamp up to which statistics to be filtered
95 * @return the participant found
97 public ParticipantStatisticsList fetchFilteredParticipantStatistics(@NonNull final String name,
98 final String version, int recordCount, Instant startTime, Instant endTime) {
99 var participantStatisticsList = new ParticipantStatisticsList();
101 // Additional parameters can be added in filterMap for filtering data.
102 Map<String, Object> filterMap = null;
103 participantStatisticsList.setStatisticsList(participantStatisticsProvider.getFilteredParticipantStatistics(name,
104 version, startTime, endTime, filterMap, DESC_ORDER, recordCount));
106 return participantStatisticsList;
110 * Get all participant statistics records found for a specific control loop. *
112 * @param controlLoopName name of the control loop
113 * @param controlLoopVersion version of the control loop
114 * @return All the participant statistics found
115 * @throws PfModelRuntimeException on errors getting participant statistics
117 public ParticipantStatisticsList fetchParticipantStatsPerControlLoop(@NonNull final String controlLoopName,
118 @NonNull final String controlLoopVersion) {
119 var statisticsList = new ParticipantStatisticsList();
120 List<ParticipantStatistics> participantStatistics = new ArrayList<>();
122 // Fetch all participantIds for a specific control loop
123 List<ToscaConceptIdentifier> participantIds =
124 getAllParticipantIdsPerControlLoop(controlLoopName, controlLoopVersion);
125 for (ToscaConceptIdentifier id : participantIds) {
126 participantStatistics.addAll(participantStatisticsProvider.getFilteredParticipantStatistics(
127 id.getName(), id.getVersion(), null, null, null, DESC_ORDER, 0));
129 statisticsList.setStatisticsList(participantStatistics);
130 } catch (PfModelException e) {
131 throw new PfModelRuntimeException(e);
133 return statisticsList;
137 * Get clElement statistics based on specific filters.
139 * @param name the name of the clElement statistics to get, null to get all statistics
140 * @param version the version of the clElement statistics to get, null to get all statistics
141 * @param id UUID of the control loop element
142 * @param startTime start of the timestamp, from statistics to be filtered
143 * @param endTime end of the timestamp up to which statistics to be filtered
144 * @param recordCount number of records to be fetched.
145 * @return the participant found
146 * @throws PfModelException on errors getting control loop statistics
148 public ClElementStatisticsList fetchFilteredClElementStatistics(@NonNull final String name, final String version,
149 final String id, Instant startTime, Instant endTime, int recordCount) throws PfModelException {
150 var clElementStatisticsList = new ClElementStatisticsList();
151 Map<String, Object> filterMap = new HashMap<>();
152 // Adding UUID in filter if present
154 filterMap.put("localName", id);
156 clElementStatisticsList.setClElementStatistics(clElementStatisticsProvider.getFilteredClElementStatistics(name,
157 version, startTime, endTime, filterMap, DESC_ORDER, recordCount));
159 return clElementStatisticsList;
163 * Get clElement statistics per control loop.
165 * @param name the name of the control loop
166 * @param version the version of the control loop
167 * @return the clElement statistics found
168 * @throws PfModelRuntimeException on errors getting control loop statistics
170 public ClElementStatisticsList fetchClElementStatsPerControlLoop(@NonNull final String name,
171 @NonNull final String version) {
172 var clElementStatisticsList = new ClElementStatisticsList();
173 List<ClElementStatistics> clElementStats = new ArrayList<>();
175 List<ControlLoopElement> clElements = new ArrayList<>();
176 // Fetch all control loop elements for the control loop
177 var controlLoop = controlLoopProvider.getControlLoop(new ToscaConceptIdentifier(name, version));
178 if (controlLoop != null) {
179 clElements.addAll(controlLoop.getElements().values());
180 // Collect control loop element statistics for each cl element.
181 for (ControlLoopElement clElement : clElements) {
182 clElementStats.addAll(fetchFilteredClElementStatistics(clElement.getParticipantId().getName(),
183 clElement.getParticipantId().getVersion(), clElement.getId().toString(), null, null, 0)
184 .getClElementStatistics());
187 clElementStatisticsList.setClElementStatistics(clElementStats);
188 } catch (PfModelException e) {
189 throw new PfModelRuntimeException(e);
191 return clElementStatisticsList;
195 * If required, REST end point can be defined for this method to fetch associated participant Ids
196 * for a control loop.
198 * @param name the name of the control loop
199 * @param version the version of the control loop
200 * @return List of participant Id
201 * @throws PfModelException on errors
203 public List<ToscaConceptIdentifier> getAllParticipantIdsPerControlLoop(String name, String version)
204 throws PfModelException {
205 List<ToscaConceptIdentifier> participantIds = new ArrayList<>();
206 var controlLoop = controlLoopProvider.getControlLoop(new ToscaConceptIdentifier(name, version));
207 if (controlLoop != null) {
208 for (ControlLoopElement clElement : controlLoop.getElements().values()) {
209 participantIds.add(clElement.getParticipantId());
212 return participantIds;
216 * If required, REST end point can be defined for this method to fetch associated control loop element Ids
217 * for a control loop.
219 * @param name the name of the control loop
220 * @param version the version of the control loop
221 * @return Map of control loop Id and participant details
222 * @throws PfModelException on errors
224 public Map<String, ToscaConceptIdentifier> getAllClElementsIdPerControlLoop(String name, String version)
225 throws PfModelException {
226 Map<String, ToscaConceptIdentifier> clElementId = new HashMap<>();
227 var controlLoop = controlLoopProvider.getControlLoop(new ToscaConceptIdentifier(name, version));
228 if (controlLoop != null) {
229 for (ControlLoopElement clElement : controlLoop.getElements().values()) {
230 clElementId.put(clElement.getId().toString(), clElement.getParticipantId());