diff options
author | Liam Fallon <liam.fallon@est.tech> | 2019-12-23 20:18:31 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-12-23 20:18:31 +0000 |
commit | 4fab7a327737ebdfaf7b55c39c49f0239ff627c8 (patch) | |
tree | 9e43171537ecc447cd03551c501d0b8bc5ad646a | |
parent | a70dbf38827f6c3b50f35c947faa81985711e242 (diff) | |
parent | 1c9d2d26b1f34567dc6d3e9c51a4fb78c9fc13d4 (diff) |
Merge "pdp statistics database provider implementation"
9 files changed, 593 insertions, 150 deletions
diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java b/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java index e635085ff..692aa08bf 100644 --- a/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java +++ b/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java @@ -21,12 +21,14 @@ package org.onap.policy.models.dao; import java.util.Collection; +import java.util.Date; import java.util.List; - +import java.util.Map; import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfTimestampKey; /** * The Interface PfDao describes the DAO interface for reading and writing Policy Framework {@link PfConcept} concepts @@ -82,6 +84,15 @@ public interface PfDao { <T extends PfConcept> void delete(Class<T> someClass, PfReferenceKey key); /** + * Delete an Policy Framework concept on the database. + * + * @param <T> the type of the object to delete, a subclass of {@link PfConcept} + * @param someClass the class of the object to delete, a subclass of {@link PfConcept} + * @param timeStampKey the PfTimestampKey of the object to delete + */ + <T extends PfConcept> void delete(Class<T> someClass, PfTimestampKey timeStampKey); + + /** * Create a collection of objects in the database. * * @param <T> the type of the object to create, a subclass of {@link PfConcept} @@ -142,8 +153,27 @@ public interface PfDao { * 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 + * @param startTime the start timeStamp to filter from database, filter rule: startTime <= filteredRecord timeStamp + * <= endTime. null for ignore start time. + * @param endTime the end timeStamp to filter from database, filter rule: startTime <= filteredRecord timeStamp <= + * endTime. null for ignore end time + * @filterMap Map store extra key/value used to filter from database, can be null. + * @return the objects that was retrieved from the database + */ + <T extends PfConcept> List<T> getFiltered(Class<T> someClass, String name, String version, Date startTime, + Date endTime, Map<String, Object> filterMap); + + /** + * 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} - * @param key the key of the object to get + * @param key the PfConceptKey of the object to get * @return the object that was retrieved from the database */ <T extends PfConcept> T get(Class<T> someClass, PfConceptKey key); @@ -153,12 +183,22 @@ public interface PfDao { * * @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} - * @param key the key of the object to get + * @param key the PfReferenceKey of the object to get * @return the object that was retrieved from the database or null if the object was not retrieved */ <T extends PfConcept> T get(Class<T> someClass, PfReferenceKey key); /** + * Get an object from the database, referred to by reference 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} + * @param timestampKey the PfTimestampKey of the object to get + * @return the object that was retrieved from the database or null if the object was not retrieved + */ + <T extends PfConcept> T get(Class<T> someClass, PfTimestampKey timestampKey); + + /** * Get all the objects in the database of a given type. * * @param <T> the type of the objects to get, a subclass of {@link PfConcept} diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java b/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java index 62b9c41a3..e98266706 100644 --- a/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java +++ b/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java @@ -23,18 +23,20 @@ package org.onap.policy.models.dao.impl; import java.util.Collection; import java.util.Collections; +import java.util.Date; import java.util.List; - +import java.util.Map; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; +import javax.persistence.TypedQuery; import javax.ws.rs.core.Response; - import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfTimestampKey; import org.onap.policy.models.base.PfUtils; import org.onap.policy.models.dao.DaoParameters; import org.onap.policy.models.dao.PfDao; @@ -51,6 +53,7 @@ public class DefaultPfDao implements PfDao { // @formatter:off private static final String NAME = "name"; private static final String VERSION = "version"; + private static final String TIMESTAMP = "timeStamp"; private static final String PARENT_NAME = "parentname"; private static final String PARENT_VERSION = "parentversion"; private static final String LOCAL_NAME = "localname"; @@ -64,11 +67,15 @@ public class DefaultPfDao implements PfDao { private static final String WHERE = " WHERE "; private static final String AND = " AND "; - private static final String NAME_FILTER = "c.key.name = :name"; - private static final String VERSION_FILTER = "c.key.version = :version"; - private static final String PARENT_NAME_FILTER = "c.key.parentKeyName = :parentname"; - private static final String PARENT_VERSION_FILTER = "c.key.parentKeyVersion = :parentversion"; - private static final String LOCAL_NAME_FILTER = "c.key.localName = :localname"; + private static final String NAME_FILTER = "c.key.name = :name"; + private static final String VERSION_FILTER = "c.key.version = :version"; + private static final String TIMESTAMP_START_FILTER = "c.key.timeStamp >= :startTime"; + private static final String TIMESTAMP_END_FILTER = "c.key.timeStamp <= :endTime"; + private static final String PARENT_NAME_FILTER = "c.key.parentKeyName = :parentname"; + private static final String PARENT_VERSION_FILTER = "c.key.parentKeyVersion = :parentversion"; + private static final String LOCAL_NAME_FILTER = "c.key.localName = :localname"; + + private static final String CLONE_ERR_MSG = "Could not clone object of class \"{}\""; private static final String DELETE_BY_CONCEPT_KEY = DELETE_FROM_TABLE + WHERE + NAME_FILTER + AND + VERSION_FILTER; @@ -206,6 +213,27 @@ public class DefaultPfDao implements PfDao { } @Override + public <T extends PfConcept> void delete(final Class<T> someClass, final PfTimestampKey key) { + if (key == null) { + return; + } + final EntityManager mg = getEntityManager(); + try { + // @formatter:off + mg.getTransaction().begin(); + mg.createQuery(setQueryTable(DELETE_BY_CONCEPT_KEY, someClass), someClass) + .setParameter(NAME, key.getName()) + .setParameter(VERSION, key.getVersion()) + .setParameter(TIMESTAMP, key.getTimeStamp()) + .executeUpdate(); + mg.getTransaction().commit(); + // @formatter:on + } finally { + mg.close(); + } + } + + @Override public <T extends PfConcept> void createCollection(final Collection<T> objs) { if (objs == null || objs.isEmpty()) { return; @@ -318,6 +346,52 @@ public class DefaultPfDao implements PfDao { } @Override + public <T extends PfConcept> List<T> getFiltered(final Class<T> someClass, final String name, final String version, + final Date startTime, final Date endTime, final Map<String, Object> filterMap) { + final EntityManager mg = getEntityManager(); + + String filterQueryString = SELECT_FROM_TABLE + WHERE; + + try { + if (filterMap != null) { + StringBuilder bld = new StringBuilder(filterQueryString); + for (String key : filterMap.keySet()) { + bld.append("c." + key + "= :" + key + AND); + } + filterQueryString = bld.toString(); + } + filterQueryString = addKeyFilterString(filterQueryString, name, startTime, endTime); + TypedQuery<T> query = mg.createQuery(setQueryTable(filterQueryString, someClass), someClass); + + if (filterMap != null) { + for (Map.Entry<String, Object> entry : filterMap.entrySet()) { + query.setParameter(entry.getKey(), entry.getValue()); + } + } + if (name != null) { + query.setParameter("name", name); + } + if (startTime != null) { + if (endTime != null) { + query.setParameter("startTime", startTime); + query.setParameter("endTime", endTime); + } else { + query.setParameter("startTime", startTime); + } + } else { + if (endTime != null) { + query.setParameter("endTime", endTime); + } + } + + LOGGER.error("filterQueryString is \"{}\"", filterQueryString); + return query.getResultList(); + } finally { + mg.close(); + } + } + + @Override public <T extends PfConcept> T get(final Class<T> someClass, final PfConceptKey key) { if (someClass == null) { return null; @@ -325,17 +399,7 @@ public class DefaultPfDao implements PfDao { final EntityManager mg = getEntityManager(); try { final T t = mg.find(someClass, key); - if (t != null) { - // This clone is created to force the JPA DAO to recurse down through the object - try { - return PfUtils.makeCopy(t); - } catch (final Exception e) { - LOGGER.warn("Could not clone object of class \"" + someClass.getName() + "\"", e); - return null; - } - } else { - return null; - } + return checkAndReturn(someClass, t); } finally { mg.close(); } @@ -349,16 +413,21 @@ public class DefaultPfDao implements PfDao { final EntityManager mg = getEntityManager(); try { final T t = mg.find(someClass, key); - if (t != null) { - try { - return PfUtils.makeCopy(t); - } catch (final Exception e) { - LOGGER.warn("Could not clone object of class \"" + someClass.getName() + "\"", e); - return null; - } - } else { - return null; - } + return checkAndReturn(someClass, t); + } finally { + mg.close(); + } + } + + @Override + public <T extends PfConcept> T get(final Class<T> someClass, final PfTimestampKey key) { + if (someClass == null) { + return null; + } + final EntityManager mg = getEntityManager(); + try { + final T t = mg.find(someClass, key); + return checkAndReturn(someClass, t); } finally { mg.close(); } @@ -515,4 +584,56 @@ public class DefaultPfDao implements PfDao { } return ret.get(0); } + + /** + * generate filter string with the filter value in TimestampKey. + * + * @param inputFilterString current filterString generated from FilterMap + * @param name the pdp name the start timeStamp to filter from database, filter rule: startTime <= filteredRecord + * timeStamp <= endTime. null for ignore start time. + * @param endTime the end timeStamp to filter from database, filter rule: startTime <= filteredRecord timeStamp <= + * endTime. null for ignore end time + * @return the filter string to query database + */ + private String addKeyFilterString(String inputFilterString, final String name, final Date startTime, + final Date endTime) { + String filterQueryString; + if (name != null) { + inputFilterString += NAME_FILTER + AND; + } + if (startTime != null) { + if (endTime != null) { + filterQueryString = inputFilterString + TIMESTAMP_START_FILTER + AND + TIMESTAMP_END_FILTER; + } else { + filterQueryString = inputFilterString + TIMESTAMP_START_FILTER; + } + } else { + if (endTime != null) { + filterQueryString = inputFilterString + TIMESTAMP_END_FILTER; + } else { + filterQueryString = inputFilterString.substring(0, inputFilterString.length() - AND.length()); + } + } + + return filterQueryString; + } + + /** + * check the result get from database and return the object. + * + * @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} + * @param t the object that was retrieved from the database + * @return the checked object or null + */ + private <T extends PfConcept> T checkAndReturn(final Class<T> someClass, final T objToCheck) { + if (objToCheck != null) { + try { + return PfUtils.makeCopy(objToCheck); + } catch (final Exception e) { + LOGGER.warn(CLONE_ERR_MSG, someClass.getName(), e); + } + } + return null; + } } diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProvider.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProvider.java new file mode 100644 index 000000000..f149a3602 --- /dev/null +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpStatisticsProvider.java @@ -0,0 +1,208 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Model + * ================================================================================ + * Copyright (C) 2019 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.models.pdp.persistence.provider; + +import java.util.ArrayList; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import javax.ws.rs.core.Response; +import lombok.NonNull; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.base.PfTimestampKey; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.dao.PfDao; +import org.onap.policy.models.pdp.concepts.PdpStatistics; +import org.onap.policy.models.pdp.persistence.concepts.JpaPdpStatistics; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * This class provides the provision of information on PAP concepts in the database to callers. + * + * @author Ning Xi (ning.xi@est.tech) + */ +public class PdpStatisticsProvider { + private static final Logger LOGGER = LoggerFactory.getLogger(PdpStatisticsProvider.class); + + // Recurring string constants + private static final String NOT_VALID = "\" is not valid \n"; + + /** + * Get PDP statistics. + * + * @param dao the DAO to use to access the database + * @param name the name of the PDP statistics to get, null to get all PDPs + * @return the PDP statistics found + * @throws PfModelException on errors getting PDP statistics + */ + public List<PdpStatistics> getPdpStatistics(@NonNull final PfDao dao, final String name, final Date timestamp) + throws PfModelException { + + List<PdpStatistics> pdpStatistics = new ArrayList<>(); + if (name != null) { + pdpStatistics + .add(dao.get(JpaPdpStatistics.class, new PfTimestampKey(name, PfKey.NULL_KEY_VERSION, timestamp)) + .toAuthorative()); + } else { + return asPdpStatisticsList(dao.getAll(JpaPdpStatistics.class)); + } + return pdpStatistics; + } + + /** + * Get filtered PDP statistics. + * + * @param dao the DAO to use to access the database + * @param name the pdpInstance name for the PDP statistics to get + * @param pdpGroupName pdpGroupName to filter statistics + * @param pdpSubGroup pdpSubGroupType name to filter statistics + * @param startTimeStamp startTimeStamp to filter statistics + * @param endTimeStamp endTimeStamp to filter statistics + * @return the PDP statistics found + * @throws PfModelException on errors getting policies + */ + public List<PdpStatistics> getFilteredPdpStatistics(@NonNull final PfDao dao, final String name, + @NonNull final String pdpGroupName, final String pdpSubGroup, final Date startTimeStamp, + final Date endTimeStamp) { + Map<String, Object> filterMap = new HashMap<>(); + filterMap.put("pdpGroupName", pdpGroupName); + if (pdpSubGroup != null) { + filterMap.put("pdpSubGroupName", pdpSubGroup); + } + + return asPdpStatisticsList(dao.getFiltered(JpaPdpStatistics.class, name, PfKey.NULL_KEY_VERSION, startTimeStamp, + endTimeStamp, filterMap)); + } + + /** + * Creates PDP statistics. + * + * @param dao the DAO to use to access the database + * @param pdpStatisticsList a specification of the PDP statistics to create + * @return the PDP statistics created + * @throws PfModelException on errors creating PDP statistics + */ + public List<PdpStatistics> createPdpStatistics(@NonNull final PfDao dao, + @NonNull final List<PdpStatistics> pdpStatisticsList) throws PfModelException { + + for (PdpStatistics pdpStatistics : pdpStatisticsList) { + JpaPdpStatistics jpaPdpStatistics = new JpaPdpStatistics(); + jpaPdpStatistics.fromAuthorative(pdpStatistics); + + PfValidationResult validationResult = jpaPdpStatistics.validate(new PfValidationResult()); + if (!validationResult.isOk()) { + String errorMessage = "pdp statictics \"" + jpaPdpStatistics.getName() + NOT_VALID + validationResult; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + + dao.create(jpaPdpStatistics); + } + + // Return the created PDP statistics + List<PdpStatistics> pdpStatistics = new ArrayList<>(pdpStatisticsList.size()); + + for (PdpStatistics pdpStatisticsItem : pdpStatisticsList) { + JpaPdpStatistics jpaPdpStatistics = + dao.get(JpaPdpStatistics.class, new PfTimestampKey(pdpStatisticsItem.getPdpInstanceId(), + PfKey.NULL_KEY_VERSION, pdpStatisticsItem.getTimeStamp())); + pdpStatistics.add(jpaPdpStatistics.toAuthorative()); + } + + return pdpStatistics; + } + + /** + * Updates PDP statistics. + * + * @param dao the DAO to use to access the database + * @param pdpStatisticsList a specification of the PDP statistics to update + * @return the PDP statistics updated + * @throws PfModelException on errors updating PDP statistics + */ + public List<PdpStatistics> updatePdpStatistics(@NonNull final PfDao dao, + @NonNull final List<PdpStatistics> pdpStatisticsList) throws PfModelException { + + for (PdpStatistics pdpStatistics : pdpStatisticsList) { + JpaPdpStatistics jpaPdpStatistics = new JpaPdpStatistics(); + jpaPdpStatistics.fromAuthorative(pdpStatistics); + + PfValidationResult validationResult = jpaPdpStatistics.validate(new PfValidationResult()); + if (!validationResult.isOk()) { + String errorMessage = "pdp statistics \"" + jpaPdpStatistics.getId() + NOT_VALID + validationResult; + LOGGER.warn(errorMessage); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); + } + + dao.update(jpaPdpStatistics); + } + + // Return the created PDP statistics + List<PdpStatistics> pdpStatistics = new ArrayList<>(pdpStatisticsList.size()); + + for (PdpStatistics pdpStatisticsItem : pdpStatisticsList) { + JpaPdpStatistics jpaPdpStatistics = + dao.get(JpaPdpStatistics.class, new PfTimestampKey(pdpStatisticsItem.getPdpInstanceId(), + PfKey.NULL_KEY_VERSION, pdpStatisticsItem.getTimeStamp())); + pdpStatistics.add(jpaPdpStatistics.toAuthorative()); + } + + return pdpStatistics; + } + + /** + * Delete a PDP statistics. + * + * @param dao the DAO to use to access the database + * @param name the name of the policy to get, null to get all PDP statistics + * @param timestamp the timeStamp of statistics to delete, null to delete all statistics record of given PDP + * @return the PDP statistics list deleted + * @throws PfModelException on errors deleting PDP statistics + */ + public List<PdpStatistics> deletePdpStatistics(@NonNull final PfDao dao, @NonNull final String name, + final Date timestamp) { + List<PdpStatistics> pdpStatisticsListToDel = asPdpStatisticsList( + dao.getFiltered(JpaPdpStatistics.class, name, PfKey.NULL_KEY_VERSION, timestamp, timestamp, null)); + + pdpStatisticsListToDel.stream().forEach(s -> dao.delete(JpaPdpStatistics.class, + new PfTimestampKey(s.getPdpInstanceId(), PfKey.NULL_KEY_VERSION, s.getTimeStamp()))); + + return pdpStatisticsListToDel; + } + + /** + * Convert JPA PDP statistics list to an PDP statistics list. + * + * @param jpaPdpStatisticsList the list to convert + * @return the PDP statistics list + */ + private List<PdpStatistics> asPdpStatisticsList(List<JpaPdpStatistics> jpaPdpStatisticsList) { + return jpaPdpStatisticsList.stream().map(JpaPdpStatistics::toAuthorative).collect(Collectors.toList()); + } +} diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProvider.java b/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProvider.java index bd28c3238..2e1c71426 100644 --- a/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProvider.java +++ b/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProvider.java @@ -20,11 +20,10 @@ package org.onap.policy.models.provider; +import java.util.Date; import java.util.List; import java.util.Map; - import lombok.NonNull; - import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.pdp.concepts.Pdp; import org.onap.policy.models.pdp.concepts.PdpGroup; @@ -354,17 +353,50 @@ public interface PolicyModelsProvider extends AutoCloseable { * @return the statistics found * @throws PfModelException on errors getting statistics */ - public List<PdpStatistics> getPdpStatistics(final String name) throws PfModelException; + public List<PdpStatistics> getPdpStatistics(final String name, final Date timestamp) throws PfModelException; + + + /** + * Get filtered PdpStatistics. + * + * @param name the pdpInstance name for the PDP statistics to get + * @param pdpGroupName pdpGroupName to filter statistics + * @param pdpSubGroup pdpSubGroupType name to filter statistics + * @param startTimeStamp startTimeStamp to filter statistics + * @param endTimeStamp endTimeStamp to filter statistics + * @return the PDP statistics found + * @throws PfModelException on errors getting policies + */ + public List<PdpStatistics> getFilteredPdpStatistics(String name, @NonNull String pdpGroupName, String pdpSubGroup, + Date startTimeStamp, Date endTimeStamp) throws PfModelException; + + /** + * Creates PDP statistics. + * + * @param pdpStatisticsList a specification of the PDP statistics to create + * @return the PDP statistics created + * @throws PfModelException on errors creating PDP statistics + */ + public List<PdpStatistics> createPdpStatistics(@NonNull List<PdpStatistics> pdpStatisticsList) + throws PfModelException; + + /** + * Updates PDP statistics. + * + * @param pdpStatisticsList a specification of the PDP statistics to update + * @return the PDP statistics updated + * @throws PfModelException on errors updating PDP statistics + */ + public List<PdpStatistics> updatePdpStatistics(@NonNull List<PdpStatistics> pdpStatisticsList) + throws PfModelException; /** - * Update PDP statistics for a PDP. + * Delete a PDP statistics. * - * @param pdpGroupName the name of the PDP group containing the PDP that the statistics are for - * @param pdpType the PDP type of the subgroup containing the PDP that the statistics are for - * @param pdpInstanceId the instance ID of the PDP to update statistics for - * @param pdpStatistics the PDP statistics - * @throws PfModelException on errors updating statistics + * @param name the name of the policy to get, null to get all PDP statistics + * @param timestamp the timestamp of statistics to delete, null to delete all statistics record of given pdp + * @return the PDP statistics deleted + * @throws PfModelException on errors deleting PDP statistics */ - public void updatePdpStatistics(@NonNull final String pdpGroupName, @NonNull final String pdpType, - @NonNull final String pdpInstanceId, @NonNull final PdpStatistics pdpStatistics) throws PfModelException; + public List<PdpStatistics> deletePdpStatistics(@NonNull String name, Date timestamp) throws PfModelException; } diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java index 9971454bd..1ac24585f 100644 --- a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java +++ b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java @@ -22,14 +22,12 @@ package org.onap.policy.models.provider.impl; import java.util.Base64; +import java.util.Date; import java.util.List; import java.util.Map; import java.util.Properties; - import javax.ws.rs.core.Response; - import lombok.NonNull; - import org.eclipse.persistence.config.PersistenceUnitProperties; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; @@ -43,6 +41,7 @@ import org.onap.policy.models.pdp.concepts.PdpGroupFilter; import org.onap.policy.models.pdp.concepts.PdpStatistics; import org.onap.policy.models.pdp.concepts.PdpSubGroup; import org.onap.policy.models.pdp.persistence.provider.PdpProvider; +import org.onap.policy.models.pdp.persistence.provider.PdpStatisticsProvider; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; @@ -140,71 +139,71 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { @Override public ToscaServiceTemplate getPolicyTypes(final String name, final String version) throws PfModelException { - assertInitilized(); + assertInitialized(); return new AuthorativeToscaProvider().getPolicyTypes(pfDao, name, version); } @Override public List<ToscaPolicyType> getPolicyTypeList(final String name, final String version) throws PfModelException { - assertInitilized(); + assertInitialized(); return new AuthorativeToscaProvider().getPolicyTypeList(pfDao, name, version); } @Override public ToscaServiceTemplate getFilteredPolicyTypes(@NonNull ToscaPolicyTypeFilter filter) throws PfModelException { - assertInitilized(); + assertInitialized(); return new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, filter); } @Override public List<ToscaPolicyType> getFilteredPolicyTypeList(@NonNull ToscaPolicyTypeFilter filter) throws PfModelException { - assertInitilized(); + assertInitialized(); return new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, filter); } @Override public ToscaServiceTemplate createPolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { - assertInitilized(); + assertInitialized(); return new AuthorativeToscaProvider().createPolicyTypes(pfDao, serviceTemplate); } @Override public ToscaServiceTemplate updatePolicyTypes(@NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { - assertInitilized(); + assertInitialized(); return new AuthorativeToscaProvider().updatePolicyTypes(pfDao, serviceTemplate); } @Override public ToscaServiceTemplate deletePolicyType(@NonNull final String name, @NonNull final String version) throws PfModelException { - assertInitilized(); + assertInitialized(); return new AuthorativeToscaProvider().deletePolicyType(pfDao, name, version); } @Override public ToscaServiceTemplate getPolicies(final String name, final String version) throws PfModelException { - assertInitilized(); + assertInitialized(); return new AuthorativeToscaProvider().getPolicies(pfDao, name, version); } @Override public List<ToscaPolicy> getPolicyList(final String name, final String version) throws PfModelException { - assertInitilized(); + assertInitialized(); return new AuthorativeToscaProvider().getPolicyList(pfDao, name, version); } @Override public ToscaServiceTemplate getFilteredPolicies(@NonNull ToscaPolicyFilter filter) throws PfModelException { - assertInitilized(); + assertInitialized(); return new AuthorativeToscaProvider().getFilteredPolicies(pfDao, filter); } @Override public List<ToscaPolicy> getFilteredPolicyList(@NonNull ToscaPolicyFilter filter) throws PfModelException { - assertInitilized(); + assertInitialized(); return new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, filter); } @@ -212,108 +211,109 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { @Override public ToscaServiceTemplate createPolicies(@NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { - assertInitilized(); + assertInitialized(); return new AuthorativeToscaProvider().createPolicies(pfDao, serviceTemplate); } @Override public ToscaServiceTemplate updatePolicies(@NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException { - assertInitilized(); + assertInitialized(); return new AuthorativeToscaProvider().updatePolicies(pfDao, serviceTemplate); } @Override public ToscaServiceTemplate deletePolicy(@NonNull final String name, @NonNull final String version) throws PfModelException { - assertInitilized(); + assertInitialized(); return new AuthorativeToscaProvider().deletePolicy(pfDao, name, version); } @Override public LegacyOperationalPolicy getOperationalPolicy(@NonNull final String policyId, final String policyVersion) throws PfModelException { - assertInitilized(); + assertInitialized(); return new LegacyProvider().getOperationalPolicy(pfDao, policyId, policyVersion); } @Override public LegacyOperationalPolicy createOperationalPolicy( @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException { - assertInitilized(); + assertInitialized(); return new LegacyProvider().createOperationalPolicy(pfDao, legacyOperationalPolicy); } @Override public LegacyOperationalPolicy updateOperationalPolicy( @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException { - assertInitilized(); + assertInitialized(); return new LegacyProvider().updateOperationalPolicy(pfDao, legacyOperationalPolicy); } @Override public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final String policyId, @NonNull final String policyVersion) throws PfModelException { - assertInitilized(); + assertInitialized(); return new LegacyProvider().deleteOperationalPolicy(pfDao, policyId, policyVersion); } @Override public Map<String, LegacyGuardPolicyOutput> getGuardPolicy(@NonNull final String policyId, final String policyVersion) throws PfModelException { - assertInitilized(); + assertInitialized(); return new LegacyProvider().getGuardPolicy(pfDao, policyId, policyVersion); } @Override public Map<String, LegacyGuardPolicyOutput> createGuardPolicy( @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException { - assertInitilized(); + assertInitialized(); return new LegacyProvider().createGuardPolicy(pfDao, legacyGuardPolicy); } @Override public Map<String, LegacyGuardPolicyOutput> updateGuardPolicy( @NonNull final LegacyGuardPolicyInput legacyGuardPolicy) throws PfModelException { - assertInitilized(); + assertInitialized(); return new LegacyProvider().updateGuardPolicy(pfDao, legacyGuardPolicy); } @Override public Map<String, LegacyGuardPolicyOutput> deleteGuardPolicy(@NonNull final String policyId, @NonNull final String policyVersion) throws PfModelException { - assertInitilized(); + assertInitialized(); return new LegacyProvider().deleteGuardPolicy(pfDao, policyId, policyVersion); } @Override public List<PdpGroup> getPdpGroups(final String name) throws PfModelException { - assertInitilized(); + assertInitialized(); return new PdpProvider().getPdpGroups(pfDao, name); } + @Override public List<PdpGroup> getFilteredPdpGroups(@NonNull PdpGroupFilter filter) throws PfModelException { - assertInitilized(); + assertInitialized(); return new PdpProvider().getFilteredPdpGroups(pfDao, filter); } @Override public List<PdpGroup> createPdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException { - assertInitilized(); + assertInitialized(); return new PdpProvider().createPdpGroups(pfDao, pdpGroups); } @Override public List<PdpGroup> updatePdpGroups(@NonNull final List<PdpGroup> pdpGroups) throws PfModelException { - assertInitilized(); + assertInitialized(); return new PdpProvider().updatePdpGroups(pfDao, pdpGroups); } @Override public void updatePdpSubGroup(@NonNull final String pdpGroupName, @NonNull final PdpSubGroup pdpSubGroup) throws PfModelException { - assertInitilized(); + assertInitialized(); new PdpProvider().updatePdpSubGroup(pfDao, pdpGroupName, pdpSubGroup); } @@ -325,27 +325,50 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { @Override public PdpGroup deletePdpGroup(@NonNull final String name) throws PfModelException { - assertInitilized(); + assertInitialized(); return new PdpProvider().deletePdpGroup(pfDao, name); } @Override - public List<PdpStatistics> getPdpStatistics(final String name) throws PfModelException { - assertInitilized(); - return new PdpProvider().getPdpStatistics(pfDao, name); + public List<PdpStatistics> getPdpStatistics(final String name, final Date timestamp) throws PfModelException { + assertInitialized(); + return new PdpStatisticsProvider().getPdpStatistics(pfDao, name, timestamp); } @Override - public void updatePdpStatistics(@NonNull final String pdpGroupName, @NonNull final String pdpType, - @NonNull final String pdpInstanceId, @NonNull final PdpStatistics pdpStatistics) throws PfModelException { - assertInitilized(); - new PdpProvider().updatePdpStatistics(pfDao, pdpGroupName, pdpType, pdpInstanceId, pdpStatistics); + public List<PdpStatistics> getFilteredPdpStatistics(final String name, @NonNull final String pdpGroupName, + final String pdpSubGroup, final Date startTimeStamp, final Date endTimeStamp) throws PfModelException { + assertInitialized(); + return new PdpStatisticsProvider().getFilteredPdpStatistics(pfDao, name, pdpGroupName, pdpSubGroup, + startTimeStamp, endTimeStamp); + } + + @Override + public List<PdpStatistics> createPdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList) + throws PfModelException { + assertInitialized(); + return new PdpStatisticsProvider().createPdpStatistics(pfDao, pdpStatisticsList); + } + + @Override + public List<PdpStatistics> updatePdpStatistics(@NonNull final List<PdpStatistics> pdpStatisticsList) + throws PfModelException { + assertInitialized(); + return new PdpStatisticsProvider().updatePdpStatistics(pfDao, pdpStatisticsList); + } + + + @Override + public List<PdpStatistics> deletePdpStatistics(@NonNull final String name, final Date timestamp) + throws PfModelException { + assertInitialized(); + return new PdpStatisticsProvider().deletePdpStatistics(pfDao, name, timestamp); } /** * Check if the model provider is initialized. */ - private void assertInitilized() { + private void assertInitialized() { if (pfDao == null) { String errorMessage = "policy models provider is not initilaized"; LOGGER.warn(errorMessage); diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java index acc000c61..2c88bcacd 100644 --- a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java +++ b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java @@ -22,12 +22,11 @@ package org.onap.policy.models.provider.impl; import java.util.ArrayList; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; - import javax.ws.rs.core.Response; - import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.base.PfModelException; @@ -229,14 +228,35 @@ public class DummyPolicyModelsProviderImpl implements PolicyModelsProvider { } @Override - public List<PdpStatistics> getPdpStatistics(final String name) throws PfModelException { + public List<PdpStatistics> getPdpStatistics(final String name, final Date timestamp) throws PfModelException { + return new ArrayList<>(); + } + + @Override + public List<PdpStatistics> getFilteredPdpStatistics(String name, String pdpGroupName, String pdpSubGroup, + Date startTimeStamp, Date endTimeStamp) { + // Not implemented + return new ArrayList<>(); + } + + @Override + public List<PdpStatistics> createPdpStatistics(final List<PdpStatistics> pdpStatisticsList) + throws PfModelException { + // Not implemented + return new ArrayList<>(); + } + + @Override + public List<PdpStatistics> updatePdpStatistics(final List<PdpStatistics> pdpStatisticsList) + throws PfModelException { + // Not implemented return new ArrayList<>(); } @Override - public void updatePdpStatistics(final String pdpGroupName, final String pdpType, final String pdpInstanceId, - final PdpStatistics pdppStatistics) throws PfModelException { + public List<PdpStatistics> deletePdpStatistics(final String name, final Date timestamp) { // Not implemented + return null; } /** diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java index d5c03a2cd..f8a3490cc 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java @@ -29,6 +29,7 @@ import static org.junit.Assert.fail; import java.util.ArrayList; import java.util.Base64; +import java.util.Date; import java.util.List; import org.junit.Before; @@ -82,6 +83,8 @@ public class DatabasePolicyModelsProviderTest { private static final String VERSION_100 = "1.0.0"; + private static final Date TIMESTAMP = new Date(); + private static final Logger LOGGER = LoggerFactory.getLogger(DatabasePolicyModelsProviderTest.class); PolicyModelsProviderParameters parameters; @@ -327,66 +330,21 @@ public class DatabasePolicyModelsProviderTest { }).hasMessage(NAME_IS_NULL); assertThatThrownBy(() -> { - databaseProvider.updatePdpStatistics(null, null, null, null); - }).hasMessage(GROUP_IS_NULL); - - assertThatThrownBy(() -> { - databaseProvider.updatePdpStatistics(null, null, null, new PdpStatistics()); - }).hasMessage(GROUP_IS_NULL); - - assertThatThrownBy(() -> { - databaseProvider.updatePdpStatistics(null, null, INSTANCE, null); - }).hasMessage(GROUP_IS_NULL); - - assertThatThrownBy(() -> { - databaseProvider.updatePdpStatistics(null, null, INSTANCE, new PdpStatistics()); - }).hasMessage(GROUP_IS_NULL); - - assertThatThrownBy(() -> { - databaseProvider.updatePdpStatistics(null, "type", null, null); - }).hasMessage(GROUP_IS_NULL); - - assertThatThrownBy(() -> { - databaseProvider.updatePdpStatistics(null, "type", null, new PdpStatistics()); - }).hasMessage(GROUP_IS_NULL); - - assertThatThrownBy(() -> { - databaseProvider.updatePdpStatistics(null, "type", INSTANCE, null); + databaseProvider.getFilteredPdpStatistics(NAME, null, "sub", TIMESTAMP, TIMESTAMP); }).hasMessage(GROUP_IS_NULL); assertThatThrownBy(() -> { - databaseProvider.updatePdpStatistics(null, "type", INSTANCE, new PdpStatistics()); - }).hasMessage(GROUP_IS_NULL); - - assertThatThrownBy(() -> { - databaseProvider.updatePdpStatistics(NAME, null, null, null); - }).hasMessage(PDP_TYPE_IS_NULL); - - assertThatThrownBy(() -> { - databaseProvider.updatePdpStatistics(NAME, null, null, new PdpStatistics()); - }).hasMessage(PDP_TYPE_IS_NULL); - - assertThatThrownBy(() -> { - databaseProvider.updatePdpStatistics(NAME, null, INSTANCE, null); - }).hasMessage(PDP_TYPE_IS_NULL); - - assertThatThrownBy(() -> { - databaseProvider.updatePdpStatistics(NAME, null, INSTANCE, new PdpStatistics()); - }).hasMessage(PDP_TYPE_IS_NULL); - - assertThatThrownBy(() -> { - databaseProvider.updatePdpStatistics(NAME, "type", null, null); - }).hasMessage("pdpInstanceId is marked @NonNull but is null"); + databaseProvider.createPdpStatistics(null); + }).hasMessage("pdpStatisticsList is marked @NonNull but is null"); assertThatThrownBy(() -> { - databaseProvider.updatePdpStatistics(NAME, "type", null, new PdpStatistics()); - }).hasMessage("pdpInstanceId is marked @NonNull but is null"); + databaseProvider.updatePdpStatistics(null); + }).hasMessage("pdpStatisticsList is marked @NonNull but is null"); assertThatThrownBy(() -> { - databaseProvider.updatePdpStatistics(NAME, "type", INSTANCE, null); - }).hasMessage("pdpStatistics is marked @NonNull but is null"); + databaseProvider.deletePdpStatistics(null, TIMESTAMP); + }).hasMessage(NAME_IS_NULL); - databaseProvider.updatePdpStatistics(NAME, "type", INSTANCE, new PdpStatistics()); } } @@ -512,6 +470,13 @@ public class DatabasePolicyModelsProviderTest { pdpSubGroup.setPdpInstances(new ArrayList<>()); pdpSubGroup.getPdpInstances().add(pdp); + PdpStatistics pdpStatistics = new PdpStatistics(); + pdpStatistics.setPdpInstanceId("Pdp1"); + pdpStatistics.setTimeStamp(new Date()); + pdpStatistics.setPdpGroupName("DefaultGroup"); + ArrayList<PdpStatistics> statisticsArrayList = new ArrayList<>(); + statisticsArrayList.add(pdpStatistics); + assertEquals(123, databaseProvider.createPdpGroups(groupList).get(0).getPdpSubgroups().get(0) .getDesiredInstanceCount()); assertEquals(1, databaseProvider.getPdpGroups(GROUP).size()); @@ -534,9 +499,23 @@ public class DatabasePolicyModelsProviderTest { assertEquals(pdpGroup.getName(), databaseProvider.deletePdpGroup(GROUP).getName()); - assertEquals(0, databaseProvider.getPdpStatistics(null).size()); + assertEquals(0, databaseProvider.getPdpStatistics(null,null).size()); + + databaseProvider.getFilteredPdpStatistics(null, GROUP,null, null, null); + databaseProvider.getFilteredPdpStatistics(null, GROUP,null, new Date(), null); + databaseProvider.getFilteredPdpStatistics(null, GROUP,null, null, new Date()); + databaseProvider.getFilteredPdpStatistics(null, GROUP,null, new Date(), new Date()); + + databaseProvider.getFilteredPdpStatistics(NAME, GROUP,null, null, null); + databaseProvider.getFilteredPdpStatistics(NAME, GROUP,null, new Date(), new Date()); + + databaseProvider.getFilteredPdpStatistics(NAME, GROUP,"type", null, null); + databaseProvider.getFilteredPdpStatistics(NAME, GROUP,"type", new Date(), new Date()); + + databaseProvider.createPdpStatistics(statisticsArrayList); + databaseProvider.updatePdpStatistics(statisticsArrayList); - databaseProvider.updatePdpStatistics(GROUP, "type", "type-0", new PdpStatistics()); + databaseProvider.deletePdpStatistics("pdp1",null); } catch (Exception exc) { LOGGER.warn("test should not throw an exception", exc); fail("test should not throw an exception"); diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java index 21d13fd83..d809f66ba 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java @@ -21,14 +21,13 @@ package org.onap.policy.models.provider.impl; +import java.util.ArrayList; import java.util.Collections; +import java.util.Date; import java.util.List; import java.util.Map; - import javax.ws.rs.core.Response; - import lombok.NonNull; - import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.pdp.concepts.Pdp; @@ -225,13 +224,34 @@ public class DummyBadProviderImpl implements PolicyModelsProvider { } @Override - public List<PdpStatistics> getPdpStatistics(String name) throws PfModelException { - return Collections.emptyList(); + public List<PdpStatistics> getPdpStatistics(final String name, final Date timestamp) throws PfModelException { + return new ArrayList<>(); } @Override - public void updatePdpStatistics(@NonNull String pdpGroupName, @NonNull String pdpType, - @NonNull String pdpInstanceId, @NonNull PdpStatistics pdppStatistics) { - // do nothing + public List<PdpStatistics> getFilteredPdpStatistics(String name, String pdpGroupName, String pdpSubGroup, + Date startTimeStamp, Date endTimeStamp) { + // Not implemented + return new ArrayList<>(); + } + + @Override + public List<PdpStatistics> createPdpStatistics(final List<PdpStatistics> pdpStatisticsList) + throws PfModelException { + // Not implemented + return new ArrayList<>(); + } + + @Override + public List<PdpStatistics> updatePdpStatistics(final List<PdpStatistics> pdpStatisticsList) + throws PfModelException { + // Not implemented + return new ArrayList<>(); + } + + @Override + public List<PdpStatistics> deletePdpStatistics(final String name, final Date timestamp) { + // Not implemented + return null; } } diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java index e30cea940..737e0fc28 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java @@ -116,8 +116,8 @@ public class DummyPolicyModelsProviderTest { dummyProvider.updatePdpSubGroup("name", new PdpSubGroup()); dummyProvider.updatePdp("name", "type", new Pdp()); - dummyProvider.updatePdpStatistics("name", "type", "type-0", new PdpStatistics()); - assertTrue(dummyProvider.getPdpStatistics("name").isEmpty()); + dummyProvider.updatePdpStatistics(new ArrayList<>()); + assertTrue(dummyProvider.getPdpStatistics("name", null).isEmpty()); } } |