aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/forwarding-plugins/src
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/forwarding-plugins/src')
-rw-r--r--plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/file/FilePolicyForwarder.java123
-rw-r--r--plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/file/FilePolicyForwarderParameterBuilder.java70
-rw-r--r--plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/file/FilePolicyForwarderParameterGroup.java64
-rw-r--r--plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/file/FilePolicyForwarderParameterGroupTest.java59
-rw-r--r--plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/file/FilePolicyForwarderTest.java167
5 files changed, 483 insertions, 0 deletions
diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/file/FilePolicyForwarder.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/file/FilePolicyForwarder.java
new file mode 100644
index 00000000..98d9af4e
--- /dev/null
+++ b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/file/FilePolicyForwarder.java
@@ -0,0 +1,123 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 Intel Crop. 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.
+ * 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.distribution.forwarding.file;
+
+import java.io.BufferedWriter;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.InvalidPathException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Collection;
+
+import org.onap.policy.common.logging.flexlogger.FlexLogger;
+import org.onap.policy.common.logging.flexlogger.Logger;
+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.model.OptimizationPolicy;
+import org.onap.policy.distribution.model.Policy;
+
+/**
+ * This class provides an implementation of {@link PolicyForwarder} interface for forwarding the given policies to
+ * a file directory.
+ */
+public class FilePolicyForwarder implements PolicyForwarder {
+
+ private static final Logger LOGGER = FlexLogger.getLogger(FilePolicyForwarder.class);
+ private FilePolicyForwarderParameterGroup fileForwarderParameters;
+
+ /**
+ * {@inheritDoc}.
+ */
+ @Override
+ public void configure(final String parameterGroupName) {
+ fileForwarderParameters = ParameterService.get(parameterGroupName);
+ try {
+ Path path = Paths.get(fileForwarderParameters.getPath());
+ if (!Files.exists(path)) {
+ Files.createDirectories(path);
+ }
+ } catch (final InvalidPathException | IOException e) {
+ LOGGER.error(e.toString());
+ }
+ }
+
+ /**
+ * {@inheritDoc}.
+ */
+ @Override
+ public void forward(final Collection<Policy> policies) throws PolicyForwardingException {
+ for (Policy policy : policies) {
+ if (policy instanceof OptimizationPolicy) {
+ forwardPolicy((OptimizationPolicy) policy);
+ } else {
+ final String message = new String("Cannot forward policy " + policy
+ + ". Unsupported policy type " + policy.getClass().getSimpleName());
+ LOGGER.error(message);
+ throw new PolicyForwardingException(message);
+ }
+ }
+ }
+
+ /**
+ * Method to forward a given policy to be logged into a file.
+ *
+ * @param pol the policy
+ * @throws PolicyForwardingException if any exception occurs while forwarding policy
+ */
+ private void forwardPolicy(final OptimizationPolicy pol) throws PolicyForwardingException {
+ final String name = pol.getPolicyName();
+ try {
+ Path path = Paths.get(fileForwarderParameters.getPath(), name);
+ BufferedWriter writer = new BufferedWriter(new FileWriter(path.toString()));
+ writer.write("policyName: " + name);
+ if (fileForwarderParameters.isVerbose()) {
+ writer.newLine();
+ writer.write("policyType: " + pol.getPolicyType());
+ writer.newLine();
+ writer.write("policyDescription: " + pol.getPolicyDescription());
+ writer.newLine();
+ writer.write("onapName: " + pol.getOnapName());
+ writer.newLine();
+ writer.write("configBodyType: " + pol.getConfigBodyType());
+ writer.newLine();
+ writer.write("configBody: " + pol.getConfigBody());
+ writer.newLine();
+ writer.write("timetolive: " + pol.getTimetolive().toString());
+ writer.newLine();
+ writer.write("guard: " + pol.getGuard());
+ writer.newLine();
+ writer.write("riskLevel: " + pol.getRiskLevel());
+ writer.newLine();
+ writer.write("riskType: " + pol.getRiskType());
+ }
+ writer.close();
+ LOGGER.debug("Sucessfully forwarded the policy to store into file: " + path.toString());
+ } catch (final InvalidPathException | IOException exp) {
+ final String message = "Error sending policy to file under path:" + fileForwarderParameters.getPath();
+ LOGGER.error(message, exp);
+ throw new PolicyForwardingException(message, exp);
+ }
+ }
+}
+
diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/file/FilePolicyForwarderParameterBuilder.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/file/FilePolicyForwarderParameterBuilder.java
new file mode 100644
index 00000000..f5281e6b
--- /dev/null
+++ b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/file/FilePolicyForwarderParameterBuilder.java
@@ -0,0 +1,70 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 Intel Corp. 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.
+ * 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.distribution.forwarding.file;
+
+/**
+ * This builder holds all the parameters needed to build an instance of {@link FilePolicyForwarderParameterGroup}
+ * class.
+ */
+public class FilePolicyForwarderParameterBuilder {
+
+ private String path;
+ private boolean verbose = false;
+
+ /**
+ * Set path to this {@link FilePolicyForwarderParameterBuilder} instance.
+ *
+ * @param path the directory to store the policies
+ */
+ public FilePolicyForwarderParameterBuilder setPath(final String path) {
+ this.path = path;
+ return this;
+ }
+
+
+ /**
+ * Set verbose flag to this {@link FilePolicyForwarderParameterBuilder} instance.
+ *
+ * @param verbose the verbose flag
+ */
+ public FilePolicyForwarderParameterBuilder setVerbose(final boolean verbose) {
+ this.verbose = verbose;
+ return this;
+ }
+
+ /**
+ * Returns the path of this {@link FilePolicyForwarderParameterBuilder} instance.
+ *
+ * @return the directory
+ */
+ public String getPath() {
+ return path;
+ }
+
+ /**
+ * Returns the verbose flag of this {@link FilePolicyForwarderParameterBuilder} instance.
+ *
+ * @return the verbose
+ */
+ public boolean isVerbose() {
+ return verbose;
+ }
+}
diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/file/FilePolicyForwarderParameterGroup.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/file/FilePolicyForwarderParameterGroup.java
new file mode 100644
index 00000000..db621ae3
--- /dev/null
+++ b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/file/FilePolicyForwarderParameterGroup.java
@@ -0,0 +1,64 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 Intel Corp. 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.
+ * 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.distribution.forwarding.file;
+
+import org.onap.policy.common.parameters.GroupValidationResult;
+import org.onap.policy.common.parameters.ValidationStatus;
+import org.onap.policy.common.utils.validation.ParameterValidationUtils;
+import org.onap.policy.distribution.main.parameters.PolicyForwarderConfigurationParameterGroup;
+
+/**
+ * Holds the parameters for the{@link FilePolicyForwarder}.
+ */
+public class FilePolicyForwarderParameterGroup extends PolicyForwarderConfigurationParameterGroup {
+ public static final String POLICY_FORWARDER_PLUGIN_CLASS = FilePolicyForwarder.class.getCanonicalName();
+
+ private String path;
+ private boolean verbose;
+
+ /**
+ * Constructor for instantiating {@link FilePolicyForwarderParameterGroup} class.
+ *
+ * @param builder the apex forwarder parameter builder
+ */
+ public FilePolicyForwarderParameterGroup(final FilePolicyForwarderParameterBuilder builder) {
+ this.path = builder.getPath();
+ this.verbose = builder.isVerbose();
+ }
+
+ public String getPath() {
+ return path;
+ }
+
+ public boolean isVerbose() {
+ return verbose;
+ }
+
+ @Override
+ public GroupValidationResult validate() {
+ final GroupValidationResult validationResult = new GroupValidationResult(this);
+ if (!ParameterValidationUtils.validateStringParameter(path)) {
+ validationResult.setResult("path", ValidationStatus.INVALID,
+ "must be a non-blank string containing path directory");
+ }
+ return validationResult;
+ }
+}
diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/file/FilePolicyForwarderParameterGroupTest.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/file/FilePolicyForwarderParameterGroupTest.java
new file mode 100644
index 00000000..20528a5a
--- /dev/null
+++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/file/FilePolicyForwarderParameterGroupTest.java
@@ -0,0 +1,59 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 Intel Corp. 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.
+ * 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.distribution.forwarding.file;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+import org.onap.policy.common.parameters.ValidationStatus;
+
+/**
+ * Class to perform unit test of {@link FilePolicyForwarderParameterGroup}.
+ */
+public class FilePolicyForwarderParameterGroupTest {
+
+ @Test
+ public void testBuilderAndGetters() {
+ final FilePolicyForwarderParameterBuilder builder = new FilePolicyForwarderParameterBuilder();
+ builder.setPath("/tmp").setVerbose(true);
+ final FilePolicyForwarderParameterGroup configurationParameters =
+ new FilePolicyForwarderParameterGroup(builder);
+ configurationParameters.setName("myConfiguration");
+
+ assertEquals("myConfiguration", configurationParameters.getName());
+ assertTrue(configurationParameters.isVerbose());
+ assertEquals("/tmp", configurationParameters.getPath());
+ assertEquals(ValidationStatus.CLEAN, configurationParameters.validate().getStatus());
+ }
+
+ @Test
+ public void testInvalidPath() {
+ final FilePolicyForwarderParameterBuilder builder = new FilePolicyForwarderParameterBuilder();
+ builder.setPath("").setVerbose(false);
+ final FilePolicyForwarderParameterGroup configurationParameters =
+ new FilePolicyForwarderParameterGroup(builder);
+ configurationParameters.setName("myConfiguration");
+
+ assertEquals(ValidationStatus.INVALID, configurationParameters.validate().getStatus());
+ }
+}
diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/file/FilePolicyForwarderTest.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/file/FilePolicyForwarderTest.java
new file mode 100644
index 00000000..9da9c1d2
--- /dev/null
+++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/file/FilePolicyForwarderTest.java
@@ -0,0 +1,167 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 Intel Corp. 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.
+ * 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.distribution.forwarding.file;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+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.OptimizationPolicy;
+import org.onap.policy.distribution.model.Policy;
+
+/**
+ * Class to perform unit test of {@link FilePolicyForwarder}.
+ *
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class FilePolicyForwarderTest {
+ @ClassRule
+ public static TemporaryFolder tempFolder = new TemporaryFolder();
+
+ private static final boolean VERBOSE = true;
+ private static final String GROUP_NAME = "fileConfiguration";
+
+ /**
+ * Set up.
+ */
+ @BeforeClass
+ public static void setUp() {
+ final FilePolicyForwarderParameterBuilder builder = new FilePolicyForwarderParameterBuilder();
+ builder.setPath(tempFolder.getRoot().getAbsolutePath().toString()).setVerbose(VERBOSE);
+ final ParameterGroup parameterGroup = new FilePolicyForwarderParameterGroup(builder);
+ parameterGroup.setName(GROUP_NAME);
+ ParameterService.register(parameterGroup);
+ }
+
+ /**
+ * Tear down.
+ */
+ @AfterClass
+ public static void tearDown() {
+ ParameterService.deregister(GROUP_NAME);
+ }
+
+ @Test
+ public void testForwardPolicy() {
+ final Collection<Policy> policies = new ArrayList<>();
+ final OptimizationPolicy policy = new OptimizationPolicy();
+
+ policy.setPolicyName("test");
+ policy.setPolicyDescription("test");
+ policy.setOnapName("");
+ policy.setConfigBody("");
+ policy.setConfigBodyType("");
+ policy.setTimetolive(new Date());
+ policy.setGuard("");
+ policy.setRiskLevel("");
+ policy.setRiskType("");
+ policies.add(policy);
+
+ final FilePolicyForwarder forwarder = new FilePolicyForwarder();
+ forwarder.configure(GROUP_NAME);
+
+ try {
+ forwarder.forward(policies);
+ Path path = Paths.get(tempFolder.getRoot().getAbsolutePath().toString(), policy.getPolicyName());
+ assertTrue(Files.exists(path));
+ } catch (final Exception exp) {
+ fail("Test must not throw an exception");
+ }
+ }
+
+ @Test
+ public void testForwardPolicyError() {
+ final Collection<Policy> policies = new ArrayList<>();
+ OptimizationPolicy policy = new OptimizationPolicy();
+ policy.setPolicyName("test");
+ policy.setPolicyDescription("test");
+ policy.setOnapName("");
+ policy.setConfigBody("");
+ policy.setConfigBodyType("");
+ policy.setTimetolive(new Date());
+ policy.setGuard("");
+ policy.setRiskLevel("");
+ policy.setRiskType("");
+
+ OptimizationPolicy spy = Mockito.spy(policy);
+ Mockito.when(spy.getRiskType()).thenThrow(IOException.class);
+ policies.add(spy);
+
+ final FilePolicyForwarder forwarder = new FilePolicyForwarder();
+ forwarder.configure(GROUP_NAME);
+
+ try {
+ forwarder.forward(policies);
+ fail("Test must throw an exception");
+ } catch (final Exception exp) {
+ assertTrue(exp.getMessage().contains("Error sending policy"));
+ }
+ }
+
+ @Test
+ public void testForwardUnsupportedPolicy() {
+ final Collection<Policy> policies = new ArrayList<>();
+ final FilePolicyForwarder forwarder = new FilePolicyForwarder();
+ forwarder.configure(GROUP_NAME);
+
+ final Policy policy = new UnsupportedPolicy();
+ policies.add(policy);
+
+ try {
+ forwarder.forward(policies);
+ fail("Test must throw an exception");
+ } catch (final Exception exp) {
+ assertTrue(exp.getMessage().contains("Cannot forward policy"));
+ }
+ }
+
+ class UnsupportedPolicy implements Policy {
+
+ @Override
+ public String getPolicyName() {
+ return "unsupported";
+ }
+
+ @Override
+ public String getPolicyType() {
+ return "unsupported";
+ }
+ }
+}