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.ControlLoop;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
35 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics;
36 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatisticsList;
37 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ClElementStatisticsProvider;
38 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
39 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantStatisticsProvider;
40 import org.onap.policy.models.base.PfModelException;
41 import org.onap.policy.models.base.PfModelRuntimeException;
42 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
46 * This class provides information about statistics data of CL elements and CL Participants in database to callers.
48 public class MonitoringProvider implements Closeable {
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 a Monitoring provider.
59 public MonitoringProvider(PolicyModelsProviderParameters parameters) {
62 participantStatisticsProvider = new ParticipantStatisticsProvider(parameters);
63 clElementStatisticsProvider = new ClElementStatisticsProvider(parameters);
64 controlLoopProvider = new ControlLoopProvider(parameters);
65 } catch (PfModelException e) {
66 throw new PfModelRuntimeException(e);
71 public void close() throws IOException {
72 controlLoopProvider.close();
73 clElementStatisticsProvider.close();
74 participantStatisticsProvider.close();
78 * Create participant statistics.
80 * @param participantStatistics the participant statistics
81 * @return the result of create operation
82 * @throws PfModelException on creation errors
84 public ParticipantStatisticsList createParticipantStatistics(List<ParticipantStatistics> participantStatistics)
85 throws PfModelException {
86 ParticipantStatisticsList participantStatisticsList = new ParticipantStatisticsList();
87 participantStatisticsList.setStatisticsList(participantStatisticsProvider
88 .createParticipantStatistics(participantStatistics));
90 return participantStatisticsList;
94 * Create clElement statistics.
96 * @param clElementStatisticsList the clElement statistics
97 * @return the result of create operation
98 * @throws PfModelException on creation errors
100 public ClElementStatisticsList createClElementStatistics(List<ClElementStatistics> clElementStatisticsList)
101 throws PfModelException {
102 ClElementStatisticsList elementStatisticsList = new ClElementStatisticsList();
103 elementStatisticsList.setClElementStatistics(clElementStatisticsProvider
104 .createClElementStatistics(clElementStatisticsList));
106 return elementStatisticsList;
110 * Get participant statistics based on specific filters.
112 * @param name the name of the participant statistics to get, null to get all statistics
113 * @param version the version of the participant statistics to get, null to get all statistics
114 * @param recordCount number of records to be fetched.
115 * @param startTime start of the timestamp, from statistics to be filtered
116 * @param endTime end of the timestamp up to which statistics to be filtered
117 * @return the participant found
119 public ParticipantStatisticsList fetchFilteredParticipantStatistics(@NonNull final String name,
120 final String version, int recordCount,
121 Instant startTime, Instant endTime) {
122 ParticipantStatisticsList 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(
127 name, 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 PfModelException on errors getting participant statistics
140 public ParticipantStatisticsList fetchParticipantStatsPerControlLoop(@NonNull final String controlLoopName,
141 @NonNull final String controlLoopVersion)
142 throws PfModelException {
143 ParticipantStatisticsList statisticsList = new ParticipantStatisticsList();
144 List<ParticipantStatistics> participantStatistics = new ArrayList<>();
146 //Fetch all participantIds for a specific control loop
147 List<ToscaConceptIdentifier> participantIds = getAllParticipantIdsPerControlLoop(controlLoopName,
149 for (ToscaConceptIdentifier id: participantIds) {
150 participantStatistics.addAll(participantStatisticsProvider.getFilteredParticipantStatistics(
151 id.getName(), id.getVersion(), null, null, null, DESC_ORDER, 0));
153 statisticsList.setStatisticsList(participantStatistics);
154 } catch (PfModelException e) {
155 throw new PfModelRuntimeException(e);
157 return statisticsList;
163 * Get clElement statistics based on specific filters.
165 * @param name the name of the clElement statistics to get, null to get all statistics
166 * @param version the version of the clElement statistics to get, null to get all statistics
167 * @param id UUID of the control loop element
168 * @param startTime start of the timestamp, from statistics to be filtered
169 * @param endTime end of the timestamp up to which statistics to be filtered
170 * @param recordCount number of records to be fetched.
171 * @return the participant found
172 * @throws PfModelException on errors getting control loop statistics
174 public ClElementStatisticsList fetchFilteredClElementStatistics(@NonNull final String name, final String version,
175 final String id, Instant startTime, Instant endTime,
176 int recordCount) throws PfModelException {
177 ClElementStatisticsList 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(
184 name, version, startTime, endTime, filterMap, DESC_ORDER, recordCount));
186 return clElementStatisticsList;
191 * Get clElement statistics per control loop.
193 * @param name the name of the control loop
194 * @param version the version of the control loop
195 * @return the clElement statistics found
196 * @throws PfModelException on errors getting control loop statistics
198 public ClElementStatisticsList fetchClElementStatsPerControlLoop(@NonNull final String name,
199 @NonNull final String version)
200 throws PfModelException {
201 ClElementStatisticsList clElementStatisticsList = new ClElementStatisticsList();
202 List<ClElementStatistics> clElementStats = new ArrayList<>();
204 List<ControlLoopElement> clElements = new ArrayList<>();
205 //Fetch all control loop elements for the control loop
206 ControlLoop controlLoop = controlLoopProvider.getControlLoop(new ToscaConceptIdentifier(name,
208 if (controlLoop != null) {
209 clElements.addAll(controlLoop.getElements().values());
210 //Collect control loop element statistics for each cl element.
211 for (ControlLoopElement clElement : clElements) {
212 clElementStats.addAll(fetchFilteredClElementStatistics(clElement.getParticipantId().getName(),
213 clElement.getParticipantId().getVersion(), clElement.getId().toString(), null,
214 null, 0).getClElementStatistics());
217 clElementStatisticsList.setClElementStatistics(clElementStats);
218 } catch (PfModelException e) {
219 throw new PfModelRuntimeException(e);
221 return clElementStatisticsList;
225 * If required, REST end point can be defined for this method to fetch associated participant Ids
226 * for a control loop.
228 * @param name the name of the control loop
229 * @param version the version of the control loop
230 * @return List of participant Id
231 * @throws PfModelException on errors
233 public List<ToscaConceptIdentifier> getAllParticipantIdsPerControlLoop(String name, String version)
234 throws PfModelException {
235 List<ToscaConceptIdentifier> participantIds = new ArrayList<>();
236 ControlLoop controlLoop = controlLoopProvider.getControlLoop(new ToscaConceptIdentifier(name, version));
237 if (controlLoop != null) {
238 for (ControlLoopElement clElement : controlLoop.getElements().values()) {
239 participantIds.add(clElement.getParticipantId());
242 return participantIds;
246 * If required, REST end point can be defined for this method to fetch associated control loop element Ids
247 * for a control loop.
249 * @param name the name of the control loop
250 * @param version the version of the control loop
251 * @return Map of control loop Id and participant details
252 * @throws PfModelException on errors
254 public Map<String, ToscaConceptIdentifier> getAllClElementsIdPerControlLoop(String name, String version)
255 throws PfModelException {
256 Map<String, ToscaConceptIdentifier> clElementId = new HashMap<>();
257 ControlLoop controlLoop = controlLoopProvider.getControlLoop(new ToscaConceptIdentifier(name, version));
258 if (controlLoop != null) {
259 for (ControlLoopElement clElement : controlLoop.getElements().values()) {
260 clElementId.put(clElement.getId().toString(), clElement.getParticipantId());
266 public void updateClElementStatistics(List<ClElementStatistics> clElementStatistics) {
267 // TODO Auto-generated method stub
270 public void updateParticipantStatistics(List<ParticipantStatistics> statisticsList) {
271 // TODO Auto-generated method stub