Merge "Rename keywords used as column names in API and PAP JPA"
authorJim Hahn <jrh3@att.com>
Fri, 15 Jan 2021 14:38:15 +0000 (14:38 +0000)
committerGerrit Code Review <gerrit@onap.org>
Fri, 15 Jan 2021 14:38:15 +0000 (14:38 +0000)
models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java
models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java
models-interactions/model-impl/cds/src/main/java/org/onap/policy/cds/client/CdsProcessorHandler.java
models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/provider/PdpProvider.java
models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpProviderTest.java
models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProvider.java
models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java
models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java
models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java
models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java
models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java

index 062ec46..6c862bb 100644 (file)
@@ -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 {
      */
     <T extends PfConcept> List<T> getAllVersions(Class<T> someClass, final String name);
 
+    /**
+     * 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}
+     * @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
+     */
+    <T extends PfConcept> List<T> getAllVersionsByParent(Class<T> someClass, final String parentKeyName);
+
     /**
      * Get a concept from the database with the given concept key.
      *
index ad9ef12..b7dda8d 100644 (file)
@@ -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 <T extends PfConcept> List<T> getAllVersionsByParent(final Class<T> 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 <T extends PfConcept> List<T> getAllVersions(final Class<T> someClass, final String conceptName) {
         if (someClass == null || conceptName == null) {
index 04b5840..660908b 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- * Copyright (C) 2019 Bell Canada.
+ * Copyright (C) 2019-2021 Bell Canada.
  * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -56,10 +56,7 @@ public class CdsProcessorHandler {
         final StreamObserver<ExecutionServiceOutput> responseObserver = new StreamObserver<ExecutionServiceOutput>() {
             @Override
             public void onNext(ExecutionServiceOutput output) {
-                LOGGER.info(LOG_MSG, EventType.IN, CommInfrastructure.REST, url, NetLoggerUtil.SYSTEM_LS,
-                                output);
                 NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, url, output.toString());
-
                 listener.onMessage(output);
             }
 
@@ -67,7 +64,6 @@ public class CdsProcessorHandler {
             public void onError(Throwable throwable) {
                 LOGGER.info(LOG_MSG, EventType.IN, CommInfrastructure.REST, url, NetLoggerUtil.SYSTEM_LS,
                                 throwable);
-                NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, url, throwable.toString());
                 listener.onError(throwable);
                 finishLatch.countDown();
             }
@@ -82,8 +78,6 @@ public class CdsProcessorHandler {
 
         final StreamObserver<ExecutionServiceInput> requestObserver = asyncStub.process(responseObserver);
         try {
-            LOGGER.info(LOG_MSG, EventType.OUT, CommInfrastructure.REST, url, NetLoggerUtil.SYSTEM_LS,
-                            request);
             NetLoggerUtil.log(EventType.OUT, CommInfrastructure.REST, url, request.toString());
 
             // Send the message to CDS backend for processing
index ed3551a..7d59166 100644 (file)
@@ -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<PdpPolicyStatus> 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<PdpPolicyStatus> 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.
      *
index 2bf942a..aadaf35 100644 (file)
@@ -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
index 6587662..e0cb44c 100644 (file)
@@ -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<PdpStatistics> 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<PdpPolicyStatus> getAllPolicyStatus()
+                    throws PfModelException;
+
+    /**
+     * Gets all deployments for a policy.
+     *
+     * @return the deployments found
+     * @throws PfModelException on errors getting PDP groups
+     */
+    public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy)
+                    throws PfModelException;
+
     /**
      * Gets the policy deployments for a PDP group.
      *
index f7c58cf..6b54a1c 100644 (file)
@@ -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<PdpPolicyStatus> getAllPolicyStatus() throws PfModelException {
+        assertInitialized();
+        return new PdpProvider().getAllPolicyStatus(pfDao);
+    }
+
+    @Override
+    public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy)
+                    throws PfModelException {
+        assertInitialized();
+        return new PdpProvider().getAllPolicyStatus(pfDao, policy);
+    }
+
     @Override
     public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull String groupName)
                     throws PfModelException {
index f0b968c..0a9ea72 100644 (file)
@@ -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<PdpPolicyStatus> getAllPolicyStatus() throws PfModelException {
+        // Not implemented
+        return new ArrayList<>();
+    }
+
+    @Override
+    public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy)
+                    throws PfModelException {
+        // Not implemented
+        return new ArrayList<>();
+    }
+
     @Override
     public List<PdpPolicyStatus> getGroupPolicyStatus(String groupName) throws PfModelException {
         // Not implemented
index cace062..fb5af1e 100644 (file)
@@ -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();
 
index 0a3acb0..008a1cc 100644 (file)
@@ -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<PdpPolicyStatus> getAllPolicyStatus() throws PfModelException {
+        // Not implemented
+        return null;
+    }
+
+    @Override
+    public List<PdpPolicyStatus> getAllPolicyStatus(@NonNull ToscaConceptIdentifierOptVersion policy)
+                    throws PfModelException {
+        // Not implemented
+        return null;
+    }
+
     @Override
     public List<PdpPolicyStatus> getGroupPolicyStatus(@NonNull String groupName) throws PfModelException {
         // Not implemented
@@ -245,7 +259,7 @@ public class DummyBadProviderImpl implements PolicyModelsProvider {
 
     @Override
     public List<ToscaServiceTemplate> getServiceTemplateList(String name, String version) throws PfModelException {
-        // TODO Auto-generated method stub
+        // Not implemented
         return null;
     }
 }
index 06c95ef..223aa5b 100644 (file)
@@ -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();
     }