diff options
author | Rashmi Pujar <rashmi.pujar1@bell.ca> | 2022-02-23 11:27:07 -0500 |
---|---|---|
committer | Ram Krishna Verma <ram_krishna.verma@bell.ca> | 2022-02-24 17:16:17 +0000 |
commit | b2e669ed9a55fea534e01262b04b4529fa2e998d (patch) | |
tree | aab49977dab5038d12e70fbd7b826cb6b5af9f0d /main | |
parent | b9a8400bb36952c2cc3c706399b3863a52ab006a (diff) |
Migrate API DB provider to springboot data jpa
PolicyProviders and PolicyTypeProvider will be removed
once the NodeTemplateController migration is complete.
Issue-ID: POLICY-3924
Signed-off-by: Rashmi Pujar <rashmi.pujar1@bell.ca>
Change-Id: I01a7491c9a09d78a79a4c887f5a49024af648b8b
Diffstat (limited to 'main')
9 files changed, 119 insertions, 84 deletions
diff --git a/main/src/main/java/org/onap/policy/api/main/config/PolicyApiConfig.java b/main/src/main/java/org/onap/policy/api/main/config/PolicyApiConfig.java index 3e4f11ac..e1cc58c6 100644 --- a/main/src/main/java/org/onap/policy/api/main/config/PolicyApiConfig.java +++ b/main/src/main/java/org/onap/policy/api/main/config/PolicyApiConfig.java @@ -69,6 +69,11 @@ public class PolicyApiConfig { return new PolicyModelsProviderFactory().createPolicyModelsProvider(modelsProviderParameters); } + /** + * Initialize the statistics report bean. + * + * @return a new instance of StatisticsReport + */ @Bean public StatisticsReport createStatisticsReport() { return new StatisticsReport(); diff --git a/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java b/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java index c564dbee..84b26c88 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java @@ -42,11 +42,11 @@ import java.net.HttpURLConnection; import java.util.List;
import java.util.UUID;
import javax.ws.rs.core.Response.Status;
+import lombok.RequiredArgsConstructor;
import org.onap.policy.api.main.exception.PolicyApiRuntimeException;
import org.onap.policy.api.main.rest.provider.HealthCheckProvider;
-import org.onap.policy.api.main.rest.provider.PolicyProvider;
-import org.onap.policy.api.main.rest.provider.PolicyTypeProvider;
import org.onap.policy.api.main.rest.provider.StatisticsProvider;
+import org.onap.policy.api.main.service.ToscaServiceTemplateService;
import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
import org.onap.policy.common.endpoints.report.HealthCheckReport;
import org.onap.policy.common.endpoints.utils.NetLoggerUtil;
@@ -54,9 +54,6 @@ import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType; import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.base.PfModelRuntimeException;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -92,6 +89,7 @@ import org.springframework.web.bind.annotation.RestController; @ExtensionProperty(name = "component", value = "Policy Framework")})}),
schemes = {SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS},
securityDefinition = @SecurityDefinition(basicAuthDefinitions = {@BasicAuthDefinition(key = "basicAuth")}))
+@RequiredArgsConstructor
public class ApiRestController extends CommonRestController {
private enum Target {
@@ -100,20 +98,10 @@ public class ApiRestController extends CommonRestController { OTHER
}
- @Autowired
- private PolicyProvider policyProvider;
-
- @Autowired
- private HealthCheckProvider healthCheckProvider;
-
- @Autowired
- private PolicyTypeProvider policyTypeProvider;
-
- @Autowired
- private ApiStatisticsManager mgr;
-
- @Autowired
- private StatisticsProvider statisticsProvider;
+ private final ToscaServiceTemplateService toscaServiceTemplateService;
+ private final HealthCheckProvider healthCheckProvider;
+ private final ApiStatisticsManager mgr;
+ private final StatisticsProvider statisticsProvider;
/**
* Retrieves the healthcheck status of the API component.
@@ -219,7 +207,7 @@ public class ApiRestController extends CommonRestController { @RequestHeader(name = REQUEST_ID_NAME, required = false)
@ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId) {
try {
- ToscaServiceTemplate serviceTemplate = policyTypeProvider.fetchPolicyTypes(null, null);
+ ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.fetchPolicyTypes(null, null);
updateApiStatisticsCounter(Target.POLICY_TYPE, HttpStatus.OK, HttpMethod.GET);
return makeOkResponse(requestId, serviceTemplate);
} catch (PfModelException | PfModelRuntimeException pfme) {
@@ -267,7 +255,7 @@ public class ApiRestController extends CommonRestController { @RequestHeader(name = REQUEST_ID_NAME, required = false)
@ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId) {
try {
- ToscaServiceTemplate serviceTemplate = policyTypeProvider.fetchPolicyTypes(policyTypeId, null);
+ ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.fetchPolicyTypes(policyTypeId, null);
updateApiStatisticsCounter(Target.POLICY_TYPE, HttpStatus.OK, HttpMethod.GET);
return makeOkResponse(requestId, serviceTemplate);
} catch (PfModelException | PfModelRuntimeException pfme) {
@@ -316,7 +304,8 @@ public class ApiRestController extends CommonRestController { @RequestHeader(name = REQUEST_ID_NAME, required = false)
@ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId) {
try {
- ToscaServiceTemplate serviceTemplate = policyTypeProvider.fetchPolicyTypes(policyTypeId, versionId);
+ ToscaServiceTemplate serviceTemplate =
+ toscaServiceTemplateService.fetchPolicyTypes(policyTypeId, versionId);
updateApiStatisticsCounter(Target.POLICY_TYPE, HttpStatus.OK, HttpMethod.GET);
return makeOkResponse(requestId, serviceTemplate);
} catch (PfModelException | PfModelRuntimeException pfme) {
@@ -363,7 +352,7 @@ public class ApiRestController extends CommonRestController { @RequestHeader(name = REQUEST_ID_NAME, required = false)
@ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId) {
try {
- ToscaServiceTemplate serviceTemplate = policyTypeProvider.fetchLatestPolicyTypes(policyTypeId);
+ ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.fetchLatestPolicyTypes(policyTypeId);
updateApiStatisticsCounter(Target.POLICY_TYPE, HttpStatus.OK, HttpMethod.GET);
return makeOkResponse(requestId, serviceTemplate);
} catch (PfModelException | PfModelRuntimeException pfme) {
@@ -414,10 +403,10 @@ public class ApiRestController extends CommonRestController { NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, "/policytypes", toJson(body));
}
try {
- ToscaServiceTemplate serviceTemplate = policyTypeProvider.createPolicyType(body);
+ ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.createPolicyType(body);
updateApiStatisticsCounter(Target.POLICY_TYPE, HttpStatus.OK, HttpMethod.POST);
return makeOkResponse(requestId, serviceTemplate);
- } catch (PfModelException | PfModelRuntimeException pfme) {
+ } catch (PfModelRuntimeException pfme) {
final var msg = "POST /policytypes";
updateApiStatisticsCounter(Target.POLICY_TYPE, HttpStatus.resolve(pfme.getErrorResponse().getResponseCode()
.getStatusCode()), HttpMethod.POST);
@@ -467,10 +456,11 @@ public class ApiRestController extends CommonRestController { @RequestHeader(name = REQUEST_ID_NAME, required = false)
@ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId) {
try {
- ToscaServiceTemplate serviceTemplate = policyTypeProvider.deletePolicyType(policyTypeId, versionId);
+ ToscaServiceTemplate serviceTemplate =
+ toscaServiceTemplateService.deletePolicyType(policyTypeId, versionId);
updateApiStatisticsCounter(Target.POLICY_TYPE, HttpStatus.OK, HttpMethod.DELETE);
return makeOkResponse(requestId, serviceTemplate);
- } catch (PfModelException | PfModelRuntimeException pfme) {
+ } catch (PfModelRuntimeException pfme) {
var msg = String.format("DELETE /policytypes/%s/versions/%s", policyTypeId, versionId);
updateApiStatisticsCounter(Target.POLICY_TYPE, HttpStatus.resolve(pfme.getErrorResponse().getResponseCode()
.getStatusCode()), HttpMethod.DELETE);
@@ -526,7 +516,7 @@ public class ApiRestController extends CommonRestController { + " policies (default), REFERENCED for fully referenced policies") PolicyFetchMode mode) {
try {
ToscaServiceTemplate serviceTemplate =
- policyProvider.fetchPolicies(policyTypeId, policyTypeVersion, null, null, mode);
+ toscaServiceTemplateService.fetchPolicies(policyTypeId, policyTypeVersion, null, null, mode);
updateApiStatisticsCounter(Target.POLICY, HttpStatus.OK, HttpMethod.GET);
return makeOkResponse(requestId, serviceTemplate);
} catch (PfModelException | PfModelRuntimeException pfme) {
@@ -585,11 +575,11 @@ public class ApiRestController extends CommonRestController { + " REFERENCED for fully referenced policies") PolicyFetchMode mode) {
try {
ToscaServiceTemplate serviceTemplate =
- policyProvider.fetchPolicies(policyTypeId, policyTypeVersion, policyId, null, mode);
+ toscaServiceTemplateService.fetchPolicies(policyTypeId, policyTypeVersion, policyId, null, mode);
updateApiStatisticsCounter(Target.POLICY, HttpStatus.OK, HttpMethod.GET);
return makeOkResponse(requestId, serviceTemplate);
} catch (PfModelException | PfModelRuntimeException pfme) {
- var msg = String.format("/policytypes/%s/versions/$s/policies/%s",
+ var msg = String.format("/policytypes/%s/versions/%s/policies/%s",
policyTypeId, policyTypeVersion, policyId);
updateApiStatisticsCounter(Target.POLICY, HttpStatus.resolve(pfme.getErrorResponse().getResponseCode()
.getStatusCode()), HttpMethod.GET);
@@ -647,8 +637,8 @@ public class ApiRestController extends CommonRestController { @RequestParam(name = "mode", defaultValue = "bare") @ApiParam("Fetch mode for policies, BARE for bare policies"
+ " (default), REFERENCED for fully referenced policies") PolicyFetchMode mode) {
try {
- ToscaServiceTemplate serviceTemplate =
- policyProvider.fetchPolicies(policyTypeId, policyTypeVersion, policyId, policyVersion, mode);
+ ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService
+ .fetchPolicies(policyTypeId, policyTypeVersion, policyId, policyVersion, mode);
updateApiStatisticsCounter(Target.POLICY, HttpStatus.OK, HttpMethod.GET);
return makeOkResponse(requestId, serviceTemplate);
} catch (PfModelException | PfModelRuntimeException pfme) {
@@ -704,7 +694,7 @@ public class ApiRestController extends CommonRestController { + "policies (default), REFERENCED for fully referenced policies") PolicyFetchMode mode) {
try {
ToscaServiceTemplate serviceTemplate =
- policyProvider.fetchLatestPolicies(policyTypeId, policyTypeVersion, policyId, mode);
+ toscaServiceTemplateService.fetchLatestPolicies(policyTypeId, policyTypeVersion, policyId, mode);
updateApiStatisticsCounter(Target.POLICY, HttpStatus.OK, HttpMethod.GET);
return makeOkResponse(requestId, serviceTemplate);
} catch (PfModelException | PfModelRuntimeException pfme) {
@@ -764,10 +754,11 @@ public class ApiRestController extends CommonRestController { "/policytypes/" + policyTypeId + "/versions/" + policyTypeVersion + "/policies", toJson(body));
}
try {
- ToscaServiceTemplate serviceTemplate = policyProvider.createPolicy(policyTypeId, policyTypeVersion, body);
+ ToscaServiceTemplate serviceTemplate =
+ toscaServiceTemplateService.createPolicy(policyTypeId, policyTypeVersion, body);
updateApiStatisticsCounter(Target.POLICY, HttpStatus.OK, HttpMethod.POST);
return makeOkResponse(requestId, serviceTemplate);
- } catch (PfModelException | PfModelRuntimeException pfme) {
+ } catch (PfModelRuntimeException pfme) {
var msg = String.format("POST /policytypes/%s/versions/%s/policies", policyTypeId, policyTypeVersion);
updateApiStatisticsCounter(Target.POLICY, HttpStatus.resolve(pfme.getErrorResponse().getResponseCode()
.getStatusCode()), HttpMethod.POST);
@@ -822,10 +813,10 @@ public class ApiRestController extends CommonRestController { @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId) {
try {
ToscaServiceTemplate serviceTemplate =
- policyProvider.deletePolicy(policyTypeId, policyTypeVersion, policyId, policyVersion);
+ toscaServiceTemplateService.deletePolicy(policyTypeId, policyTypeVersion, policyId, policyVersion);
updateApiStatisticsCounter(Target.POLICY, HttpStatus.OK, HttpMethod.DELETE);
return makeOkResponse(requestId, serviceTemplate);
- } catch (PfModelException | PfModelRuntimeException pfme) {
+ } catch (PfModelRuntimeException pfme) {
var msg = String.format("DELETE /policytypes/%s/versions/%s/policies/%s/versions/%s",
policyTypeId, policyTypeVersion, policyId, policyVersion);
updateApiStatisticsCounter(Target.POLICY, HttpStatus.resolve(pfme.getErrorResponse().getResponseCode()
@@ -875,7 +866,7 @@ public class ApiRestController extends CommonRestController { + " policies (default), REFERENCED for fully referenced policies") PolicyFetchMode mode) {
try {
ToscaServiceTemplate serviceTemplate =
- policyProvider.fetchPolicies(null, null, null, null, mode);
+ toscaServiceTemplateService.fetchPolicies(null, null, null, null, mode);
updateApiStatisticsCounter(Target.POLICY, HttpStatus.OK, HttpMethod.GET);
return makeOkResponse(requestId, serviceTemplate);
} catch (PfModelException | PfModelRuntimeException pfme) {
@@ -936,7 +927,7 @@ public class ApiRestController extends CommonRestController { + " policies (default), REFERENCED for fully referenced policies") PolicyFetchMode mode) {
try {
ToscaServiceTemplate serviceTemplate =
- policyProvider.fetchPolicies(null, null, policyId, policyVersion, mode);
+ toscaServiceTemplateService.fetchPolicies(null, null, policyId, policyVersion, mode);
updateApiStatisticsCounter(Target.POLICY, HttpStatus.OK, HttpMethod.GET);
return makeOkResponse(requestId, serviceTemplate);
} catch (PfModelException | PfModelRuntimeException pfme) {
@@ -989,10 +980,10 @@ public class ApiRestController extends CommonRestController { NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, "/policies", toJson(body));
}
try {
- ToscaServiceTemplate serviceTemplate = policyProvider.createPolicies(body);
+ ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.createPolicies(body);
updateApiStatisticsCounter(Target.POLICY, HttpStatus.OK, HttpMethod.POST);
return makeOkResponse(requestId, serviceTemplate);
- } catch (PfModelException | PfModelRuntimeException pfme) {
+ } catch (PfModelRuntimeException pfme) {
final var msg = "POST /policies";
updateApiStatisticsCounter(Target.POLICY, HttpStatus.resolve(pfme.getErrorResponse().getResponseCode()
.getStatusCode()), HttpMethod.POST);
@@ -1041,10 +1032,10 @@ public class ApiRestController extends CommonRestController { @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId) {
try {
ToscaServiceTemplate serviceTemplate =
- policyProvider.deletePolicy(null, null, policyId, policyVersion);
+ toscaServiceTemplateService.deletePolicy(null, null, policyId, policyVersion);
updateApiStatisticsCounter(Target.POLICY, HttpStatus.OK, HttpMethod.DELETE);
return makeOkResponse(requestId, serviceTemplate);
- } catch (PfModelException | PfModelRuntimeException pfme) {
+ } catch (PfModelRuntimeException pfme) {
var msg = String.format("DELETE /policies/%s/versions/%s", policyId, policyVersion);
updateApiStatisticsCounter(Target.POLICY, HttpStatus.resolve(pfme.getErrorResponse().getResponseCode()
.getStatusCode()), HttpMethod.DELETE);
diff --git a/main/src/main/java/org/onap/policy/api/main/rest/ApiStatisticsManager.java b/main/src/main/java/org/onap/policy/api/main/rest/ApiStatisticsManager.java index b497c7f0..91097425 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/ApiStatisticsManager.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/ApiStatisticsManager.java @@ -23,7 +23,7 @@ package org.onap.policy.api.main.rest; -import org.springframework.beans.factory.annotation.Autowired; +import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; /** @@ -32,10 +32,10 @@ import org.springframework.stereotype.Component; * @author Chenfei Gao (cgao@research.att.com) */ @Component +@RequiredArgsConstructor public class ApiStatisticsManager { - @Autowired - private StatisticsReport report; + private final StatisticsReport report; private long totalPolicyDeleteCount; private long totalPolicyTypeDeleteCount; diff --git a/main/src/main/java/org/onap/policy/api/main/rest/provider/HealthCheckProvider.java b/main/src/main/java/org/onap/policy/api/main/rest/provider/HealthCheckProvider.java index 40b841ef..139e5282 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/provider/HealthCheckProvider.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/provider/HealthCheckProvider.java @@ -25,14 +25,15 @@ package org.onap.policy.api.main.rest.provider; +import lombok.RequiredArgsConstructor; import org.onap.policy.api.main.rest.PolicyFetchMode; +import org.onap.policy.api.main.service.ToscaServiceTemplateService; import org.onap.policy.common.endpoints.report.HealthCheckReport; import org.onap.policy.common.utils.network.NetworkUtil; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** @@ -40,17 +41,16 @@ import org.springframework.stereotype.Service; * */ @Service +@RequiredArgsConstructor public class HealthCheckProvider { private static final Logger LOGGER = LoggerFactory.getLogger(HealthCheckProvider.class); - private static final String ALIVE = "alive"; private static final String URL = NetworkUtil.getHostname(); private static final String NAME = "Policy API"; private static final String DB_CONN_FAILURE = "unable to connect with database"; - @Autowired - private PolicyProvider policyProvider; + private final ToscaServiceTemplateService toscaServiceTemplateService; /** * Performs the health check of api service. @@ -75,9 +75,9 @@ public class HealthCheckProvider { */ private boolean verifyApiDatabase() { try { - policyProvider.fetchPolicies(null, null, null, null, PolicyFetchMode.BARE); + toscaServiceTemplateService.getDefaultJpaToscaServiceTemplate(); return true; - } catch (PfModelException | PfModelRuntimeException pfme) { + } catch (PfModelRuntimeException pfme) { LOGGER.warn("Api to database connection check failed. Details - ", pfme); return false; } diff --git a/main/src/main/java/org/onap/policy/api/main/rest/provider/StatisticsProvider.java b/main/src/main/java/org/onap/policy/api/main/rest/provider/StatisticsProvider.java index 9dfc0dd6..b7089ad9 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/provider/StatisticsProvider.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/provider/StatisticsProvider.java @@ -24,8 +24,8 @@ package org.onap.policy.api.main.rest.provider; +import lombok.RequiredArgsConstructor; import org.onap.policy.api.main.rest.StatisticsReport; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** @@ -34,10 +34,10 @@ import org.springframework.stereotype.Service; * @author Chenfei Gao (cgao@research.att.com) */ @Service +@RequiredArgsConstructor public class StatisticsProvider { - @Autowired - private StatisticsReport report; + private final StatisticsReport report; /** * Return the current API statistics. diff --git a/main/src/main/java/org/onap/policy/api/main/service/ToscaServiceTemplateService.java b/main/src/main/java/org/onap/policy/api/main/service/ToscaServiceTemplateService.java index ba61dd41..0fa02258 100644 --- a/main/src/main/java/org/onap/policy/api/main/service/ToscaServiceTemplateService.java +++ b/main/src/main/java/org/onap/policy/api/main/service/ToscaServiceTemplateService.java @@ -21,6 +21,7 @@ package org.onap.policy.api.main.service; import java.util.List; +import java.util.Optional; import javax.ws.rs.core.Response; import lombok.NonNull; import lombok.RequiredArgsConstructor; @@ -75,7 +76,9 @@ public class ToscaServiceTemplateService { */ public ToscaServiceTemplate fetchPolicyTypes(final String policyTypeName, final String policyTypeVersion) throws PfModelException { - return getFilteredPolicyTypes(policyTypeName, policyTypeVersion); + final var policyTypeFilter = + ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeName).version(policyTypeVersion).build(); + return getFilteredPolicyTypes(policyTypeFilter); } /** @@ -85,7 +88,9 @@ public class ToscaServiceTemplateService { * @return the ToscaServiceTemplate object */ public ToscaServiceTemplate fetchLatestPolicyTypes(final String policyTypeName) throws PfModelException { - return getFilteredPolicyTypes(policyTypeName, ToscaEntityFilter.LATEST_VERSION); + final var policyTypeFilter = ToscaEntityFilter.<ToscaPolicyType>builder() + .name(policyTypeName).version(ToscaEntityFilter.LATEST_VERSION).build(); + return getFilteredPolicyTypes(policyTypeFilter); } /** @@ -104,8 +109,14 @@ public class ToscaServiceTemplateService { ToscaUtils.assertPolicyTypesExist(incomingServiceTemplate); // append the incoming fragment to the DB TOSCA service template - final var serviceTemplateToWrite = - ToscaServiceTemplateUtils.addFragment(getDefaultJpaToscaServiceTemplate(), incomingServiceTemplate); + var dbServiceTemplateOpt = getDefaultJpaToscaServiceTemplateOpt(); + JpaToscaServiceTemplate serviceTemplateToWrite; + if (dbServiceTemplateOpt.isEmpty()) { + serviceTemplateToWrite = incomingServiceTemplate; + } else { + serviceTemplateToWrite = + ToscaServiceTemplateUtils.addFragment(dbServiceTemplateOpt.get(), incomingServiceTemplate); + } final var result = serviceTemplateToWrite.validate("service template"); if (!result.isValid()) { @@ -244,8 +255,14 @@ public class ToscaServiceTemplateService { ToscaUtils.assertPoliciesExist(incomingServiceTemplate); // append the incoming fragment to the DB TOSCA service template - final var serviceTemplateToWrite = - ToscaServiceTemplateUtils.addFragment(getDefaultJpaToscaServiceTemplate(), incomingServiceTemplate); + var dbServiceTemplateOpt = getDefaultJpaToscaServiceTemplateOpt(); + JpaToscaServiceTemplate serviceTemplateToWrite; + if (dbServiceTemplateOpt.isEmpty()) { + serviceTemplateToWrite = incomingServiceTemplate; + } else { + serviceTemplateToWrite = + ToscaServiceTemplateUtils.addFragment(dbServiceTemplateOpt.get(), incomingServiceTemplate); + } final var result = serviceTemplateToWrite.validate("Policies CRUD service template."); if (!result.isValid()) { @@ -254,7 +271,7 @@ public class ToscaServiceTemplateService { toscaServiceTemplateRepository.save(serviceTemplateToWrite); - LOGGER.debug("<-appendServiceTemplateFragment: returnServiceTempalate={}", serviceTemplateToWrite); + LOGGER.debug("<-appendServiceTemplateFragment: returnServiceTemplate={}", serviceTemplateToWrite); return body; } @@ -308,16 +325,13 @@ public class ToscaServiceTemplateService { /** * Retrieves TOSCA service template with the specified version of the policy type. * - * @param policyTypeName the name of the policy type - * @param policyTypeVersion the version of the policy type + * @param policyTypeFilter the policy type filter containing name and version of the policy type * @return the TOSCA service template containing the specified version of the policy type * @throws PfModelException on errors getting the policy type */ - private ToscaServiceTemplate getFilteredPolicyTypes(final String policyTypeName, final String policyTypeVersion) + public ToscaServiceTemplate getFilteredPolicyTypes(final ToscaEntityFilter<ToscaPolicyType> policyTypeFilter) throws PfModelException { final var dbServiceTemplate = getDefaultJpaToscaServiceTemplate(); - final var policyTypeFilter = - ToscaEntityFilter.<ToscaPolicyType>builder().name(policyTypeName).version(policyTypeVersion).build(); LOGGER.debug("->getFilteredPolicyTypes: filter={}, serviceTemplate={}", policyTypeFilter, dbServiceTemplate); // validate that policyTypes exist in db @@ -326,9 +340,11 @@ public class ToscaServiceTemplateService { "policy types for filter " + policyTypeFilter + DO_NOT_EXIST_MSG); } + var version = ToscaTypedEntityFilter.LATEST_VERSION + .equals(policyTypeFilter.getVersion()) ? null : policyTypeFilter.getVersion(); // fetch all polices and filter by policyType, policy name and version final var serviceTemplate = new SimpleToscaProvider() - .getCascadedPolicyTypes(dbServiceTemplate, policyTypeName, policyTypeVersion); + .getCascadedPolicyTypes(dbServiceTemplate, policyTypeFilter.getName(), version); var simpleToscaProvider = new SimpleToscaProvider(); List<ToscaPolicyType> filteredPolicyTypes = serviceTemplate.getPolicyTypes().toAuthorativeList(); @@ -414,15 +430,26 @@ public class ToscaServiceTemplateService { /** * Get Service Template. + * * @return the Service Template read from the database + * @throws PfModelRuntimeException if service template if not found in database. */ - private JpaToscaServiceTemplate getDefaultJpaToscaServiceTemplate() { - final var defaultServiceTemplateOpt = toscaServiceTemplateRepository - .findById(new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION)); + public JpaToscaServiceTemplate getDefaultJpaToscaServiceTemplate() throws PfModelRuntimeException { + final var defaultServiceTemplateOpt = getDefaultJpaToscaServiceTemplateOpt(); if (defaultServiceTemplateOpt.isEmpty()) { throw new PfModelRuntimeException(Response.Status.NOT_FOUND, SERVICE_TEMPLATE_NOT_FOUND_MSG); } LOGGER.debug("<-getDefaultJpaToscaServiceTemplate: serviceTemplate={}", defaultServiceTemplateOpt.get()); return defaultServiceTemplateOpt.get(); } + + /** + * Get Service Template Optional object. + * + * @return the Optional object for Service Template read from the database + */ + private Optional<JpaToscaServiceTemplate> getDefaultJpaToscaServiceTemplateOpt() { + return toscaServiceTemplateRepository + .findById(new PfConceptKey(JpaToscaServiceTemplate.DEFAULT_NAME, JpaToscaServiceTemplate.DEFAULT_VERSION)); + } }
\ No newline at end of file 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 4aa25168..87a53896 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 @@ -28,21 +28,21 @@ import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import javax.annotation.PostConstruct; +import lombok.RequiredArgsConstructor; import org.onap.policy.api.main.config.PolicyPreloadConfig; import org.onap.policy.api.main.exception.PolicyApiException; +import org.onap.policy.api.main.service.ToscaServiceTemplateService; 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.tosca.authorative.concepts.ToscaEntityFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.stereotype.Component; @@ -53,17 +53,14 @@ import org.springframework.stereotype.Component; */ @Component @ConditionalOnProperty(value = "database.initialize", havingValue = "true", matchIfMissing = true) +@RequiredArgsConstructor public class ApiDatabaseInitializer { private static final Logger LOGGER = LoggerFactory.getLogger(ApiDatabaseInitializer.class); - private static final StandardYamlCoder coder = new StandardYamlCoder(); - @Autowired - PolicyModelsProvider policyModelsProvider; - - @Autowired - PolicyPreloadConfig policyPreloadConfig; + private final ToscaServiceTemplateService toscaServiceTemplateService; + private final PolicyPreloadConfig policyPreloadConfig; @PostConstruct public void loadData() throws PolicyApiException { @@ -91,8 +88,8 @@ public class ApiDatabaseInitializer { serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_1_0"); ToscaServiceTemplate createdPolicyTypes = - preloadServiceTemplate(serviceTemplate, policyTypes, policyModelsProvider::createPolicyTypes); - preloadServiceTemplate(createdPolicyTypes, policies, policyModelsProvider::createPolicies); + preloadServiceTemplate(serviceTemplate, policyTypes, toscaServiceTemplateService::createPolicyType); + preloadServiceTemplate(createdPolicyTypes, policies, toscaServiceTemplateService::createPolicies); } catch (final PolicyApiException | PfModelException | CoderException exp) { throw new PolicyApiException(exp); } @@ -100,8 +97,8 @@ public class ApiDatabaseInitializer { private boolean alreadyExists() throws PfModelException { try { - ToscaServiceTemplate serviceTemplate = - policyModelsProvider.getFilteredPolicyTypes(ToscaEntityFilter.<ToscaPolicyType>builder().build()); + ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService + .getFilteredPolicyTypes(ToscaEntityFilter.<ToscaPolicyType>builder().build()); if (!serviceTemplate.getPolicyTypes().isEmpty()) { return true; } diff --git a/main/src/test/java/org/onap/policy/api/main/rest/TestApiStatisticsManager.java b/main/src/test/java/org/onap/policy/api/main/rest/TestApiStatisticsManager.java index bc67d21c..48484d6e 100644 --- a/main/src/test/java/org/onap/policy/api/main/rest/TestApiStatisticsManager.java +++ b/main/src/test/java/org/onap/policy/api/main/rest/TestApiStatisticsManager.java @@ -26,6 +26,7 @@ package org.onap.policy.api.main.rest; import static org.junit.Assert.assertEquals; +import lombok.RequiredArgsConstructor; import org.junit.Test; import org.junit.runner.RunWith; import org.onap.policy.api.main.PolicyApiApplication; @@ -40,7 +41,7 @@ import org.springframework.test.context.junit4.SpringRunner; public class TestApiStatisticsManager { @Autowired - private ApiStatisticsManager mgr = new ApiStatisticsManager(); + private ApiStatisticsManager mgr; @Test public void testUpdateMethods() { diff --git a/main/src/test/resources/application-test.yaml b/main/src/test/resources/application-test.yaml index 240d3640..6ed69ea0 100644 --- a/main/src/test/resources/application-test.yaml +++ b/main/src/test/resources/application-test.yaml @@ -1,3 +1,17 @@ +spring: + datasource: + url: jdbc:h2:mem:testdb + driverClassName: org.h2.Driver + jpa: + properties: + hibernate: + dialect: org.hibernate.dialect.H2Dialect + hibernate: + ddl-auto: update + naming: + physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl + implicit-strategy: org.onap.policy.common.spring.utils.CustomImplicitNamingStrategy + database: name: PolicyProviderParameterGroup implementation: org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl |