cd9309422b0a9cf9bda7988b5988979e9d6db3ba
[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.time.Instant;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import lombok.AllArgsConstructor;
29 import lombok.NonNull;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
31 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatisticsList;
32 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ControlLoopElement;
33 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics;
34 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatisticsList;
35 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ClElementStatisticsProvider;
36 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ControlLoopProvider;
37 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider.ParticipantStatisticsProvider;
38 import org.onap.policy.models.base.PfModelException;
39 import org.onap.policy.models.base.PfModelRuntimeException;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41 import org.springframework.stereotype.Component;
42
43 /**
44  * This class provides information about statistics data of CL elements and CL Participants in database to callers.
45  */
46 @Component
47 @AllArgsConstructor
48 public class MonitoringProvider {
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 participant statistics.
57      *
58      * @param participantStatistics the participant statistics
59      * @return the result of create operation
60      * @throws PfModelException on creation errors
61      */
62     public ParticipantStatisticsList createParticipantStatistics(List<ParticipantStatistics> participantStatistics)
63             throws PfModelException {
64         var participantStatisticsList = new ParticipantStatisticsList();
65         participantStatisticsList
66                 .setStatisticsList(participantStatisticsProvider.createParticipantStatistics(participantStatistics));
67
68         return participantStatisticsList;
69     }
70
71     /**
72      * Create clElement statistics.
73      *
74      * @param clElementStatisticsList the clElement statistics
75      * @return the result of create operation
76      * @throws PfModelException on creation errors
77      */
78     public ClElementStatisticsList createClElementStatistics(List<ClElementStatistics> clElementStatisticsList)
79             throws PfModelException {
80         var elementStatisticsList = new ClElementStatisticsList();
81         elementStatisticsList
82                 .setClElementStatistics(clElementStatisticsProvider.createClElementStatistics(clElementStatisticsList));
83
84         return elementStatisticsList;
85     }
86
87     /**
88      * Get participant statistics based on specific filters.
89      *
90      * @param name the name of the participant statistics to get, null to get all statistics
91      * @param version the version of the participant statistics to get, null to get all statistics
92      * @param recordCount number of records to be fetched.
93      * @param startTime start of the timestamp, from statistics to be filtered
94      * @param endTime end of the timestamp up to which statistics to be filtered
95      * @return the participant found
96      */
97     public ParticipantStatisticsList fetchFilteredParticipantStatistics(@NonNull final String name,
98             final String version, int recordCount, Instant startTime, Instant endTime) {
99         var participantStatisticsList = new ParticipantStatisticsList();
100
101         // Additional parameters can be added in filterMap for filtering data.
102         Map<String, Object> filterMap = null;
103         participantStatisticsList.setStatisticsList(participantStatisticsProvider.getFilteredParticipantStatistics(name,
104                 version, startTime, endTime, filterMap, DESC_ORDER, recordCount));
105
106         return participantStatisticsList;
107     }
108
109     /**
110      * Get all participant statistics records found for a specific control loop. *
111      *
112      * @param controlLoopName name of the control loop
113      * @param controlLoopVersion version of the control loop
114      * @return All the participant statistics found
115      * @throws PfModelRuntimeException on errors getting participant statistics
116      */
117     public ParticipantStatisticsList fetchParticipantStatsPerControlLoop(@NonNull final String controlLoopName,
118             @NonNull final String controlLoopVersion) {
119         var statisticsList = new ParticipantStatisticsList();
120         List<ParticipantStatistics> participantStatistics = new ArrayList<>();
121         try {
122             // Fetch all participantIds for a specific control loop
123             List<ToscaConceptIdentifier> participantIds =
124                     getAllParticipantIdsPerControlLoop(controlLoopName, controlLoopVersion);
125             for (ToscaConceptIdentifier id : participantIds) {
126                 participantStatistics.addAll(participantStatisticsProvider.getFilteredParticipantStatistics(
127                         id.getName(), id.getVersion(), null, null, null, DESC_ORDER, 0));
128             }
129             statisticsList.setStatisticsList(participantStatistics);
130         } catch (PfModelException e) {
131             throw new PfModelRuntimeException(e);
132         }
133         return statisticsList;
134     }
135
136     /**
137      * Get clElement statistics based on specific filters.
138      *
139      * @param name the name of the clElement statistics to get, null to get all statistics
140      * @param version the version of the clElement statistics to get, null to get all statistics
141      * @param id UUID of the control loop element
142      * @param startTime start of the timestamp, from statistics to be filtered
143      * @param endTime end of the timestamp up to which statistics to be filtered
144      * @param recordCount number of records to be fetched.
145      * @return the participant found
146      * @throws PfModelException on errors getting control loop statistics
147      */
148     public ClElementStatisticsList fetchFilteredClElementStatistics(@NonNull final String name, final String version,
149             final String id, Instant startTime, Instant endTime, int recordCount) throws PfModelException {
150         var clElementStatisticsList = new ClElementStatisticsList();
151         Map<String, Object> filterMap = new HashMap<>();
152         // Adding UUID in filter if present
153         if (id != null) {
154             filterMap.put("localName", id);
155         }
156         clElementStatisticsList.setClElementStatistics(clElementStatisticsProvider.getFilteredClElementStatistics(name,
157                 version, startTime, endTime, filterMap, DESC_ORDER, recordCount));
158
159         return clElementStatisticsList;
160     }
161
162     /**
163      * Get clElement statistics per control loop.
164      *
165      * @param name the name of the control loop
166      * @param version the version of the control loop
167      * @return the clElement statistics found
168      * @throws PfModelRuntimeException on errors getting control loop statistics
169      */
170     public ClElementStatisticsList fetchClElementStatsPerControlLoop(@NonNull final String name,
171             @NonNull final String version) {
172         var clElementStatisticsList = new ClElementStatisticsList();
173         List<ClElementStatistics> clElementStats = new ArrayList<>();
174         try {
175             List<ControlLoopElement> clElements = new ArrayList<>();
176             // Fetch all control loop elements for the control loop
177             var controlLoopOpt = controlLoopProvider.findControlLoop(new ToscaConceptIdentifier(name, version));
178             if (controlLoopOpt.isPresent()) {
179                 clElements.addAll(controlLoopOpt.get().getElements().values());
180                 // Collect control loop element statistics for each cl element.
181                 for (ControlLoopElement clElement : clElements) {
182                     clElementStats.addAll(fetchFilteredClElementStatistics(clElement.getParticipantId().getName(),
183                             clElement.getParticipantId().getVersion(), clElement.getId().toString(), null, null, 0)
184                                     .getClElementStatistics());
185                 }
186             }
187             clElementStatisticsList.setClElementStatistics(clElementStats);
188         } catch (PfModelException e) {
189             throw new PfModelRuntimeException(e);
190         }
191         return clElementStatisticsList;
192     }
193
194     /**
195      * If required, REST end point can be defined for this method to fetch associated participant Ids
196      * for a control loop.
197      *
198      * @param name the name of the control loop
199      * @param version the version of the control loop
200      * @return List of participant Id
201      * @throws PfModelException on errors
202      */
203     public List<ToscaConceptIdentifier> getAllParticipantIdsPerControlLoop(String name, String version)
204             throws PfModelException {
205         List<ToscaConceptIdentifier> participantIds = new ArrayList<>();
206         var controlLoopOpt = controlLoopProvider.findControlLoop(new ToscaConceptIdentifier(name, version));
207         if (controlLoopOpt.isPresent()) {
208             for (ControlLoopElement clElement : controlLoopOpt.get().getElements().values()) {
209                 participantIds.add(clElement.getParticipantId());
210             }
211         }
212         return participantIds;
213     }
214
215     /**
216      * If required, REST end point can be defined for this method to fetch associated control loop element Ids
217      * for a control loop.
218      *
219      * @param name the name of the control loop
220      * @param version the version of the control loop
221      * @return Map of control loop Id and participant details
222      * @throws PfModelException on errors
223      */
224     public Map<String, ToscaConceptIdentifier> getAllClElementsIdPerControlLoop(String name, String version)
225             throws PfModelException {
226         Map<String, ToscaConceptIdentifier> clElementId = new HashMap<>();
227         var controlLoopOpt = controlLoopProvider.findControlLoop(new ToscaConceptIdentifier(name, version));
228         if (controlLoopOpt.isPresent()) {
229             for (ControlLoopElement clElement : controlLoopOpt.get().getElements().values()) {
230                 clElementId.put(clElement.getId().toString(), clElement.getParticipantId());
231             }
232         }
233         return clElementId;
234     }
235 }