aeabce7a5a9af3138c503a73eb59ad98b085bb31
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.runtime.monitoring;
22
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;
29 import java.util.Map;
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;
44
45 /**
46  * This class provides information about statistics data of CL elements and CL Participants in database to callers.
47  */
48 public class MonitoringProvider implements Closeable {
49
50     private static final String DESC_ORDER = "DESC";
51     private final ParticipantStatisticsProvider participantStatisticsProvider;
52     private final ClElementStatisticsProvider clElementStatisticsProvider;
53     private final ControlLoopProvider controlLoopProvider;
54
55     /**
56      * Create a Monitoring provider.
57      *
58      */
59     public MonitoringProvider(PolicyModelsProviderParameters parameters) {
60
61         try {
62             participantStatisticsProvider = new ParticipantStatisticsProvider(parameters);
63             clElementStatisticsProvider = new ClElementStatisticsProvider(parameters);
64             controlLoopProvider = new ControlLoopProvider(parameters);
65         } catch (PfModelException e) {
66             throw new PfModelRuntimeException(e);
67         }
68     }
69
70     @Override
71     public void close() throws IOException {
72         controlLoopProvider.close();
73         clElementStatisticsProvider.close();
74         participantStatisticsProvider.close();
75     }
76
77     /**
78      * Create participant statistics.
79      *
80      * @param participantStatistics the participant statistics
81      * @return the result of create operation
82      * @throws PfModelException on creation errors
83      */
84     public ParticipantStatisticsList createParticipantStatistics(List<ParticipantStatistics> participantStatistics)
85         throws PfModelException {
86         ParticipantStatisticsList participantStatisticsList = new ParticipantStatisticsList();
87         participantStatisticsList.setStatisticsList(participantStatisticsProvider
88             .createParticipantStatistics(participantStatistics));
89
90         return participantStatisticsList;
91     }
92
93     /**
94      * Create clElement statistics.
95      *
96      * @param clElementStatisticsList the clElement statistics
97      * @return the result of create operation
98      * @throws PfModelException on creation errors
99      */
100     public ClElementStatisticsList createClElementStatistics(List<ClElementStatistics> clElementStatisticsList)
101         throws PfModelException {
102         ClElementStatisticsList elementStatisticsList = new ClElementStatisticsList();
103         elementStatisticsList.setClElementStatistics(clElementStatisticsProvider
104             .createClElementStatistics(clElementStatisticsList));
105
106         return elementStatisticsList;
107     }
108
109     /**
110      * Get participant statistics based on specific filters.
111      *
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
118      */
119     public ParticipantStatisticsList fetchFilteredParticipantStatistics(@NonNull final String name,
120                                                                         final String version, int recordCount,
121                                                                         Instant startTime, Instant endTime)  {
122         ParticipantStatisticsList participantStatisticsList = new ParticipantStatisticsList();
123
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));
128
129         return participantStatisticsList;
130     }
131
132     /**
133      * Get all participant statistics records found for a specific control loop.     *
134      *
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
139      */
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<>();
145         try {
146             //Fetch all participantIds for a specific control loop
147             List<ToscaConceptIdentifier> participantIds = getAllParticipantIdsPerControlLoop(controlLoopName,
148                 controlLoopVersion);
149             for (ToscaConceptIdentifier id: participantIds) {
150                 participantStatistics.addAll(participantStatisticsProvider.getFilteredParticipantStatistics(
151                     id.getName(), id.getVersion(), null, null, null, DESC_ORDER, 0));
152             }
153             statisticsList.setStatisticsList(participantStatistics);
154         } catch (PfModelException e) {
155             throw new PfModelRuntimeException(e);
156         }
157         return statisticsList;
158     }
159
160
161
162     /**
163      * Get clElement statistics based on specific filters.
164      *
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
173      */
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
180         if (id != null) {
181             filterMap.put("localName", id);
182         }
183         clElementStatisticsList.setClElementStatistics(clElementStatisticsProvider.getFilteredClElementStatistics(
184             name, version, startTime, endTime, filterMap, DESC_ORDER, recordCount));
185
186         return clElementStatisticsList;
187     }
188
189
190     /**
191      * Get clElement statistics per control loop.
192      *
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
197      */
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<>();
203         try {
204             List<ControlLoopElement> clElements = new ArrayList<>();
205             //Fetch all control loop elements for the control loop
206             ControlLoop controlLoop = controlLoopProvider.getControlLoop(new ToscaConceptIdentifier(name,
207                 version));
208             clElements.addAll(controlLoop.getElements());
209
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());
215             }
216             clElementStatisticsList.setClElementStatistics(clElementStats);
217         } catch (PfModelException e) {
218             throw new PfModelRuntimeException(e);
219         }
220
221         return clElementStatisticsList;
222     }
223
224     /**
225      * If required, REST end point can be defined for this method to fetch associated participant Ids
226      * for a control loop.
227      *
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
232      */
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()) {
239                 participantIds.add(clElement.getParticipantId());
240             }
241         }
242         return participantIds;
243     }
244
245     /**
246      * If required, REST end point can be defined for this method to fetch associated control loop element Ids
247      * for a control loop.
248      *
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
253      */
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         for (ControlLoopElement clElement : controlLoop.getElements()) {
259             clElementId.put(clElement.getId().toString(), clElement.getParticipantId());
260         }
261         return clElementId;
262     }
263
264
265
266     public void updateClElementStatistics(List<ClElementStatistics> clElementStatistics) {
267         // TODO Auto-generated method stub
268     }
269
270     public void updateParticipantStatistics(List<ParticipantStatistics> statisticsList) {
271         // TODO Auto-generated method stub
272     }
273 }