Adding code for db config & initial group creation 19/85119/2
authorramverma <ram.krishna.verma@est.tech>
Thu, 11 Apr 2019 22:09:38 +0000 (22:09 +0000)
committerramverma <ram.krishna.verma@est.tech>
Thu, 11 Apr 2019 22:09:38 +0000 (22:09 +0000)
1) Adding code for initial PdpGroup/Subgroup creation.
2) Adding db configuration
3) Updated code as per change in policy/models

Change-Id: If37870925001b333e0537e364eecb266c351cf5a
Issue-ID: POLICY-1635
Signed-off-by: ramverma <ram.krishna.verma@est.tech>
main/src/main/java/org/onap/policy/pap/main/startstop/Main.java
main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java
main/src/main/java/org/onap/policy/pap/main/startstop/PapDatabaseInitializer.java [new file with mode: 0644]
main/src/main/resources/META-INF/persistence.xml
main/src/main/resources/PapDb.json [new file with mode: 0644]
main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java
main/src/test/resources/META-INF/persistence.xml
main/src/test/resources/parameters/MinimumParameters.json
main/src/test/resources/parameters/PapConfigParameters.json
main/src/test/resources/parameters/PapConfigParameters_InvalidName.json
packages/policy-pap-tarball/src/main/resources/etc/defaultConfig.json

index d58be4e..f159816 100644 (file)
@@ -24,6 +24,7 @@ package org.onap.policy.pap.main.startstop;
 import java.io.FileInputStream;
 import java.util.Arrays;
 import java.util.Properties;
+
 import org.onap.policy.common.utils.services.Registry;
 import org.onap.policy.pap.main.PapConstants;
 import org.onap.policy.pap.main.PolicyPapException;
@@ -78,9 +79,9 @@ public class Main {
         }
 
         // Read the properties
