summaryrefslogtreecommitdiffstats
path: root/main
diff options
context:
space:
mode:
authorramverma <ram.krishna.verma@est.tech>2019-04-04 08:57:18 +0000
committerramverma <ram.krishna.verma@est.tech>2019-04-04 08:57:18 +0000
commit0621ae5c8a1dfcfa81b678ae6cc8679b88f58cc4 (patch)
tree679c219fbffc39b876de0c3f4d7ee9c6ef8115fb /main
parent5769b4d5d7d8a7bf9788b2bbda781a9384df7626 (diff)
Adding more code to pdp registration handler
1) Adding more code to pdp registration handler. 2) Fixing few broken files. Change-Id: Ic8b68f95f7022ce0572345ea358ff380ec8a3e61 Issue-ID: POLICY-1443 Signed-off-by: ramverma <ram.krishna.verma@est.tech>
Diffstat (limited to 'main')
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/comm/PdpStatusMessageHandler.java73
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeployControllerV1.java4
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeployProvider.java4
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployControllerV1.java16
4 files changed, 67 insertions, 30 deletions
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 45452356..c25ef97d 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
@@ -30,12 +30,15 @@ import org.onap.policy.models.base.PfModelException;
import org.onap.policy.models.pdp.concepts.Pdp;
import org.onap.policy.models.pdp.concepts.PdpGroup;
import org.onap.policy.models.pdp.concepts.PdpStateChange;
+import org.onap.policy.models.pdp.concepts.PdpStatistics;
import org.onap.policy.models.pdp.concepts.PdpStatus;
import org.onap.policy.models.pdp.concepts.PdpSubGroup;
import org.onap.policy.models.pdp.concepts.PdpUpdate;
+import org.onap.policy.models.pdp.concepts.ToscaPolicyIdentifier;
import org.onap.policy.models.pdp.concepts.ToscaPolicyTypeIdentifier;
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.pap.main.PapConstants;
import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
import org.onap.policy.pap.main.PolicyPapException;
@@ -61,9 +64,6 @@ public class PdpStatusMessageHandler {
final PolicyModelsProviderFactoryWrapper modelProviderWrapper =
Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
try (PolicyModelsProvider databaseProvider = modelProviderWrapper.create()) {
- // TODO: remove the init() call when PolicyModelProviderFactory changes to call init() before returning the
- // object
- databaseProvider.init();
if (message.getPdpGroup().isEmpty() && message.getPdpSubgroup().isEmpty()) {
handlePdpRegistration(message, databaseProvider);
} else {
@@ -99,7 +99,7 @@ public class PdpStatusMessageHandler {
if (!findPdpInstance(message, subGroup.get()).isPresent()) {
updatePdpSubGroup(pdpGroup, subGroup.get(), message, databaseProvider);
}
- sendPdpMessage(pdpGroup.getName(), subGroup.get(), message.getInstance(), null);
+ sendPdpMessage(pdpGroup.getName(), subGroup.get(), message.getInstance(), null, databaseProvider);
pdpGroupFound = true;
break;
}
@@ -145,7 +145,7 @@ public class PdpStatusMessageHandler {
if (pdpSubgroup.isPresent()) {
pdpInstance = findPdpInstance(message, pdpSubgroup.get());
if (pdpInstance.isPresent()) {
- processPdpDetails(message, pdpSubgroup, pdpInstance, pdpGroup);
+ processPdpDetails(message, pdpSubgroup.get(), pdpInstance.get(), pdpGroup, databaseProvider);
pdpInstanceFound = true;
}
}
@@ -179,15 +179,16 @@ public class PdpStatusMessageHandler {
return Optional.ofNullable(pdpInstance);
}
- private void processPdpDetails(final PdpStatus message, final Optional<PdpSubGroup> pdpSubgroup,
- final Optional<Pdp> pdpInstance, final PdpGroup pdpGroup) {
- if (validatePdpDetails(message, pdpGroup, pdpSubgroup.get(), pdpInstance.get())) {
- LOGGER.debug("PdpInstance details are correct. Saving current state in DB - {}", pdpInstance.get());
- // TODO: details are correct save health & statistics details in DB
+ 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)) {
+ LOGGER.debug("PdpInstance details are correct. Saving current state in DB - {}", pdpInstance);
+ updatePdpHealthStatus(message, pdpSubgroup, pdpInstance, pdpGroup, databaseProvider);
+ updatePdpStatistics(message, pdpSubgroup, pdpInstance, pdpGroup, databaseProvider);
} else {
- LOGGER.debug("PdpInstance details are not correct. Sending PdpUpdate message - {}", pdpInstance.get());
- sendPdpMessage(pdpGroup.getName(), pdpSubgroup.get(), pdpInstance.get().getInstanceId(),
- pdpInstance.get().getPdpState());
+ LOGGER.debug("PdpInstance details are not correct. Sending PdpUpdate message - {}", pdpInstance);
+ sendPdpMessage(pdpGroup.getName(), pdpSubgroup, pdpInstance.getInstanceId(), pdpInstance.getPdpState(),
+ databaseProvider);
}
}
@@ -201,9 +202,35 @@ public class PdpStatusMessageHandler {
&& message.getPdpType().equals(subGroup.getPdpType());
}
+ private void updatePdpHealthStatus(final PdpStatus message, final PdpSubGroup pdpSubgroup, final Pdp pdpInstance,
+ final PdpGroup pdpGroup, final PolicyModelsProvider databaseProvider) throws PfModelException {
+ pdpInstance.setHealthy(message.getHealthy());
+ databaseProvider.updatePdp(pdpGroup.getName(), pdpGroup.getVersion(), pdpSubgroup.getPdpType(), pdpInstance);
+
+ 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.getInstance());
+ 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 PdpUpdate pdpUpdatemessage = createPdpUpdateMessage(pdpGroupName, subGroup, pdpInstanceId);
+ final PdpState pdpState, final PolicyModelsProvider databaseProvider) throws PfModelException {
+ final PdpUpdate pdpUpdatemessage =
+ createPdpUpdateMessage(pdpGroupName, subGroup, pdpInstanceId, databaseProvider);
final PdpStateChange pdpStateChangeMessage =
createPdpStateChangeMessage(pdpGroupName, subGroup, pdpInstanceId, pdpState);
final PdpModifyRequestMap requestMap = Registry.get(PapConstants.REG_PDP_MODIFY_MAP, PdpModifyRequestMap.class);
@@ -213,18 +240,28 @@ public class PdpStatusMessageHandler {
}
private PdpUpdate createPdpUpdateMessage(final String pdpGroupName, final PdpSubGroup subGroup,
- final String pdpInstanceId) {
+ final String pdpInstanceId, final PolicyModelsProvider databaseProvider) throws PfModelException {
final PdpUpdate update = new PdpUpdate();
update.setName(pdpInstanceId);
update.setPdpGroup(pdpGroupName);
update.setPdpSubgroup(subGroup.getPdpType());
- // TODO: create code for fetching ToscaPolicy from DB and setting here.
- // update.setPolicies(subGroup.getPolicies());
+ 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) {
diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeployControllerV1.java b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeployControllerV1.java
index b60f6d11..d67974dd 100644
--- a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeployControllerV1.java
+++ b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeployControllerV1.java
@@ -38,8 +38,8 @@ 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.pap.concepts.PdpDeployPolicies;
import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse;
-import org.onap.policy.models.pap.concepts.PdpPolicies;
import org.onap.policy.models.pdp.concepts.PdpGroups;
/**
@@ -124,7 +124,7 @@ public class PdpGroupDeployControllerV1 extends PapRestControllerV1 {
public Response deployPolicies(@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) UUID requestId,
@ApiParam(value = "PDP Policies; only the name and policyVersion are required",
- required = true) PdpPolicies policies) {
+ required = true) PdpDeployPolicies policies) {
Pair<Status, PdpGroupDeployResponse> pair = provider.deployPolicies(policies);
diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeployProvider.java b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeployProvider.java
index dc38e01b..f0636fc6 100644
--- a/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeployProvider.java
+++ b/main/src/main/java/org/onap/policy/pap/main/rest/PdpGroupDeployProvider.java
@@ -23,8 +23,8 @@ package org.onap.policy.pap.main.rest;
import javax.ws.rs.core.Response;
import org.apache.commons.lang3.tuple.Pair;
+import org.onap.policy.models.pap.concepts.PdpDeployPolicies;
import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse;
-import org.onap.policy.models.pap.concepts.PdpPolicies;
import org.onap.policy.models.pdp.concepts.PdpGroups;
/**
@@ -63,7 +63,7 @@ public class PdpGroupDeployProvider {
* @param policies PDP policies
* @return a pair containing the status and the response
*/
- public Pair<Response.Status, PdpGroupDeployResponse> deployPolicies(PdpPolicies policies) {
+ public Pair<Response.Status, PdpGroupDeployResponse> deployPolicies(PdpDeployPolicies policies) {
/*
* TODO Lock for updates - return error if already locked.
diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployControllerV1.java b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployControllerV1.java
index e02e85c1..18e17b09 100644
--- a/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployControllerV1.java
+++ b/main/src/test/java/org/onap/policy/pap/main/rest/TestPdpGroupDeployControllerV1.java
@@ -32,11 +32,11 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.junit.Test;
+import org.onap.policy.models.pap.concepts.PdpDeployPolicies;
import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse;
-import org.onap.policy.models.pap.concepts.PdpPolicies;
-import org.onap.policy.models.pap.concepts.Policy;
import org.onap.policy.models.pdp.concepts.PdpGroup;
import org.onap.policy.models.pdp.concepts.PdpSubGroup;
+import org.onap.policy.models.pdp.concepts.ToscaPolicyIdentifierOptVersion;
public class TestPdpGroupDeployControllerV1 extends CommonPapRestServer {
@@ -70,7 +70,7 @@ public class TestPdpGroupDeployControllerV1 extends CommonPapRestServer {
@Test
public void testDeployPolicies() throws Exception {
- final Entity<PdpPolicies> entgrp = makePdpPoliciesEntity();
+ final Entity<PdpDeployPolicies> entgrp = makePdpPoliciesEntity();
final Invocation.Builder invocationBuilder = sendRequest(DEPLOY_POLICIES_ENDPOINT);
Response rawresp = invocationBuilder.post(entgrp);
@@ -100,15 +100,15 @@ public class TestPdpGroupDeployControllerV1 extends CommonPapRestServer {
return Entity.entity(group, MediaType.APPLICATION_JSON);
}
- private Entity<PdpPolicies> makePdpPoliciesEntity() {
- final Policy pol1 = new Policy();
+ private Entity<PdpDeployPolicies> makePdpPoliciesEntity() {
+ final ToscaPolicyIdentifierOptVersion pol1 = new ToscaPolicyIdentifierOptVersion();
pol1.setName("policy-a");
- pol1.setPolicyVersion("1");
+ pol1.setVersion("1");
- final Policy pol2 = new Policy();
+ final ToscaPolicyIdentifierOptVersion pol2 = new ToscaPolicyIdentifierOptVersion();
pol2.setName("policy-b");
- final PdpPolicies policies = new PdpPolicies();
+ final PdpDeployPolicies policies = new PdpDeployPolicies();
policies.setPolicies(Arrays.asList(pol1, pol2));
return Entity.entity(policies, MediaType.APPLICATION_JSON);