summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorramverma <ram.krishna.verma@est.tech>2019-04-09 16:49:43 +0000
committerramverma <ram.krishna.verma@est.tech>2019-04-09 16:49:43 +0000
commit63d40cdd0f11bf195163facdc6233a89dd8e89e9 (patch)
tree14ad383e213bf1d2ba0c076fc4ea0122fbae8101
parent0510ea5ac447d2a348775fac5095cee956b34c55 (diff)
Adding PdpGroup query & statechange providers
1) Adding implementation of PdpGroup Query & State change providers for PAP REST API. 2) Adding support for unregistering Pdp after recieving TERMINATED state in PdpStatus message. 3) Updating PdpStatusMessageHandler with new filter changes in models. 4) Removing updatePdpStatistics method as we will implement db provider and REST provider in next release. 5) Fixing few bugs found during Pdp registration testing. 6) Updating the existing test cases to work. 7) Will be adding more exhaustive test cases in another review. Change-Id: I7aa20bf422671fdc1e06dab5e90aef4ee19a6a02 Issue-ID: POLICY-1541 Signed-off-by: ramverma <ram.krishna.verma@est.tech>
-rw-r--r--main/pom.xml5
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/comm/PdpStatusMessageHandler.java67
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupQueryControllerV1.java14
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupQueryProvider.java25
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeControllerV1.java17
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeProvider.java157
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/rest/depundep/SessionData.java7
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/parameters/CommonTestData.java5
-rw-r--r--main/src/test/resources/META-INF/persistence.xml81
9 files changed, 306 insertions, 72 deletions
diff --git a/main/pom.xml b/main/pom.xml
index 0c84e27a..b2a31806 100644
--- a/main/pom.xml
+++ b/main/pom.xml
@@ -94,6 +94,11 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>com.h2database</groupId>
+ <artifactId>h2</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
diff --git a/main/src/main/java/org/onap/policy/pap/main/comm/PdpStatusMessageHandler.java b/main/src/main/java/org/onap/policy/pap/main/comm/PdpStatusMessageHandler.java
index e0b00c71..3c871352 100644
--- a/main/src/main/java/org/onap/policy/pap/main/comm/PdpStatusMessageHandler.java
+++ b/main/src/main/java/org/onap/policy/pap/main/comm/PdpStatusMessageHandler.java
@@ -23,7 +23,7 @@ package org.onap.policy.pap.main.comm;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
-import org.apache.commons.lang3.tuple.Pair;
+
import org.onap.policy.common.utils.services.Registry;
import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.pdp.concepts.Pdp;
@@ -38,7 +38,6 @@ import org.onap.policy.models.pdp.enums.PdpState;
import org.onap.policy.models.provider.PolicyModelsProvider;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
import org.onap.policy.pap.main.PapConstants;
import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
import org.onap.policy.pap.main.PolicyPapException;
@@ -64,7 +63,7 @@ public class PdpStatusMessageHandler {
final PolicyModelsProviderFactoryWrapper modelProviderWrapper =
Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
try (PolicyModelsProvider databaseProvider = modelProviderWrapper.create()) {
- if (message.getPdpGroup().isEmpty() && message.getPdpSubgroup().isEmpty()) {
+ if (message.getPdpGroup() == null && message.getPdpSubgroup() == null) {
handlePdpRegistration(message, databaseProvider);
} else {
handlePdpHeartbeat(message, databaseProvider);
@@ -89,11 +88,9 @@ public class PdpStatusMessageHandler {
throws PfModelException {
boolean pdpGroupFound = false;
Optional<PdpSubGroup> subGroup = null;
- final List<Pair<String, String>> supportedPolicyTypesPair = createSupportedPolictTypesPair(message);
- final PdpGroupFilter filter = PdpGroupFilter.builder().pdpType(message.getPdpType()).build();
- // TODO setSupportedTypes(supportedPolicyTypesPair)
- final List<PdpGroup> pdpGroups =
- databaseProvider.getFilteredPdpGroups(filter);
+ final PdpGroupFilter filter = PdpGroupFilter.builder().pdpType(message.getPdpType())
+ .policyTypeList(message.getSupportedPolicyTypes()).matchPolicyTypesExactly(true).build();
+ final List<PdpGroup> pdpGroups = databaseProvider.getFilteredPdpGroups(filter);
for (final PdpGroup pdpGroup : pdpGroups) {
subGroup = findPdpSubGroup(message, pdpGroup);
if (subGroup.isPresent()) {
@@ -109,20 +106,12 @@ public class PdpStatusMessageHandler {
return pdpGroupFound;
}
- private List<Pair<String, String>> createSupportedPolictTypesPair(final PdpStatus message) {
- final List<Pair<String, String>> supportedPolicyTypesPair = new ArrayList<>();
- for (final ToscaPolicyTypeIdentifier policyTypeIdent : message.getSupportedPolicyTypes()) {
- supportedPolicyTypesPair.add(Pair.of(policyTypeIdent.getName(), policyTypeIdent.getVersion()));
- }
- return supportedPolicyTypesPair;
- }
-
private void updatePdpSubGroup(final PdpGroup pdpGroup, final PdpSubGroup pdpSubGroup, final PdpStatus message,
final PolicyModelsProvider databaseProvider) throws PfModelException {
final Pdp pdpInstance = new Pdp();
pdpInstance.setInstanceId(message.getName());
- pdpInstance.setPdpState(message.getState());
+ pdpInstance.setPdpState(PdpState.ACTIVE);
pdpInstance.setHealthy(message.getHealthy());
pdpInstance.setMessage(message.getDescription());
pdpSubGroup.getPdpInstances().add(pdpInstance);
@@ -140,8 +129,8 @@ public class PdpStatusMessageHandler {
Optional<PdpSubGroup> pdpSubgroup = null;
Optional<Pdp> pdpInstance = null;
- final PdpGroupFilter filter = PdpGroupFilter.builder().name(message.getPdpGroup())
- .version(PdpGroupFilter.LATEST_VERSION).build();
+ final PdpGroupFilter filter =
+ PdpGroupFilter.builder().name(message.getPdpGroup()).version(PdpGroupFilter.LATEST_VERSION).build();
final List<PdpGroup> pdpGroups = databaseProvider.getFilteredPdpGroups(filter);
if (!pdpGroups.isEmpty()) {
final PdpGroup pdpGroup = pdpGroups.get(0);
@@ -164,7 +153,7 @@ public class PdpStatusMessageHandler {
private Optional<PdpSubGroup> findPdpSubGroup(final PdpStatus message, final PdpGroup pdpGroup) {
PdpSubGroup pdpSubgroup = null;
for (final PdpSubGroup subGroup : pdpGroup.getPdpSubgroups()) {
- if (message.getPdpSubgroup().equals(subGroup.getPdpType())) {
+ if (message.getPdpType().equals(subGroup.getPdpType())) {
pdpSubgroup = subGroup;
break;
}
@@ -183,19 +172,30 @@ public class PdpStatusMessageHandler {
return Optional.ofNullable(pdpInstance);
}
- private void processPdpDetails(final PdpStatus message, final PdpSubGroup pdpSubgroup, final Pdp pdpInstance,
+ private void processPdpDetails(final PdpStatus message, final PdpSubGroup pdpSubGroup, final Pdp pdpInstance,
final PdpGroup pdpGroup, final PolicyModelsProvider databaseProvider) throws PfModelException {
- if (validatePdpDetails(message, pdpGroup, pdpSubgroup, pdpInstance)) {
+ if (PdpState.TERMINATED.equals(message.getState())) {
+ processPdpTermination(message, pdpSubGroup, pdpInstance, pdpGroup, databaseProvider);
+ } else if (validatePdpDetails(message, pdpGroup, pdpSubGroup, pdpInstance)) {
LOGGER.debug("PdpInstance details are correct. Saving current state in DB - {}", pdpInstance);
- updatePdpHealthStatus(message, pdpSubgroup, pdpInstance, pdpGroup, databaseProvider);
- updatePdpStatistics(message, pdpSubgroup, pdpInstance, pdpGroup, databaseProvider);
+ updatePdpHealthStatus(message, pdpSubGroup, pdpInstance, pdpGroup, databaseProvider);
} else {
LOGGER.debug("PdpInstance details are not correct. Sending PdpUpdate message - {}", pdpInstance);
- sendPdpMessage(pdpGroup.getName(), pdpSubgroup, pdpInstance.getInstanceId(), pdpInstance.getPdpState(),
+ sendPdpMessage(pdpGroup.getName(), pdpSubGroup, pdpInstance.getInstanceId(), pdpInstance.getPdpState(),
databaseProvider);
}
}
+ private void processPdpTermination(final PdpStatus message, final PdpSubGroup pdpSubGroup, final Pdp pdpInstance,
+ final PdpGroup pdpGroup, final PolicyModelsProvider databaseProvider) throws PfModelException {
+ pdpSubGroup.getPdpInstances().remove(pdpInstance);
+ pdpSubGroup.setCurrentInstanceCount(pdpSubGroup.getCurrentInstanceCount() - 1);
+ databaseProvider.updatePdpSubGroup(pdpGroup.getName(), pdpGroup.getVersion(), pdpSubGroup);
+
+ LOGGER.debug("Deleted PdpInstance - {} belonging to PdpSubGroup - {} and PdpGroup - {}", pdpInstance,
+ pdpSubGroup, pdpGroup);
+ }
+
private boolean validatePdpDetails(final PdpStatus message, final PdpGroup pdpGroup, final PdpSubGroup subGroup,
final Pdp pdpInstanceDetails) {
@@ -214,23 +214,6 @@ public class PdpStatusMessageHandler {
LOGGER.debug("Updated Pdp in DB - {}", pdpInstance);
}
- private void updatePdpStatistics(final PdpStatus message, final PdpSubGroup pdpSubgroup, final Pdp pdpInstance,
- final PdpGroup pdpGroup, final PolicyModelsProvider databaseProvider) throws PfModelException {
- final PdpStatistics pdpStatistics = new PdpStatistics();
- pdpStatistics.setPdpInstanceId(message.getName());
- pdpStatistics.setPolicyDeployCount(message.getStatistics().getPolicyDeployCount());
- pdpStatistics.setPolicyDeploySuccessCount(message.getStatistics().getPolicyDeploySuccessCount());
- pdpStatistics.setPolicyDeployFailCount(message.getStatistics().getPolicyDeployFailCount());
- pdpStatistics.setPolicyExecutedCount(message.getStatistics().getPolicyExecutedCount());
- pdpStatistics.setPolicyExecutedSuccessCount(message.getStatistics().getPolicyExecutedSuccessCount());
- pdpStatistics.setPolicyExecutedFailCount(message.getStatistics().getPolicyExecutedFailCount());
-
- databaseProvider.updatePdpStatistics(pdpGroup.getName(), pdpGroup.getVersion(), pdpSubgroup.getPdpType(),
- pdpInstance.getInstanceId(), pdpStatistics);
-
- LOGGER.debug("Updated PdpStatistics in DB - {}", pdpStatistics);
- }
-
private void sendPdpMessage(final String pdpGroupName, final PdpSubGroup subGroup, final String pdpInstanceId,
final PdpState pdpState, final PolicyModelsProvider databaseProvider) throws PfModelException {
final PdpUpdate pdpUpdatemessage =
diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupQueryControllerV1.java b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupQueryControllerV1.java
index fb0c2f12..74d6e619 100644
--- a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupQueryControllerV1.java
+++ b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupQueryControllerV1.java
@@ -38,6 +38,7 @@ import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.apache.commons.lang3.tuple.Pair;
+import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.pdp.concepts.PdpGroups;
/**
@@ -83,9 +84,14 @@ public class PdpGroupQueryControllerV1 extends PapRestControllerV1 {
public Response queryGroupDetails(
@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId) {
- final Pair<Status, PdpGroups> pair = provider.fetchPdpGroupDetails();
-
- return addLoggingHeaders(addVersionControlHeaders(Response.status(pair.getLeft())), requestId)
- .entity(pair.getRight()).build();
+ try {
+ final Pair<Status, PdpGroups> pair = provider.fetchPdpGroupDetails();
+ return addLoggingHeaders(addVersionControlHeaders(Response.status(pair.getLeft())), requestId)
+ .entity(pair.getRight()).build();
+ } catch (final PfModelException exp) {
+ return addLoggingHeaders(
+ addVersionControlHeaders(Response.status(exp.getErrorResponse().getResponseCode())), requestId)
+ .entity(exp.getErrorResponse()).build();
+ }
}
}
diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupQueryProvider.java b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupQueryProvider.java
index 4dd42af4..1012fbdc 100644
--- a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupQueryProvider.java
+++ b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupQueryProvider.java
@@ -23,7 +23,14 @@ package org.onap.policy.pap.main.rest;
import javax.ws.rs.core.Response;
import org.apache.commons.lang3.tuple.Pair;
+import org.onap.policy.common.utils.services.Registry;
+import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.pdp.concepts.PdpGroups;
+import org.onap.policy.models.provider.PolicyModelsProvider;
+import org.onap.policy.pap.main.PapConstants;
+import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Provider for PAP component to query details of all PDP groups.
@@ -32,17 +39,23 @@ import org.onap.policy.models.pdp.concepts.PdpGroups;
*/
public class PdpGroupQueryProvider {
+ private static final Logger LOGGER = LoggerFactory.getLogger(PdpGroupQueryProvider.class);
+
/**
* Queries details of all PDP groups.
*
* @return a pair containing the status and the response
+ * @throws PfModelException in case of errors
*/
- public Pair<Response.Status, PdpGroups> fetchPdpGroupDetails() {
-
- /*
- * TODO Fetch all the details from DB and create the PdpGroups object.
- */
+ public Pair<Response.Status, PdpGroups> fetchPdpGroupDetails() throws PfModelException {
- return Pair.of(Response.Status.OK, new PdpGroups());
+ final PdpGroups pdpGroups = new PdpGroups();
+ final PolicyModelsProviderFactoryWrapper modelProviderWrapper =
+ Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
+ try (PolicyModelsProvider databaseProvider = modelProviderWrapper.create()) {
+ pdpGroups.setGroups(databaseProvider.getPdpGroups(null, null));
+ }
+ LOGGER.debug("PdpGroup Query Response - {}", pdpGroups);
+ return Pair.of(Response.Status.OK, pdpGroups);
}
}
diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeControllerV1.java b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeControllerV1.java
index 72e1fe58..5c53bdc3 100644
--- a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeControllerV1.java
+++ b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeControllerV1.java
@@ -28,7 +28,9 @@ import io.swagger.annotations.Authorization;
import io.swagger.annotations.Extension;
import io.swagger.annotations.ExtensionProperty;
import io.swagger.annotations.ResponseHeader;
+
import java.util.UUID;
+
import javax.ws.rs.HeaderParam;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
@@ -36,7 +38,9 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
+
import org.apache.commons.lang3.tuple.Pair;
+import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.pap.concepts.PdpGroupStateChangeResponse;
import org.onap.policy.models.pdp.enums.PdpState;
@@ -89,9 +93,14 @@ public class PdpGroupStateChangeControllerV1 extends PapRestControllerV1 {
@ApiParam(value = "PDP Group Version", required = true) @PathParam("version") final String version,
@ApiParam(value = "PDP Group State", required = true) @QueryParam("state") final PdpState state) {
- final Pair<Status, PdpGroupStateChangeResponse> pair = provider.changeGroupState(groupName, version, state);
-
- return addLoggingHeaders(addVersionControlHeaders(Response.status(pair.getLeft())), requestId)
- .entity(pair.getRight()).build();
+ try {
+ final Pair<Status, PdpGroupStateChangeResponse> pair = provider.changeGroupState(groupName, version, state);
+ return addLoggingHeaders(addVersionControlHeaders(Response.status(pair.getLeft())), requestId)
+ .entity(pair.getRight()).build();
+ } catch (final PfModelException exp) {
+ return addLoggingHeaders(
+ addVersionControlHeaders(Response.status(exp.getErrorResponse().getResponseCode())), requestId)
+ .entity(exp.getErrorResponse()).build();
+ }
}
}
diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeProvider.java b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeProvider.java
index 49f55e2d..f8032ee3 100644
--- a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeProvider.java
+++ b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupStateChangeProvider.java
@@ -20,10 +20,30 @@
package org.onap.policy.pap.main.rest;
+import java.util.ArrayList;
+import java.util.List;
+
import javax.ws.rs.core.Response;
+
import org.apache.commons.lang3.tuple.Pair;
+import org.onap.policy.common.utils.services.Registry;
+import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.pap.concepts.PdpGroupStateChangeResponse;
+import org.onap.policy.models.pdp.concepts.Pdp;
+import org.onap.policy.models.pdp.concepts.PdpGroup;
+import org.onap.policy.models.pdp.concepts.PdpGroupFilter;
+import org.onap.policy.models.pdp.concepts.PdpStateChange;
+import org.onap.policy.models.pdp.concepts.PdpSubGroup;
+import org.onap.policy.models.pdp.concepts.PdpUpdate;
import org.onap.policy.models.pdp.enums.PdpState;
+import org.onap.policy.models.provider.PolicyModelsProvider;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
+import org.onap.policy.pap.main.PapConstants;
+import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
+import org.onap.policy.pap.main.comm.PdpModifyRequestMap;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Provider for PAP component to change state of PDP group.
@@ -32,30 +52,141 @@ import org.onap.policy.models.pdp.enums.PdpState;
*/
public class PdpGroupStateChangeProvider {
+ private static final Logger LOGGER = LoggerFactory.getLogger(PdpGroupStateChangeProvider.class);
+
+ PolicyModelsProviderFactoryWrapper modelProviderWrapper;
+
+ /**
+ * Constructs the object.
+ */
+ public PdpGroupStateChangeProvider() {
+ modelProviderWrapper = Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
+ }
+
/**
* Changes state of a PDP group.
*
* @param groupName name of the PDP group
- * @param version version of the PDP group
+ * @param groupVersion version of the PDP group
* @param pdpGroupState state of the PDP group
* @return a pair containing the status and the response
+ * @throws PfModelException in case of errors
*/
public Pair<Response.Status, PdpGroupStateChangeResponse> changeGroupState(final String groupName,
- final String version, final PdpState pdpGroupState) {
+ final String groupVersion, final PdpState pdpGroupState) throws PfModelException {
- /*
- * TODO Check preconditions - return error if any.
- */
+ switch (pdpGroupState) {
+ case ACTIVE:
+ handleActiveState(groupName, groupVersion);
+ break;
+ case PASSIVE:
+ handlePassiveState(groupName, groupVersion);
+ break;
+ default:
+ throw new PfModelException(Response.Status.BAD_REQUEST,
+ "Only ACTIVE or PASSIVE state changes are allowed");
+ }
+ return Pair.of(Response.Status.OK, new PdpGroupStateChangeResponse());
+ }
- /*
- * TODO Change state - sending state change messages to PDPs and arranging for listeners to complete the state
- * change actions (in the background).
- */
+ private void handleActiveState(final String groupName, final String groupVersion) throws PfModelException {
+ try (PolicyModelsProvider databaseProvider = modelProviderWrapper.create()) {
+ final PdpGroupFilter filter = PdpGroupFilter.builder().name(groupName).groupState(PdpState.ACTIVE).build();
+ final List<PdpGroup> activePdpGroups = databaseProvider.getFilteredPdpGroups(filter);
+ final List<PdpGroup> pdpGroups = databaseProvider.getPdpGroups(groupName, groupVersion);
+ if (activePdpGroups.isEmpty() && !pdpGroups.isEmpty()) {
+ sendPdpMessage(pdpGroups.get(0), PdpState.ACTIVE, databaseProvider);
+ updatePdpGroupAndPdp(databaseProvider, pdpGroups, PdpState.ACTIVE);
+ } else if (!pdpGroups.isEmpty() && !activePdpGroups.isEmpty()
+ && !pdpGroups.get(0).getVersion().equals(activePdpGroups.get(0).getVersion())) {
+ sendPdpMessage(pdpGroups.get(0), PdpState.ACTIVE, databaseProvider);
+ updatePdpGroupAndPdp(databaseProvider, pdpGroups, PdpState.ACTIVE);
+ updatePdpGroup(databaseProvider, activePdpGroups, PdpState.PASSIVE);
+ }
+ }
+ }
- /*
- * TODO Return error if unable to send state change to all PDPs.
- */
+ private void handlePassiveState(final String groupName, final String groupVersion) throws PfModelException {
+ try (PolicyModelsProvider databaseProvider = modelProviderWrapper.create()) {
+ final List<PdpGroup> pdpGroups = databaseProvider.getPdpGroups(groupName, groupVersion);
+ if (!pdpGroups.isEmpty() && !PdpState.PASSIVE.equals(pdpGroups.get(0).getPdpGroupState())) {
+ sendPdpMessage(pdpGroups.get(0), PdpState.PASSIVE, databaseProvider);
+ updatePdpGroupAndPdp(databaseProvider, pdpGroups, PdpState.PASSIVE);
+ }
+ }
+ }
- return Pair.of(Response.Status.OK, new PdpGroupStateChangeResponse());
+ private void updatePdpGroup(final PolicyModelsProvider databaseProvider, final List<PdpGroup> pdpGroups,
+ final PdpState pdpState) throws PfModelException {
+ pdpGroups.get(0).setPdpGroupState(pdpState);
+ databaseProvider.updatePdpGroups(pdpGroups);
+
+ LOGGER.debug("Updated PdpGroup in DB - {} ", pdpGroups);
+ }
+
+ private void updatePdpGroupAndPdp(final PolicyModelsProvider databaseProvider, final List<PdpGroup> pdpGroups,
+ final PdpState pdpState) throws PfModelException {
+ pdpGroups.get(0).setPdpGroupState(pdpState);
+ for (final PdpSubGroup subGroup : pdpGroups.get(0).getPdpSubgroups()) {
+ for (final Pdp pdp : subGroup.getPdpInstances()) {
+ pdp.setPdpState(pdpState);
+ }
+ }
+ databaseProvider.updatePdpGroups(pdpGroups);
+
+ LOGGER.debug("Updated PdpGroup and Pdp in DB - {} ", pdpGroups);
+ }
+
+ private void sendPdpMessage(final PdpGroup pdpGroup, final PdpState pdpState,
+ final PolicyModelsProvider databaseProvider) throws PfModelException {
+
+ for (final PdpSubGroup subGroup : pdpGroup.getPdpSubgroups()) {
+ for (final Pdp pdp : subGroup.getPdpInstances()) {
+ final PdpUpdate pdpUpdatemessage =
+ createPdpUpdateMessage(pdpGroup.getName(), subGroup, pdp.getInstanceId(), databaseProvider);
+ final PdpStateChange pdpStateChangeMessage =
+ createPdpStateChangeMessage(pdpGroup.getName(), subGroup, pdp.getInstanceId(), pdpState);
+ final PdpModifyRequestMap requestMap =
+ Registry.get(PapConstants.REG_PDP_MODIFY_MAP, PdpModifyRequestMap.class);
+ requestMap.addRequest(pdpUpdatemessage, pdpStateChangeMessage);
+ LOGGER.debug("Sent PdpUpdate message - {}", pdpUpdatemessage);
+ LOGGER.debug("Sent PdpStateChange message - {}", pdpStateChangeMessage);
+ }
+ }
+ }
+
+ private PdpUpdate createPdpUpdateMessage(final String pdpGroupName, final PdpSubGroup subGroup,
+ final String pdpInstanceId, final PolicyModelsProvider databaseProvider) throws PfModelException {
+
+ final PdpUpdate update = new PdpUpdate();
+ update.setName(pdpInstanceId);
+ update.setPdpGroup(pdpGroupName);
+ update.setPdpSubgroup(subGroup.getPdpType());
+ update.setPolicies(getToscaPolicies(subGroup, databaseProvider));
+
+ LOGGER.debug("Created PdpUpdate message - {}", update);
+ return update;
+ }
+
+ private List<ToscaPolicy> getToscaPolicies(final PdpSubGroup subGroup, final PolicyModelsProvider databaseProvider)
+ throws PfModelException {
+ final List<ToscaPolicy> policies = new ArrayList<>();
+ for (final ToscaPolicyIdentifier policyIdentifier : subGroup.getPolicies()) {
+ policies.addAll(databaseProvider.getPolicyList(policyIdentifier.getName(), policyIdentifier.getVersion()));
+ }
+ LOGGER.debug("Created ToscaPolicy list - {}", policies);
+ return policies;
+ }
+
+ private PdpStateChange createPdpStateChangeMessage(final String pdpGroupName, final PdpSubGroup subGroup,
+ final String pdpInstanceId, final PdpState pdpState) {
+
+ final PdpStateChange stateChange = new PdpStateChange();
+ stateChange.setName(pdpInstanceId);
+ stateChange.setPdpGroup(pdpGroupName);
+ stateChange.setPdpSubgroup(subGroup.getPdpType());
+ stateChange.setState(pdpState);
+ LOGGER.debug("Created PdpStateChange message - {}", stateChange);
+ return stateChange;
}
}
diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/SessionData.java b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/SessionData.java
index 5bb96b9d..0c83a1c2 100644
--- a/main/src/main/java/org/onap/policy/pap/main/rest/depundep/SessionData.java
+++ b/main/src/main/java/org/onap/policy/pap/main/rest/depundep/SessionData.java
@@ -20,6 +20,7 @@
package org.onap.policy.pap.main.rest.depundep;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
@@ -225,7 +226,11 @@ public class SessionData {
return data.stream().map(GroupData::getCurrentGroup).collect(Collectors.toList());
}
- PdpGroupFilter filter = PdpGroupFilter.builder().policyType(type).groupState(PdpState.ACTIVE).build();
+ final List<ToscaPolicyTypeIdentifier> policyTypeList = new ArrayList<>(1);
+ policyTypeList.add(type);
+
+ PdpGroupFilter filter = PdpGroupFilter.builder().policyTypeList(policyTypeList).matchPolicyTypesExactly(true)
+ .groupState(PdpState.ACTIVE).build();
List<PdpGroup> groups = dao.getFilteredPdpGroups(filter);
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 38d8621e..7de63cc2 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
@@ -31,6 +31,7 @@ import org.onap.policy.common.utils.coder.Coder;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.models.provider.PolicyModelsProviderParameters;
+import org.onap.policy.models.provider.impl.DatabasePolicyModelsProviderImpl;
/**
* Class to hold/create all parameters for test cases.
@@ -169,11 +170,11 @@ public class CommonTestData {
public Map<String, Object> getPolicyModelsProviderParametersMap() {
final Map<String, Object> map = new TreeMap<>();
map.put("name", PolicyModelsProviderParameters.class.getSimpleName());
- map.put("implementation", REST_SERVER_HTTPS);
+ map.put("implementation", DatabasePolicyModelsProviderImpl.class.getCanonicalName());
map.put("databaseUrl", "jdbc:h2:mem:testdb");
map.put("databaseUser", "policy");
map.put("databasePassword", Base64.getEncoder().encodeToString("P01icY".getBytes()));
- map.put("persistenceUnit", "PdpGroupTest");
+ map.put("persistenceUnit", "ToscaConceptTest");
return map;
}
diff --git a/main/src/test/resources/META-INF/persistence.xml b/main/src/test/resources/META-INF/persistence.xml
new file mode 100644
index 00000000..a43afafc
--- /dev/null
+++ b/main/src/test/resources/META-INF/persistence.xml
@@ -0,0 +1,81 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ============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=========================================================
+-->
+
+<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
+ <persistence-unit name="ToscaConceptTest" 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.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" />
+ </properties>
+ </persistence-unit>
+</persistence>