diff options
Diffstat (limited to 'services/services-onappf')
15 files changed, 1136 insertions, 181 deletions
diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java index 4f68b90ae..1953939b7 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java @@ -21,17 +21,21 @@ package org.onap.policy.apex.services.onappf.handler; import com.google.gson.JsonObject; - import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; - +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; import org.onap.policy.apex.service.engine.main.ApexMain; import org.onap.policy.apex.services.onappf.exception.ApexStarterException; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -46,31 +50,41 @@ public class ApexEngineHandler { private ApexMain apexMain; /** - * Constructs the object. Extracts the apex config and model files and instantiates the apex engine. + * Constructs the object. Extracts the config and model files from each policy and instantiates the apex engine. * - * @param properties the properties which contains the policies and configurations received from pap - * @throws ApexStarterException if the apex engine instantiation failed using the properties passed + * @param policies the list of policies + * @throws ApexStarterException if the apex engine instantiation failed using the policies passed */ - public ApexEngineHandler(final Object properties) throws ApexStarterException { - final StandardCoder standardCoder = new StandardCoder(); - String policyModel; - String apexConfig; - try { - JsonObject body = standardCoder.decode(standardCoder.encode(properties), JsonObject.class); - final JsonObject engineServiceParameters = body.get("engineServiceParameters").getAsJsonObject(); - policyModel = standardCoder.encode(engineServiceParameters.get("policy_type_impl")); - engineServiceParameters.remove("policy_type_impl"); - apexConfig = standardCoder.encode(body); - } catch (final CoderException e) { - throw new ApexStarterException(e); - } + public ApexEngineHandler(List<ToscaPolicy> policies) throws ApexStarterException { + Map<ToscaPolicyIdentifier, String[]> policyArgsMap = new LinkedHashMap<>(); + for (ToscaPolicy policy : policies) { + Object properties = policy.getProperties().get("content"); + final StandardCoder standardCoder = new StandardCoder(); + String policyModel; + String apexConfig; + try { + JsonObject body = standardCoder.decode(standardCoder.encode(properties), JsonObject.class); + final JsonObject engineServiceParameters = body.get("engineServiceParameters").getAsJsonObject(); + policyModel = standardCoder.encode(engineServiceParameters.get("policy_type_impl")); + engineServiceParameters.remove("policy_type_impl"); + apexConfig = standardCoder.encode(body); + } catch (final CoderException e) { + throw new ApexStarterException(e); + } + + final String modelFilePath = createFile(policyModel, "modelFile"); - final String modelFilePath = createFile(policyModel, "modelFile"); + final String apexConfigFilePath = createFile(apexConfig, "apexConfigFile"); + final String[] apexArgs = { "-c", apexConfigFilePath, "-m", modelFilePath }; + policyArgsMap.put(policy.getIdentifier(), apexArgs); + } - final String apexConfigFilePath = createFile(apexConfig, "apexConfigFile"); - final String[] apexArgs = { "-c", apexConfigFilePath, "-m", modelFilePath }; LOGGER.debug("Starting apex engine."); - apexMain = new ApexMain(apexArgs); + try { + apexMain = new ApexMain(policyArgsMap); + } catch (ApexException e) { + throw new ApexStarterException(e); + } } /** @@ -100,6 +114,13 @@ public class ApexEngineHandler { } /** + * Method that return the list of running policies in the apex engine. + */ + public List<ToscaPolicyIdentifier> getRunningPolicies() { + return new ArrayList<>(apexMain.getApexParametersMap().keySet()); + } + + /** * Method to shut down the apex engine. */ public void shutdown() throws ApexStarterException { diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpStateChangeMessageHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpStateChangeMessageHandler.java index 8658150c0..fd95b47b7 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpStateChangeMessageHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpStateChangeMessageHandler.java @@ -20,6 +20,7 @@ package org.onap.policy.apex.services.onappf.handler; +import java.util.HashSet; import java.util.List; import org.onap.policy.apex.services.onappf.ApexStarterConstants; import org.onap.policy.apex.services.onappf.comm.PdpStatusPublisher; @@ -31,6 +32,7 @@ import org.onap.policy.models.pdp.concepts.PdpStatus; import org.onap.policy.models.pdp.enums.PdpResponseStatus; import org.onap.policy.models.pdp.enums.PdpState; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -93,25 +95,55 @@ public class PdpStateChangeMessageHandler { pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(), PdpResponseStatus.SUCCESS, "State changed to active. No policies found."); } else { - try { - // assumed that the apex policies list contains only one entry. - final ApexEngineHandler apexEngineHandler = - new ApexEngineHandler(policies.get(0).getProperties().get("content")); - Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler); - if (apexEngineHandler.isApexEngineRunning()) { - pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(), + pdpResponseDetails = startApexEngine(pdpStateChangeMsg, pdpStatusContext, pdpMessageHandler, policies); + } + } + return pdpResponseDetails; + } + + /** + * Method to start apex engine. + * + * @param pdpStateChangeMsg pdp state change message + * @param pdpStatusContext pdp status in memory + * @param pdpMessageHandler the pdp message handler + * @param policies list of policies + * @return pdp response details + */ + private PdpResponseDetails startApexEngine(final PdpStateChange pdpStateChangeMsg, final PdpStatus pdpStatusContext, + final PdpMessageHandler pdpMessageHandler, final List<ToscaPolicy> policies) { + PdpResponseDetails pdpResponseDetails; + try { + final ApexEngineHandler apexEngineHandler = new ApexEngineHandler(policies); + Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler); + if (apexEngineHandler.isApexEngineRunning()) { + List<ToscaPolicyIdentifier> runningPolicies = apexEngineHandler.getRunningPolicies(); + // only the policies which are succesfully executed should be there in the heartbeat + pdpStatusContext.setPolicies(runningPolicies); + if (new HashSet<>(runningPolicies) + .equals(new HashSet<>(pdpMessageHandler.getToscaPolicyIdentifiers(policies)))) { + pdpResponseDetails = + pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(), PdpResponseStatus.SUCCESS, "Apex engine started. State changed to active."); - pdpStatusContext.setState(PdpState.ACTIVE); - } else { - pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(), - PdpResponseStatus.FAIL, "Apex engine failed to start. State cannot be changed to active."); + } else { + StringBuilder message = new StringBuilder( + "Apex engine started. But, only the following polices are running - "); + for (ToscaPolicyIdentifier policy : runningPolicies) { + message.append(policy.getName()).append(":").append(policy.getVersion()).append(" "); } - } catch (final ApexStarterException e) { - LOGGER.error("Pdp update failed as the policies couldn't be undeployed.", e); - pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(), - PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage()); + message.append(". Other policies failed execution. Please see the logs for more details."); + pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails( + pdpStateChangeMsg.getRequestId(), PdpResponseStatus.SUCCESS, message.toString()); } + pdpStatusContext.setState(PdpState.ACTIVE); + } else { + pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(), + PdpResponseStatus.FAIL, "Apex engine failed to start. State cannot be changed to active."); } + } catch (final ApexStarterException e) { + LOGGER.error("Pdp State Change failed.", e); + pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(), + PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage()); } return pdpResponseDetails; } diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java index d807dc50e..ecc0bec21 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java @@ -20,6 +20,8 @@ package org.onap.policy.apex.services.onappf.handler; +import java.util.Collections; +import java.util.HashSet; import java.util.List; import org.onap.policy.apex.services.onappf.ApexStarterConstants; import org.onap.policy.apex.services.onappf.comm.PdpStatusPublisher; @@ -31,6 +33,7 @@ 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; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -54,27 +57,11 @@ public class PdpUpdateMessageHandler { 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)) { pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(), PdpResponseStatus.SUCCESS, "Pdp already updated"); } else { - if (null != pdpUpdateMsg.getPdpHeartbeatIntervalMs() && pdpUpdateMsg.getPdpHeartbeatIntervalMs() > 0 - && pdpStatusPublisher.getInterval() != pdpUpdateMsg.getPdpHeartbeatIntervalMs()) { - updateInterval(pdpUpdateMsg.getPdpHeartbeatIntervalMs()); - } - pdpStatusContext.setPdpGroup(pdpUpdateMsg.getPdpGroup()); - pdpStatusContext.setPdpSubgroup(pdpUpdateMsg.getPdpSubgroup()); - pdpStatusContext - .setPolicies(new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies())); - if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) { - pdpResponseDetails = startOrStopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler); - } - Registry.registerOrReplace(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST, pdpUpdateMsg.getPolicies()); - if (null == pdpResponseDetails) { - pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(), - PdpResponseStatus.SUCCESS, "Pdp update successful."); - } + pdpResponseDetails = handlePdpUpdate(pdpUpdateMsg, pdpMessageHandler, pdpStatusContext); } final PdpStatusPublisher pdpStatusPublisherTemp = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER); @@ -86,6 +73,47 @@ public class PdpUpdateMessageHandler { } /** + * Method to do pdp update. + * + * @param pdpUpdateMsg the pdp update message + * @param pdpMessageHandler the message handler + * @param pdpStatusContext the pdp status in memory + * @return pdpResponseDetails the pdp response + */ + private PdpResponseDetails handlePdpUpdate(final PdpUpdate pdpUpdateMsg, final PdpMessageHandler pdpMessageHandler, + final PdpStatus pdpStatusContext) { + PdpResponseDetails pdpResponseDetails = null; + final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER); + if (null != pdpUpdateMsg.getPdpHeartbeatIntervalMs() && pdpUpdateMsg.getPdpHeartbeatIntervalMs() > 0 + && pdpStatusPublisher.getInterval() != pdpUpdateMsg.getPdpHeartbeatIntervalMs()) { + updateInterval(pdpUpdateMsg.getPdpHeartbeatIntervalMs()); + } + pdpStatusContext.setPdpGroup(pdpUpdateMsg.getPdpGroup()); + pdpStatusContext.setPdpSubgroup(pdpUpdateMsg.getPdpSubgroup()); + pdpStatusContext + .setPolicies(new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies())); + Registry.registerOrReplace(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST, pdpUpdateMsg.getPolicies()); + if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) { + pdpResponseDetails = startOrStopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler); + + ApexEngineHandler apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER); + // in hearbeat while in active state, only the policies which are running should be there. + // if some policy fails, that shouldn't go in the heartbeat. + // If no policies are running, then the policy list in the heartbeat can be empty + if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) { + pdpStatusContext.setPolicies(apexEngineHandler.getRunningPolicies()); + } else { + pdpStatusContext.setPolicies(Collections.emptyList()); + } + } + if (null == pdpResponseDetails) { + pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(), + PdpResponseStatus.SUCCESS, "Pdp update successful."); + } + return pdpResponseDetails; + } + + /** * 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. @@ -118,6 +146,8 @@ public class PdpUpdateMessageHandler { if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) { try { apexEngineHandler.shutdown(); + pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(), + PdpResponseStatus.SUCCESS, "Pdp update successful. No policies are running."); } catch (final ApexStarterException e) { LOGGER.error("Pdp update failed as the policies couldn't be undeployed.", e); pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(), @@ -134,12 +164,24 @@ public class PdpUpdateMessageHandler { if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) { apexEngineHandler.shutdown(); } - apexEngineHandler = - new ApexEngineHandler(pdpUpdateMsg.getPolicies().get(0).getProperties().get("content")); + apexEngineHandler = new ApexEngineHandler(pdpUpdateMsg.getPolicies()); Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler); if (apexEngineHandler.isApexEngineRunning()) { - pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(), - PdpResponseStatus.SUCCESS, "Apex engine started and policies are running."); + List<ToscaPolicyIdentifier> runningPolicies = apexEngineHandler.getRunningPolicies(); + if (new HashSet<>(runningPolicies) + .equals(new HashSet<>(pdpMessageHandler.getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies())))) { + pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(), + PdpResponseStatus.SUCCESS, "Apex engine started and policies are running."); + } else { + StringBuilder message = + new StringBuilder("Apex engine started. But, only the following polices are running - "); + for (ToscaPolicyIdentifier policy : runningPolicies) { + message.append(policy.getName()).append(":").append(policy.getVersion()).append(" "); + } + message.append(". Other policies failed execution. Please see the logs for more details."); + pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(), + PdpResponseStatus.SUCCESS, message.toString()); + } } else { pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(), PdpResponseStatus.FAIL, "Apex engine failed to start."); diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterActivator.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterActivator.java index c8f889fde..77ffa5317 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterActivator.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterActivator.java @@ -56,7 +56,7 @@ public class TestApexStarterActivator { @Before public void setUp() throws Exception { Registry.newRegistry(); - final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json"}; + final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParametersNoop.json"}; final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments(apexStarterConfigParameters); final ApexStarterParameterGroup parGroup = new ApexStarterParameterHandler().getParameters(arguments); diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterMain.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterMain.java index 68d30258b..2fcfe886f 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterMain.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterMain.java @@ -27,9 +27,6 @@ import static org.junit.Assert.assertTrue; import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.onap.policy.apex.services.onappf.ApexStarterActivator; -import org.onap.policy.apex.services.onappf.ApexStarterConstants; -import org.onap.policy.apex.services.onappf.ApexStarterMain; import org.onap.policy.apex.services.onappf.exception.ApexStarterException; import org.onap.policy.apex.services.onappf.parameters.CommonTestData; import org.onap.policy.common.utils.services.Registry; @@ -67,7 +64,7 @@ public class TestApexStarterMain { @Test public void testApexStarter() throws ApexStarterException { - final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json"}; + final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParametersNoop.json"}; apexStarter = new ApexStarterMain(apexStarterConfigParameters); assertTrue(apexStarter.getParameters().isValid()); assertEquals(CommonTestData.APEX_STARTER_GROUP_NAME, apexStarter.getParameters().getName()); diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestListenerUtils.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestListenerUtils.java new file mode 100644 index 000000000..78d9d769b --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestListenerUtils.java @@ -0,0 +1,99 @@ +/*- + * ============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========================================================= + */ + +package org.onap.policy.apex.services.onappf.comm; + +import com.google.gson.JsonObject; +import java.io.File; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.models.pdp.concepts.PdpStateChange; +import org.onap.policy.models.pdp.concepts.PdpStatus; +import org.onap.policy.models.pdp.concepts.PdpUpdate; +import org.onap.policy.models.pdp.enums.PdpState; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; + +public class TestListenerUtils { + + /** + * Method to create PdpUpdate message from the arguments passed. + * + * @param pdpStatus pdp status + * @param toscaPolicies list of tosca policies + * + * @return PdpUpdate message + */ + public static PdpUpdate createPdpUpdateMsg(final PdpStatus pdpStatus, List<ToscaPolicy> toscaPolicies ) { + final PdpUpdate pdpUpdateMsg = new PdpUpdate(); + pdpUpdateMsg.setDescription("dummy pdp status for test"); + pdpUpdateMsg.setPdpGroup("pdpGroup"); + pdpUpdateMsg.setPdpSubgroup("pdpSubgroup"); + pdpUpdateMsg.setName(pdpStatus.getName()); + pdpUpdateMsg.setPdpHeartbeatIntervalMs(Long.valueOf(3000)); + pdpUpdateMsg.setPolicies(toscaPolicies); + return pdpUpdateMsg; + } + + /** + * Method to create ToscaPolicy using the arguments passed. + * + * @param policyName the name of the policy + * @param policyVersion the version of the policy + * @param policyFilePath the path of the policy content + * + * @return PdpUpdate message + * @throws CoderException exception while reading the file to object + */ + public static ToscaPolicy createToscaPolicy(String policyName, String policyVersion, String policyFilePath) + throws CoderException { + final ToscaPolicy toscaPolicy = new ToscaPolicy(); + toscaPolicy.setType("apexpolicytype"); + toscaPolicy.setVersion(policyVersion); + toscaPolicy.setName(policyName); + final Map<String, Object> propertiesMap = new LinkedHashMap<>(); + Object properties = new StandardCoder().decode(new File(policyFilePath), JsonObject.class); + propertiesMap.put("content", properties); + toscaPolicy.setProperties(propertiesMap); + return toscaPolicy; + } + + /** + * Method to create PdpStateChange message from the arguments passed. + * + * @param state the new pdp state + * @param pdpGroup name of the pdpGroup + * @param pdpSubgroup name of the pdpSubGroup + * @param name the name of the message + * + * @return PdpStateChange message + */ + public static PdpStateChange createPdpStateChangeMsg(PdpState state, String pdpGroup, String pdpSubgroup, + String name) { + final PdpStateChange pdpStateChangeMsg = new PdpStateChange(); + pdpStateChangeMsg.setState(state); + pdpStateChangeMsg.setPdpGroup(pdpGroup); + pdpStateChangeMsg.setPdpSubgroup(pdpSubgroup); + pdpStateChangeMsg.setName(name); + return pdpStateChangeMsg; + } +} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java index c475b50ff..7f7de3b42 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java @@ -23,16 +23,15 @@ package org.onap.policy.apex.services.onappf.comm; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Paths; +import java.io.OutputStream; +import java.io.PrintStream; import java.util.ArrayList; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -40,9 +39,11 @@ import org.onap.policy.apex.services.onappf.ApexStarterActivator; import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments; import org.onap.policy.apex.services.onappf.ApexStarterConstants; import org.onap.policy.apex.services.onappf.exception.ApexStarterException; +import org.onap.policy.apex.services.onappf.handler.ApexEngineHandler; import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup; import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; +import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.services.Registry; import org.onap.policy.models.pdp.concepts.PdpStateChange; import org.onap.policy.models.pdp.concepts.PdpStatus; @@ -61,6 +62,8 @@ public class TestPdpStateChangeListener { private static final CommInfrastructure INFRA = CommInfrastructure.NOOP; private static final String TOPIC = "my-topic"; private ApexStarterActivator activator; + private ApexEngineHandler apexEngineHandler; + private PrintStream stdout = System.out; /** * Method for setup before each test. @@ -74,7 +77,7 @@ public class TestPdpStateChangeListener { pdpUpdateMessageListener = new PdpUpdateListener(); pdpStateChangeListener = new PdpStateChangeListener(); Registry.newRegistry(); - final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json" }; + final String[] apexStarterConfigParameters = {"-c", "src/test/resources/ApexStarterConfigParametersNoop.json"}; final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments(); ApexStarterParameterGroup parameterGroup; // The arguments return a string if there is a message to print and we should @@ -101,55 +104,25 @@ public class TestPdpStateChangeListener { */ @After public void teardown() throws Exception { - + System.setOut(stdout); + apexEngineHandler = + Registry.getOrDefault(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, ApexEngineHandler.class, null); + if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) { + apexEngineHandler.shutdown(); + } // clear the apex starter activator if (activator != null && activator.isAlive()) { activator.terminate(); } } - /** - * Method to initiate a PdpUpdate. - * - * @param instance the instance id - * @return PdpUpdate the pdp update message - */ - private PdpUpdate performPdpUpdate(final String instance) { - final PdpUpdate pdpUpdateMsg = new PdpUpdate(); - pdpUpdateMsg.setDescription("dummy pdp status for test"); - pdpUpdateMsg.setPdpGroup("pdpGroup"); - pdpUpdateMsg.setPdpSubgroup("pdpSubgroup"); - pdpUpdateMsg.setName(instance); - final ToscaPolicy toscaPolicy = new ToscaPolicy(); - toscaPolicy.setType("apexpolicytype"); - toscaPolicy.setVersion("1.0"); - final Map<String, Object> propertiesMap = new LinkedHashMap<>(); - - String properties; - try { - properties = new String(Files.readAllBytes(Paths.get("src\\test\\resources\\dummyProperties.json")), - StandardCharsets.UTF_8); - propertiesMap.put("content", properties); - } catch (final IOException e) { - propertiesMap.put("content", ""); - } - toscaPolicy.setProperties(propertiesMap); - final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>(); - toscaPolicies.add(toscaPolicy); - pdpUpdateMsg.setPolicies(toscaPolicies); - pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg); - return pdpUpdateMsg; - } - @Test public void testPdpStateChangeMessageListener_passivetopassive() { final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT); - performPdpUpdate(pdpStatus.getName()); - final PdpStateChange pdpStateChangeMsg = new PdpStateChange(); - pdpStateChangeMsg.setState(PdpState.PASSIVE); - pdpStateChangeMsg.setPdpGroup("pdpGroup"); - pdpStateChangeMsg.setPdpSubgroup("pdpSubgroup"); - pdpStateChangeMsg.setName(pdpStatus.getName()); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, + TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<ToscaPolicy>())); + PdpStateChange pdpStateChangeMsg = + TestListenerUtils.createPdpStateChangeMsg(PdpState.PASSIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName()); pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg); assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState()); @@ -158,15 +131,57 @@ public class TestPdpStateChangeListener { @Test public void testPdpStateChangeMessageListener_activetoactive() { final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT); - performPdpUpdate(pdpStatus.getName()); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, + TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<ToscaPolicy>())); pdpStatus.setState(PdpState.ACTIVE); - final PdpStateChange pdpStateChangeMsg = new PdpStateChange(); - pdpStateChangeMsg.setState(PdpState.ACTIVE); - pdpStateChangeMsg.setPdpGroup("pdpGroup"); - pdpStateChangeMsg.setPdpSubgroup("pdpSubgroup"); - pdpStateChangeMsg.setName(pdpStatus.getName()); + PdpStateChange pdpStateChangeMsg = + TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName()); pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg); assertEquals(pdpStatus.getState(), pdpStateChangeMsg.getState()); } + + @Test + public void testPdpStateChangeMessageListener() throws InterruptedException, CoderException { + OutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, + TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<ToscaPolicy>())); + PdpStateChange pdpStateChangeMsg = + TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName()); + pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg); + assertTrue(outContent.toString().contains("State changed to active. No policies found.")); + + final ToscaPolicy toscaPolicy = + TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json"); + final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>(); + toscaPolicies.add(toscaPolicy); + final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg); + assertTrue(outContent.toString().contains("Apex engine started and policies are running.")); + assertEquals(PdpState.ACTIVE, pdpStatus.getState()); + } + + @Test + public void testPdpStateChangeMessageListener_activetopassive() throws InterruptedException, CoderException { + final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT); + final ToscaPolicy toscaPolicy = + TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json"); + final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>(); + toscaPolicies.add(toscaPolicy); + final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg); + PdpStateChange pdpStateChangeMsg = + TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName()); + pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg); + pdpStateChangeMsg = + TestListenerUtils.createPdpStateChangeMsg(PdpState.PASSIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName()); + OutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg); + final String outString = outContent.toString(); + assertTrue(outString.contains("Apex pdp state changed from Active to Passive.")); + assertEquals(PdpState.PASSIVE, pdpStatus.getState()); + } } diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java index 700797812..05656af96 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java @@ -2,7 +2,6 @@ * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,16 +22,15 @@ package org.onap.policy.apex.services.onappf.comm; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Paths; +import java.io.OutputStream; +import java.io.PrintStream; import java.util.ArrayList; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -40,13 +38,17 @@ import org.onap.policy.apex.services.onappf.ApexStarterActivator; import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments; import org.onap.policy.apex.services.onappf.ApexStarterConstants; import org.onap.policy.apex.services.onappf.exception.ApexStarterException; +import org.onap.policy.apex.services.onappf.handler.ApexEngineHandler; import org.onap.policy.apex.services.onappf.handler.PdpMessageHandler; import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup; import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; +import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.services.Registry; +import org.onap.policy.models.pdp.concepts.PdpStateChange; import org.onap.policy.models.pdp.concepts.PdpStatus; import org.onap.policy.models.pdp.concepts.PdpUpdate; +import org.onap.policy.models.pdp.enums.PdpState; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; /** @@ -56,9 +58,12 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; */ public class TestPdpUpdateListener { private PdpUpdateListener pdpUpdateMessageListener; + private PdpStateChangeListener pdpStateChangeListener; private static final CommInfrastructure INFRA = CommInfrastructure.NOOP; private static final String TOPIC = "my-topic"; private ApexStarterActivator activator; + private ApexEngineHandler apexEngineHandler; + private PrintStream stdout = System.out; /** * Method for setup before each test. @@ -70,7 +75,7 @@ public class TestPdpUpdateListener { @Before public void setUp() throws ApexStarterException, FileNotFoundException, IOException { Registry.newRegistry(); - final String[] apexStarterConfigParameters = { "-c", "src/test/resources/ApexStarterConfigParameters.json" }; + final String[] apexStarterConfigParameters = {"-c", "src/test/resources/ApexStarterConfigParametersNoop.json"}; final ApexStarterCommandLineArguments arguments = new ApexStarterCommandLineArguments(); ApexStarterParameterGroup parameterGroup; // The arguments return a string if there is a message to print and we should @@ -89,6 +94,7 @@ public class TestPdpUpdateListener { Registry.register(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, activator); activator.initialize(); pdpUpdateMessageListener = new PdpUpdateListener(); + pdpStateChangeListener = new PdpStateChangeListener(); } /** @@ -98,7 +104,12 @@ public class TestPdpUpdateListener { */ @After public void teardown() throws Exception { - + System.setOut(stdout); + apexEngineHandler = + Registry.getOrDefault(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, ApexEngineHandler.class, null); + if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) { + apexEngineHandler.shutdown(); + } // clear the apex starter activator if (activator != null && activator.isAlive()) { activator.terminate(); @@ -106,35 +117,88 @@ public class TestPdpUpdateListener { } @Test - public void testPdpUpdateMssageListener() { + public void testPdpUpdateMssageListener() throws CoderException { final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT); - final PdpUpdate pdpUpdateMsg = new PdpUpdate(); - pdpUpdateMsg.setDescription("dummy pdp status for test"); - pdpUpdateMsg.setPdpGroup("pdpGroup"); - pdpUpdateMsg.setPdpSubgroup("pdpSubgroup"); - pdpUpdateMsg.setName(pdpStatus.getName()); - pdpUpdateMsg.setPdpHeartbeatIntervalMs(Long.valueOf(3000)); - final ToscaPolicy toscaPolicy = new ToscaPolicy(); - toscaPolicy.setType("apexpolicytype"); - toscaPolicy.setVersion("1.0"); - toscaPolicy.setName("apex policy name"); - final Map<String, Object> propertiesMap = new LinkedHashMap<>(); - String properties; - try { - properties = new String(Files.readAllBytes(Paths.get("src\\test\\resources\\dummyProperties.json")), - StandardCharsets.UTF_8); - propertiesMap.put("content", properties); - } catch (final IOException e) { - propertiesMap.put("content", ""); - } - toscaPolicy.setProperties(propertiesMap); + final ToscaPolicy toscaPolicy = + TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json"); + final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>(); + toscaPolicies.add(toscaPolicy); + final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg); + assertEquals(pdpStatus.getPdpGroup(), pdpUpdateMsg.getPdpGroup()); + assertEquals(pdpStatus.getPdpSubgroup(), pdpUpdateMsg.getPdpSubgroup()); + assertEquals(pdpStatus.getPolicies(), + new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies())); + } + + @Test + public void testPdpUpdateMssageListener_success() throws InterruptedException, CoderException { + OutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, + TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<ToscaPolicy>())); + PdpStateChange pdpStateChangeMsg = + TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName()); + pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg); + final ToscaPolicy toscaPolicy = + TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json"); final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>(); toscaPolicies.add(toscaPolicy); - pdpUpdateMsg.setPolicies(toscaPolicies); + final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies); pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg); + final String outString = outContent.toString(); assertEquals(pdpStatus.getPdpGroup(), pdpUpdateMsg.getPdpGroup()); assertEquals(pdpStatus.getPdpSubgroup(), pdpUpdateMsg.getPdpSubgroup()); assertEquals(pdpStatus.getPolicies(), new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies())); + assertTrue(outString.contains("Apex engine started and policies are running.")); + } + + @Test + public void testPdpUpdateMssageListener_undeploy() throws InterruptedException, CoderException { + final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, + TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<ToscaPolicy>())); + PdpStateChange pdpStateChangeMsg = + TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName()); + pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg); + final ToscaPolicy toscaPolicy = + TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json"); + final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>(); + toscaPolicies.add(toscaPolicy); + final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg); + OutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, + TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<ToscaPolicy>())); + final String outString = outContent.toString(); + assertTrue(outString.contains("Pdp update successful. No policies are running.")); + + } + + @Test + public void testPdpUpdateMssageListener_multi_policy_duplicate() + throws InterruptedException, ApexStarterException, CoderException { + OutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT); + final ToscaPolicy toscaPolicy = + TestListenerUtils.createToscaPolicy("apex policy name", "1.0", "src/test/resources/dummyProperties.json"); + final ToscaPolicy toscaPolicy2 = + TestListenerUtils.createToscaPolicy("apexpolicy2", "1.0", "src/test/resources/dummyProperties.json"); + final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>(); + toscaPolicies.add(toscaPolicy); + toscaPolicies.add(toscaPolicy2); + final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies); + pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg); + PdpStateChange pdpStateChangeMsg = + TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName()); + pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg); + final String outString = outContent.toString(); + assertTrue(outString.contains( + "Apex engine started. But, only the following polices are running - apex policy name:1.0 . " + + "Other policies failed execution. Please see the logs for more details.")); } } diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/DummyStateFinalizerExecutor.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/DummyStateFinalizerExecutor.java new file mode 100644 index 000000000..cdbd27b8f --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/DummyStateFinalizerExecutor.java @@ -0,0 +1,42 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications 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========================================================= + */ + +package org.onap.policy.apex.services.onappf.parameters.dummyclasses; + +import java.util.Map; +import java.util.Properties; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor; +import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; + +/** + * Dummy state finalizer executor for testing. + */ +public class DummyStateFinalizerExecutor extends StateFinalizerExecutor { + public DummyStateFinalizerExecutor() {} + + @Override + public String execute(final long executionId, final Properties executorProperties, + final Map<String, Object> newIncomingFields) throws StateMachineException, ContextException { + + return "stateOutput0"; + } +} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/DummyTaskExecutor.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/DummyTaskExecutor.java new file mode 100644 index 000000000..d3fa9b570 --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/DummyTaskExecutor.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications 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========================================================= + */ + +package org.onap.policy.apex.services.onappf.parameters.dummyclasses; + +import java.util.Map; +import java.util.Properties; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.core.engine.event.EnEvent; +import org.onap.policy.apex.core.engine.executor.TaskExecutor; +import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; +import org.onap.policy.apex.model.policymodel.concepts.AxTask; + +/** + * Dummy task executor for testing. + */ +public class DummyTaskExecutor extends TaskExecutor { + public DummyTaskExecutor() {} + + @Override + public void prepare() throws StateMachineException {} + + @Override + public Map<String, Object> execute(final long executionId, final Properties executorProperties, + final Map<String, Object> newIncomingFields) throws StateMachineException, ContextException { + + AxArtifactKey event0Key = new AxArtifactKey("Event0:0.0.1"); + return new EnEvent(event0Key); + } + + @Override + public AxTask getSubject() { + AxArtifactKey taskKey = new AxArtifactKey("FirstTask:0.0.1"); + return new AxTask(taskKey); + } + + @Override + public void cleanUp() throws StateMachineException {} +} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/DummyTaskSelectExecutor.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/DummyTaskSelectExecutor.java new file mode 100644 index 000000000..b27ff6ebc --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/DummyTaskSelectExecutor.java @@ -0,0 +1,49 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications 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========================================================= + */ + +package org.onap.policy.apex.services.onappf.parameters.dummyclasses; + +import java.util.Properties; +import org.onap.policy.apex.context.ContextException; +import org.onap.policy.apex.core.engine.event.EnEvent; +import org.onap.policy.apex.core.engine.executor.TaskSelectExecutor; +import org.onap.policy.apex.core.engine.executor.exception.StateMachineException; +import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; + +/** + * Dummy task selection executor for testing. + */ +public class DummyTaskSelectExecutor extends TaskSelectExecutor { + public DummyTaskSelectExecutor() {} + + @Override + public void prepare() throws StateMachineException {} + + @Override + public AxArtifactKey execute(final long executionId, final Properties executorProperties, + final EnEvent newIncomingEvent) throws StateMachineException, ContextException { + + return new AxArtifactKey("task:0.0.1"); + } + + @Override + public void cleanUp() throws StateMachineException {} +} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/SuperDooperExecutorParameters.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/SuperDooperExecutorParameters.java new file mode 100644 index 000000000..cbcd23c73 --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/dummyclasses/SuperDooperExecutorParameters.java @@ -0,0 +1,39 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications 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========================================================= + */ + +package org.onap.policy.apex.services.onappf.parameters.dummyclasses; + +import org.onap.policy.apex.core.engine.ExecutorParameters; + +/** + * Dummy executor parameters. + */ +public class SuperDooperExecutorParameters extends ExecutorParameters { + + /** + * Instantiates a new super dooper executor parameters. + */ + public SuperDooperExecutorParameters() { + this.setTaskExecutorPluginClass(DummyTaskExecutor.class.getName()); + this.setTaskSelectionExecutorPluginClass(DummyTaskSelectExecutor.class.getName()); + this.setStateFinalizerExecutorPluginClass(DummyStateFinalizerExecutor.class.getName()); + } +} diff --git a/services/services-onappf/src/test/resources/ApexStarterConfigParametersNoop.json b/services/services-onappf/src/test/resources/ApexStarterConfigParametersNoop.json new file mode 100644 index 000000000..bce20da9d --- /dev/null +++ b/services/services-onappf/src/test/resources/ApexStarterConfigParametersNoop.json @@ -0,0 +1,28 @@ +{ + "name":"ApexStarterParameterGroup", + "restServerParameters": { + "host": "0.0.0.0", + "port": 6969, + "userName": "healthcheck", + "password": "zb!XztG34", + "https": true + }, + "pdpStatusParameters":{ + "timeIntervalMs": 120000, + "pdpType":"apex", + "description":"Pdp Heartbeat", + "supportedPolicyTypes":[{"name":"onap.policies.controlloop.operational.Apex","version":"1.0.0"}] + }, + "topicParameterGroup": { + "topicSources" : [ { + "topic" : "my-topic", + "servers" : [ "my-server" ], + "topicCommInfrastructure" : "noop" + }], + "topicSinks" : [ { + "topic" : "my-topic", + "servers" : [ "my-server" ], + "topicCommInfrastructure" : "noop" + }] + } +}
\ No newline at end of file diff --git a/services/services-onappf/src/test/resources/TestPojoEvent.json b/services/services-onappf/src/test/resources/TestPojoEvent.json new file mode 100644 index 000000000..ce2cb2b6b --- /dev/null +++ b/services/services-onappf/src/test/resources/TestPojoEvent.json @@ -0,0 +1,15 @@ +{ + "anInt": 1, + "anInteger": 2, + "someString": "a string", + "testSubPojo": { + "anInt": 10, + "anInteger": 20, + "someString": "a sub string", + "testSubSubPojo": { + "anInt": 100, + "anInteger": 200, + "someString": "a sub sub string" + } + } +} diff --git a/services/services-onappf/src/test/resources/dummyProperties.json b/services/services-onappf/src/test/resources/dummyProperties.json index 849b86e69..daf2d5049 100644 --- a/services/services-onappf/src/test/resources/dummyProperties.json +++ b/services/services-onappf/src/test/resources/dummyProperties.json @@ -1,43 +1,497 @@ { - "engineServiceParameters": { - "name": "MyApexEngine", - "version": "0.0.1", - "id": 45, - "instanceCount": 2, - "deploymentPort": 65522, - "policy_type_impl": "onap.policies.controlloop.operational.apex.sampledomain.Impl", - "engineParameters": { - "executorParameters": { - "JAVASCRIPT": { - "parameterClassName": "org.onap.policy.apex.service.engine.parameters.dummyclasses.SuperDooperExecutorParameters" - } - } + "engineServiceParameters": { + "name": "MyApexEngine", + "version": "0.0.1", + "id": 45, + "instanceCount": 2, + "deploymentPort": 65522, + "policy_type_impl": { + "apexPolicyModel": { + "key": { + "name": "SmallModel", + "version": "0.0.1" + }, + "keyInformation": { + "key": { + "name": "SmallModel_KeyInfo", + "version": "0.0.1" + }, + "keyInfoMap": { + "entry": [ + { + "key": { + "name": "BasicContextAlbum", + "version": "0.0.1" + }, + "value": { + "key": { + "name": "BasicContextAlbum", + "version": "0.0.1" + }, + "UUID": "fec1b353-b35f-4384-b7d9-69622059c248", + "description": "Generated description for a concept called \"BasicContextAlbum\" with version \"0.0.1\" and UUID \"fec1b353-b35f-4384-b7d9-69622059c248\"" + } + }, + { + "key": { + "name": "BasicEvent", + "version": "0.0.1" + }, + "value": { + "key": { + "name": "BasicEvent", + "version": "0.0.1" + }, + "UUID": "cc8d3c1a-e975-459a-bcd2-69f423eaa1f3", + "description": "Generated description for a concept called \"BasicEvent\" with version \"0.0.1\" and UUID \"cc8d3c1a-e975-459a-bcd2-69f423eaa1f3\"" + } + }, + { + "key": { + "name": "BasicPolicy", + "version": "0.0.1" + }, + "value": { + "key": { + "name": "BasicPolicy", + "version": "0.0.1" + }, + "UUID": "d0c5d8ee-5fe7-4978-89ce-4a3e69cad043", + "description": "Generated description for a concept called \"BasicPolicy\" with version \"0.0.1\" and UUID \"d0c5d8ee-5fe7-4978-89ce-4a3e69cad043\"" + } + }, + { + "key": { + "name": "BasicTask", + "version": "0.0.1" + }, + "value": { + "key": { + "name": "BasicTask", + "version": "0.0.1" + }, + "UUID": "c5651414-fc1c-493b-878d-75f0ce685c36", + "description": "Generated description for a concept called \"BasicTask\" with version \"0.0.1\" and UUID \"c5651414-fc1c-493b-878d-75f0ce685c36\"" + } + }, + { + "key": { + "name": "IntType", + "version": "0.0.1" + }, + "value": { + "key": { + "name": "IntType", + "version": "0.0.1" + }, + "UUID": "790ff718-8dc0-44e0-89d8-1b3bbe238310", + "description": "Generated description for a concept called \"IntType\" with version \"0.0.1\" and UUID \"790ff718-8dc0-44e0-89d8-1b3bbe238310\"" + } + }, + { + "key": { + "name": "SmallModel", + "version": "0.0.1" + }, + "value": { + "key": { + "name": "SmallModel", + "version": "0.0.1" + }, + "UUID": "a1bd1f4e-713b-456b-b1a8-bb48beee28e8", + "description": "Generated description for a concept called \"SmallModel\" with version \"0.0.1\" and UUID \"a1bd1f4e-713b-456b-b1a8-bb48beee28e8\"" + } + }, + { + "key": { + "name": "SmallModel_Albums", + "version": "0.0.1" + }, + "value": { + "key": { + "name": "SmallModel_Albums", + "version": "0.0.1" + }, + "UUID": "72bed9af-ab7d-3379-b9f7-b5eca5c9ef22", + "description": "Generated description for concept referred to by key \"SmallModel_Albums:0.0.1\"" + } + }, + { + "key": { + "name": "SmallModel_Events", + "version": "0.0.1" + }, + "value": { + "key": { + "name": "SmallModel_Events", + "version": "0.0.1" + }, + "UUID": "796dc6b0-627d-34ae-a5e2-1bc4b4b486b8", + "description": "Generated description for concept referred to by key \"SmallModel_Events:0.0.1\"" + } + }, + { + "key": { + "name": "SmallModel_KeyInfo", + "version": "0.0.1" + }, + "value": { + "key": { + "name": "SmallModel_KeyInfo", + "version": "0.0.1" + }, + "UUID": "b4876774-6907-3d27-a2b8-f05737c5ee4a", + "description": "Generated description for concept referred to by key \"SmallModel_KeyInfo:0.0.1\"" + } + }, + { + "key": { + "name": "SmallModel_Policies", + "version": "0.0.1" + }, + "value": { + "key": { + "name": "SmallModel_Policies", + "version": "0.0.1" + }, + "UUID": "5bcf946b-67be-3190-a906-f954896f999f", + "description": "Generated description for concept referred to by key \"SmallModel_Policies:0.0.1\"" + } + }, + { + "key": { + "name": "SmallModel_Schemas", + "version": "0.0.1" + }, + "value": { + "key": { + "name": "SmallModel_Schemas", + "version": "0.0.1" + }, + "UUID": "c25bf5c3-7f1e-3667-b8a9-971ba21517bc", + "description": "Generated description for concept referred to by key \"SmallModel_Schemas:0.0.1\"" + } + }, + { + "key": { + "name": "SmallModel_Tasks", + "version": "0.0.1" + }, + "value": { + "key": { + "name": "SmallModel_Tasks", + "version": "0.0.1" + }, + "UUID": "43b015ca-2ed1-3a35-b103-e8a5aa68f1ef", + "description": "Generated description for concept referred to by key \"SmallModel_Tasks:0.0.1\"" + } + } + ] + } + }, + "policies": { + "key": { + "name": "SmallModel_Policies", + "version": "0.0.1" + }, + "policyMap": { + "entry": [ + { + "key": { + "name": "BasicPolicy", + "version": "0.0.1" + }, + "value": { + "policyKey": { + "name": "BasicPolicy", + "version": "0.0.1" + }, + "template": "FREEFORM", + "state": { + "entry": [ + { + "key": "OnlyState", + "value": { + "stateKey": { + "parentKeyName": "BasicPolicy", + "parentKeyVersion": "0.0.1", + "parentLocalName": "NULL", + "localName": "OnlyState" + }, + "trigger": { + "name": "BasicEvent", + "version": "0.0.1" + }, + "stateOutputs": { + "entry": [ + { + "key": "OnlyOutput", + "value": { + "key": { + "parentKeyName": "BasicPolicy", + "parentKeyVersion": "0.0.1", + "parentLocalName": "OnlyState", + "localName": "OnlyOutput" + }, + "outgoingEvent": { + "name": "BasicEvent", + "version": "0.0.1" + }, + "nextState": { + "parentKeyName": "NULL", + "parentKeyVersion": "0.0.0", + "parentLocalName": "NULL", + "localName": "NULL" + } + } + } + ] + }, + "contextAlbumReference": [ + { + "name": "BasicContextAlbum", + "version": "0.0.1" + } + ], + "taskSelectionLogic": { + "key": "NULL", + "logicFlavour": "UNDEFINED", + "logic": "" + }, + "stateFinalizerLogicMap": { + "entry": [] + }, + "defaultTask": { + "name": "BasicTask", + "version": "0.0.1" + }, + "taskReferences": { + "entry": [ + { + "key": { + "name": "BasicTask", + "version": "0.0.1" + }, + "value": { + "key": { + "parentKeyName": "BasicPolicy", + "parentKeyVersion": "0.0.1", + "parentLocalName": "OnlyState", + "localName": "BasicTask" + }, + "outputType": "DIRECT", + "output": { + "parentKeyName": "BasicPolicy", + "parentKeyVersion": "0.0.1", + "parentLocalName": "OnlyState", + "localName": "OnlyOutput" + } + } + } + ] + } + } + } + ] + }, + "firstState": "OnlyState" + } + } + ] + } + }, + "tasks": { + "key": { + "name": "SmallModel_Tasks", + "version": "0.0.1" + }, + "taskMap": { + "entry": [ + { + "key": { + "name": "BasicTask", + "version": "0.0.1" + }, + "value": { + "key": { + "name": "BasicTask", + "version": "0.0.1" + }, + "inputFields": { + "entry": [ + { + "key": "intPar", + "value": { + "key": "intPar", + "fieldSchemaKey": { + "name": "IntType", + "version": "0.0.1" + }, + "optional": false + } + } + ] + }, + "outputFields": { + "entry": [ + { + "key": "intPar", + "value": { + "key": "intPar", + "fieldSchemaKey": { + "name": "IntType", + "version": "0.0.1" + }, + "optional": false + } + } + ] + }, + "taskParameters": { + "entry": [] + }, + "contextAlbumReference": [ + { + "name": "BasicContextAlbum", + "version": "0.0.1" + } + ], + "taskLogic": { + "key": "TaskLogic", + "logicFlavour": "JAVASCRIPT", + "logic": "executor.logger.debug(executor.subject.id);\nvar gc = executor.getContextAlbum(\"BasicContextAlbum\");\nexecutor.logger.debug(gc.name);\nexecutor.logger.debug(executor.inFields);\n\nexecutor.logger.debug(executor.eo);\n\nvar returnValue = executor.isTrue;" + } + } + } + ] + } + }, + "events": { + "key": { + "name": "SmallModel_Events", + "version": "0.0.1" + }, + "eventMap": { + "entry": [ + { + "key": { + "name": "BasicEvent", + "version": "0.0.1" + }, + "value": { + "key": { + "name": "BasicEvent", + "version": "0.0.1" + }, + "nameSpace": "org.onap.policy.apex.events", + "source": "source", + "target": "target", + "parameter": { + "entry": [ + { + "key": "intPar", + "value": { + "key": "intPar", + "fieldSchemaKey": { + "name": "IntType", + "version": "0.0.1" + }, + "optional": false + } + } + ] + } + } + } + ] + } + }, + "albums": { + "key": { + "name": "SmallModel_Albums", + "version": "0.0.1" + }, + "albums": { + "entry": [ + { + "key": { + "name": "BasicContextAlbum", + "version": "0.0.1" + }, + "value": { + "key": { + "name": "BasicContextAlbum", + "version": "0.0.1" + }, + "scope": "GLOBAL", + "isWritable": true, + "itemSchema": { + "name": "IntType", + "version": "0.0.1" + } + } + } + ] + } + }, + "schemas": { + "key": { + "name": "SmallModel_Schemas", + "version": "0.0.1" + }, + "schemas": { + "entry": [ + { + "key": { + "name": "IntType", + "version": "0.0.1" + }, + "value": { + "key": { + "name": "IntType", + "version": "0.0.1" + }, + "schemaFlavour": "Java", + "schemaDefinition": "java.lang.Integer" + } + } + ] + } } + } }, - "eventOutputParameters": { - "FirstProducer": { - "carrierTechnologyParameters": { - "carrierTechnology": "FILE", - "parameters": { - "standardIo": true - } - }, - "eventProtocolParameters": { - "eventProtocol": "JSON" - } + "engineParameters": { + "executorParameters": { + "JAVASCRIPT": { + "parameterClassName": "org.onap.policy.apex.services.onappf.parameters.dummyclasses.SuperDooperExecutorParameters" } - }, - "eventInputParameters": { - "TheFileConsumer1": { - "carrierTechnologyParameters": { - "carrierTechnology": "FILE", - "parameters": { - "fileName": "src/test/resources/events/TestPojoEvent.json" - } - }, - "eventProtocolParameters": { - "eventProtocol": "JSON" - } + } + } + }, + "eventOutputParameters": { + "FirstProducer": { + "carrierTechnologyParameters": { + "carrierTechnology": "FILE", + "parameters": { + "standardIo": true + } + }, + "eventProtocolParameters": { + "eventProtocol": "JSON" + } + } + }, + "eventInputParameters": { + "TheFileConsumer1": { + "carrierTechnologyParameters": { + "carrierTechnology": "FILE", + "parameters": { + "fileName": "src/test/resources/TestPojoEvent.json" } + }, + "eventProtocolParameters": { + "eventProtocol": "JSON" + } } + } }
\ No newline at end of file |