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.models.controlloop.persistence.provider;
23 import java.util.ArrayList;
24 import java.util.Date;
25 import java.util.List;
27 import java.util.stream.Collectors;
28 import javax.ws.rs.core.Response;
29 import lombok.NonNull;
30 import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
31 import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaClElementStatistics;
32 import org.onap.policy.common.parameters.BeanValidationResult;
33 import org.onap.policy.models.base.PfModelException;
34 import org.onap.policy.models.base.PfModelRuntimeException;
35 import org.onap.policy.models.base.PfTimestampKey;
36 import org.onap.policy.models.provider.PolicyModelsProviderParameters;
37 import org.onap.policy.models.provider.impl.AbstractModelsProvider;
40 * This class provides the provision of information on control loop element statistics in the database to callers.
42 * @author Ramesh Murugan Iyer (ramesh.murugan.iyer@est.tech)
44 public class ClElementStatisticsProvider extends AbstractModelsProvider {
47 * Create a provider for control loop element statistics.
49 * @param parameters the parameters for database access
50 * @throws PfModelException on initiation errors
52 public ClElementStatisticsProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException {
58 * Creates control loop element statistics.
60 * @param clElementStatisticsList a specification of the CL element statistics to create
61 * @return the clElement statistics created
62 * @throws PfModelException on errors creating clElement statistics
64 public List<ClElementStatistics> createClElementStatistics(
65 @NonNull final List<ClElementStatistics> clElementStatisticsList) throws PfModelException {
67 BeanValidationResult validationResult =
68 new BeanValidationResult("control loop element statistics list", clElementStatisticsList);
69 for (ClElementStatistics clElementStatistics : clElementStatisticsList) {
70 JpaClElementStatistics jpaClElementStatistics = new JpaClElementStatistics();
71 jpaClElementStatistics.fromAuthorative(clElementStatistics);
73 validationResult.addResult(jpaClElementStatistics.validate("control loop element statistics"));
76 if (!validationResult.isValid()) {
77 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
80 for (ClElementStatistics clElementStatistics : clElementStatisticsList) {
81 JpaClElementStatistics jpaClElementStatistics = new JpaClElementStatistics();
82 jpaClElementStatistics.fromAuthorative(clElementStatistics);
84 getPfDao().create(jpaClElementStatistics);
87 // Return the created control loop element statistics
88 List<ClElementStatistics> elementStatistics = new ArrayList<>(clElementStatisticsList.size());
90 for (ClElementStatistics clElementStat : clElementStatisticsList) {
91 JpaClElementStatistics jpaClElementStatistics = getPfDao().get(JpaClElementStatistics.class,
92 new PfTimestampKey(clElementStat.getControlLoopElementId().getName(),
93 clElementStat.getControlLoopElementId().getVersion(), clElementStat.getTimeStamp()));
94 elementStatistics.add(jpaClElementStatistics.toAuthorative());
97 return elementStatistics;
101 * Convert JPA clElement statistics list to clElement statistics list.
103 * @param jpaClElementStatistics the list to convert
104 * @return the clElement statistics list
106 private List<ClElementStatistics> asClElementStatisticsList(List<JpaClElementStatistics> jpaClElementStatistics) {
107 return jpaClElementStatistics.stream().map(JpaClElementStatistics::toAuthorative).collect(Collectors.toList());
111 * Get clElement statistics.
113 * @param name the name of the clElement statistics to get, null to get all stats
114 * @return the clElement statistics found
115 * @throws PfModelException on errors getting clElement statistics
117 public List<ClElementStatistics> getClElementStatistics(final String name, final String version,
118 final Date timestamp) throws PfModelException {
120 if (name != null && version != null && timestamp != null) {
121 List<ClElementStatistics> clElementStatistics = new ArrayList<>(1);
122 clElementStatistics.add(getPfDao()
123 .get(JpaClElementStatistics.class, new PfTimestampKey(name, version, timestamp)).toAuthorative());
124 return clElementStatistics;
126 return asClElementStatisticsList(getPfDao().getAll(JpaClElementStatistics.class));
131 * Get filtered clElement statistics.
133 * @param name the clElement name for the statistics to get
134 * @param startTimeStamp startTimeStamp to filter statistics
135 * @param endTimeStamp endTimeStamp to filter statistics
136 * @param sortOrder sortOrder to query database
137 * @param getRecordNum Total query count from database
138 * @return the clElement statistics found
139 * @throws PfModelException on errors getting policies
141 public List<ClElementStatistics> getFilteredClElementStatistics(final String name, final String version,
142 final Date startTimeStamp, final Date endTimeStamp, Map<String, Object> filterMap, final String sortOrder,
143 final int getRecordNum) {
144 return asClElementStatisticsList(getPfDao().getFiltered(JpaClElementStatistics.class, name, version,
145 startTimeStamp, endTimeStamp, filterMap, sortOrder, getRecordNum));