a685058773a86b0204c22da39741d013cc2950ac
[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.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;
44
45 /**
46  * This class provides information about statistics data of CL elements and CL Participants in database to callers.
47  */
48 @Component
49 public class MonitoringProvider implements Closeable {
50
51     private static final String DESC_ORDER = "DESC";
52     private final ParticipantStatisticsProvider participantStatisticsProvider;
53     private final ClElementStatisticsProvider clElementStatisticsProvider;
54     private final ControlLoopProvider controlLoopProvider;
55
56     /**
57      * Create a Monitoring provider.
58      *
59      * @param controlLoopParameters the parameters for access to the database
60      * @throws PfModelRuntimeException on errors creating the provider
61      */
62     public MonitoringProvider(ClRuntimeParameterGroup controlLoopParameters) {
63
64         try {
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);
72         }
73     }
74
75     @Override
76     public void close() throws IOException {
77         controlLoopProvider.close();
78         clElementStatisticsProvider.close();
79         participantStatisticsProvider.close();
80     }
81
82     /**
83      * Create participant statistics.
84      *
85      * @param participantStatistics the participant statistics
86      * @return the result of create operation
87      * @throws PfModelException on creation errors
88      */
89     public ParticipantStatisticsList createParticipantStatistics(List<ParticipantStatistics> participantStatistics)
90             throws PfModelException {
91         var participantStatisticsList = new ParticipantStatisticsList();
92         participantStatisticsList
93                 .setStatisticsList(participantStatisticsProvider.createParticipantStatistics(participantStatistics));
94
95         return participantStatisticsList;
96     }
97
98     /**
99      * Create clElement statistics.
100      *
101      * @param clElementStatisticsList the clElement statistics
102      * @return the result of create operation
103      * @throws PfModelException on creation errors
104      */
105     public ClElementStatisticsList createClElementStatistics(List<ClElementStatistics> clElementStatisticsList)
106             throws PfModelException {
107         var elementStatisticsList = new ClElementStatisticsList();
108         elementStatisticsList
109                 .setClElementStatistics(clElementStatisticsProvider.createClElementStatistics(clElementStatisticsList));
110
111         return elementStatisticsList;
112     }
113
114     /**
115      * Get participant statistics based on specific filters.
116      *
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
123      */
124     public ParticipantStatisticsList fetchFilteredParticipantStatistics(@NonNull final String name,
125             final String version, int recordCount, Instant startTime, Instant endTime) {
126         var participantStatisticsList = new ParticipantStatisticsList();
127
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));
132
133         return participantStatisticsList;
134     }
135
136     /**
137      * Get all participant statistics records found for a specific control loop. *
138      *
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
143      */
144     public ParticipantStatisticsList fetchParticipantStatsPerControlLoop(@NonNull final String controlLoopName,
145             @NonNull final String controlLoopVersion) {
146         var statisticsList = new ParticipantStatisticsList();
147         List<ParticipantStatistics> participantStatistics = new ArrayList<>();
148         try {
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));
155             }
156             statisticsList.setStatisticsList(participantStatistics);
157         } catch (PfModelException e) {
158             throw new PfModelRuntimeException(e);
159         }
160         return statisticsList;
161     }
162
163     /**
164      * Get clElement statistics based on specific filters.
165      *
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
174      */
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
180         if (id != null) {
181             filterMap.put("localName", id);
182         }
183         clElementStatisticsList.setClElementStatistics(clElementStatisticsProvider.getFilteredClElementStatistics(name,
184                 version, startTime, endTime, filterMap, DESC_ORDER, recordCount));
185
186         return clElementStatisticsList;
187     }
188
189     /**
190      * Get clElement statistics per control loop.
191      *
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
196      */
197     public ClElementStatisticsList fetchClElementStatsPerControlLoop(@NonNull final String name,
198             @NonNull final String version) {
199         var clElementStatisticsList = new ClElementStatisticsList();
200         List<ClElementStatistics> clElementStats = new ArrayList<>();
201         try {
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());
212                 }
213             }
214             clElementStatisticsList.setClElementStatistics(clElementStats);
215         } catch (PfModelException e) {
216             throw new PfModelRuntimeException(e);
217         }
218         return clElementStatisticsList;
219     }
220
221     /**
222      * If required, REST end point can be defined for this method to fetch associated participant Ids
223      * for a control loop.
224      *
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
229      */
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());
237             }
238         }
239         return participantIds;
240     }
241
242     /**
243      * If required, REST end point can be defined for this method to fetch associated control loop element Ids
244      * for a control loop.
245      *
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
250      */
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());
258             }
259         }
260         return clElementId;
261     }
262
263     public void updateClElementStatistics(List<ClElementStatistics> clElementStatistics) {
264         // TODO Auto-generated method stub
265     }
266
267     public void updateParticipantStatistics(List<ParticipantStatistics> statisticsList) {
268         // TODO Auto-generated method stub
269     }
270 }