summaryrefslogtreecommitdiffstats
path: root/main/src
diff options
context:
space:
mode:
Diffstat (limited to 'main/src')
-rw-r--r--main/src/main/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializer.java25
-rw-r--r--main/src/test/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializerTest.java74
2 files changed, 98 insertions, 1 deletions
diff --git a/main/src/main/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializer.java b/main/src/main/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializer.java
index 199b6bd6..cbd89cb8 100644
--- a/main/src/main/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializer.java
+++ b/main/src/main/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializer.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP Policy API
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2019-2020 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -32,10 +32,12 @@ import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardYamlCoder;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.base.PfModelRuntimeException;
import org.onap.policy.models.provider.PolicyModelsProvider;
import org.onap.policy.models.provider.PolicyModelsProviderFactory;
import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate;
import org.slf4j.Logger;
@@ -70,6 +72,12 @@ public class ApiDatabaseInitializer {
try (PolicyModelsProvider databaseProvider =
factory.createPolicyModelsProvider(apiParameterGroup.getDatabaseProviderParameters())) {
+
+ if (alreadyExists(databaseProvider)) {
+ LOGGER.warn("DB already contains policy data - skipping preload");
+ return;
+ }
+
ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
serviceTemplate.setDataTypes(new LinkedHashMap<String, ToscaDataType>());
serviceTemplate.setPolicyTypes(new LinkedHashMap<String, ToscaPolicyType>());
@@ -84,6 +92,21 @@ public class ApiDatabaseInitializer {
}
}
+ private boolean alreadyExists(PolicyModelsProvider databaseProvider) throws PfModelException {
+ try {
+ ToscaServiceTemplate serviceTemplate =
+ databaseProvider.getFilteredPolicyTypes(ToscaPolicyTypeFilter.builder().build());
+ if (!serviceTemplate.getPolicyTypes().isEmpty()) {
+ return true;
+ }
+
+ } catch (PfModelRuntimeException e) {
+ LOGGER.trace("DB does not yet contain policy types", e);
+ }
+
+ return false;
+ }
+
private ToscaServiceTemplate preloadServiceTemplate(ToscaServiceTemplate serviceTemplate,
List<String> entities, FunctionWithEx<ToscaServiceTemplate, ToscaServiceTemplate> getter)
throws PolicyApiException, CoderException, PfModelException {
diff --git a/main/src/test/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializerTest.java b/main/src/test/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializerTest.java
new file mode 100644
index 00000000..cfc2a30e
--- /dev/null
+++ b/main/src/test/java/org/onap/policy/api/main/startstop/ApiDatabaseInitializerTest.java
@@ -0,0 +1,74 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2020 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.
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.api.main.startstop;
+
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.junit.Assert.fail;
+
+import java.io.File;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.onap.policy.api.main.exception.PolicyApiException;
+import org.onap.policy.api.main.parameters.ApiParameterGroup;
+import org.onap.policy.api.main.parameters.CommonTestData;
+import org.onap.policy.common.parameters.GroupValidationResult;
+import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.models.provider.PolicyModelsProvider;
+import org.onap.policy.models.provider.PolicyModelsProviderFactory;
+
+public class ApiDatabaseInitializerTest {
+ private static final String PARAM_FILE = "src/test/resources/parameters/ApiConfigParameters_Https.json";
+ private static final CommonTestData COMMON_TEST_DATA = new CommonTestData();
+ private static ApiParameterGroup params;
+ private static PolicyModelsProvider provider;
+
+ /**
+ * Creates the DB and keeps it open.
+ */
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ COMMON_TEST_DATA.makeParameters(PARAM_FILE, "src/test/resources/parameters/ApiConfigParametersXXX.json", 6969);
+
+ params = new StandardCoder().decode(new File(PARAM_FILE), ApiParameterGroup.class);
+ GroupValidationResult result = params.validate();
+ if (!result.isValid()) {
+ fail(result.getResult());
+ }
+
+ // keep the DB open until the test completes
+ provider = new PolicyModelsProviderFactory().createPolicyModelsProvider(params.getDatabaseProviderParameters());
+ }
+
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception {
+ provider.close();
+ }
+
+ @Test
+ public void testInitializeApiDatabase() throws PolicyApiException {
+ ApiDatabaseInitializer adi = new ApiDatabaseInitializer();
+ assertThatCode(() -> adi.initializeApiDatabase(params)).doesNotThrowAnyException();
+
+ // invoke it again - should still be OK
+ assertThatCode(() -> adi.initializeApiDatabase(params)).doesNotThrowAnyException();
+ }
+}