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.io.Closeable;
24 import java.io.IOException;
25 import java.time.Instant;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
30 import lombok.NonNull;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatisticsList;
36 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ClElementStatisticsProvider;
37 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
38 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantStatisticsProvider;
39 import org.onap.policy.models.base.PfModelException;
40 import org.onap.policy.models.base.PfModelRuntimeException;
41 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
45 * This class provides information about statistics data of CL elements and CL Participants in database to callers.
47 public class MonitoringProvider implements Closeable {
49 private static final String DESC_ORDER = "DESC";
50 private final ParticipantStatisticsProvider participantStatisticsProvider;
51 private final ClElementStatisticsProvider clElementStatisticsProvider;
52 private final ControlLoopProvider controlLoopProvider;
55 * Create a Monitoring provider.
57 * @param parameters parameters for accessing the database for monitoring
58 * @throws PfModelRuntimeException on errors creating the provider
60 public MonitoringProvider(PolicyModelsProviderParameters parameters) {
63 participantStatisticsProvider = new ParticipantStatisticsProvider(parameters);
64 clElementStatisticsProvider = new ClElementStatisticsProvider(parameters);
65 controlLoopProvider = new ControlLoopProvider(parameters);
66 } catch (PfModelException e) {
67 throw new PfModelRuntimeException(e);
72 public void close() throws IOException {
73 controlLoopProvider.close();
74 clElementStatisticsProvider.close();
75 participantStatisticsProvider.close();
79 * Create participant statistics.
81 * @param participantStatistics the participant statistics
82 * @return the result of create operation
83 * @throws PfModelException on creation errors
85 public ParticipantStatisticsList createParticipantStatistics(List<ParticipantStatistics> participantStatistics)
86 throws PfModelException {
87 var participantStatisticsList = new ParticipantStatisticsList();
88 participantStatisticsList
89 .setStatisticsList(participantStatisticsProvider.createParticipantStatistics(participantStatistics));
91 return participantStatisticsList;
95 * Create clElement statistics.
97 * @param clElementStatisticsList the clElement statistics
98 * @return the result of create operation
99 * @throws PfModelException on creation errors
101 public ClElementStatisticsList createClElementStatistics(List<ClElementStatistics> clElementStatisticsList)
102 throws PfModelException {
103 var elementStatisticsList = new ClElementStatisticsList();
104 elementStatisticsList
105 .setClElementStatistics(clElementStatisticsProvider.createClElementStatistics(clElementStatisticsList));
107 return elementStatisticsList;
111 * Get participant statistics based on specific filters.
113 * @param name the name of the participant statistics to get, null to get all statistics
114 * @param version the version of the participant statistics to get, null to get all statistics
115 * @param recordCount number of records to be fetched.
116 * @param startTime start of the timestamp, from statistics to be filtered
117 * @param endTime end of the timestamp up to which statistics to be filtered
118 * @return the participant found
120 public ParticipantStatisticsList fetchFilteredParticipantStatistics(@NonNull final String name,
121 final String version, int recordCount, Instant startTime, Instant endTime) {
122 var participantStatisticsList = new ParticipantStatisticsList();
124 // Additional parameters can be added in filterMap for filtering data.
125 Map<String, Object> filterMap = null;
126 participantStatisticsList.setStatisticsList(participantStatisticsProvider.getFilteredParticipantStatistics(name,
127 version, startTime, endTime, filterMap, DESC_ORDER, recordCount));
129 return participantStatisticsList;
133 * Get all participant statistics records found for a specific control loop. *
135 * @param controlLoopName name of the control loop
136 * @param controlLoopVersion version of the control loop
137 * @return All the participant statistics found
138 * @throws PfModelRuntimeException on errors getting participant statistics
140 public ParticipantStatisticsList fetchParticipantStatsPerControlLoop(@NonNull final String controlLoopName,
141 @NonNull final String controlLoopVersion) {
142 var statisticsList = new ParticipantStatisticsList();
143 List<ParticipantStatistics> participantStatistics = new ArrayList<>();
145 // Fetch all participantIds for a specific control loop
146 List<ToscaConceptIdentifier> participantIds =
147 getAllParticipantIdsPerControlLoop(controlLoopName, controlLoopVersion);
148 for (ToscaConceptIdentifier id : participantIds) {
149 participantStatistics.addAll(participantStatisticsProvider
150 .getFilteredParticipantStatistics(id.getName(), id.getVersion(), null, null, null, DESC_ORDER, 0));
152 statisticsList.setStatisticsList(participantStatistics);
153 } catch (PfModelException e) {
154 throw new PfModelRuntimeException(e);
156 return statisticsList;
160 * Get clElement statistics based on specific filters.
162 * @param name the name of the clElement statistics to get, null to get all statistics
163 * @param version the version of the clElement statistics to get, null to get all statistics
164 * @param id UUID of the control loop element
165 * @param startTime start of the timestamp, from statistics to be filtered
166 * @param endTime end of the timestamp up to which statistics to be filtered
167 * @param recordCount number of records to be fetched.
168 * @return the participant found
169 * @throws PfModelException on errors getting control loop statistics
171 public ClElementStatisticsList fetchFilteredClElementStatistics(@NonNull final String name, final String version,
172 final String id, Instant startTime, Instant endTime, int recordCount) throws PfModelException {
173 var clElementStatisticsList = new ClElementStatisticsList();
174 Map<String, Object> filterMap = new HashMap<>();
175 // Adding UUID in filter if present
177 filterMap.put("localName", id);
179 clElementStatisticsList.setClElementStatistics(clElementStatisticsProvider.getFilteredClElementStatistics(name,
180 version, startTime, endTime, filterMap, DESC_ORDER, recordCount));
182 return clElementStatisticsList;
186 * Get clElement statistics per control loop.
188 * @param name the name of the control loop
189 * @param version the version of the control loop
190 * @return the clElement statistics found
191 * @throws PfModelRuntimeException on errors getting control loop statistics
193 public ClElementStatisticsList fetchClElementStatsPerControlLoop(@NonNull final String name,
194 @NonNull final String version) {
195 var clElementStatisticsList = new ClElementStatisticsList();
196 List<ClElementStatistics> clElementStats = new ArrayList<>();
198 List<ControlLoopElement> clElements = new ArrayList<>();
199 // Fetch all control loop elements for the control loop
200 var controlLoop = controlLoopProvider.getControlLoop(new ToscaConceptIdentifier(name, version));
201 if (controlLoop != null) {
202 clElements.addAll(controlLoop.getElements().values());
203 // Collect control loop element statistics for each cl element.
204 for (ControlLoopElement clElement : clElements) {
205 clElementStats.addAll(fetchFilteredClElementStatistics(clElement.getParticipantId().getName(),
206 clElement.getParticipantId().getVersion(), clElement.getId().toString(), null, null, 0)
207 .getClElementStatistics());
210 clElementStatisticsList.setClElementStatistics(clElementStats);
211 } catch (PfModelException e) {
212 throw new PfModelRuntimeException(e);
214 return clElementStatisticsList;
218 * If required, REST end point can be defined for this method to fetch associated participant Ids
219 * for a control loop.
221 * @param name the name of the control loop
222 * @param version the version of the control loop
223 * @return List of participant Id
224 * @throws PfModelException on errors
226 public List<ToscaConceptIdentifier> getAllParticipantIdsPerControlLoop(String name, String version)
227 throws PfModelException {
228 List<ToscaConceptIdentifier> participantIds = new ArrayList<>();
229 var controlLoop = controlLoopProvider.getControlLoop(new ToscaConceptIdentifier(name, version));
230 if (controlLoop != null) {
231 for (ControlLoopElement clElement : controlLoop.getElements().values()) {
232 participantIds.add(clElement.getParticipantId());
235 return participantIds;
239 * If required, REST end point can be defined for this method to fetch associated control loop element Ids
240 * for a control loop.
242 * @param name the name of the control loop
243 * @param version the version of the control loop
244 * @return Map of control loop Id and participant details
245 * @throws PfModelException on errors
247 public Map<String, ToscaConceptIdentifier> getAllClElementsIdPerControlLoop(String name, String version)
248 throws PfModelException {
249 Map<String, ToscaConceptIdentifier> clElementId = new HashMap<>();
250 var controlLoop = controlLoopProvider.getControlLoop(new ToscaConceptIdentifier(name, version));
251 if (controlLoop != null) {
252 for (ControlLoopElement clElement : controlLoop.getElements().values()) {
253 clElementId.put(clElement.getId().toString(), clElement.getParticipantId());
259 public void updateClElementStatistics(List<ClElementStatistics> clElementStatistics) {
260 // TODO Auto-generated method stub
263 public void updateParticipantStatistics(List<ParticipantStatistics> statisticsList) {
264 // TODO Auto-generated method stub