From b6505e6ef2236fec91d010ff789ce19c6450484a Mon Sep 17 00:00:00 2001 From: "a.sreekumar" Date: Tue, 9 Apr 2019 20:48:32 +0000 Subject: Some changes and bug fixes for PdpMessage handling 1) Updating pom file of services-onappf to fix dependency related issues. 2) Some changes to accomodate recent updates. 3) Fix for some issues identified during testing. Change-Id: I8150ed9858902ebbe12321a793770df3fdc7e773 Issue-ID: POLICY-1453 Signed-off-by: a.sreekumar --- .../policy/apex/starter/ApexStarterActivator.java | 11 +-- .../apex/starter/engine/ApexEngineHandler.java | 13 ++-- .../apex/starter/handler/PdpMessageHandler.java | 8 +-- .../handler/PdpStateChangeMessageHandler.java | 54 ++------------- .../starter/handler/PdpUpdateMessageHandler.java | 81 ++++++++++++++++------ .../starter/comm/TestPdpStateChangeListener.java | 14 ---- 6 files changed, 79 insertions(+), 102 deletions(-) (limited to 'services/services-onappf/src') diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/ApexStarterActivator.java b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/ApexStarterActivator.java index a6bd702aa..b33d792a0 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/ApexStarterActivator.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/ApexStarterActivator.java @@ -40,6 +40,7 @@ import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher; import org.onap.policy.common.utils.services.Registry; import org.onap.policy.common.utils.services.ServiceManager; import org.onap.policy.common.utils.services.ServiceManagerException; +import org.onap.policy.models.pdp.enums.PdpMessageType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -78,7 +79,7 @@ public class ApexStarterActivator { // TODO: instanceId currently set as a random string, could be fetched from actual deployment final int random = (int) (Math.random() * 100); final String instanceId = "apex_" + random; - + LOGGER.debug("ApexStarterActivator initializing with instance id:" + instanceId); try { this.apexStarterParameterGroup = apexStarterParameterGroup; this.msgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES); @@ -110,11 +111,11 @@ public class ApexStarterActivator { apexStarterParameterGroup.getPdpStatusParameters().getTimeIntervalMs())), () -> stopAndRemovePdpStatusPublisher()) .addAction("Register pdp update listener", - () -> msgDispatcher.register("PDP_UPDATE", pdpUpdateListener), - () -> msgDispatcher.unregister("PDP_UPDATE")) + () -> msgDispatcher.register(PdpMessageType.PDP_UPDATE.name(), pdpUpdateListener), + () -> msgDispatcher.unregister(PdpMessageType.PDP_UPDATE.name())) .addAction("Register pdp state change request dispatcher", - () -> msgDispatcher.register("PDP_STATE_CHANGE", pdpStateChangeListener), - () -> msgDispatcher.unregister("PDP_STATE_CHANGE")) + () -> msgDispatcher.register(PdpMessageType.PDP_STATE_CHANGE.name(), pdpStateChangeListener), + () -> msgDispatcher.unregister(PdpMessageType.PDP_STATE_CHANGE.name())) .addAction("Message Dispatcher", () -> registerMsgDispatcher(), () -> unregisterMsgDispatcher()); diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/engine/ApexEngineHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/engine/ApexEngineHandler.java index 4682a3bc5..e58b0db76 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/engine/ApexEngineHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/engine/ApexEngineHandler.java @@ -20,12 +20,13 @@ package org.onap.policy.apex.starter.engine; +import com.google.gson.JsonObject; + import java.io.IOException; import java.io.StringReader; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Map; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; import org.onap.policy.apex.service.engine.main.ApexMain; @@ -53,16 +54,15 @@ public class ApexEngineHandler { * @throws ApexStarterException */ - @SuppressWarnings("unchecked") public ApexEngineHandler(final String properties) throws ApexStarterException { final StandardCoder standardCoder = new StandardCoder(); - Map> body; + JsonObject body; try { - body = standardCoder.decode(new StringReader(properties), Map.class); + body = standardCoder.decode(new StringReader(properties), JsonObject.class); } catch (final CoderException e) { throw new ApexStarterException(e); } - final Map engineServiceParameters = body.get("engineServiceParameters"); + final JsonObject engineServiceParameters = body.get("engineServiceParameters").getAsJsonObject(); final String policyModel = engineServiceParameters.get("policy_type_impl").toString(); engineServiceParameters.remove("policy_type_impl"); final String apexConfig = body.toString(); @@ -70,8 +70,8 @@ public class ApexEngineHandler { final String modelFilePath = createFile(policyModel, "modelFile"); final String apexConfigFilePath = createFile(apexConfig, "apexConfigFile"); - final String[] apexArgs = { "-rfr", "target/classes", "-c", apexConfigFilePath, "-m", modelFilePath }; + LOGGER.debug("Starting apex engine."); apexMain = new ApexMain(apexArgs); } @@ -100,6 +100,7 @@ public class ApexEngineHandler { public void shutdown() throws ApexStarterException { try { + LOGGER.debug("Shutting down apex engine."); apexMain.shutdown(); apexMain = null; } catch (final ApexException e) { diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/PdpMessageHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/PdpMessageHandler.java index a022a8a08..be76938f3 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/PdpMessageHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/PdpMessageHandler.java @@ -96,12 +96,8 @@ public class PdpMessageHandler { pdpStatus.setDescription(pdpStatusContext.getDescription()); pdpStatus.setSupportedPolicyTypes(pdpStatusContext.getSupportedPolicyTypes()); pdpStatus.setPolicies(pdpStatusContext.getPolicies()); - if (null != pdpStatusContext.getPdpGroup()) { - pdpStatus.setPdpGroup(pdpStatusContext.getPdpGroup()); - } - if (null != pdpStatusContext.getPdpSubgroup()) { - pdpStatus.setPdpSubgroup(pdpStatusContext.getPdpSubgroup()); - } + pdpStatus.setPdpGroup(pdpStatusContext.getPdpGroup()); + pdpStatus.setPdpSubgroup(pdpStatusContext.getPdpSubgroup()); return pdpStatus; } diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/PdpStateChangeMessageHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/PdpStateChangeMessageHandler.java index c0ec690d8..9f97fb24b 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/PdpStateChangeMessageHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/PdpStateChangeMessageHandler.java @@ -51,11 +51,9 @@ public class PdpStateChangeMessageHandler { final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER); final PdpMessageHandler pdpMessageHandler = new PdpMessageHandler(); PdpResponseDetails pdpResponseDetails = null; - if (isMessageRelevant(pdpStateChangeMsg, pdpStatusContext)) { + if (pdpStateChangeMsg.appliesTo(pdpStatusContext.getName(), pdpStatusContext.getPdpGroup(), + pdpStatusContext.getPdpSubgroup())) { switch (pdpStateChangeMsg.getState()) { - case TERMINATED: - pdpResponseDetails = handleTerminatedState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler); - break; case PASSIVE: pdpResponseDetails = handlePassiveState(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler); break; @@ -67,23 +65,11 @@ public class PdpStateChangeMessageHandler { } final PdpStatus pdpStatus = pdpMessageHandler.createPdpStatusFromContext(); pdpStatus.setResponse(pdpResponseDetails); + pdpStatus.setDescription("Pdp status response message for PdpStateChange"); pdpStatusPublisher.send(pdpStatus); } } - /** - * Check if the pdp state change message is meant for this pdp. - * - * @param pdpStateChangeMsg - * @param pdpStatusContext - * @return boolean value if relevant or not - */ - private boolean isMessageRelevant(final PdpStateChange pdpStateChangeMsg, final PdpStatus pdpStatusContext) { - return pdpStatusContext.getName().equals(pdpStateChangeMsg.getName()) || (null == pdpStateChangeMsg.getName() - && pdpStateChangeMsg.getPdpGroup().equals(pdpStatusContext.getPdpGroup()) - && pdpStateChangeMsg.getPdpSubgroup().equals(pdpStatusContext.getPdpSubgroup())); - } - /** * Method to handle when the new state from pap is active. * @@ -108,7 +94,7 @@ public class PdpStateChangeMessageHandler { // assumed that the apex policies list contains only one entry. final ApexEngineHandler apexEngineHandler = new ApexEngineHandler((String) policies.get(0).getProperties().get("content")); - Registry.register(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler); + Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler); pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(), PdpResponseStatus.SUCCESS, "Apex engine started. State changed to active."); pdpStatusContext.setState(PdpState.ACTIVE); @@ -150,36 +136,4 @@ public class PdpStateChangeMessageHandler { } return pdpResponseDetails; } - - /** - * Method to handle when the new state from pap is terminated. - * - * @param pdpStateChangeMsg - * @param pdpStatusPublisher - * @param pdpMessageHandler - * @return pdpResponseDetails - */ - private PdpResponseDetails handleTerminatedState(final PdpStateChange pdpStateChangeMsg, - final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) { - - PdpResponseDetails pdpResponseDetails; - if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) { - final ApexEngineHandler apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER); - try { - apexEngineHandler.shutdown(); - pdpStatusContext.setState(PdpState.PASSIVE); - pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(), - PdpResponseStatus.SUCCESS, "Apex Engine stopped. State changed to passive."); - } catch (final Exception e) { - pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(), - PdpResponseStatus.FAIL, - "Stopping apex engine failed. State cannot be changed." + e.getMessage()); - } - } else { - pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(), - PdpResponseStatus.SUCCESS, "Pdp already in passive state"); - } - return pdpResponseDetails; - - } } diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/PdpUpdateMessageHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/PdpUpdateMessageHandler.java index 835c2993a..5198c229e 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/PdpUpdateMessageHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/PdpUpdateMessageHandler.java @@ -32,6 +32,7 @@ import org.onap.policy.models.pdp.concepts.PdpResponseDetails; import org.onap.policy.models.pdp.concepts.PdpStatus; import org.onap.policy.models.pdp.concepts.PdpUpdate; import org.onap.policy.models.pdp.enums.PdpResponseStatus; +import org.onap.policy.models.pdp.enums.PdpState; /** * This class supports the handling of pdp update messages. @@ -40,7 +41,6 @@ import org.onap.policy.models.pdp.enums.PdpResponseStatus; */ public class PdpUpdateMessageHandler { - /** * Method which handles a pdp update event from PAP. * @@ -49,8 +49,9 @@ public class PdpUpdateMessageHandler { public void handlePdpUpdateEvent(final PdpUpdate pdpUpdateMsg) { final PdpMessageHandler pdpMessageHandler = new PdpMessageHandler(); final PdpStatus pdpStatusContext = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class); - PdpResponseDetails pdpResponseDetails; - if (pdpStatusContext.getName().equals(pdpUpdateMsg.getName())) { + PdpResponseDetails pdpResponseDetails = null; + if (pdpUpdateMsg.appliesTo(pdpStatusContext.getName(), pdpStatusContext.getPdpGroup(), + pdpStatusContext.getPdpSubgroup())) { final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER); if (checkIfAlreadyHandled(pdpUpdateMsg, pdpStatusContext, pdpStatusPublisher.getInterval())) { pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(), @@ -64,31 +65,67 @@ public class PdpUpdateMessageHandler { pdpStatusContext.setPdpSubgroup(pdpUpdateMsg.getPdpSubgroup()); pdpStatusContext .setPolicies(new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies())); - if (pdpUpdateMsg.getPolicies().isEmpty()) { - final ApexEngineHandler apexEngineHandler = - Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER); - if (apexEngineHandler.isApexEngineRunning()) { - try { - apexEngineHandler.shutdown(); - } catch (final ApexStarterException e) { - pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(), - PdpResponseStatus.FAIL, - "Pdp update failed as the policies couldn't be undeployed."); - } - } + if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) { + pdpResponseDetails = + startOrStopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler, pdpStatusContext); } Registry.registerOrReplace(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST, pdpUpdateMsg.getPolicies()); - pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(), - PdpResponseStatus.SUCCESS, "Pdp update successful."); + if (null == pdpResponseDetails) { + pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(), + PdpResponseStatus.SUCCESS, "Pdp update successful."); + } } final PdpStatusPublisher pdpStatusPublisherTemp = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER); final PdpStatus pdpStatus = pdpMessageHandler.createPdpStatusFromContext(); pdpStatus.setResponse(pdpResponseDetails); + pdpStatus.setDescription("Pdp status response message for PdpUpdate"); pdpStatusPublisherTemp.send(pdpStatus); } } + /** + * Method to start or stop apex engine based on the list of policies received from pap. When current state is + * active, if PAP sends PdpUpdate with empty policies list, stop apex engine, or, if there is a change in policies, + * stop the current running policies and the deploy the new ones. + * + * @param pdpUpdateMsg + * @param pdpMessageHandler + * @param pdpStatusContext + * @return pdpResponseDetails + */ + private PdpResponseDetails startOrStopApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg, + final PdpMessageHandler pdpMessageHandler, final PdpStatus pdpStatusContext) { + PdpResponseDetails pdpResponseDetails = null; + if (pdpUpdateMsg.getPolicies().isEmpty()) { + final ApexEngineHandler apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER); + if (apexEngineHandler.isApexEngineRunning()) { + try { + apexEngineHandler.shutdown(); + } catch (final ApexStarterException e) { + pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(), + PdpResponseStatus.FAIL, "Pdp update failed as the policies couldn't be undeployed."); + } + } + } else { + try { + ApexEngineHandler apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER); + if (apexEngineHandler.isApexEngineRunning()) { + apexEngineHandler.shutdown(); + } + apexEngineHandler = new ApexEngineHandler( + (String) pdpUpdateMsg.getPolicies().get(0).getProperties().get("content")); + Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler); + pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(), + PdpResponseStatus.SUCCESS, "Apex engine started and policies are running."); + } catch (final ApexStarterException e) { + pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(), + PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage()); + } + } + return pdpResponseDetails; + } + /** * Method checks of the Pdp update message is already handled by checking the values in the context. * @@ -104,10 +141,12 @@ public class PdpUpdateMessageHandler { && null != pdpStatusContext.getPdpSubgroup() && pdpStatusContext.getPdpSubgroup().equals(pdpUpdateMsg.getPdpSubgroup()) && null != pdpStatusContext.getPolicies() - && pdpStatusContext.getPolicies() - .containsAll(new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies())) - && null != pdpUpdateMsg.getPdpHeartbeatIntervalMs() && pdpUpdateMsg.getPdpHeartbeatIntervalMs() > 0 - && interval == pdpUpdateMsg.getPdpHeartbeatIntervalMs(); + && new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()) + .equals(pdpStatusContext.getPolicies()) + && (null == pdpUpdateMsg.getPdpHeartbeatIntervalMs() + || (null != pdpUpdateMsg.getPdpHeartbeatIntervalMs() + && pdpUpdateMsg.getPdpHeartbeatIntervalMs() > 0 + && interval == pdpUpdateMsg.getPdpHeartbeatIntervalMs())); } /** diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/comm/TestPdpStateChangeListener.java b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/comm/TestPdpStateChangeListener.java index b25d473da..d0a85f9f2 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/comm/TestPdpStateChangeListener.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/starter/comm/TestPdpStateChangeListener.java @@ -168,18 +168,4 @@ public class TestPdpStateChangeListener { assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState()); } - - @Test - public void testPdpStateChangeMessageListener_passivetoterminated() { - final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT); - pdpStatus.setState(PdpState.PASSIVE); - performPdpUpdate(pdpStatus.getName()); - final PdpStateChange pdpStateChangeMsg = new PdpStateChange(); - pdpStateChangeMsg.setState(PdpState.TERMINATED); - pdpStateChangeMsg.setPdpGroup("pdpGroup"); - pdpStateChangeMsg.setPdpSubgroup("pdpSubgroup"); - pdpStateChangeMsg.setName(pdpStatus.getName()); - pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg); - assertEquals(pdpStatus.getState(), PdpState.PASSIVE); - } } -- cgit 1.2.3-korg