summaryrefslogtreecommitdiffstats
path: root/plugins/forwarding-plugins/src/main/java
diff options
context:
space:
mode:
authorLianhao Lu <lianhao.lu@intel.com>2019-04-10 12:39:16 +0800
committerLianhao Lu <lianhao.lu@intel.com>2019-04-10 12:44:22 +0800
commit42370a06ea7681b6e422b1d5739ec4d6c1287103 (patch)
treec83b930ac69f2b59d01ce5f9eaa3ae76d2f93c07 /plugins/forwarding-plugins/src/main/java
parentf2c8a75c605ac97459973ce8ad4d57c0e050e402 (diff)
Add a new policy file log forwarder.
By introducing the new file log policy forwarder, we now could remove dependency to external pap/pdp engine during s3p test, and focus only on the performance and stability of the pssd instead. Change-Id: I769d4f4a5425c82fa84e5fae20f08b6527bd5d70 Issue-ID: POLICY-1274 Signed-off-by: Lianhao Lu <lianhao.lu@intel.com>
Diffstat (limited to 'plugins/forwarding-plugins/src/main/java')
-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
3 files changed, 257 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;
+ }
+}