diff options
Diffstat (limited to 'main/src')
10 files changed, 165 insertions, 48 deletions
diff --git a/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java b/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java index d58be4ec..f1598165 100644 --- a/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java +++ b/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java @@ -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); diff --git a/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java b/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java index a60232fd..f34d13ad 100644 --- a/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java +++ b/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java @@ -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 index 00000000..5d72b6d7 --- /dev/null +++ b/main/src/main/java/org/onap/policy/pap/main/startstop/PapDatabaseInitializer.java @@ -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); + } + } +} diff --git a/main/src/main/resources/META-INF/persistence.xml b/main/src/main/resources/META-INF/persistence.xml index 0f8a8ecc..1d4abd42 100644 --- a/main/src/main/resources/META-INF/persistence.xml +++ b/main/src/main/resources/META-INF/persistence.xml @@ -33,10 +33,6 @@ <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 index 00000000..c6452cc1 --- /dev/null +++ b/main/src/main/resources/PapDb.json @@ -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 diff --git a/main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java b/main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java index 7de63cc2..e7ffce07 100644 --- a/main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java +++ b/main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java @@ -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())); diff --git a/main/src/test/resources/META-INF/persistence.xml b/main/src/test/resources/META-INF/persistence.xml index a43afafc..7c455fb7 100644 --- a/main/src/test/resources/META-INF/persistence.xml +++ b/main/src/test/resources/META-INF/persistence.xml @@ -33,46 +33,7 @@ <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" /> diff --git a/main/src/test/resources/parameters/MinimumParameters.json b/main/src/test/resources/parameters/MinimumParameters.json index 7e7d3ee7..e611dc90 100644 --- a/main/src/test/resources/parameters/MinimumParameters.json +++ b/main/src/test/resources/parameters/MinimumParameters.json @@ -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", diff --git a/main/src/test/resources/parameters/PapConfigParameters.json b/main/src/test/resources/parameters/PapConfigParameters.json index c967fdf1..05f25bb8 100644 --- a/main/src/test/resources/parameters/PapConfigParameters.json +++ b/main/src/test/resources/parameters/PapConfigParameters.json @@ -20,9 +20,10 @@ "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" } } diff --git a/main/src/test/resources/parameters/PapConfigParameters_InvalidName.json b/main/src/test/resources/parameters/PapConfigParameters_InvalidName.json index 8cb74172..d06ecfc8 100644 --- a/main/src/test/resources/parameters/PapConfigParameters_InvalidName.json +++ b/main/src/test/resources/parameters/PapConfigParameters_InvalidName.json @@ -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", |