From 9a67d321484b935eaf63e55373088fa64b0ffd76 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Tue, 12 Jan 2021 11:52:29 -0500 Subject: Add more methods to query deployment status Issue-ID: POLICY-2648 Change-Id: I398fa1332eb5a862dabd97ed409ef6413bb0c202 Signed-off-by: Jim Hahn --- .../java/org/onap/policy/models/dao/PfDao.java | 11 +++++ .../onap/policy/models/dao/impl/DefaultPfDao.java | 22 ++++++++- .../pdp/persistence/provider/PdpProvider.java | 35 +++++++++++++ .../pdp/persistence/provider/PdpProviderTest.java | 57 +++++++++++++++++++++- .../models/provider/PolicyModelsProvider.java | 19 ++++++++ .../impl/DatabasePolicyModelsProviderImpl.java | 14 ++++++ .../impl/DummyPolicyModelsProviderImpl.java | 14 ++++++ .../impl/DatabasePolicyModelsProviderTest.java | 4 ++ .../models/provider/impl/DummyBadProviderImpl.java | 16 +++++- .../impl/DummyPolicyModelsProviderTest.java | 3 ++ 10 files changed, 191 insertions(+), 4 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 062ec4662..6c862bb64 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 @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -229,6 +230,16 @@ public interface PfDao { */ List getAllVersions(Class someClass, final String name); + /** + * Get all the objects in the database of a given type. + * + * @param the type of the objects to get, a subclass of {@link PfConcept} + * @param someClass the class of the objects to get, a subclass of {@link PfConcept} + * @param parentKeyName the name of the concepts for which to get all versions + * @return the objects or null if no objects were retrieved + */ + List getAllVersionsByParent(Class someClass, final String parentKeyName); + /** * Get a concept from the database with the given concept key. * 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 ad9ef1215..b7dda8dbb 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 @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019-2020 Nordix Foundation. - * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -91,6 +91,9 @@ public class DefaultPfDao implements PfDao { private static final String SELECT_ALL_FOR_PARENT = SELECT_FROM_TABLE + WHERE + PARENT_NAME_FILTER + AND + PARENT_VERSION_FILTER; + private static final String SELECT_ALL_VERSIONS_FOR_PARENT = + SELECT_FROM_TABLE + WHERE + PARENT_NAME_FILTER; + private static final String SELECT_ALL_VERSIONS = SELECT_FROM_TABLE + WHERE + NAME_FILTER; private static final String SELECT_BY_CONCEPT_KEY = @@ -469,6 +472,23 @@ public class DefaultPfDao implements PfDao { } } + @Override + public List getAllVersionsByParent(final Class someClass, final String parentKeyName) { + if (someClass == null || parentKeyName == null) { + return Collections.emptyList(); + } + final EntityManager mg = getEntityManager(); + try { + // @formatter:off + return mg.createQuery(setQueryTable(SELECT_ALL_VERSIONS_FOR_PARENT, someClass), someClass) + .setParameter(PARENT_NAME, parentKeyName) + .getResultList(); + // @formatter:on + } finally { + mg.close(); + } + } + @Override public List getAllVersions(final Class someClass, final String conceptName) { if (someClass == null || conceptName == null) { diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpProvider.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpProvider.java index ed3551a9e..7d59166e2 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpProvider.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpProvider.java @@ -46,6 +46,7 @@ import org.onap.policy.models.pdp.persistence.concepts.JpaPdp; import org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup; import org.onap.policy.models.pdp.persistence.concepts.JpaPdpPolicyStatus; import org.onap.policy.models.pdp.persistence.concepts.JpaPdpSubGroup; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion; /** * This class provides the provision of information on PAP concepts in the database to callers. @@ -254,6 +255,40 @@ public class PdpProvider { // Not implemented yet } + /** + * Gets all policy deployments. + * + * @param dao the DAO to use to access the database + * @return the deployments found + * @throws PfModelException on errors getting PDP groups + */ + public List getAllPolicyStatus(@NonNull final PfDao dao) + throws PfModelException { + + return dao.getAll(JpaPdpPolicyStatus.class).stream().map(JpaPdpPolicyStatus::toAuthorative) + .collect(Collectors.toList()); + } + + /** + * Gets all deployments for a policy. + * + * @param dao the DAO to use to access the database + * @return the deployments found + * @throws PfModelException on errors getting PDP groups + */ + public List getAllPolicyStatus(@NonNull final PfDao dao, + @NonNull ToscaConceptIdentifierOptVersion policy) throws PfModelException { + + if (policy.getVersion() != null) { + return dao.getAll(JpaPdpPolicyStatus.class, new PfConceptKey(policy.getName(), policy.getVersion())) + .stream().map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList()); + + } else { + return dao.getAllVersionsByParent(JpaPdpPolicyStatus.class, policy.getName()).stream() + .map(JpaPdpPolicyStatus::toAuthorative).collect(Collectors.toList()); + } + } + /** * Gets the policy deployments for a PDP group. * diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpProviderTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpProviderTest.java index 2bf942a6a..aadaf3500 100644 --- a/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpProviderTest.java +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpProviderTest.java @@ -57,6 +57,7 @@ import org.onap.policy.models.pdp.concepts.PdpSubGroup; import org.onap.policy.models.pdp.enums.PdpHealthStatus; import org.onap.policy.models.pdp.enums.PdpState; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion; import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider; /** @@ -73,6 +74,9 @@ public class PdpProviderTest { private static final String PDP_GROUP0 = "PdpGroup0"; private static final String GROUP_A = "groupA"; private static final String GROUP_B = "groupB"; + private static final ToscaConceptIdentifier MY_POLICY = new ToscaConceptIdentifier("MyPolicy", "1.2.3"); + private static final ToscaConceptIdentifier MY_POLICY2 = new ToscaConceptIdentifier("MyPolicyB", "2.3.4"); + private PfDao pfDao; private StandardCoder standardCoder; private PdpPolicyStatusBuilder statusBuilder; @@ -117,10 +121,9 @@ public class PdpProviderTest { */ @Before public void setupBuilder() { - ToscaConceptIdentifier policy = new ToscaConceptIdentifier("MyPolicy", "1.2.3"); ToscaConceptIdentifier policyType = new ToscaConceptIdentifier("MyPolicyType", "1.2.4"); - statusBuilder = PdpPolicyStatus.builder().deploy(true).pdpType("MyPdpType").policy(policy) + statusBuilder = PdpPolicyStatus.builder().deploy(true).pdpType("MyPdpType").policy(MY_POLICY) .policyType(policyType).state(State.SUCCESS); } @@ -644,6 +647,53 @@ public class PdpProviderTest { new PdpProvider().updatePdpStatistics(pfDao, "name", "TYPE", "inst", new PdpStatistics()); } + @Test + public void testGetAllPolicyStatusPfDao() throws PfModelException { + assertThatThrownBy(() -> { + new PdpProvider().getAllPolicyStatus(null); + }).hasMessageMatching(DAO_IS_NULL); + + assertThat(new PdpProvider().getAllPolicyStatus(pfDao)).isEmpty(); + + PdpProvider provider = loadDeployments(); + assertThat(provider.getAllPolicyStatus(pfDao)).hasSize(5); + } + + private PdpProvider loadDeployments() { + PdpProvider provider = new PdpProvider(); + + // same name, different version + final ToscaConceptIdentifier policy3 = new ToscaConceptIdentifier(MY_POLICY.getName(), "10.20.30"); + + PdpPolicyStatus id1 = statusBuilder.pdpGroup(GROUP_A).pdpId("pdp1").policy(MY_POLICY).build(); + PdpPolicyStatus id2 = statusBuilder.pdpGroup(GROUP_A).pdpId("pdp2").policy(MY_POLICY2).build(); + PdpPolicyStatus id3 = statusBuilder.pdpGroup(GROUP_A).pdpId("pdp3").policy(policy3).build(); + PdpPolicyStatus id4 = statusBuilder.pdpGroup(GROUP_B).pdpId("pdp4").policy(MY_POLICY).build(); + PdpPolicyStatus id5 = statusBuilder.pdpGroup(GROUP_B).pdpId("pdp5").policy(MY_POLICY2).build(); + provider.cudPolicyStatus(pfDao, List.of(id1, id2, id3, id4, id5), null, null); + + return provider; + } + + @Test + public void testGetAllPolicyStatusPfDaoToscaConceptIdentifierOptVersion() throws PfModelException { + assertThatThrownBy(() -> { + new PdpProvider().getAllPolicyStatus(null, new ToscaConceptIdentifierOptVersion("somePdp", null)); + }).hasMessageMatching(DAO_IS_NULL); + + assertThatThrownBy(() -> { + new PdpProvider().getAllPolicyStatus(pfDao, null); + }).hasMessageContaining("policy").hasMessageContaining("null"); + + assertThat(new PdpProvider().getAllPolicyStatus(pfDao, new ToscaConceptIdentifierOptVersion("somePdp", null))) + .isEmpty(); + + PdpProvider provider = loadDeployments(); + assertThat(provider.getAllPolicyStatus(pfDao, new ToscaConceptIdentifierOptVersion(MY_POLICY))).hasSize(2); + assertThat(provider.getAllPolicyStatus(pfDao, new ToscaConceptIdentifierOptVersion(MY_POLICY.getName(), null))) + .hasSize(3); + } + @Test public void testGetGroupPolicyStatus() throws PfModelException { assertThatThrownBy(() -> { @@ -655,6 +705,9 @@ public class PdpProviderTest { }).hasMessageContaining("group").hasMessageContaining("null"); assertThat(new PdpProvider().getGroupPolicyStatus(pfDao, PDP_GROUP0)).isEmpty(); + + PdpProvider provider = loadDeployments(); + assertThat(provider.getGroupPolicyStatus(pfDao, GROUP_A)).hasSize(3); } @Test 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 65876629c..e0cb44cb6 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 @@ -33,6 +33,7 @@ import org.onap.policy.models.pdp.concepts.PdpGroupFilter; import org.onap.policy.models.pdp.concepts.PdpPolicyStatus; import org.onap.policy.models.pdp.concepts.PdpStatistics; import org.onap.policy.models.pdp.concepts.PdpSubGroup; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; @@ -371,6 +372,24 @@ public interface PolicyModelsProvider extends AutoCloseable { */ public List deletePdpStatistics(@NonNull String name, Date timestamp) throws PfModelException; + /** + * Gets all policy deployments. + * + * @return the deployments found + * @throws PfModelException on errors getting PDP groups + */ + public List getAllPolicyStatus() + throws PfModelException; + + /** + * Gets all deployments for a policy. + * + * @return the deployments found + * @throws PfModelException on errors getting PDP groups + */ + public List getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy) + throws PfModelException; + /** * Gets the policy deployments for a PDP group. * 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 f7c58cf19..6b54a1c24 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 @@ -46,6 +46,7 @@ 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.ToscaConceptIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; @@ -347,6 +348,19 @@ public class DatabasePolicyModelsProviderImpl implements PolicyModelsProvider { return new PdpStatisticsProvider().deletePdpStatistics(pfDao, name, timestamp); } + @Override + public List getAllPolicyStatus() throws PfModelException { + assertInitialized(); + return new PdpProvider().getAllPolicyStatus(pfDao); + } + + @Override + public List getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy) + throws PfModelException { + assertInitialized(); + return new PdpProvider().getAllPolicyStatus(pfDao, policy); + } + @Override public List getGroupPolicyStatus(@NonNull String groupName) throws PfModelException { 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 f0b968c10..0a9ea723f 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 @@ -40,6 +40,7 @@ import org.onap.policy.models.pdp.concepts.PdpStatistics; import org.onap.policy.models.pdp.concepts.PdpSubGroup; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderParameters; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; @@ -240,6 +241,19 @@ public class DummyPolicyModelsProviderImpl implements PolicyModelsProvider { return new ArrayList<>(); } + @Override + public List getAllPolicyStatus() throws PfModelException { + // Not implemented + return new ArrayList<>(); + } + + @Override + public List getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy) + throws PfModelException { + // Not implemented + return new ArrayList<>(); + } + @Override public List getGroupPolicyStatus(String groupName) throws PfModelException { // Not implemented 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 cace06224..fb5af1e1b 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 @@ -46,6 +46,7 @@ import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; @@ -425,6 +426,9 @@ public class DatabasePolicyModelsProviderTest { assertEquals(NAME, databaseProvider.deletePdpStatistics(NAME, null).get(0).getPdpInstanceId()); assertEquals(0, databaseProvider.getPdpStatistics(null, null).size()); + assertThat(databaseProvider.getAllPolicyStatus()).isEmpty(); + assertThat(databaseProvider.getAllPolicyStatus(new ToscaConceptIdentifierOptVersion("MyPolicy", null))) + .isEmpty(); assertThat(databaseProvider.getGroupPolicyStatus(GROUP)).isEmpty(); assertThatCode(() -> databaseProvider.cudPolicyStatus(null, null, null)).doesNotThrowAnyException(); 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 0a3acb019..008a1ccf9 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 @@ -38,6 +38,7 @@ import org.onap.policy.models.pdp.concepts.PdpPolicyStatus; import org.onap.policy.models.pdp.concepts.PdpStatistics; import org.onap.policy.models.pdp.concepts.PdpSubGroup; import org.onap.policy.models.provider.PolicyModelsProvider; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; @@ -231,6 +232,19 @@ public class DummyBadProviderImpl implements PolicyModelsProvider { return null; } + @Override + public List getAllPolicyStatus() throws PfModelException { + // Not implemented + return null; + } + + @Override + public List getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy) + throws PfModelException { + // Not implemented + return null; + } + @Override public List getGroupPolicyStatus(@NonNull String groupName) throws PfModelException { // Not implemented @@ -245,7 +259,7 @@ public class DummyBadProviderImpl implements PolicyModelsProvider { @Override public List getServiceTemplateList(String name, String version) throws PfModelException { - // TODO Auto-generated method stub + // 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 06c95ef64..223aa5b36 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 @@ -39,6 +39,7 @@ import org.onap.policy.models.pdp.concepts.PdpSubGroup; import org.onap.policy.models.provider.PolicyModelsProvider; import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.onap.policy.models.provider.PolicyModelsProviderParameters; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; @@ -114,6 +115,8 @@ public class DummyPolicyModelsProviderTest { assertTrue(dummyProvider.updatePdpStatistics(null).isEmpty()); assertTrue(dummyProvider.deletePdpStatistics(null, new Date()).isEmpty()); + assertThat(dummyProvider.getAllPolicyStatus()).isEmpty(); + assertThat(dummyProvider.getAllPolicyStatus(new ToscaConceptIdentifierOptVersion("MyPolicy", null))).isEmpty(); assertThat(dummyProvider.getGroupPolicyStatus("name")).isEmpty(); assertThatCode(() -> dummyProvider.cudPolicyStatus(null, null, null)).doesNotThrowAnyException(); } -- cgit 1.2.3-korg