From e52c9ed74a40d86f1494befd89f55f499b28dcfe Mon Sep 17 00:00:00 2001 From: ramverma Date: Tue, 11 Sep 2018 12:55:27 +0100 Subject: Adding policy forwarder for apex-pdp * Adding policy forwarder for apex-pdp * Adding ApexPdpPolicy to hold policies related to apex-pdp * Adding forwarder parameter for configuring apex-pdp forwarder * Adding test cases for all code changes. Change-Id: Ic1cb9bfc11b2b95321ea2d81882c588b025d33fb Issue-ID: POLICY-1101 Signed-off-by: ramverma --- .../apex/pdp/ApexPdpPolicyForwarder.java | 105 +++++++++++++++++++ .../ApexPdpPolicyForwarderParameterBuilder.java | 111 +++++++++++++++++++++ .../pdp/ApexPdpPolicyForwarderParameterGroup.java | 79 +++++++++++++++ 3 files changed, 295 insertions(+) create mode 100644 plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarder.java create mode 100644 plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterBuilder.java create mode 100644 plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroup.java (limited to 'plugins/forwarding-plugins/src/main') 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 new file mode 100644 index 00000000..1a603f04 --- /dev/null +++ b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarder.java @@ -0,0 +1,105 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.apex.pdp; + +import java.util.Collection; + +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; +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.forwarding.xacml.pdp.XacmlPdpPolicyForwarder; +import org.onap.policy.distribution.model.ApexPdpPolicy; +import org.onap.policy.distribution.model.Policy; + +/** + * This class provides an implementation of {@link PolicyForwarder} interface for forwarding the given policies to + * apex-pdp. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class ApexPdpPolicyForwarder implements PolicyForwarder { + + private static final Logger LOGGER = FlexLogger.getLogger(XacmlPdpPolicyForwarder.class); + + private ApexPdpPolicyForwarderParameterGroup apexForwarderParameters; + private EngineServiceFacade engineServiceFacade; + + /** + * {@inheritDoc}. + */ + @Override + public void configure(final String parameterGroupName) { + apexForwarderParameters = ParameterService.get(parameterGroupName); + engineServiceFacade = + new EngineServiceFacade(apexForwarderParameters.getHostname(), apexForwarderParameters.getPort()); + + } + + /** + * {@inheritDoc}. + */ + @Override + public void forward(final Collection policies) throws PolicyForwardingException { + if (policies.size() > 1) { + final String message = "More than one apex policy cannot be forwarded to an apex engine"; + LOGGER.debug(message); + throw new PolicyForwardingException(message); + + } else { + final Policy policy = (Policy) policies.toArray()[0]; + if (policy.getClass().isAssignableFrom(ApexPdpPolicy.class)) { + forwardPolicy((ApexPdpPolicy) policy); + } else { + final String message = "Ignoring the policy as it is not an apex-pdp policy"; + LOGGER.debug(message); + throw new PolicyForwardingException(message); + } + } + } + + /** + * Method to forward a given policy to apex-pdp. + * + * @param apexPolicy the apex policy + * @throws PolicyForwardingException if any exception occurs while forwarding policy + */ + private void forwardPolicy(final ApexPdpPolicy apexPolicy) throws PolicyForwardingException { + try { + engineServiceFacade.init(); + engineServiceFacade.deployModel(apexPolicy.getPolicyName(), apexPolicy.getPolicyInputStream(), + apexForwarderParameters.isIgnoreConflicts(), apexForwarderParameters.isForceUpdate()); + + LOGGER.debug("Sucessfully forwarded the policy to apex-pdp egine at " + + apexForwarderParameters.getHostname() + ":" + apexForwarderParameters.getPort()); + + } catch (final ApexException exp) { + final String message = "Error sending policy to apex-pdp engine at" + apexForwarderParameters.getHostname() + + ":" + apexForwarderParameters.getPort(); + LOGGER.error(message, exp); + throw new PolicyForwardingException(message, exp); + } + } +} + diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterBuilder.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterBuilder.java new file mode 100644 index 00000000..faa066fe --- /dev/null +++ b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterBuilder.java @@ -0,0 +1,111 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.apex.pdp; + +/** + * This builder holds all the parameters needed to build an instance of {@link ApexPdpPolicyForwarderParameterGroup} + * class. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class ApexPdpPolicyForwarderParameterBuilder { + + private String hostname; + private int port; + private boolean ignoreConflicts; + private boolean forceUpdate; + + /** + * Set host name to this {@link ApexPdpPolicyForwarderParameterBuilder} instance. + * + * @param hostname the host name + */ + public ApexPdpPolicyForwarderParameterBuilder setHostname(final String hostname) { + this.hostname = hostname; + return this; + } + + /** + * Set port to this {@link ApexPdpPolicyForwarderParameterBuilder} instance. + * + * @param port the port number + */ + public ApexPdpPolicyForwarderParameterBuilder setPort(final int port) { + this.port = port; + return this; + } + + /** + * Set ignore conflicts flag to this {@link ApexPdpPolicyForwarderParameterBuilder} instance. + * + * @param ignoreConflicts the ignore conflicts flag + */ + public ApexPdpPolicyForwarderParameterBuilder setIgnoreConflicts(final boolean ignoreConflicts) { + this.ignoreConflicts = ignoreConflicts; + return this; + } + + /** + * Set force update flag to this {@link ApexPdpPolicyForwarderParameterBuilder} instance. + * + * @param forceUpdate the force update flag + */ + public ApexPdpPolicyForwarderParameterBuilder setForceUpdate(final boolean forceUpdate) { + this.forceUpdate = forceUpdate; + return this; + } + + /** + * Returns the host name of this {@link ApexPdpPolicyForwarderParameterBuilder} instance. + * + * @return the host name + */ + public String getHostname() { + return hostname; + } + + /** + * Returns the port of this {@link ApexPdpPolicyForwarderParameterBuilder} instance. + * + * @return the port + */ + public int getPort() { + return port; + } + + /** + * Returns the ignore conflicts flag of this {@link ApexPdpPolicyForwarderParameterBuilder} instance. + * + * @return the ignoreConflicts + */ + public boolean isIgnoreConflicts() { + return ignoreConflicts; + } + + /** + * Returns the force update flag of this {@link ApexPdpPolicyForwarderParameterBuilder} instance. + * + * @return the forceUpdate + */ + public boolean isForceUpdate() { + return forceUpdate; + } +} diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroup.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroup.java new file mode 100644 index 00000000..b236fabd --- /dev/null +++ b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroup.java @@ -0,0 +1,79 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. 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.apex.pdp; + +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 ApexPdpPolicyForwarder}. + */ +public class ApexPdpPolicyForwarderParameterGroup extends PolicyForwarderConfigurationParameterGroup { + public static final String POLICY_FORWARDER_PLUGIN_CLASS = ApexPdpPolicyForwarder.class.getCanonicalName(); + + private String hostname; + private int port; + private boolean ignoreConflicts; + private boolean forceUpdate; + + /** + * Constructor for instantiating {@link ApexPdpPolicyForwarderParameterGroup} class. + * + * @param builder the apex forwarder parameter builder + */ + public ApexPdpPolicyForwarderParameterGroup(final ApexPdpPolicyForwarderParameterBuilder builder) { + this.hostname = builder.getHostname(); + this.port = builder.getPort(); + this.ignoreConflicts = builder.isIgnoreConflicts(); + this.forceUpdate = builder.isForceUpdate(); + } + + public String getHostname() { + return hostname; + } + + public int getPort() { + return port; + } + + public boolean isIgnoreConflicts() { + return ignoreConflicts; + } + + public boolean isForceUpdate() { + return forceUpdate; + } + + @Override + public GroupValidationResult validate() { + final GroupValidationResult validationResult = new GroupValidationResult(this); + if (!ParameterValidationUtils.validateStringParameter(hostname)) { + validationResult.setResult("hostname", ValidationStatus.INVALID, + "must be a non-blank string containing hostname/ipaddress"); + } + if (!ParameterValidationUtils.validateIntParameter(port)) { + validationResult.setResult("port", ValidationStatus.INVALID, "must be a positive integer containing port"); + } + return validationResult; + } +} -- cgit 1.2.3-korg