*
* @param toscaPolicy object
*/
- boolean loadPolicy(ToscaPolicy toscaPolicy) throws XacmlApplicationException;
+ void loadPolicy(ToscaPolicy toscaPolicy) throws XacmlApplicationException;
/**
* unloadPolicy a Tosca Policy.
}
@Override
- public synchronized boolean loadPolicy(ToscaPolicy toscaPolicy) {
+ public synchronized void loadPolicy(ToscaPolicy toscaPolicy) throws XacmlApplicationException {
try {
//
// Convert the policies first
//
this.mapLoadedPolicies.put(toscaPolicy, refPath);
} catch (IOException | ToscaPolicyConversionException e) {
- LOGGER.error("Failed to loadPolicies {}", e);
- return false;
+ throw new XacmlApplicationException("loadPolicy failed", e);
}
- return true;
}
@Override
}
@Test
- public void testLoadPolicy_ConversionError() throws ToscaPolicyConversionException {
+ public void testLoadPolicy_ConversionError() throws XacmlApplicationException, ToscaPolicyConversionException {
when(trans.convertPolicy(policy)).thenReturn(null);
- assertFalse(prov.loadPolicy(policy));
+ assertThatThrownBy(() -> prov.loadPolicy(policy)).isInstanceOf(XacmlApplicationException.class);
}
@Test
final Set<String> set = XACMLProperties.getRootPolicyIDs(prov.getProperties());
- assertTrue(prov.loadPolicy(policy));
+ // Load policy
+ prov.loadPolicy(policy);
// policy file should have been created
File policyFile = new File(TEMP_DIR, "my-name_1.2.3.xml");
package org.onap.policy.pdpx.main;
import java.util.Collections;
+import org.apache.commons.lang3.StringUtils;
import org.onap.policy.common.utils.network.NetworkUtil;
import org.onap.policy.models.pdp.concepts.PdpMessage;
import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
*/
private final PdpStatus status;
-
/**
* Constructs the object, initializing the state.
*/
* within a group/subgroup.
*/
- PdpStatus status2 = makeResponse(message);
+ PdpStatus status2 = makeResponse(message, "");
// start/stop rest controller based on state change
handleXacmlRestController();
* @param message message from which to update the internal state
* @return a response to the message
*/
- public PdpStatus updateInternalState(PdpUpdate message) {
+ public PdpStatus updateInternalState(PdpUpdate message, String errMessage) {
status.setPdpGroup(message.getPdpGroup());
status.setPdpSubgroup(message.getPdpSubgroup());
status.setPolicies(appManager.getToscaPolicyIdentifiers());
- return makeResponse(message);
+ return makeResponse(message, errMessage);
}
/**
* Makes a response to the given message, based on the current state.
*
* @param message message for which the response should be made
+ * @param errMessage the error message to be sent to PAP
* @return a new response
*/
- private PdpStatus makeResponse(PdpMessage message) {
+ private PdpStatus makeResponse(PdpMessage message, String errMessage) {
PdpResponseDetails resp = new PdpResponseDetails();
- resp.setResponseStatus(PdpResponseStatus.SUCCESS);
+
+ if (StringUtils.isBlank(errMessage)) {
+ resp.setResponseStatus(PdpResponseStatus.SUCCESS);
+ } else {
+ resp.setResponseStatus(PdpResponseStatus.FAIL);
+ resp.setResponseMessage(errMessage);
+ }
resp.setResponseTo(message.getRequestId());
PdpStatus status2 = new PdpStatus(status);
package org.onap.policy.pdpx.main.comm;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.onap.policy.models.pdp.concepts.PdpStatus;
import org.onap.policy.models.pdp.concepts.PdpUpdate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
+import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
import org.onap.policy.pdpx.main.XacmlState;
import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
}
}
+ StringBuilder errorMessage = new StringBuilder();
// Deploy a policy
// if deployed policies do not contain the incoming policy load it
for (ToscaPolicy policy : incomingPolicies) {
if (!deployedPolicies.contains(policy)) {
- appManager.loadDeployedPolicy(policy);
+ try {
+ appManager.loadDeployedPolicy(policy);
+ } catch (XacmlApplicationException e) {
+ // Failed to load policy, return error(s) to PAP
+ LOGGER.error("Failed to load policy: {}", policy, e);
+ errorMessage.append("Failed to load policy: " + policy + ": "
+ + e.getMessage() + XacmlPolicyUtils.LINE_SEPARATOR);
+ }
}
}
+ // Return current deployed policies
+ message.setPolicies(new ArrayList<ToscaPolicy>(appManager.getToscaPolicies().keySet()));
+ LOGGER.debug("Returning current deployed policies: {} ", message.getPolicies());
// update the policy count statistic
XacmlPdpStatisticsManager stats = XacmlPdpStatisticsManager.getCurrent();
stats.setTotalPolicyCount(appManager.getPolicyCount());
}
- sendPdpUpdate(state.updateInternalState(message));
+ sendPdpUpdate(state.updateInternalState(message, errorMessage.toString()));
}
private void sendPdpUpdate(PdpStatus status) {
}
/**
- * Finds the appropriate application and loads the policy.
+ * Finds the appropriate application and loads the policy, throws an exception if it fails.
*
* @param policy Incoming policy
+ * @throws XacmlApplicationException if loadPolicy fails
*/
- public void loadDeployedPolicy(ToscaPolicy policy) {
+ public void loadDeployedPolicy(ToscaPolicy policy) throws XacmlApplicationException {
for (XacmlApplicationServiceProvider application : applicationLoader) {
- try {
- //
- // There should be only one application per policytype. We can
- // put more logic surrounding enforcement of that later. For now,
- // just use the first one found.
- //
- if (application.canSupportPolicyType(policy.getTypeIdentifier())) {
- if (application.loadPolicy(policy)) {
- if (LOGGER.isInfoEnabled()) {
- LOGGER.info("Loaded ToscaPolicy {} into application {}", policy.getMetadata(),
- application.applicationName());
- }
- mapLoadedPolicies.put(policy, application);
- }
- return;
+ //
+ // There should be only one application per policytype. We can
+ // put more logic surrounding enforcement of that later. For now,
+ // just use the first one found.
+ //
+ if (application.canSupportPolicyType(policy.getTypeIdentifier())) {
+ application.loadPolicy(policy);
+ mapLoadedPolicies.put(policy, application);
+ if (LOGGER.isInfoEnabled()) {
+ LOGGER.info("Loaded ToscaPolicy {} into application {}", policy.getMetadata(),
+ application.applicationName());
}
- } catch (XacmlApplicationException e) {
- LOGGER.error("Failed to load the Tosca Policy", e);
+ return;
}
}
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
req.setPdpGroup(GROUP);
req.setPdpSubgroup(SUBGROUP);
- PdpStatus status = state.updateInternalState(req);
+ PdpStatus status = state.updateInternalState(req, "");
PdpResponseDetails resp = status.getResponse();
assertNotNull(resp);
assertEquals(req.getRequestId(), resp.getResponseTo());
assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
+ assertNull(resp.getResponseMessage());
// ensure info was saved
status = state.genHeartbeat();
assertEquals(GROUP, status.getPdpGroup());
assertEquals(SUBGROUP, status.getPdpSubgroup());
+
+ status = state.updateInternalState(req, "Failed to load policy: failLoadPolicy1: null");
+ assertEquals(status.getResponse().getResponseMessage(), "Failed to load policy: failLoadPolicy1: null");
+ assertEquals(status.getResponse().getResponseStatus(), PdpResponseStatus.FAIL);
}
@Test
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.startsWith;
+import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.onap.policy.models.pdp.concepts.PdpStatus;
import org.onap.policy.models.pdp.concepts.PdpUpdate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
import org.onap.policy.pdpx.main.XacmlState;
import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
@Mock
private ToscaPolicy added2;
+ @Mock
+ private ToscaPolicy failPolicy1;
+
+ @Mock
+ private ToscaPolicy failPolicy2;
+
@Mock
private PdpUpdate update;
+ @Mock
+ private PdpUpdate failurePdpUpdate;
+
private XacmlPdpUpdatePublisher publisher;
List<ToscaPolicy> updatePolicies = Arrays.asList(added1, deployed2, deployed3, added2);
when(update.getPolicies()).thenReturn(updatePolicies);
+ List<ToscaPolicy> failureUpdatePolicies = Arrays.asList(added1, deployed2, deployed3, failPolicy1, failPolicy2);
+ when(failurePdpUpdate.getPolicies()).thenReturn(failureUpdatePolicies);
+
when(appmgr.getPolicyCount()).thenReturn(NEW_COUNT);
- when(state.updateInternalState(update)).thenReturn(status);
+ when(state.updateInternalState(any(), any())).thenReturn(status);
when(client.send(any())).thenReturn(true);
}
@Test
- public void testHandlePdpUpdate() {
+ public void testHandlePdpUpdate() throws XacmlApplicationException {
XacmlPdpStatisticsManager statmgr = new XacmlPdpStatisticsManager();
XacmlPdpStatisticsManager.setCurrent(statmgr);
}
@Test
- public void testHandlePdpUpdate_NullPolicies() {
+ public void testHandlePdpUpdate_LoadPolicyFailed() throws XacmlApplicationException {
+ // Set loadPolicy to fail
+ doThrow(new XacmlApplicationException()).when(appmgr).loadDeployedPolicy(failPolicy1);
+ doThrow(new XacmlApplicationException()).when(appmgr).loadDeployedPolicy(failPolicy2);
+
+ publisher.handlePdpUpdate(failurePdpUpdate);
+
+ // two removed
+ verify(appmgr).removeUndeployedPolicy(deployed1);
+ verify(appmgr).removeUndeployedPolicy(deployed4);
+
+ verify(failurePdpUpdate).setPolicies(any());
+
+ verify(state).updateInternalState(any(), startsWith("Failed to load policy"));
+ verify(client).send(status);
+ }
+
+ @Test
+ public void testHandlePdpUpdate_NullPolicies() throws XacmlApplicationException {
when(update.getPolicies()).thenReturn(null);
publisher.handlePdpUpdate(update);
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
-
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardYamlCoder;
import org.onap.policy.common.utils.resources.ResourceUtils;
//
for (Map<String, ToscaPolicy> policies : completedJtst.getToscaTopologyTemplate().getPolicies()) {
for (ToscaPolicy policy : policies.values()) {
- if (service.loadPolicy(policy)) {
+ try {
+ service.loadPolicy(policy);
loadedPolicies.add(policy);
- } else {
- LOGGER.error("Application failed to load policy");
+ } catch (XacmlApplicationException e) {
+ LOGGER.error("Application failed to load policy", e);
}
}
}