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.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
40 import org.onap.policy.models.base.PfModelException;
41 import org.onap.policy.models.base.PfModelRuntimeException;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43 import org.springframework.stereotype.Component;
46 * This class provides information about statistics data of CL elements and CL Participants in database to callers.
49 public class MonitoringProvider implements Closeable {
51 private static final String DESC_ORDER = "DESC";
52 private final ParticipantStatisticsProvider participantStatisticsProvider;
53 private final ClElementStatisticsProvider clElementStatisticsProvider;
54 private final ControlLoopProvider controlLoopProvider;
57 * Create a Monitoring provider.
59 * @param controlLoopParameters the parameters for access to the database
60 * @throws PfModelRuntimeException on errors creating the provider
62 public MonitoringProvider(ClRuntimeParameterGroup controlLoopParameters) {
65 participantStatisticsProvider =
66 new ParticipantStatisticsProvider(controlLoopParameters.getDatabaseProviderParameters());
67 clElementStatisticsProvider =
68 new ClElementStatisticsProvider(controlLoopParameters.getDatabaseProviderParameters());
69 controlLoopProvider = new ControlLoopProvider(controlLoopParameters.getDatabaseProviderParameters());
70 } catch (PfModelException e) {
71 throw new PfModelRuntimeException(e);
76 public void close() throws IOException {
77 controlLoopProvider.close();
78 clElementStatisticsProvider.close();
79 participantStatisticsProvider.close();
83 * Create participant statistics.
85 * @param participantStatistics the participant statistics
86 * @return the result of create operation
87 * @throws PfModelException on creation errors
89 public ParticipantStatisticsList createParticipantStatistics(List<ParticipantStatistics> participantStatistics)
90 throws PfModelException {
91 var participantStatisticsList = new ParticipantStatisticsList();
92 participantStatisticsList
93 .setStatisticsList(participantStatisticsProvider.createParticipantStatistics(participantStatistics));
95 return participantStatisticsList;
99 * Create clElement statistics.
101 * @param clElementStatisticsList the clElement statistics
102 * @return the result of create operation
103 * @throws PfModelException on creation errors
105 public ClElementStatisticsList createClElementStatistics(List<ClElementStatistics> clElementStatisticsList)
106 throws PfModelException {
107 var elementStatisticsList = new ClElementStatisticsList();
108 elementStatisticsList
109 .setClElementStatistics(clElementStatisticsProvider.createClElementStatistics(clElementStatisticsList));
111 return elementStatisticsList;
115 * Get participant statistics based on specific filters.
117 * @param name the name of the participant statistics to get, null to get all statistics
118 * @param version the version of the participant statistics to get, null to get all statistics
119 * @param recordCount number of records to be fetched.
120 * @param startTime start of the timestamp, from statistics to be filtered
121 * @param endTime end of the timestamp up to which statistics to be filtered
122 * @return the participant found
124 public ParticipantStatisticsList fetchFilteredParticipantStatistics(@NonNull final String name,
125 final String version, int recordCount, Instant startTime, Instant endTime) {
126 var participantStatisticsList = new ParticipantStatisticsList();
128 // Additional parameters can be added in filterMap for filtering data.
129 Map<String, Object> filterMap = null;
130 participantStatisticsList.setStatisticsList(participantStatisticsProvider.getFilteredParticipantStatistics(name,
131 version, startTime, endTime, filterMap, DESC_ORDER, recordCount));
133 return participantStatisticsList;
137 * Get all participant statistics records found for a specific control loop. *
139 * @param controlLoopName name of the control loop
140 * @param controlLoopVersion version of the control loop
141 * @return All the participant statistics found
142 * @throws PfModelRuntimeException on errors getting participant statistics
144 public ParticipantStatisticsList fetchParticipantStatsPerControlLoop(@NonNull final String controlLoopName,
145 @NonNull final String controlLoopVersion) {
146 var statisticsList = new ParticipantStatisticsList();
147 List<ParticipantStatistics> participantStatistics = new ArrayList<>();
149 // Fetch all participantIds for a specific control loop
150 List<ToscaConceptIdentifier> participantIds =
151 getAllParticipantIdsPerControlLoop(controlLoopName, controlLoopVersion);
152 for (ToscaConceptIdentifier id : participantIds) {
153 participantStatistics.addAll(participantStatisticsProvider.getFilteredParticipantStatistics(
154 id.getName(), id.getVersion(), null, null, null, DESC_ORDER, 0));
156 statisticsList.setStatisticsList(participantStatistics);
157 } catch (PfModelException e) {
158 throw new PfModelRuntimeException(e);
160 return statisticsList;
164 * Get clElement statistics based on specific filters.
166 * @param name the name of the clElement statistics to get, null to get all statistics
167 * @param version the version of the clElement statistics to get, null to get all statistics
168 * @param id UUID of the control loop element
169 * @param startTime start of the timestamp, from statistics to be filtered
170 * @param endTime end of the timestamp up to which statistics to be filtered
171 * @param recordCount number of records to be fetched.
172 * @return the participant found
173 * @throws PfModelException on errors getting control loop statistics
175 public ClElementStatisticsList fetchFilteredClElementStatistics(@NonNull final String name, final String version,
176 final String id, Instant startTime, Instant endTime, int recordCount) throws PfModelException {
177 var clElementStatisticsList = new ClElementStatisticsList();
178 Map<String, Object> filterMap = new HashMap<>();
179 // Adding UUID in filter if present
181 filterMap.put("localName", id);
183 clElementStatisticsList.setClElementStatistics(clElementStatisticsProvider.getFilteredClElementStatistics(name,
184 version, startTime, endTime, filterMap, DESC_ORDER, recordCount));
186 return clElementStatisticsList;
190 * Get clElement statistics per control loop.
192 * @param name the name of the control loop
193 * @param version the version of the control loop
194 * @return the clElement statistics found
195 * @throws PfModelRuntimeException on errors getting control loop statistics
197 public ClElementStatisticsList fetchClElementStatsPerControlLoop(@NonNull final String name,
198 @NonNull final String version) {
199 var clElementStatisticsList = new ClElementStatisticsList();
200 List<ClElementStatistics> clElementStats = new ArrayList<>();
202 List<ControlLoopElement> clElements = new ArrayList<>();
203 // Fetch all control loop elements for the control loop
204 var controlLoop = controlLoopProvider.getControlLoop(new ToscaConceptIdentifier(name, version));
205 if (controlLoop != null) {
206 clElements.addAll(controlLoop.getElements().values());
207 // Collect control loop element statistics for each cl element.
208 for (ControlLoopElement clElement : clElements) {
209 clElementStats.addAll(fetchFilteredClElementStatistics(clElement.getParticipantId().getName(),
210 clElement.getParticipantId().getVersion(), clElement.getId().toString(), null, null, 0)
211 .getClElementStatistics());
214 clElementStatisticsList.setClElementStatistics(clElementStats);
215 } catch (PfModelException e) {
216 throw new PfModelRuntimeException(e);
218 return clElementStatisticsList;
222 * If required, REST end point can be defined for this method to fetch associated participant Ids
223 * for a control loop.
225 * @param name the name of the control loop
226 * @param version the version of the control loop
227 * @return List of participant Id
228 * @throws PfModelException on errors
230 public List<ToscaConceptIdentifier> getAllParticipantIdsPerControlLoop(String name, String version)
231 throws PfModelException {
232 List<ToscaConceptIdentifier> participantIds = new ArrayList<>();
233 var controlLoop = controlLoopProvider.getControlLoop(new ToscaConceptIdentifier(name, version));
234 if (controlLoop != null) {
235 for (ControlLoopElement clElement : controlLoop.getElements().values()) {
236 participantIds.add(clElement.getParticipantId());
239 return participantIds;
243 * If required, REST end point can be defined for this method to fetch associated control loop element Ids
244 * for a control loop.
246 * @param name the name of the control loop
247 * @param version the version of the control loop
248 * @return Map of control loop Id and participant details
249 * @throws PfModelException on errors
251 public Map<String, ToscaConceptIdentifier> getAllClElementsIdPerControlLoop(String name, String version)
252 throws PfModelException {
253 Map<String, ToscaConceptIdentifier> clElementId = new HashMap<>();
254 var controlLoop = controlLoopProvider.getControlLoop(new ToscaConceptIdentifier(name, version));
255 if (controlLoop != null) {
256 for (ControlLoopElement clElement : controlLoop.getElements().values()) {
257 clElementId.put(clElement.getId().toString(), clElement.getParticipantId());
263 public void updateClElementStatistics(List<ClElementStatistics> clElementStatistics) {
264 // TODO Auto-generated method stub
267 public void updateParticipantStatistics(List<ParticipantStatistics> statisticsList) {
268 // TODO Auto-generated method stub