/**
* Updates the Apex Engine with the policy model created from new list of policies.
*
- * @param policies the list of policies
+
+ * @param polsToDeploy list of policies to deploy which will be modified to remove running policies
+ * @param polsToUndeploy list of policies to undeploy which will be modified to remove policies not running
* @throws ApexStarterException if the apex engine instantiation failed using the policies passed
*/
- public void updateApexEngine(List<ToscaPolicy> policies) throws ApexStarterException {
+ public void updateApexEngine(List<ToscaPolicy> polsToDeploy, List<ToscaConceptIdentifier> polsToUndeploy)
+ throws ApexStarterException {
List<ToscaConceptIdentifier> runningPolicies = getRunningPolicies();
- List<ToscaPolicy> policiesToDeploy = policies.stream()
- .filter(policy -> !runningPolicies.contains(policy.getIdentifier())).collect(Collectors.toList());
- List<ToscaConceptIdentifier> policiesToUnDeploy = runningPolicies.stream()
- .filter(polId -> policies.stream().noneMatch(policy -> policy.getIdentifier().equals(polId)))
- .collect(Collectors.toList());
+ List<ToscaPolicy> policiesToDeploy = polsToDeploy;
+ policiesToDeploy.removeIf(p -> runningPolicies.contains(p.getIdentifier()));
+ List<ToscaConceptIdentifier> policiesToUnDeploy = polsToUndeploy;
+ policiesToUnDeploy.removeIf(p -> !runningPolicies.contains(p));
Map<ToscaConceptIdentifier, ApexMain> undeployedPoliciesMainMap = new LinkedHashMap<>();
policiesToUnDeploy.forEach(policyId -> {
ApexMain apexMain = apexMainMap.get(policyId);
package org.onap.policy.apex.services.onappf.handler;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
+import java.util.stream.Collectors;
import org.onap.policy.apex.service.engine.main.ApexPolicyStatisticsManager;
import org.onap.policy.apex.services.onappf.ApexStarterConstants;
import org.onap.policy.apex.services.onappf.comm.PdpStatusPublisher;
import org.onap.policy.models.pdp.enums.PdpResponseStatus;
import org.onap.policy.models.pdp.enums.PdpState;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
}
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());
+ @SuppressWarnings("unchecked")
+ List<ToscaPolicy> policies = Registry.getOrDefault(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST,
+ List.class, new ArrayList<>());
+ policies.addAll(pdpUpdateMsg.getPoliciesToBeDeployed());
+ pdpStatusContext.setPolicies(policies.stream().map(ToscaPolicy::getIdentifier)
+ .collect(Collectors.toList()));
+ Registry.registerOrReplace(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST,
+ policies);
if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) {
pdpResponseDetails = startOrStopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler);
} catch (final IllegalArgumentException e) {
LOGGER.debug("ApenEngineHandler not in registry.", e);
}
- if (pdpUpdateMsg.getPolicies().isEmpty()) {
+ if (null != apexEngineHandler
+ && pdpUpdateMsg.getPoliciesToBeUndeployed().containsAll(apexEngineHandler.getRunningPolicies())
+ && pdpUpdateMsg.getPoliciesToBeDeployed().isEmpty()) {
pdpResponseDetails = stopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
} else {
pdpResponseDetails = startApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler, apexEngineHandler);
try {
if (null != apexEngineHandler && apexEngineHandler.isApexEngineRunning()) {
- apexEngineHandler.updateApexEngine(pdpUpdateMsg.getPolicies());
+ apexEngineHandler.updateApexEngine(pdpUpdateMsg.getPoliciesToBeDeployed(),
+ pdpUpdateMsg.getPoliciesToBeUndeployed());
} else {
- apexEngineHandler = new ApexEngineHandler(pdpUpdateMsg.getPolicies());
+ apexEngineHandler = new ApexEngineHandler(pdpUpdateMsg.getPoliciesToBeDeployed());
Registry.registerOrReplace(ApexStarterConstants.REG_APEX_ENGINE_HANDLER, apexEngineHandler);
}
if (apexEngineHandler.isApexEngineRunning()) {
List<ToscaConceptIdentifier> runningPolicies = apexEngineHandler.getRunningPolicies();
- if (new HashSet<>(runningPolicies)
- .equals(new HashSet<>(pdpMessageHandler.getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies())))) {
+ if (new HashSet<>(runningPolicies).containsAll(new HashSet<>(pdpMessageHandler
+ .getToscaPolicyIdentifiers(pdpUpdateMsg.getPoliciesToBeDeployed())))
+ && !containsAny(runningPolicies, pdpUpdateMsg.getPoliciesToBeUndeployed())) {
pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(),
PdpResponseStatus.SUCCESS, "Apex engine started and policies are running.");
} else {
&& pdpStatusContext.getPdpGroup().equals(pdpUpdateMsg.getPdpGroup())
&& null != pdpStatusContext.getPdpSubgroup()
&& pdpStatusContext.getPdpSubgroup().equals(pdpUpdateMsg.getPdpSubgroup())
- && null != pdpStatusContext.getPolicies() && new PdpMessageHandler()
- .getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()).equals(pdpStatusContext.getPolicies());
+ && null != pdpStatusContext.getPolicies()
+ && pdpStatusContext.getPolicies().containsAll(new PdpMessageHandler().getToscaPolicyIdentifiers(
+ pdpUpdateMsg.getPoliciesToBeDeployed()))
+ && !containsAny(pdpStatusContext.getPolicies(), pdpUpdateMsg.getPoliciesToBeUndeployed());
}
/**
Registry.registerOrReplace(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER,
new PdpStatusPublisher(topicSinks, interval));
}
+
+ /**
+ * Checks if one list contains any element of another.
+ *
+ * @param listToCheckWith list to check contents of
+ * @param listToCheckAgainst list to check against other list for similarities
+ * @return boolean flag which tells if lists share same elements or not
+ */
+ private boolean containsAny(List<ToscaConceptIdentifier> listToCheckWith,
+ List<ToscaConceptIdentifier> listToCheckAgainst) {
+ return listToCheckAgainst.stream().anyMatch(listToCheckWith::contains);
+ }
}
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019-2020 Nordix Foundation.
+ * Copyright (C) 2019-2021 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
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.ToscaConceptIdentifier;
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
+ * @param toscaPoliciesToBeDeployed list of tosca policies to deploy
+ * @param toscaPoliciesToBeUnDeployed list of tosca policies to undeploy
*
* @return PdpUpdate message
*/
- public static PdpUpdate createPdpUpdateMsg(final PdpStatus pdpStatus, List<ToscaPolicy> toscaPolicies) {
+ public static PdpUpdate createPdpUpdateMsg(final PdpStatus pdpStatus, List<ToscaPolicy> toscaPoliciesToBeDeployed,
+ List<ToscaConceptIdentifier> toscaPoliciesToBeUnDeployed) {
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);
+ pdpUpdateMsg.setPoliciesToBeDeployed(toscaPoliciesToBeDeployed);
+ pdpUpdateMsg.setPoliciesToBeUndeployed(toscaPoliciesToBeUnDeployed);
return pdpUpdateMsg;
}
package org.onap.policy.apex.services.onappf.comm;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public void testPdpStateChangeMessageListener_passivetopassive() {
final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
- TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<ToscaPolicy>()));
+ TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<>(),
+ new ArrayList<>()));
PdpStateChange pdpStateChangeMsg =
TestListenerUtils.createPdpStateChangeMsg(PdpState.PASSIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
public void testPdpStateChangeMessageListener_activetoactive() {
final PdpStatus pdpStatus = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT);
pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null,
- TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<ToscaPolicy>()));
+ TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<>(),
+ new ArrayList<>()));
pdpStatus.setState(PdpState.ACTIVE);
PdpStateChange pdpStateChangeMsg =
TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
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>()));
+ TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<>(),
+ new ArrayList<>()));
PdpStateChange pdpStateChangeMsg =
TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
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);
+ final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies,
+ new ArrayList<>());
pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
- assertTrue(outContent.toString().contains("Apex engine started and policies are running."));
+ assertThat(outContent.toString()).contains("Apex engine started and policies are running.");
assertEquals(PdpState.ACTIVE, pdpStatus.getState());
final ApexPolicyStatisticsManager policyCounterManager = ApexPolicyStatisticsManager.getInstanceFromRegistry();
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);
+ final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies,
+ new ArrayList<>());
pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
PdpStateChange pdpStateChangeMsg =
TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
+ * Copyright (C) 2019-2021 Nordix Foundation.
* Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
* ================================================================================
package org.onap.policy.apex.services.onappf.comm;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
+import java.util.LinkedList;
import java.util.List;
+import java.util.stream.Collectors;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
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);
+ final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies,
+ new LinkedList<>());
pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
assertEquals(pdpStatus.getPdpGroup(), pdpUpdateMsg.getPdpGroup());
assertEquals(pdpStatus.getPdpSubgroup(), pdpUpdateMsg.getPdpSubgroup());
assertEquals(pdpStatus.getPolicies(),
- new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()));
+ new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPoliciesToBeDeployed()));
}
@Test
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>()));
+ TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<>(),
+ new ArrayList<>()));
PdpStateChange pdpStateChangeMsg =
TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
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);
+ final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies,
+ new LinkedList<>());
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."));
+ new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPoliciesToBeDeployed()));
+ assertThat(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>()));
+ TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<>(),
+ new ArrayList<>()));
PdpStateChange pdpStateChangeMsg =
TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());
pdpStateChangeListener.onTopicEvent(INFRA, TOPIC, null, pdpStateChangeMsg);
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);
+ final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies,
+ new ArrayList<>());
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>()));
+ TestListenerUtils.createPdpUpdateMsg(pdpStatus, new ArrayList<>(),
+ toscaPolicies.stream().map(ToscaPolicy::getIdentifier)
+ .collect(Collectors.toList())));
final String outString = outContent.toString();
- assertTrue(outString.contains("Pdp update successful. No policies are running."));
-
+ assertThat(outString).contains("Pdp update successful. No policies are running.");
}
@Test
final List<ToscaPolicy> toscaPolicies = new ArrayList<ToscaPolicy>();
toscaPolicies.add(toscaPolicy);
toscaPolicies.add(toscaPolicy2);
- final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies);
+ final PdpUpdate pdpUpdateMsg = TestListenerUtils.createPdpUpdateMsg(pdpStatus, toscaPolicies,
+ new LinkedList<>());
pdpUpdateMessageListener.onTopicEvent(INFRA, TOPIC, null, pdpUpdateMsg);
PdpStateChange pdpStateChangeMsg =
TestListenerUtils.createPdpStateChangeMsg(PdpState.ACTIVE, "pdpGroup", "pdpSubgroup", pdpStatus.getName());