summaryrefslogtreecommitdiffstats
path: root/models/src/main/java
diff options
context:
space:
mode:
authorFrancescoFioraEst <francesco.fiora@est.tech>2021-11-02 16:08:46 +0000
committerFrancescoFioraEst <francesco.fiora@est.tech>2021-11-10 12:58:26 +0000
commita88c9a232d486865ce007886daff56392ebf2d7b (patch)
treef3531235a4b12dfd48a8199f2b841126ffcf36e1 /models/src/main/java
parente920b54e30c58f117d0c021ca82301900c9d5541 (diff)
Add support transaction in Statistics Providers
Add support transaction in ClElementStatisticsProvider and ParticipantStatisticsProvider Issue-ID: POLICY-3801 Change-Id: Iaf9d55a268627f9d548afdf108476441b19e1413 Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
Diffstat (limited to 'models/src/main/java')
-rw-r--r--models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ClElementStatisticsProvider.java69
-rw-r--r--models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ParticipantStatisticsProvider.java61
-rw-r--r--models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/ClElementStatisticsRepository.java32
-rw-r--r--models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/FilterRepository.java53
-rw-r--r--models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/FilterRepositoryImpl.java51
-rw-r--r--models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/ParticipantStatisticsRepository.java32
6 files changed, 219 insertions, 79 deletions
diff --git a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ClElementStatisticsProvider.java b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ClElementStatisticsProvider.java
index e5b062bc1..bdb0d4d9b 100644
--- a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ClElementStatisticsProvider.java
+++ b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ClElementStatisticsProvider.java
@@ -21,19 +21,20 @@
package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider;
import java.time.Instant;
-import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
+import javax.ws.rs.core.Response.Status;
+import lombok.AllArgsConstructor;
import lombok.NonNull;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ClElementStatistics;
import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaClElementStatistics;
+import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ClElementStatisticsRepository;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.base.PfReferenceTimestampKey;
import org.onap.policy.models.dao.PfFilterParameters;
-import org.onap.policy.models.provider.PolicyModelsProviderParameters;
-import org.onap.policy.models.provider.impl.AbstractModelsProvider;
import org.springframework.stereotype.Component;
+import org.springframework.transaction.annotation.Transactional;
/**
* This class provides the provision of information on control loop element statistics in the database to callers.
@@ -41,46 +42,34 @@ import org.springframework.stereotype.Component;
* @author Ramesh Murugan Iyer (ramesh.murugan.iyer@est.tech)
*/
@Component
-public class ClElementStatisticsProvider extends AbstractModelsProvider {
+@Transactional
+@AllArgsConstructor
+public class ClElementStatisticsProvider {
- /**
- * Create a provider for control loop element statistics.
- *
- * @param parameters the parameters for database access
- * @throws PfModelException on initiation errors
- */
- public ClElementStatisticsProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException {
- super(parameters);
- this.init();
- }
+ private ClElementStatisticsRepository clElementStatisticsRepository;
/**
* Creates control loop element statistics.
*
* @param clElementStatisticsList a specification of the CL element statistics to create
* @return the clElement statistics created
- * @throws PfModelException on errors creating clElement statistics
+ * @throws PfModelException on initiation errors
*/
public List<ClElementStatistics> createClElementStatistics(
@NonNull final List<ClElementStatistics> clElementStatisticsList) throws PfModelException {
- List<JpaClElementStatistics> jpaClElementStatisticsList = ProviderUtils.getJpaAndValidate(
- clElementStatisticsList, JpaClElementStatistics::new, "control loop element statistics");
+ try {
+ var jpaClElementStatisticsList = ProviderUtils.getJpaAndValidate(clElementStatisticsList,
+ JpaClElementStatistics::new, "control loop element statistics");
- jpaClElementStatisticsList.forEach(jpaClElementStatistics -> getPfDao().create(jpaClElementStatistics));
+ var jpaClElementStatisticsSaved = clElementStatisticsRepository.saveAll(jpaClElementStatisticsList);
- // Return the created control loop element statistics
- List<ClElementStatistics> elementStatistics = new ArrayList<>(clElementStatisticsList.size());
-
- for (ClElementStatistics clElementStat : clElementStatisticsList) {
- var jpaClElementStatistics = getPfDao().get(JpaClElementStatistics.class,
- new PfReferenceTimestampKey(clElementStat.getParticipantId().getName(),
- clElementStat.getParticipantId().getVersion(), clElementStat.getId().toString(),
- clElementStat.getTimeStamp()));
- elementStatistics.add(jpaClElementStatistics.toAuthorative());
+ // Return the saved control loop element statistics
+ return asClElementStatisticsList(jpaClElementStatisticsSaved);
+ } catch (IllegalArgumentException e) {
+ throw new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error in save control loop element statistics",
+ e);
}
-
- return elementStatistics;
}
/**
@@ -101,22 +90,17 @@ public class ClElementStatisticsProvider extends AbstractModelsProvider {
* @param id of the control loop element
* @param timestamp timestamp of the statistics
* @return the clElement statistics found
- * @throws PfModelException on errors getting clElement statistics
*/
+ @Transactional(readOnly = true)
public List<ClElementStatistics> getClElementStatistics(final String name, final String version, final String id,
- final Instant timestamp) throws PfModelException {
- List<ClElementStatistics> clElementStatistics = new ArrayList<>(1);
+ final Instant timestamp) {
if (name != null && version != null && timestamp != null && id != null) {
- clElementStatistics.add(getPfDao()
- .get(JpaClElementStatistics.class, new PfReferenceTimestampKey(name, version, id, timestamp))
- .toAuthorative());
- return clElementStatistics;
+ return asClElementStatisticsList(clElementStatisticsRepository
+ .findAllById(List.of(new PfReferenceTimestampKey(name, version, id, timestamp))));
} else if (name != null) {
- clElementStatistics.addAll(getFilteredClElementStatistics(name, version, null, null, null, "DESC", 0));
- } else {
- clElementStatistics.addAll(asClElementStatisticsList(getPfDao().getAll(JpaClElementStatistics.class)));
+ return getFilteredClElementStatistics(name, version, null, null, null, "DESC", 0);
}
- return clElementStatistics;
+ return asClElementStatisticsList(clElementStatisticsRepository.findAll());
}
/**
@@ -130,8 +114,8 @@ public class ClElementStatisticsProvider extends AbstractModelsProvider {
* @param getRecordNum Total query count from database
* @param filterMap the filters to apply to the get operation
* @return the clElement statistics found
- * @throws PfModelException on errors getting policies
*/
+ @Transactional(readOnly = true)
public List<ClElementStatistics> getFilteredClElementStatistics(final String name, final String version,
final Instant startTimeStamp, final Instant endTimeStamp, Map<String, Object> filterMap,
final String sortOrder, final int getRecordNum) {
@@ -148,6 +132,7 @@ public class ClElementStatisticsProvider extends AbstractModelsProvider {
.recordNum(getRecordNum)
.build();
// @formatter:on
- return asClElementStatisticsList(getPfDao().getFiltered(JpaClElementStatistics.class, filterParams));
+ return asClElementStatisticsList(
+ clElementStatisticsRepository.getFiltered(JpaClElementStatistics.class, filterParams));
}
}
diff --git a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ParticipantStatisticsProvider.java b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ParticipantStatisticsProvider.java
index fa27a41c2..06c7a11f8 100644
--- a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ParticipantStatisticsProvider.java
+++ b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ParticipantStatisticsProvider.java
@@ -21,36 +21,30 @@
package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider;
import java.time.Instant;
-import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
+import javax.ws.rs.core.Response.Status;
+import lombok.AllArgsConstructor;
import lombok.NonNull;
import org.onap.policy.clamp.controlloop.models.controlloop.concepts.ParticipantStatistics;
import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaParticipantStatistics;
+import org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository.ParticipantStatisticsRepository;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.base.PfTimestampKey;
import org.onap.policy.models.dao.PfFilterParameters;
-import org.onap.policy.models.provider.PolicyModelsProviderParameters;
-import org.onap.policy.models.provider.impl.AbstractModelsProvider;
import org.springframework.stereotype.Component;
+import org.springframework.transaction.annotation.Transactional;
/**
* This class provides the provision of information on participant statistics in the database to callers.
*/
@Component
-public class ParticipantStatisticsProvider extends AbstractModelsProvider {
+@Transactional
+@AllArgsConstructor
+public class ParticipantStatisticsProvider {
- /**
- * Create a provider for control loops statistics.
- *
- * @param parameters the parameters for database access
- * @throws PfModelException on initiation errors
- */
- public ParticipantStatisticsProvider(@NonNull PolicyModelsProviderParameters parameters) throws PfModelException {
- super(parameters);
- this.init();
- }
+ private ParticipantStatisticsRepository participantStatisticsRepository;
/**
* Get Participant statistics.
@@ -59,20 +53,18 @@ public class ParticipantStatisticsProvider extends AbstractModelsProvider {
* @param version the version of the participant statistics to get, null to get all stats for a name
* @param timestamp the time stamp for the stats to get
* @return the participant statistics found
- * @throws PfModelException on errors getting participant statistics
*/
+ @Transactional(readOnly = true)
public List<ParticipantStatistics> getParticipantStatistics(final String name, final String version,
- final Instant timestamp) throws PfModelException {
+ final Instant timestamp) {
if (name != null && version != null && timestamp != null) {
- List<ParticipantStatistics> participantStatistics = new ArrayList<>(1);
- participantStatistics.add(getPfDao()
- .get(JpaParticipantStatistics.class, new PfTimestampKey(name, version, timestamp)).toAuthorative());
- return participantStatistics;
+ return asParticipantStatisticsList(
+ participantStatisticsRepository.findAllById(List.of(new PfTimestampKey(name, version, timestamp))));
} else if (name != null) {
return getFilteredParticipantStatistics(name, version, timestamp, null, null, "DESC", 0);
} else {
- return asParticipantStatisticsList(getPfDao().getAll(JpaParticipantStatistics.class));
+ return asParticipantStatisticsList(participantStatisticsRepository.findAll());
}
}
@@ -87,8 +79,8 @@ public class ParticipantStatisticsProvider extends AbstractModelsProvider {
* @param getRecordNum Total query count from database
* @param filterMap the filters to apply to the get operation
* @return the participant statistics found
- * @throws PfModelException on errors getting policies
*/
+ @Transactional(readOnly = true)
public List<ParticipantStatistics> getFilteredParticipantStatistics(final String name, final String version,
final Instant startTimeStamp, final Instant endTimeStamp, Map<String, Object> filterMap,
final String sortOrder, final int getRecordNum) {
@@ -106,7 +98,8 @@ public class ParticipantStatisticsProvider extends AbstractModelsProvider {
.build();
// @formatter:on
- return asParticipantStatisticsList(getPfDao().getFiltered(JpaParticipantStatistics.class, filterParams));
+ return asParticipantStatisticsList(
+ participantStatisticsRepository.getFiltered(JpaParticipantStatistics.class, filterParams));
}
/**
@@ -119,23 +112,17 @@ public class ParticipantStatisticsProvider extends AbstractModelsProvider {
public List<ParticipantStatistics> createParticipantStatistics(
@NonNull final List<ParticipantStatistics> participantStatisticsList) throws PfModelException {
- List<JpaParticipantStatistics> jpaParticipantStatisticsList = ProviderUtils
- .getJpaAndValidate(participantStatisticsList, JpaParticipantStatistics::new, "Participant Statistics");
-
- jpaParticipantStatisticsList.forEach(jpaParticipantStatistics -> getPfDao().update(jpaParticipantStatistics));
+ try {
+ var jpaParticipantStatisticsList = ProviderUtils.getJpaAndValidate(participantStatisticsList,
+ JpaParticipantStatistics::new, "Participant Statistics");
- // Return the created participant statistics
- List<ParticipantStatistics> participantStatistics = new ArrayList<>(participantStatisticsList.size());
+ var jpaParticipantStatisticsSaved = participantStatisticsRepository.saveAll(jpaParticipantStatisticsList);
- for (ParticipantStatistics participantStatisticsItem : participantStatisticsList) {
- var jpaParticipantStatistics = getPfDao().get(JpaParticipantStatistics.class,
- new PfTimestampKey(participantStatisticsItem.getParticipantId().getName(),
- participantStatisticsItem.getParticipantId().getVersion(),
- participantStatisticsItem.getTimeStamp()));
- participantStatistics.add(jpaParticipantStatistics.toAuthorative());
+ // Return the saved participant statistics
+ return asParticipantStatisticsList(jpaParticipantStatisticsSaved);
+ } catch (IllegalArgumentException e) {
+ throw new PfModelException(Status.INTERNAL_SERVER_ERROR, "Error in save participant statistics", e);
}
-
- return participantStatistics;
}
/**
diff --git a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/ClElementStatisticsRepository.java b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/ClElementStatisticsRepository.java
new file mode 100644
index 000000000..1e07a4fe6
--- /dev/null
+++ b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/ClElementStatisticsRepository.java
@@ -0,0 +1,32 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2021 Nordix Foundation.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository;
+
+import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaClElementStatistics;
+import org.onap.policy.models.base.PfReferenceTimestampKey;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface ClElementStatisticsRepository
+ extends JpaRepository<JpaClElementStatistics, PfReferenceTimestampKey>, FilterRepository {
+
+}
diff --git a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/FilterRepository.java b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/FilterRepository.java
new file mode 100644
index 000000000..ce1f3d8c3
--- /dev/null
+++ b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/FilterRepository.java
@@ -0,0 +1,53 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2021 Nordix Foundation.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository;
+
+import java.util.List;
+import org.onap.policy.models.base.PfConcept;
+import org.onap.policy.models.dao.PfFilterParametersIntfc;
+
+public interface FilterRepository {
+
+ /**
+ * Get an object from the database, referred to by concept key.
+ *
+ * @param <T> the type of the object to get, a subclass of {@link PfConcept}
+ * @param someClass the class of the object to get, a subclass of {@link PfConcept}, if name is null, all concepts
+ * of type T are returned, if name is not null and version is null, all versions of that concept matching the
+ * name are returned.
+ * @param filterParams filter parameters
+ * @return the objects that was retrieved from the database
+ */
+ <T extends PfConcept> List<T> getFiltered(Class<T> someClass, PfFilterParametersIntfc filterParams);
+
+ /**
+ * Get an object from the database, referred to by concept key.
+ *
+ * @param <T> the type of the object to get, a subclass of {@link PfConcept}
+ * @param someClass the class of the object to get, a subclass of {@link PfConcept}, if name is null, all concepts
+ * of type T are returned, if name is not null and version is null, all versions of that concept matching the
+ * name are returned.
+ * @param name the name of the object to get, null returns all objects
+ * @param version the version the object to get, null returns all objects for a specified name
+ * @return the objects that was retrieved from the database
+ */
+ <T extends PfConcept> List<T> getFiltered(Class<T> someClass, String name, String version);
+}
diff --git a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/FilterRepositoryImpl.java b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/FilterRepositoryImpl.java
new file mode 100644
index 000000000..0dc8fc373
--- /dev/null
+++ b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/FilterRepositoryImpl.java
@@ -0,0 +1,51 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2021 Nordix Foundation.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository;
+
+import java.util.List;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import org.onap.policy.models.base.PfConcept;
+import org.onap.policy.models.dao.PfDao;
+import org.onap.policy.models.dao.PfFilterParametersIntfc;
+import org.onap.policy.models.dao.impl.ProxyDao;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public class FilterRepositoryImpl implements FilterRepository {
+
+ @PersistenceContext
+ private EntityManager entityManager;
+
+ protected PfDao getPfDao() {
+ return new ProxyDao(entityManager);
+ }
+
+ @Override
+ public <T extends PfConcept> List<T> getFiltered(Class<T> someClass, PfFilterParametersIntfc filterParams) {
+ return getPfDao().getFiltered(someClass, filterParams);
+ }
+
+ @Override
+ public <T extends PfConcept> List<T> getFiltered(Class<T> someClass, String name, String version) {
+ return getPfDao().getFiltered(someClass, name, version);
+ }
+}
diff --git a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/ParticipantStatisticsRepository.java b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/ParticipantStatisticsRepository.java
new file mode 100644
index 000000000..6fecd6143
--- /dev/null
+++ b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/repository/ParticipantStatisticsRepository.java
@@ -0,0 +1,32 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2021 Nordix Foundation.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.clamp.controlloop.models.controlloop.persistence.repository;
+
+import org.onap.policy.clamp.controlloop.models.controlloop.persistence.concepts.JpaParticipantStatistics;
+import org.onap.policy.models.base.PfTimestampKey;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface ParticipantStatisticsRepository
+ extends JpaRepository<JpaParticipantStatistics, PfTimestampKey>, FilterRepository {
+
+}