-        Properties props = new Properties();
+        final Properties props = new Properties();
         try {
-            String propFile = arguments.getFullPropertyFilePath();
+            final String propFile = arguments.getFullPropertyFilePath();
             try (FileInputStream stream = new FileInputStream(propFile)) {
                 props.load(stream);
             }
@@ -89,6 +90,14 @@ public class Main {
             return;
         }
 
+        // Initialize database
+        try {
+            new PapDatabaseInitializer().initializePapDatabase(parameterGroup.getDatabaseProviderParameters());
+        } catch (final PolicyPapException exp) {
+            LOGGER.error("start of policy pap service failed, used parameters are {}", Arrays.toString(args), exp);
+            return;
+        }
+
         // Now, create the activator for the policy pap service
         activator = new PapActivator(parameterGroup, props);
         Registry.register(PapConstants.REG_PAP_ACTIVATOR, activator);
index a60232f..f34d13a 100644 (file)
@@ -48,8 +48,8 @@ import org.onap.policy.pap.main.rest.PapRestServer;
 import org.onap.policy.pap.main.rest.PapStatisticsManager;
 
 /**
- * This class wraps a distributor so that it can be activated as a complete service together with all its pap and
- * forwarding handlers.
+ * This class activates Policy Administration (PAP) as a complete service together with all its controllers, listeners &
+ * handlers.
  *
  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
  */
diff --git a/main/src/main/java/org/onap/policy/pap/main/startstop/PapDatabaseInitializer.java b/main/src/main/java/org/onap/policy/pap/main/startstop/PapDatabaseInitializer.java
new file mode 100644 (file)
index 0000000..5d72b6d
--- /dev/null
@@ -0,0 +1,83 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  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.pap.main.startstop;
+
+import java.util.List;
+
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.resources.ResourceUtils;
+import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.pdp.concepts.PdpGroup;
+import org.onap.policy.models.pdp.concepts.PdpGroups;
+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.pap.main.PolicyPapException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This class creates initial PdpGroup/SubGroup in the database.
+ *
+ * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
+ */
+public class PapDatabaseInitializer {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(PapDatabaseInitializer.class);
+
+    private StandardCoder standardCoder;
+    private PolicyModelsProviderFactory factory;
+
+    /**
+     * Constructs the object.
+     */
+    public PapDatabaseInitializer() {
+        factory = new PolicyModelsProviderFactory();
+        standardCoder = new StandardCoder();
+    }
+
+    /**
+     * Initializes database.
+     *
+     * @param policyModelsProviderParameters the database parameters
+     * @throws PolicyPapException in case of errors.
+     */
+    public void initializePapDatabase(final PolicyModelsProviderParameters policyModelsProviderParameters)
+            throws PolicyPapException {
+
+        try (PolicyModelsProvider databaseProvider =
+                factory.createPolicyModelsProvider(policyModelsProviderParameters)) {
+            final String originalJson = ResourceUtils.getResourceAsString("PapDb.json");
+            final PdpGroups pdpGroupsToCreate = standardCoder.decode(originalJson, PdpGroups.class);
+            final List<PdpGroup> pdpGroupsFromDb = databaseProvider.getPdpGroups(
+                    pdpGroupsToCreate.getGroups().get(0).getName(), pdpGroupsToCreate.getGroups().get(0).getVersion());
+            if (pdpGroupsFromDb.isEmpty()) {
+                databaseProvider.createPdpGroups(pdpGroupsToCreate.getGroups());
+                LOGGER.debug("Created initial pdpGroup in DB - {}", pdpGroupsToCreate);
+            } else {
+                LOGGER.debug("Initial pdpGroup already exists in DB, skipping create - {}", pdpGroupsFromDb);
+            }
+        } catch (final PfModelException | CoderException exp) {
+            throw new PolicyPapException(exp);
+        }
+    }
+}
index 0f8a8ec..1d4abd4 100644 (file)
         <class>org.onap.policy.models.pdp.persistence.concepts.JpaPdp</class>
 
         <properties>
-            <property name="javax.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver" />
-            <property name="javax.persistence.jdbc.url" value="jdbc:mariadb://policydb:3306/policy" />
-            <property name="javax.persistence.jdbc.user" value="policy_user" />
-            <property name="javax.persistence.jdbc.password" value="policy_user" />
             <property name="javax.persistence.schema-generation.database.action" value="create" />
             <property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
             <property name="eclipselink.ddl-generation.output-mode" value="database" />
diff --git a/main/src/main/resources/PapDb.json b/main/src/main/resources/PapDb.json
new file mode 100644 (file)
index 0000000..c6452cc
--- /dev/null
@@ -0,0 +1,64 @@
+{
+  "groups": [
+    {
+      "name": "controlloop",
+      "version": "1.0.0",
+      "description": "This group should be used for managing all control loop related policies and pdps",
+      "pdpGroupState": "ACTIVE",
+      "pdpSubgroups": [
+        {
+          "pdpType": "xacml",
+          "supportedPolicyTypes": [
+            {
+              "name": "onap.policies.controlloop.Guard",
+              "version": "1.0.0"
+            }
+          ],
+          "currentInstanceCount": 0,
+          "desiredInstanceCount": 1
+        },
+        {
+          "pdpType": "drools",
+          "supportedPolicyTypes": [
+            {
+              "name": "onap.policies.controlloop.Operational",
+              "version": "1.0.0"
+            }
+          ],
+          "currentInstanceCount": 0,
+          "desiredInstanceCount": 1
+        },
+        {
+          "pdpType": "apex",
+          "supportedPolicyTypes": [
+            {
+              "name": "onap.policies.controlloop.Operational",
+              "version": "1.0.0"
+            }
+          ],
+          "currentInstanceCount": 0,
+          "desiredInstanceCount": 1
+        }
+      ]
+    },
+    {
+      "name": "monitoring",
+      "version": "1.0.0",
+      "description": "This group should be used for managing all monitoring related policies and pdps",
+      "pdpGroupState": "ACTIVE",
+      "pdpSubgroups": [
+        {
+          "pdpType": "xacml",
+          "supportedPolicyTypes": [
+            {
+              "name": "onap.policies.Monitoring",
+              "version": "1.0.0"
+            }
+          ],
+          "currentInstanceCount": 0,
+          "desiredInstanceCount": 1
+        }
+      ]
+    }
+  ]
+}
\ No newline at end of file
index 7de63cc..e7ffce0 100644 (file)
@@ -171,6 +171,7 @@ public class CommonTestData {
         final Map<String, Object> map = new TreeMap<>();
         map.put("name", PolicyModelsProviderParameters.class.getSimpleName());
         map.put("implementation", DatabasePolicyModelsProviderImpl.class.getCanonicalName());
+        map.put("databaseDriver", "org.h2.Driver");
         map.put("databaseUrl", "jdbc:h2:mem:testdb");
         map.put("databaseUser", "policy");
         map.put("databasePassword", Base64.getEncoder().encodeToString("P01icY".getBytes()));
index a43afaf..7c455fb 100644 (file)
         <class>org.onap.policy.models.pdp.persistence.concepts.JpaPdp</class>
 
         <properties>
-            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
-            <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:testdb" />
-            <property name="javax.persistence.jdbc.user" value="policy" />
-            <property name="javax.persistence.jdbc.password" value="P01icY" />
-            <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
-            <property name="eclipselink.ddl-generation.output-mode" value="database" />
-            <property name="eclipselink.logging.level" value="INFO" />
-        </properties>
-    </persistence-unit>
-
-    <persistence-unit name="ToscaConceptMariaDBTest" transaction-type="RESOURCE_LOCAL">
-        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
-
-        <class>org.onap.policy.models.dao.converters.CDataConditioner</class>
-        <class>org.onap.policy.models.dao.converters.Uuid2String</class>
-        <class>org.onap.policy.models.base.PfConceptKey</class>
-        <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType</class>
-        <class>org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy</class>
-        <class>org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup</class>
-        <class>org.onap.policy.models.pdp.persistence.concepts.JpaPdpSubGroup</class>
-        <class>org.onap.policy.models.pdp.persistence.concepts.JpaPdp</class>
-
-        <properties>
-            <property name="javax.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver" />
-            <property name="javax.persistence.jdbc.url" value="jdbc:mariadb://localhost:3306/policy" />
-            <property name="javax.persistence.jdbc.user" value="policy" />
-            <property name="javax.persistence.jdbc.password" value="P01icY" />
             <property name="javax.persistence.schema-generation.database.action" value="create" />
-
-            <!-- property name="eclipselink.logging.level" value="ALL" />
-            <property name="eclipselink.logging.level.jpa" value="ALL" />
-            <property name="eclipselink.logging.level.ddl" value="ALL" />
-            <property name="eclipselink.logging.level.connection" value="ALL" />
-            <property name="eclipselink.logging.level.sql" value="ALL" />
-            <property name="eclipselink.logging.level.transaction" value="ALL" />
-            <property name="eclipselink.logging.level.sequencing" value="ALL" />
-            <property name="eclipselink.logging.level.server" value="ALL" />
-            <property name="eclipselink.logging.level.query" value="ALL" />
-            <property name="eclipselink.logging.level.properties" value="ALL" /-->
-
             <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
             <property name="eclipselink.ddl-generation.output-mode" value="database" />
             <property name="eclipselink.logging.level" value="INFO" />
index 7e7d3ee..e611dc9 100644 (file)
@@ -19,6 +19,7 @@
     "databaseProviderParameters": {
         "name": "PolicyProviderParameterGroup",
         "implementation": "org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl",
+        "databaseDriver": "org.h2.Driver",
         "databaseUrl": "jdbc:h2:mem:testdb",
         "databaseUser": "policy",
         "databasePassword": "P01icY",
index c967fdf..05f25bb 100644 (file)
     "databaseProviderParameters": {
         "name": "PolicyProviderParameterGroup",
         "implementation": "org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl",
+        "databaseDriver": "org.h2.Driver",
         "databaseUrl": "jdbc:h2:mem:testdb",
         "databaseUser": "policy",
         "databasePassword": "P01icY",
-        "persistenceUnit": "PdpGroupTest"
+        "persistenceUnit": "ToscaConceptTest"
     }
 }
index 8cb7417..d06ecfc 100644 (file)
@@ -19,6 +19,7 @@
     "databaseProviderParameters": {
         "name": "PolicyProviderParameterGroup",
         "implementation": "org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl",
+        "databaseDriver": "org.h2.Driver",
         "databaseUrl": "jdbc:h2:mem:testdb",
         "databaseUser": "policy",
         "databasePassword": "P01icY",
index bdcabc4..32b6c30 100644 (file)
@@ -21,7 +21,8 @@
     "databaseProviderParameters": {
         "name": "PolicyProviderParameterGroup",
         "implementation": "org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl",
-        "databaseUrl": "jdbc:mariadb://policydb:3306/policy",
+        "databaseDriver": "org.mariadb.jdbc.Driver",
+        "databaseUrl": "jdbc:mariadb://mariadb:3306/policyadmin",
         "databaseUser": "policy_user",
         "databasePassword": "cG9saWN5X3VzZXI=",
         "persistenceUnit": "PolicyMariaDb"