aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/forwarding-plugins/src
diff options
context:
space:
mode:
authorramverma <ram.krishna.verma@ericsson.com>2018-09-13 16:31:35 +0100
committerramverma <ram.krishna.verma@ericsson.com>2018-09-13 22:06:26 +0100
commite80efa0dbe903e976f5b2799144658c7ba02e534 (patch)
tree231204269e12123ea3577679bcf91de2a39500e9 /plugins/forwarding-plugins/src
parent31def9f82514182e6761fac5ecae70333155aabc (diff)
Adding policy decoder to extract file from csar
* Adding decoder configuration parameters infrastructure to support plugin based architecture. Adding a new policy decoder after this will be just creating a new decoder class and its corresponding parameter class. * Adding a new decoder which extracts policy file from given csar. It is written in a generic way to extract file for any pdp like apex, drools. * Adding configuration parameters for the new decoder. The policy file name and policy type is passed as parameter to the decoder. * Fixing few broken package declaration in pdpx decoder tests. * Adding test cases for all code changes. Change-Id: I95e68cebce0f9747ca63b090f9b9116ce8836939 Issue-ID: POLICY-1101 Signed-off-by: ramverma <ram.krishna.verma@ericsson.com>
Diffstat (limited to 'plugins/forwarding-plugins/src')
-rw-r--r--plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarder.java18
-rw-r--r--plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroupTest.java5
-rw-r--r--plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderTest.java30
-rw-r--r--plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/DummyDecoder.java3
4 files changed, 36 insertions, 20 deletions
diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarder.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarder.java
index 1a603f04..f71c6aee 100644
--- a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarder.java
+++ b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarder.java
@@ -20,8 +20,11 @@
package org.onap.policy.distribution.forwarding.apex.pdp;
+import java.io.IOException;
+import java.io.InputStream;
import java.util.Collection;
+import org.apache.commons.io.IOUtils;
import org.onap.policy.apex.core.deployment.EngineServiceFacade;
import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
import org.onap.policy.common.logging.flexlogger.FlexLogger;
@@ -30,8 +33,8 @@ import org.onap.policy.common.parameters.ParameterService;
import org.onap.policy.distribution.forwarding.PolicyForwarder;
import org.onap.policy.distribution.forwarding.PolicyForwardingException;
import org.onap.policy.distribution.forwarding.xacml.pdp.XacmlPdpPolicyForwarder;
-import org.onap.policy.distribution.model.ApexPdpPolicy;
import org.onap.policy.distribution.model.Policy;
+import org.onap.policy.distribution.model.PolicyAsString;
/**
* This class provides an implementation of {@link PolicyForwarder} interface for forwarding the given policies to
@@ -42,6 +45,7 @@ import org.onap.policy.distribution.model.Policy;
public class ApexPdpPolicyForwarder implements PolicyForwarder {
private static final Logger LOGGER = FlexLogger.getLogger(XacmlPdpPolicyForwarder.class);
+ private static final String POLICY_TYPE = "APEX";
private ApexPdpPolicyForwarderParameterGroup apexForwarderParameters;
private EngineServiceFacade engineServiceFacade;
@@ -69,8 +73,9 @@ public class ApexPdpPolicyForwarder implements PolicyForwarder {
} else {
final Policy policy = (Policy) policies.toArray()[0];
- if (policy.getClass().isAssignableFrom(ApexPdpPolicy.class)) {
- forwardPolicy((ApexPdpPolicy) policy);
+ if (policy.getClass().isAssignableFrom(PolicyAsString.class)
+ && policy.getPolicyType().equalsIgnoreCase(POLICY_TYPE)) {
+ forwardPolicy((PolicyAsString) policy);
} else {
final String message = "Ignoring the policy as it is not an apex-pdp policy";
LOGGER.debug(message);
@@ -85,16 +90,17 @@ public class ApexPdpPolicyForwarder implements PolicyForwarder {
* @param apexPolicy the apex policy
* @throws PolicyForwardingException if any exception occurs while forwarding policy
*/
- private void forwardPolicy(final ApexPdpPolicy apexPolicy) throws PolicyForwardingException {
+ private void forwardPolicy(final PolicyAsString apexPolicy) throws PolicyForwardingException {
try {
engineServiceFacade.init();
- engineServiceFacade.deployModel(apexPolicy.getPolicyName(), apexPolicy.getPolicyInputStream(),
+ final InputStream policyInputStream = IOUtils.toInputStream(apexPolicy.getPolicy(), "UTF-8");
+ engineServiceFacade.deployModel(apexPolicy.getPolicyName(), policyInputStream,
apexForwarderParameters.isIgnoreConflicts(), apexForwarderParameters.isForceUpdate());
LOGGER.debug("Sucessfully forwarded the policy to apex-pdp egine at "
+ apexForwarderParameters.getHostname() + ":" + apexForwarderParameters.getPort());
- } catch (final ApexException exp) {
+ } catch (final ApexException | IOException exp) {
final String message = "Error sending policy to apex-pdp engine at" + apexForwarderParameters.getHostname()
+ ":" + apexForwarderParameters.getPort();
LOGGER.error(message, exp);
diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroupTest.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroupTest.java
index ca0efb5c..7feef452 100644
--- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroupTest.java
+++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroupTest.java
@@ -27,6 +27,11 @@ import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.onap.policy.common.parameters.ValidationStatus;
+/**
+ * Class to perform unit test of {@link ApexPdpPolicyForwarderParameterGroup}.
+ *
+ * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
+ */
public class ApexPdpPolicyForwarderParameterGroupTest {
@Test
diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderTest.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderTest.java
index abf5b508..a4d1b9fd 100644
--- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderTest.java
+++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderTest.java
@@ -22,11 +22,11 @@ package org.onap.policy.distribution.forwarding.apex.pdp;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import static org.mockito.Matchers.anyObject;
+import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
-import java.io.File;
-import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Field;
@@ -45,9 +45,14 @@ import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
import org.onap.policy.common.parameters.ParameterGroup;
import org.onap.policy.common.parameters.ParameterService;
import org.onap.policy.distribution.forwarding.PolicyForwardingException;
-import org.onap.policy.distribution.model.ApexPdpPolicy;
import org.onap.policy.distribution.model.Policy;
+import org.onap.policy.distribution.model.PolicyAsString;
+/**
+ * Class to perform unit test of {@link ApexPdpPolicyForwarder}.
+ *
+ * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
+ */
@RunWith(MockitoJUnitRunner.class)
public class ApexPdpPolicyForwarderTest {
@@ -84,7 +89,6 @@ public class ApexPdpPolicyForwarderTest {
public void testForwardPolicy() throws ApexException, FileNotFoundException, IOException, PolicyForwardingException,
NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
- final FileInputStream fis = new FileInputStream(File.createTempFile("policy1", null));
final Collection<Policy> policies = new ArrayList<>();
final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
forwarder.configure(GROUP_NAME);
@@ -93,13 +97,14 @@ public class ApexPdpPolicyForwarderTest {
forwarderField.setAccessible(true);
forwarderField.set(forwarder, engineServiceFacade);
- final ApexPdpPolicy policy = new ApexPdpPolicy("policy", fis);
+ final PolicyAsString policy = new PolicyAsString("policy", "APEX", "Sample Policy of apex");
policies.add(policy);
try {
forwarder.forward(policies);
verify(engineServiceFacade, times(1)).init();
- verify(engineServiceFacade, times(1)).deployModel("policy", fis, IGNORE_CONFLICTS, FORCE_UPDATE);
+ verify(engineServiceFacade, times(1)).deployModel(eq("policy"), anyObject(), eq(IGNORE_CONFLICTS),
+ eq(FORCE_UPDATE));
} catch (final Exception exp) {
fail("Test must not throw an exception");
}
@@ -110,19 +115,18 @@ public class ApexPdpPolicyForwarderTest {
throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException,
SecurityException, IllegalArgumentException, IllegalAccessException {
- final FileInputStream fis = new FileInputStream(File.createTempFile("policy1", null));
final Collection<Policy> policies = new ArrayList<>();
final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder();
forwarder.configure(GROUP_NAME);
- Mockito.doThrow(new ApexException("Failed")).when(engineServiceFacade).deployModel("policy1", fis,
- IGNORE_CONFLICTS, FORCE_UPDATE);
+ Mockito.doThrow(new ApexException("Failed")).when(engineServiceFacade).deployModel(eq("policy1"), anyObject(),
+ eq(IGNORE_CONFLICTS), eq(FORCE_UPDATE));
final Field decodersField = forwarder.getClass().getDeclaredField("engineServiceFacade");
decodersField.setAccessible(true);
decodersField.set(forwarder, engineServiceFacade);
- final ApexPdpPolicy policy1 = new ApexPdpPolicy("policy1", fis);
+ final PolicyAsString policy1 = new PolicyAsString("policy1", "APEX", "Sample Policy of apex");
policies.add(policy1);
try {
@@ -147,12 +151,10 @@ public class ApexPdpPolicyForwarderTest {
forwarderField.setAccessible(true);
forwarderField.set(forwarder, engineServiceFacade);
- final ApexPdpPolicy policy1 =
- new ApexPdpPolicy("policy1", new FileInputStream(File.createTempFile("policy1", null)));
+ final PolicyAsString policy1 = new PolicyAsString("policy1", "APEX", "Sample Policy of apex");
policies.add(policy1);
- final ApexPdpPolicy policy2 =
- new ApexPdpPolicy("policy2", new FileInputStream(File.createTempFile("policy2", null)));
+ final PolicyAsString policy2 = new PolicyAsString("policy2", "APEX", "Sample Policy of apex");
policies.add(policy2);
try {
diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/DummyDecoder.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/DummyDecoder.java
index e09357cf..c90917b8 100644
--- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/DummyDecoder.java
+++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/DummyDecoder.java
@@ -56,4 +56,7 @@ public class DummyDecoder implements PolicyDecoder<PolicyInput, Policy> {
public Collection<Policy> decode(final PolicyInput input) throws PolicyDecodingException {
return policesToReturn;
}
+
+ @Override
+ public void configure(final String parameterGroupName) {}
}