From ccc3e28512988345e640097381d2ae0be46fe986 Mon Sep 17 00:00:00 2001 From: Ram Krishna Verma Date: Mon, 2 Mar 2020 15:27:21 -0500 Subject: Remove policy/engine & apex dependency from distribution Issue-ID: POLICY-2404 Change-Id: Ia3084c55ea65a3119354079d9afd3ee35689b7d4 Signed-off-by: Ram Krishna Verma --- .../apex/pdp/ApexPdpPolicyForwarder.java | 120 --------- .../pdp/ApexPdpPolicyForwarderParameterGroup.java | 50 ---- .../xacml/pdp/XacmlPdpPolicyAdapter.java | 53 ---- .../xacml/pdp/XacmlPdpPolicyForwarder.java | 146 ---------- .../pdp/XacmlPdpPolicyForwarderParameterGroup.java | 54 ---- .../adapters/AbstractXacmlPdpPolicyAdapter.java | 54 ---- .../XacmlPdpOptimizationPolicyAdapter.java | 47 ---- .../ApexPdpPolicyForwarderParameterGroupTest.java | 70 ----- .../apex/pdp/ApexPdpPolicyForwarderTest.java | 204 -------------- .../api/LifecycleApiForwarderParametersTest.java | 2 +- .../api/LifecycleApiPolicyForwarderTest.java | 4 +- .../forwarding/testclasses/CommonTestData.java | 53 ++++ .../forwarding/testclasses/DummyDecoder.java | 64 +++++ .../testclasses/DummyReceptionHandler.java | 40 +++ .../testclasses/LifecycleApiSimulatorMain.java | 66 +++++ .../LifecycycleApiSimulatorEndpoint.java | 95 +++++++ .../XacmlPdpPolicyForwarderParameterGroupTest.java | 74 ----- .../pdp/engine/XacmlPdpPolicyForwarderTest.java | 298 --------------------- .../xacml/pdp/testclasses/CommonTestData.java | 53 ---- .../xacml/pdp/testclasses/DummyDecoder.java | 64 ----- .../pdp/testclasses/DummyReceptionHandler.java | 40 --- .../pdp/testclasses/LifecycleApiSimulatorMain.java | 66 ----- .../LifecycycleApiSimulatorEndpoint.java | 95 ------- 23 files changed, 321 insertions(+), 1491 deletions(-) delete mode 100644 plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarder.java delete mode 100644 plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroup.java delete mode 100644 plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyAdapter.java delete mode 100644 plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyForwarder.java delete mode 100644 plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyForwarderParameterGroup.java delete mode 100644 plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/adapters/AbstractXacmlPdpPolicyAdapter.java delete mode 100644 plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/adapters/XacmlPdpOptimizationPolicyAdapter.java delete mode 100644 plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroupTest.java delete mode 100644 plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderTest.java create mode 100644 plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/CommonTestData.java create mode 100644 plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/DummyDecoder.java create mode 100644 plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/DummyReceptionHandler.java create mode 100644 plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/LifecycleApiSimulatorMain.java create mode 100644 plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/LifecycycleApiSimulatorEndpoint.java delete mode 100644 plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderParameterGroupTest.java delete mode 100644 plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderTest.java delete mode 100644 plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/CommonTestData.java delete mode 100644 plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/DummyDecoder.java delete mode 100644 plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/DummyReceptionHandler.java delete mode 100644 plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/LifecycleApiSimulatorMain.java delete mode 100644 plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/LifecycycleApiSimulatorEndpoint.java (limited to 'plugins/forwarding-plugins/src') 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 deleted file mode 100644 index 996b887d..00000000 --- a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarder.java +++ /dev/null @@ -1,120 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * 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.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.parameters.ParameterService; -import org.onap.policy.distribution.forwarding.PolicyForwarder; -import org.onap.policy.distribution.forwarding.PolicyForwardingException; -import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * 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 = LoggerFactory.getLogger(ApexPdpPolicyForwarder.class); - private static final String POLICY_TYPE = "APEX"; - - 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.isEmpty()) { - final String message = "No apex policy to be forwarded to an apex engine"; - LOGGER.debug(message); - throw new PolicyForwardingException(message); - } else 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 ToscaEntity policy = (ToscaEntity) policies.toArray()[0]; - if (policy instanceof ToscaPolicy) { - final ToscaPolicy toscaPolicy = (ToscaPolicy) policy; - if (POLICY_TYPE.equalsIgnoreCase(toscaPolicy.getType())) { - forwardPolicy(toscaPolicy); - } else { - final String message = "Ignoring the policy as it is not an apex-pdp policy"; - LOGGER.debug(message); - throw new PolicyForwardingException(message); - } - } else { - final String message = "Ignoring the policy as it is not of type ToscaPolicy"; - 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 ToscaPolicy apexPolicy) throws PolicyForwardingException { - try { - engineServiceFacade.init(); - final InputStream policyInputStream = IOUtils.toInputStream(apexPolicy.toString(), "UTF-8"); - engineServiceFacade.deployModel(apexPolicy.getName(), policyInputStream, - apexForwarderParameters.isIgnoreConflicts(), apexForwarderParameters.isForceUpdate()); - - LOGGER.debug("Sucessfully forwarded the policy to apex-pdp egine at {}:{}", - apexForwarderParameters.getHostname(), apexForwarderParameters.getPort()); - - } catch (final ApexException | IOException 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/ApexPdpPolicyForwarderParameterGroup.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroup.java deleted file mode 100644 index 77c98a2d..00000000 --- a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroup.java +++ /dev/null @@ -1,50 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 AT&T Intellectual Property. - * Modifications Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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 lombok.Getter; - -import org.onap.policy.common.parameters.annotations.Min; -import org.onap.policy.common.parameters.annotations.NotBlank; -import org.onap.policy.common.parameters.annotations.NotNull; -import org.onap.policy.distribution.main.parameters.PolicyForwarderConfigurationParameterGroup; - -/** - * Holds the parameters for the{@link ApexPdpPolicyForwarder}. - */ -@Getter -@NotNull -@NotBlank -public class ApexPdpPolicyForwarderParameterGroup extends PolicyForwarderConfigurationParameterGroup { - public static final String POLICY_FORWARDER_PLUGIN_CLASS = ApexPdpPolicyForwarder.class.getName(); - - private String hostname; - @Min(value = 1) - private int port; - private boolean ignoreConflicts; - private boolean forceUpdate; - - public ApexPdpPolicyForwarderParameterGroup() { - super(ApexPdpPolicyForwarderParameterGroup.class.getSimpleName()); - } -} diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyAdapter.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyAdapter.java deleted file mode 100644 index 613a406f..00000000 --- a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyAdapter.java +++ /dev/null @@ -1,53 +0,0 @@ -/*- - * ============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.xacml.pdp; - -import org.onap.policy.api.PolicyParameters; -import org.onap.policy.api.PushPolicyParameters; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; - -/** - * Adapts {@link ToscaPolicy} objects to objects compatible with the XACML PDP API. - */ -public interface XacmlPdpPolicyAdapter { - - /** - * Get the policy. - * - * @return the policy - */ - T getPolicy(); - - /** - * Get as a {@link PolicyParameters} object. - * - * @returna {@link PolicyParameters} object - */ - PolicyParameters getAsPolicyParameters(); - - /** - * Get as a {@link PushPolicyParameters} object. - * - * @returna {@link PushPolicyParameters} object - */ - PushPolicyParameters getAsPushPolicyParameters(final String pdpGroups); - -} diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyForwarder.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyForwarder.java deleted file mode 100644 index 8eeeda7e..00000000 --- a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyForwarder.java +++ /dev/null @@ -1,146 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * Copyright (C) 2019 Intel Corp. All rights reserved. - * Modifications Copyright (C) 2019 AT&T Intellectual Property. 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.xacml.pdp; - -import java.util.Collection; -import java.util.Collections; - -import javax.ws.rs.client.Entity; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.onap.policy.api.PolicyParameters; -import org.onap.policy.api.PushPolicyParameters; -import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; -import org.onap.policy.common.endpoints.http.client.HttpClient; -import org.onap.policy.common.endpoints.http.client.HttpClientConfigException; -import org.onap.policy.common.endpoints.http.client.HttpClientFactory; -import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance; -import org.onap.policy.common.parameters.ParameterService; -import org.onap.policy.distribution.forwarding.PolicyForwarder; -import org.onap.policy.distribution.forwarding.xacml.pdp.adapters.XacmlPdpOptimizationPolicyAdapter; -import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.http.HttpStatus; - -/** - * Forwards policies to the XACML PDP. - */ -public class XacmlPdpPolicyForwarder implements PolicyForwarder { - - private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpPolicyForwarder.class); - private static final String BASE_PATH = "pdp/api/"; - - private XacmlPdpPolicyForwarderParameterGroup configurationParameters = null; - - - @Override - public void forward(final Collection policies) { - for (final ToscaEntity policy : policies) { - forward(policy); - } - } - - private void forward(final ToscaEntity policy) { - final XacmlPdpPolicyAdapter policyAdapter = getXacmlPdpPolicyAdapter(policy); - - if (policyAdapter == null) { - LOGGER.error("Cannot forward policy {}. Unsupported policy type {}", policy, - policy.getClass().getSimpleName()); - return; - } - - final boolean policyCreated = createPolicy(policyAdapter); - if (policyCreated) { - pushPolicy(policyAdapter); - } - } - - private XacmlPdpPolicyAdapter getXacmlPdpPolicyAdapter(final ToscaEntity policy) { - if (policy instanceof ToscaPolicy) { - return new XacmlPdpOptimizationPolicyAdapter((ToscaPolicy) policy); - } - return null; - } - - private boolean createPolicy(final XacmlPdpPolicyAdapter policyAdapter) { - final PolicyParameters policyParameters = policyAdapter.getAsPolicyParameters(); - final Entity entity = Entity.entity(policyParameters, MediaType.APPLICATION_JSON); - - return invokeHttpClient(entity, "createPolicy", policyAdapter.getPolicy().getName()); - } - - private boolean pushPolicy(final XacmlPdpPolicyAdapter policyAdapter) { - final PushPolicyParameters pushPolicyParameters = - policyAdapter.getAsPushPolicyParameters(configurationParameters.getPdpGroup()); - final Entity entity = Entity.entity(pushPolicyParameters, MediaType.APPLICATION_JSON); - - return invokeHttpClient(entity, "pushPolicy", policyAdapter.getPolicy().getName()); - } - - private boolean invokeHttpClient(final Entity entity, final String method, final String policyName) { - - try { - final Response response = getHttpClient().put(method, entity, - Collections.singletonMap("ClientAuth", configurationParameters.getClientAuth())); - - if (response.getStatus() != HttpStatus.OK.value()) { - LOGGER.error( - "Invocation of method {} failed for policy {}. Response status: {}, Response status info: {}", - method, policyName, response.getStatus(), response.getStatusInfo()); - return false; - } - } catch (final HttpClientConfigException exception) { - LOGGER.error("Invocation of method " + method + " failed for policy " + policyName - + " due to error opening Http client", exception); - return false; - } - return true; - } - - private HttpClient getHttpClient() throws HttpClientConfigException { - final boolean useHttps = configurationParameters.isUseHttps(); - final String hostname = configurationParameters.getHostname(); - final int port = configurationParameters.getPort(); - final String userName = configurationParameters.getUserName(); - final String password = configurationParameters.getPassword(); - final boolean managed = configurationParameters.isManaged(); - final BusTopicParams params = - BusTopicParams.builder().clientName("SDC Dist").useHttps(useHttps).hostname(hostname).port(port) - .userName(userName).password(password).basePath(BASE_PATH).managed(managed).build(); - return getHttpClientFactory().build(params); - } - - @Override - public void configure(final String parameterGroupName) { - configurationParameters = ParameterService.get(parameterGroupName); - } - - // these may be overridden by junit tests - - protected HttpClientFactory getHttpClientFactory() { - return HttpClientFactoryInstance.getClientFactory(); - } -} diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyForwarderParameterGroup.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyForwarderParameterGroup.java deleted file mode 100644 index 4ce35f20..00000000 --- a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyForwarderParameterGroup.java +++ /dev/null @@ -1,54 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 AT&T Intellectual Property. - * Modifications Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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.xacml.pdp; - -import lombok.Getter; - -import org.onap.policy.common.parameters.annotations.Min; -import org.onap.policy.common.parameters.annotations.NotBlank; -import org.onap.policy.common.parameters.annotations.NotNull; -import org.onap.policy.distribution.main.parameters.PolicyForwarderConfigurationParameterGroup; - -/** - * Holds the parameters for the{@link XacmlPdpPolicyForwarder}. - */ -@Getter -@NotNull -@NotBlank -public class XacmlPdpPolicyForwarderParameterGroup extends PolicyForwarderConfigurationParameterGroup { - public static final String POLICY_FORWARDER_PLUGIN_CLASS = XacmlPdpPolicyForwarder.class.getName(); - - private boolean useHttps; - private String hostname; - @Min(value = 1) - private int port; - private String userName; - private String password; - private String clientAuth; - private boolean isManaged; - private String pdpGroup; - - public XacmlPdpPolicyForwarderParameterGroup() { - super(XacmlPdpPolicyForwarderParameterGroup.class.getSimpleName()); - } -} diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/adapters/AbstractXacmlPdpPolicyAdapter.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/adapters/AbstractXacmlPdpPolicyAdapter.java deleted file mode 100644 index f978d3a9..00000000 --- a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/adapters/AbstractXacmlPdpPolicyAdapter.java +++ /dev/null @@ -1,54 +0,0 @@ -/*- - * ============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.xacml.pdp.adapters; - -import org.onap.policy.api.PushPolicyParameters; -import org.onap.policy.distribution.forwarding.xacml.pdp.XacmlPdpPolicyAdapter; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; - -/** - * Base class for {@link XacmlPdpPolicyAdapter} implementations. - * - * @param the type of policy the adapter handles - */ -public abstract class AbstractXacmlPdpPolicyAdapter implements XacmlPdpPolicyAdapter { - - private T policy; - - protected AbstractXacmlPdpPolicyAdapter(final T policy) { - this.policy = policy; - } - - @Override - public T getPolicy() { - return policy; - } - - @Override - public PushPolicyParameters getAsPushPolicyParameters(final String pdpGroups) { - final PushPolicyParameters pushPolicyParameters = new PushPolicyParameters(); - pushPolicyParameters.setPolicyName(policy.getName()); - pushPolicyParameters.setPolicyType(policy.getType()); - pushPolicyParameters.setPdpGroup(pdpGroups); - return pushPolicyParameters; - } - -} diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/adapters/XacmlPdpOptimizationPolicyAdapter.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/adapters/XacmlPdpOptimizationPolicyAdapter.java deleted file mode 100644 index c9633b15..00000000 --- a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/adapters/XacmlPdpOptimizationPolicyAdapter.java +++ /dev/null @@ -1,47 +0,0 @@ -/*- - * ============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.xacml.pdp.adapters; - -import org.onap.policy.api.PolicyParameters; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; - -/** - * Adapts {@link ToscaPolicy} objects to objects compatible with the XACML PDP API. - */ -public class XacmlPdpOptimizationPolicyAdapter extends AbstractXacmlPdpPolicyAdapter { - - /** - * Create an instance to adapt the given {@link ToscaPolicy}. - * - * @param optimizationPolicy the {@link ToscaPolicy} to be adapted - */ - public XacmlPdpOptimizationPolicyAdapter(final ToscaPolicy optimizationPolicy) { - super(optimizationPolicy); - } - - @Override - public PolicyParameters getAsPolicyParameters() { - final PolicyParameters policyParameters = new PolicyParameters(); - policyParameters.setPolicyName(getPolicy().getName()); - return policyParameters; - } - -} 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 deleted file mode 100644 index fc0fe3db..00000000 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderParameterGroupTest.java +++ /dev/null @@ -1,70 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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 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; -import org.onap.policy.distribution.forwarding.xacml.pdp.testclasses.CommonTestData; - -/** - * Class to perform unit test of {@link ApexPdpPolicyForwarderParameterGroup}. - * - * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) - */ -public class ApexPdpPolicyForwarderParameterGroupTest { - - @Test - public void testValidParameters() { - final ApexPdpPolicyForwarderParameterGroup configurationParameters = CommonTestData - .getPolicyForwarderParameters("src/test/resources/parameters/ApexPdpPolicyForwarderParameters.json", - ApexPdpPolicyForwarderParameterGroup.class); - assertEquals(ApexPdpPolicyForwarderParameterGroup.class.getSimpleName(), configurationParameters.getName()); - assertTrue(configurationParameters.isForceUpdate()); - assertEquals("10.10.10.10", configurationParameters.getHostname()); - assertEquals(1234, configurationParameters.getPort()); - assertFalse(configurationParameters.isIgnoreConflicts()); - assertEquals(ValidationStatus.CLEAN, configurationParameters.validate().getStatus()); - } - - @Test - public void testInvalidParameters() { - final ApexPdpPolicyForwarderParameterGroup configurationParameters = - CommonTestData.getPolicyForwarderParameters( - "src/test/resources/parameters/ApexPdpPolicyForwarderParametersInvalid.json", - ApexPdpPolicyForwarderParameterGroup.class); - - assertEquals(ValidationStatus.INVALID, configurationParameters.validate().getStatus()); - } - - @Test - public void testEmptyParameters() { - final ApexPdpPolicyForwarderParameterGroup configurationParameters = - CommonTestData.getPolicyForwarderParameters("src/test/resources/parameters/EmptyParameters.json", - ApexPdpPolicyForwarderParameterGroup.class); - - assertEquals(ValidationStatus.INVALID, configurationParameters.validate().getStatus()); - } -} 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 deleted file mode 100644 index 7d6743e1..00000000 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/apex/pdp/ApexPdpPolicyForwarderTest.java +++ /dev/null @@ -1,204 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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 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.FileNotFoundException; -import java.io.IOException; -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.Collection; - -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.runners.MockitoJUnitRunner; -import org.onap.policy.apex.core.deployment.EngineServiceFacade; -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.forwarding.xacml.pdp.testclasses.CommonTestData; -import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; - -/** - * Class to perform unit test of {@link ApexPdpPolicyForwarder}. - * - * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) - */ -@RunWith(MockitoJUnitRunner.class) -public class ApexPdpPolicyForwarderTest { - - private static final boolean IGNORE_CONFLICTS = false; - private static final boolean FORCE_UPDATE = true; - private static final String GROUP_NAME = "apexPdpConfiguration"; - - @Mock - EngineServiceFacade engineServiceFacade; - - /** - * Set up. - */ - @BeforeClass - public static void setUp() { - final ParameterGroup parameterGroup = CommonTestData.getPolicyForwarderParameters( - "src/test/resources/parameters/ApexPdpPolicyForwarderParameters.json", - ApexPdpPolicyForwarderParameterGroup.class); - - parameterGroup.setName(GROUP_NAME); - ParameterService.register(parameterGroup); - } - - /** - * Tear down. - */ - @AfterClass - public static void tearDown() { - ParameterService.deregister(GROUP_NAME); - } - - @Test - public void testForwardPolicy() throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, - NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { - - final Collection policies = new ArrayList<>(); - final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder(); - forwarder.configure(GROUP_NAME); - - final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade"); - forwarderField.setAccessible(true); - forwarderField.set(forwarder, engineServiceFacade); - - createPolicy(policies, "policy", "APEX", "Sample Policy of apex"); - - try { - forwarder.forward(policies); - verify(engineServiceFacade, times(1)).init(); - 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"); - } - } - - @Test - public void testForwardPolicyError() - throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException, - SecurityException, IllegalArgumentException, IllegalAccessException { - - final Collection policies = new ArrayList<>(); - final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder(); - forwarder.configure(GROUP_NAME); - - 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); - - createPolicy(policies, "policy1", "APEX", "Sample Policy of apex"); - - try { - forwarder.forward(policies); - fail("Test must throw an exception"); - } catch (final Exception exp) { - assertTrue(exp.getMessage().contains("Error sending policy to apex-pdp engine")); - } - - } - - @Test - public void testForwardMoreThanOnePolicy() - throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException, - SecurityException, IllegalArgumentException, IllegalAccessException { - - final Collection policies = new ArrayList<>(); - final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder(); - forwarder.configure(GROUP_NAME); - - final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade"); - forwarderField.setAccessible(true); - forwarderField.set(forwarder, engineServiceFacade); - - createPolicy(policies, "policy1", "APEX", "Sample Policy of apex"); - createPolicy(policies, "policy2", "APEX", "Sample Policy of apex"); - - try { - forwarder.forward(policies); - fail("Test must throw an exception"); - } catch (final Exception exp) { - assertTrue(exp.getMessage().contains("More than one apex policy cannot be forwarded to an apex engine")); - } - } - - @Test - public void testForwardUnsupportedPolicy() - throws ApexException, FileNotFoundException, IOException, PolicyForwardingException, NoSuchFieldException, - SecurityException, IllegalArgumentException, IllegalAccessException { - - final Collection policies = new ArrayList<>(); - final ApexPdpPolicyForwarder forwarder = new ApexPdpPolicyForwarder(); - forwarder.configure(GROUP_NAME); - - final Field forwarderField = forwarder.getClass().getDeclaredField("engineServiceFacade"); - forwarderField.setAccessible(true); - forwarderField.set(forwarder, engineServiceFacade); - - final ToscaEntity policy = new UnsupportedPolicy(); - policies.add(policy); - - try { - forwarder.forward(policies); - fail("Test must throw an exception"); - } catch (final Exception exp) { - assertTrue(exp.getMessage().contains("Ignoring the policy as it is not of type ToscaPolicy")); - } - } - - class UnsupportedPolicy extends ToscaEntity { - - @Override - public String getName() { - return "unsupported"; - } - } - - private void createPolicy(final Collection policies, final String name, final String type, - final String description) { - final ToscaPolicy policy = new ToscaPolicy(); - policy.setName(name); - policy.setType(type); - policy.setDescription(description); - policies.add(policy); - } -} diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/lifecycle/api/LifecycleApiForwarderParametersTest.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/lifecycle/api/LifecycleApiForwarderParametersTest.java index fd8422d6..0f010bfc 100644 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/lifecycle/api/LifecycleApiForwarderParametersTest.java +++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/lifecycle/api/LifecycleApiForwarderParametersTest.java @@ -26,7 +26,7 @@ import static org.junit.Assert.assertTrue; import org.junit.Test; import org.onap.policy.common.parameters.ValidationStatus; -import org.onap.policy.distribution.forwarding.xacml.pdp.testclasses.CommonTestData; +import org.onap.policy.distribution.forwarding.testclasses.CommonTestData; /** * Class to perform unit test of {@link LifecycleApiForwarderParameters}. diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/lifecycle/api/LifecycleApiPolicyForwarderTest.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/lifecycle/api/LifecycleApiPolicyForwarderTest.java index 4d837cdf..7d84657a 100644 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/lifecycle/api/LifecycleApiPolicyForwarderTest.java +++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/lifecycle/api/LifecycleApiPolicyForwarderTest.java @@ -35,8 +35,8 @@ import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.network.NetworkUtil; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.distribution.forwarding.PolicyForwardingException; -import org.onap.policy.distribution.forwarding.xacml.pdp.testclasses.CommonTestData; -import org.onap.policy.distribution.forwarding.xacml.pdp.testclasses.LifecycleApiSimulatorMain; +import org.onap.policy.distribution.forwarding.testclasses.CommonTestData; +import org.onap.policy.distribution.forwarding.testclasses.LifecycleApiSimulatorMain; import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/CommonTestData.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/CommonTestData.java new file mode 100644 index 00000000..9da1b613 --- /dev/null +++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/CommonTestData.java @@ -0,0 +1,53 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.testclasses; + +import java.io.File; + +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; + +/** + * Class to create parameters for test cases. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +public class CommonTestData { + + public static final Coder coder = new StandardCoder(); + + /** + * Returns PolicyForwarderParameters for test cases. + * + * @param fileName the file name to load the parameters + * @param clazz the parameter class to be returned + * @return the specific PolicyForwarderParameters object + */ + public static T getPolicyForwarderParameters(final String fileName, final Class clazz) { + final StandardCoder coder = new StandardCoder(); + try { + return coder.decode(new File(fileName), clazz); + } catch (final CoderException exp) { + throw new RuntimeException("cannot read/decode " + fileName, exp); + } + } +} diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/DummyDecoder.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/DummyDecoder.java new file mode 100644 index 00000000..b40aabf8 --- /dev/null +++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/DummyDecoder.java @@ -0,0 +1,64 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2020 AT&T Intellectual Property. 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.testclasses; + +import java.util.Collection; + +import org.onap.policy.distribution.model.PolicyInput; +import org.onap.policy.distribution.reception.decoding.PolicyDecoder; +import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; +import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; + +/** + * Class to create a dummy decoder for test cases. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class DummyDecoder implements PolicyDecoder { + + private boolean canHandleValue; + private Collection policesToReturn; + + public DummyDecoder() { + this.canHandleValue = false; + this.policesToReturn = null; + } + + public DummyDecoder(final boolean canHandleValue, final Collection policesToReturn) { + this.canHandleValue = canHandleValue; + this.policesToReturn = policesToReturn; + } + + @Override + public boolean canHandle(final PolicyInput policyInput) { + return canHandleValue; + } + + @Override + public Collection decode(final PolicyInput input) throws PolicyDecodingException { + return policesToReturn; + } + + @Override + public void configure(final String parameterGroupName) { + } +} diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/DummyReceptionHandler.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/DummyReceptionHandler.java new file mode 100644 index 00000000..b3cf489c --- /dev/null +++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/DummyReceptionHandler.java @@ -0,0 +1,40 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2020 AT&T Intellectual Property. 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.testclasses; + +import org.onap.policy.distribution.reception.handling.AbstractReceptionHandler; + +/** + * Class to create a dummy reception handler for test cases. + * + * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) + */ +public class DummyReceptionHandler extends AbstractReceptionHandler { + + @Override + public void initializeReception(final String parameterGroupName) { + } + + @Override + public void destroy() { + } +} diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/LifecycleApiSimulatorMain.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/LifecycleApiSimulatorMain.java new file mode 100644 index 00000000..07c4b6c6 --- /dev/null +++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/LifecycleApiSimulatorMain.java @@ -0,0 +1,66 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.testclasses; + +import org.onap.policy.common.endpoints.http.server.RestServer; +import org.onap.policy.common.endpoints.parameters.RestServerParameters; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.distribution.forwarding.PolicyForwardingException; +import org.onap.policy.distribution.forwarding.lifecycle.api.LifecycleApiPolicyForwarder; +import org.onap.policy.distribution.main.rest.aaf.AafDistributionFilter; + +/** + * The class for starting/stopping simulator for testing {@link LifecycleApiPolicyForwarder} . + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +public class LifecycleApiSimulatorMain { + private RestServer restServer; + + /** + * Starts the simulator. + * + * @throws PolicyForwardingException if error occurs + * @throws CoderException if error occurs + */ + public void startLifecycycleApiSimulator() throws PolicyForwardingException, CoderException { + final StandardCoder standardCoder = new StandardCoder(); + final RestServerParameters restServerParameters = standardCoder.decode( + ResourceUtils.getResourceAsString("src/test/resources/parameters/RestServerParameters.json"), + RestServerParameters.class); + restServer = new RestServer(restServerParameters, AafDistributionFilter.class, + LifecycycleApiSimulatorEndpoint.class); + if (!restServer.start()) { + throw new PolicyForwardingException("Failed to start rest simulator. Check log for more details..."); + } + } + + /** + * Shut down Execution. + */ + public void stopLifecycycleApiSimulator() { + if (restServer != null) { + restServer.stop(); + } + } +} diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/LifecycycleApiSimulatorEndpoint.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/LifecycycleApiSimulatorEndpoint.java new file mode 100644 index 00000000..4d7d2600 --- /dev/null +++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/testclasses/LifecycycleApiSimulatorEndpoint.java @@ -0,0 +1,95 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.testclasses; + +import io.swagger.annotations.ApiParam; + +import javax.ws.rs.Consumes; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.onap.policy.models.pap.concepts.PdpDeployPolicies; +import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; + +/** + * Class to provide rest end points for LifecycycleApiSimulator. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +@Path("/policy") +@Produces(MediaType.APPLICATION_JSON) +public class LifecycycleApiSimulatorEndpoint { + + /** + * Create policy type endpoint. + * + * @param body the post body + * @return the response object + */ + @POST + @Path("/api/v1/policytypes") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Response createPolicyTypes(final ToscaServiceTemplate body) { + return Response.status(Response.Status.OK).entity(body).build(); + } + + /** + * Create policy endpoint. + * + * @param policyTypeId the policy type id + * @param policyTypeVersion the policy type version + * @param body the post body + * @return the response object + */ + @POST + @Path("/api/v1/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Response createPolicies(@PathParam("policyTypeId") final String policyTypeId, + @PathParam("policyTypeVersion") final String policyTypeVersion, + @ApiParam(value = "Entity body of policy", required = true) final ToscaServiceTemplate body) { + if ("onap.policies.controlloop.operational.ApexFailure".equals(policyTypeId)) { + return Response.status(Response.Status.NOT_FOUND).build(); + } else { + return Response.status(Response.Status.OK).entity(body).build(); + } + } + + /** + * Deploy policy endpoint. + * + * @param policies the post body + * @return the response object + */ + @POST + @Path("/pap/v1/pdps/policies") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Response deployPolicies(final PdpDeployPolicies policies) { + return Response.status(Response.Status.OK).entity(new PdpGroupDeployResponse()).build(); + } +} diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderParameterGroupTest.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderParameterGroupTest.java deleted file mode 100644 index 6e98b225..00000000 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderParameterGroupTest.java +++ /dev/null @@ -1,74 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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.xacml.pdp.engine; - -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; -import org.onap.policy.distribution.forwarding.xacml.pdp.XacmlPdpPolicyForwarderParameterGroup; -import org.onap.policy.distribution.forwarding.xacml.pdp.testclasses.CommonTestData; - -public class XacmlPdpPolicyForwarderParameterGroupTest { - - private static final String CLIENT_AUTH = "myClientAuth"; - private static final String PASSWORD = "myPassword"; - private static final String USER = "myUser"; - private static final int PORT = 1234; - private static final String HOST_NAME = "10.10.10.10"; - - @Test - public void testValidParameters() { - final XacmlPdpPolicyForwarderParameterGroup configurationParameters = CommonTestData - .getPolicyForwarderParameters("src/test/resources/parameters/XacmlPdpPolicyForwarderParameters.json", - XacmlPdpPolicyForwarderParameterGroup.class); - - assertTrue(configurationParameters.isUseHttps()); - assertEquals(HOST_NAME, configurationParameters.getHostname()); - assertEquals(PORT, configurationParameters.getPort()); - assertEquals(USER, configurationParameters.getUserName()); - assertEquals(PASSWORD, configurationParameters.getPassword()); - assertEquals(CLIENT_AUTH, configurationParameters.getClientAuth()); - assertFalse(configurationParameters.isManaged()); - } - - @Test - public void testInvalidParameters() { - final XacmlPdpPolicyForwarderParameterGroup configurationParameters = - CommonTestData.getPolicyForwarderParameters( - "src/test/resources/parameters/XacmlPdpPolicyForwarderParametersInvalid.json", - XacmlPdpPolicyForwarderParameterGroup.class); - - assertEquals(ValidationStatus.INVALID, configurationParameters.validate().getStatus()); - } - - @Test - public void testEmptyParameters() { - final XacmlPdpPolicyForwarderParameterGroup configurationParameters = - CommonTestData.getPolicyForwarderParameters("src/test/resources/parameters/EmptyParameters.json", - XacmlPdpPolicyForwarderParameterGroup.class); - - assertEquals(ValidationStatus.INVALID, configurationParameters.validate().getStatus()); - } -} diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderTest.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderTest.java deleted file mode 100644 index 222614d9..00000000 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderTest.java +++ /dev/null @@ -1,298 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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.xacml.pdp.engine; - -import static org.junit.Assert.assertSame; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.argThat; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; - -import javax.ws.rs.client.Entity; -import javax.ws.rs.core.Response; - -import org.junit.BeforeClass; -import org.junit.Test; -import org.mockito.ArgumentMatcher; -import org.onap.policy.api.PolicyParameters; -import org.onap.policy.api.PushPolicyParameters; -import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams; -import org.onap.policy.common.endpoints.http.client.HttpClient; -import org.onap.policy.common.endpoints.http.client.HttpClientConfigException; -import org.onap.policy.common.endpoints.http.client.HttpClientFactory; -import org.onap.policy.common.parameters.ParameterGroup; -import org.onap.policy.common.parameters.ParameterService; -import org.onap.policy.distribution.forwarding.xacml.pdp.XacmlPdpPolicyForwarder; -import org.onap.policy.distribution.forwarding.xacml.pdp.XacmlPdpPolicyForwarderParameterGroup; -import org.onap.policy.distribution.forwarding.xacml.pdp.testclasses.CommonTestData; -import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; - -public class XacmlPdpPolicyForwarderTest { - - private static final BusTopicParams BUS_TOPIC_PARAMS = BusTopicParams.builder().useHttps(true) - .hostname("10.10.10.10").port(1234).userName("myUser").password("myPassword").managed(false).build(); - private static final String CLIENT_AUTH = "ClientAuth"; - private static final String CLIENT_AUTH_VALUE = "myClientAuth"; - private static final String PDP_GROUP_VALUE = "myPdpGroup"; - private HashMap headers = new HashMap<>(); - private BusTopicParamsMatcher matcher = new BusTopicParamsMatcher(BUS_TOPIC_PARAMS); - - /** - * Set up. - */ - @BeforeClass - public static void setUp() { - final ParameterGroup parameterGroup = CommonTestData.getPolicyForwarderParameters( - "src/test/resources/parameters/XacmlPdpPolicyForwarderParameters.json", - XacmlPdpPolicyForwarderParameterGroup.class); - parameterGroup.setName("xacmlPdpConfiguration"); - ParameterService.register(parameterGroup); - } - - @Test - public void testForwardPolicy() throws Exception { - - final HttpClient httpClientMock = mock(HttpClient.class); - headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE); - when(httpClientMock.put(eq("createPolicy"), any(), eq(headers))).thenReturn(Response.ok().build()); - when(httpClientMock.put(eq("pushPolicy"), any(), eq(headers))).thenReturn(Response.ok().build()); - - final HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class); - when(httpClientFactoryMock.build(argThat(matcher))).thenReturn(httpClientMock); - - final XacmlPdpPolicyForwarder forwarder = new MyXacmlPdpPolicyForwarder(httpClientFactoryMock); - forwarder.configure("xacmlPdpConfiguration"); - - final Collection policies = new ArrayList<>(); - - final ToscaPolicy policy1 = createPolicy(policies, "policy1", "optimization"); - - final ToscaEntity policy2 = new UnsupportedPolicy(); - policies.add(policy2); - - final ToscaPolicy policy3 = createPolicy(policies, "policy3", "optimization"); - - forwarder.forward(policies); - - verify(httpClientMock).put(eq("createPolicy"), argThat(new PolicyParametersEntityMatcher(policy1)), - eq(headers)); - verify(httpClientMock).put(eq("createPolicy"), argThat(new PolicyParametersEntityMatcher(policy3)), - eq(headers)); - verify(httpClientMock).put(eq("pushPolicy"), argThat(new PushPolicyParametersEntityMatcher(policy1)), - eq(headers)); - verify(httpClientMock).put(eq("pushPolicy"), argThat(new PushPolicyParametersEntityMatcher(policy3)), - eq(headers)); - } - - @Test - public void testForwardPolicy_CreateFailsPushNotInvoked() throws Exception { - - final HttpClient httpClientMock = mock(HttpClient.class); - headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE); - when(httpClientMock.put(eq("createPolicy"), any(), eq(headers))).thenReturn(Response.status(400).build()); - when(httpClientMock.put(eq("pushPolicy"), any(), eq(headers))).thenReturn(Response.ok().build()); - - final HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class); - when(httpClientFactoryMock.build(argThat(matcher))).thenReturn(httpClientMock); - - final XacmlPdpPolicyForwarder forwarder = new MyXacmlPdpPolicyForwarder(httpClientFactoryMock); - forwarder.configure("xacmlPdpConfiguration"); - - final Collection policies = new ArrayList<>(); - final ToscaPolicy policy = createPolicy(policies, "policy", "optimization"); - forwarder.forward(policies); - - verify(httpClientMock).put(eq("createPolicy"), argThat(new PolicyParametersEntityMatcher(policy)), eq(headers)); - verify(httpClientMock, times(0)).put(eq("pushPolicy"), any(), any()); - } - - @Test - public void testForwardPolicy_PushFails() throws Exception { - - final HttpClient httpClientMock = mock(HttpClient.class); - headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE); - when(httpClientMock.put(eq("createPolicy"), any(), eq(headers))).thenReturn(Response.ok().build()); - when(httpClientMock.put(eq("pushPolicy"), any(), eq(headers))).thenReturn(Response.status(400).build()); - - final HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class); - when(httpClientFactoryMock.build(argThat(matcher))).thenReturn(httpClientMock); - - final XacmlPdpPolicyForwarder forwarder = new MyXacmlPdpPolicyForwarder(httpClientFactoryMock); - forwarder.configure("xacmlPdpConfiguration"); - - final Collection policies = new ArrayList<>(); - final ToscaPolicy policy = createPolicy(policies, "policy", "optimization"); - forwarder.forward(policies); - - verify(httpClientMock).put(eq("createPolicy"), argThat(new PolicyParametersEntityMatcher(policy)), eq(headers)); - verify(httpClientMock).put(eq("pushPolicy"), argThat(new PushPolicyParametersEntityMatcher(policy)), - eq(headers)); - } - - @Test - public void testForwardPolicy_HttpClientInitFailureForPolicyCreate() throws Exception { - - final HttpClient httpClientMock = mock(HttpClient.class); - headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE); - when(httpClientMock.put(eq("createPolicy"), any(), eq(headers))).thenReturn(Response.ok().build()); - when(httpClientMock.put(eq("pushPolicy"), any(), eq(headers))).thenReturn(Response.status(400).build()); - - final HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class); - when(httpClientFactoryMock.build(argThat(matcher))).thenThrow(new HttpClientConfigException()); - - final XacmlPdpPolicyForwarder forwarder = new MyXacmlPdpPolicyForwarder(httpClientFactoryMock); - forwarder.configure("xacmlPdpConfiguration"); - - final Collection policies = new ArrayList<>(); - final ToscaPolicy policy = createPolicy(policies, "policy", "optimization"); - forwarder.forward(policies); - - assertSame(policy, policies.iterator().next()); - - verify(httpClientMock, times(0)).put(eq("createPolicy"), any(), any()); - verify(httpClientMock, times(0)).put(eq("pushPolicy"), any(), any()); - } - - @Test - public void testForwardPolicy_HttpClientInitFailureForPolicyPush() throws Exception { - - final HttpClient httpClientMock = mock(HttpClient.class); - headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE); - when(httpClientMock.put(eq("createPolicy"), any(), eq(headers))).thenReturn(Response.ok().build()); - when(httpClientMock.put(eq("pushPolicy"), any(), eq(headers))).thenReturn(Response.status(400).build()); - - final HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class); - when(httpClientFactoryMock.build(argThat(matcher))).thenReturn(httpClientMock) - .thenThrow(new HttpClientConfigException()); - - final XacmlPdpPolicyForwarder forwarder = new MyXacmlPdpPolicyForwarder(httpClientFactoryMock); - forwarder.configure("xacmlPdpConfiguration"); - - final Collection policies = new ArrayList<>(); - final ToscaPolicy policy = createPolicy(policies, "policy", "optimization"); - forwarder.forward(policies); - - verify(httpClientMock).put(eq("createPolicy"), argThat(new PolicyParametersEntityMatcher(policy)), eq(headers)); - verify(httpClientMock, times(0)).put(eq("pushPolicy"), any(), any()); - } - - class BusTopicParamsMatcher implements ArgumentMatcher { - - private BusTopicParams busTopicParams; - - BusTopicParamsMatcher(final BusTopicParams busTopicParams) { - this.busTopicParams = busTopicParams; - } - - @Override - public boolean matches(final BusTopicParams arg0) { - if (arg0 instanceof BusTopicParams) { - final BusTopicParams toCompareTo = (BusTopicParams) arg0; - return toCompareTo.isUseHttps() == busTopicParams.isUseHttps() - && toCompareTo.getHostname().equals(busTopicParams.getHostname()) - && toCompareTo.getPort() == busTopicParams.getPort() - && toCompareTo.getUserName().equals(busTopicParams.getUserName()) - && toCompareTo.getPassword().equals(busTopicParams.getPassword()) - && toCompareTo.isManaged() == busTopicParams.isManaged(); - } - return false; - } - } - - class PolicyParametersEntityMatcher implements ArgumentMatcher> { - - private ToscaPolicy policy; - - PolicyParametersEntityMatcher(final ToscaPolicy policy) { - this.policy = policy; - } - - @SuppressWarnings("unchecked") - @Override - public boolean matches(final Entity arg0) { - if (arg0 instanceof Entity) { - final PolicyParameters toCompareTo = ((Entity) arg0).getEntity(); - return toCompareTo.getPolicyName().equals(policy.getName()); - } - return false; - } - } - - class PushPolicyParametersEntityMatcher implements ArgumentMatcher> { - - private ToscaPolicy policy; - - PushPolicyParametersEntityMatcher(final ToscaPolicy policy) { - this.policy = policy; - } - - @SuppressWarnings("unchecked") - @Override - public boolean matches(final Entity arg0) { - if (arg0 instanceof Entity) { - final PushPolicyParameters toCompareTo = ((Entity) arg0).getEntity(); - return toCompareTo.getPolicyName().equals(policy.getName()) - && toCompareTo.getPolicyType().equals(policy.getType()) - && toCompareTo.getPdpGroup().equals(PDP_GROUP_VALUE); - } - return false; - } - } - - class UnsupportedPolicy extends ToscaEntity { - - @Override - public String getName() { - return "unsupported"; - } - } - - private class MyXacmlPdpPolicyForwarder extends XacmlPdpPolicyForwarder { - private HttpClientFactory factory; - - public MyXacmlPdpPolicyForwarder(final HttpClientFactory httpClientFactory) { - this.factory = httpClientFactory; - } - - @Override - protected HttpClientFactory getHttpClientFactory() { - return this.factory; - } - } - - private ToscaPolicy createPolicy(final Collection policies, final String name, final String type) { - final ToscaPolicy policy1 = new ToscaPolicy(); - policy1.setName(name); - policy1.setType(type); - policies.add(policy1); - return policy1; - } -} diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/CommonTestData.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/CommonTestData.java deleted file mode 100644 index 386526e5..00000000 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/CommonTestData.java +++ /dev/null @@ -1,53 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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.xacml.pdp.testclasses; - -import java.io.File; - -import org.onap.policy.common.utils.coder.Coder; -import org.onap.policy.common.utils.coder.CoderException; -import org.onap.policy.common.utils.coder.StandardCoder; - -/** - * Class to create parameters for test cases. - * - * @author Ram Krishna Verma (ram.krishna.verma@est.tech) - */ -public class CommonTestData { - - public static final Coder coder = new StandardCoder(); - - /** - * Returns PolicyForwarderParameters for test cases. - * - * @param fileName the file name to load the parameters - * @param clazz the parameter class to be returned - * @return the specific PolicyForwarderParameters object - */ - public static T getPolicyForwarderParameters(final String fileName, final Class clazz) { - final StandardCoder coder = new StandardCoder(); - try { - return coder.decode(new File(fileName), clazz); - } catch (final CoderException exp) { - throw new RuntimeException("cannot read/decode " + fileName, exp); - } - } -} 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 deleted file mode 100644 index f2fb144a..00000000 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/DummyDecoder.java +++ /dev/null @@ -1,64 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 AT&T Intellectual Property. 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.xacml.pdp.testclasses; - -import java.util.Collection; - -import org.onap.policy.distribution.model.PolicyInput; -import org.onap.policy.distribution.reception.decoding.PolicyDecoder; -import org.onap.policy.distribution.reception.decoding.PolicyDecodingException; -import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; - -/** - * Class to create a dummy decoder for test cases. - * - * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) - */ -public class DummyDecoder implements PolicyDecoder { - - private boolean canHandleValue; - private Collection policesToReturn; - - public DummyDecoder() { - this.canHandleValue = false; - this.policesToReturn = null; - } - - public DummyDecoder(final boolean canHandleValue, final Collection policesToReturn) { - this.canHandleValue = canHandleValue; - this.policesToReturn = policesToReturn; - } - - @Override - public boolean canHandle(final PolicyInput policyInput) { - return canHandleValue; - } - - @Override - public Collection decode(final PolicyInput input) throws PolicyDecodingException { - return policesToReturn; - } - - @Override - public void configure(final String parameterGroupName) { - } -} diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/DummyReceptionHandler.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/DummyReceptionHandler.java deleted file mode 100644 index f9bde3c3..00000000 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/DummyReceptionHandler.java +++ /dev/null @@ -1,40 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 AT&T Intellectual Property. 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.xacml.pdp.testclasses; - -import org.onap.policy.distribution.reception.handling.AbstractReceptionHandler; - -/** - * Class to create a dummy reception handler for test cases. - * - * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com) - */ -public class DummyReceptionHandler extends AbstractReceptionHandler { - - @Override - public void initializeReception(final String parameterGroupName) { - } - - @Override - public void destroy() { - } -} diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/LifecycleApiSimulatorMain.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/LifecycleApiSimulatorMain.java deleted file mode 100644 index 9aa68876..00000000 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/LifecycleApiSimulatorMain.java +++ /dev/null @@ -1,66 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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.xacml.pdp.testclasses; - -import org.onap.policy.common.endpoints.http.server.RestServer; -import org.onap.policy.common.endpoints.parameters.RestServerParameters; -import org.onap.policy.common.utils.coder.CoderException; -import org.onap.policy.common.utils.coder.StandardCoder; -import org.onap.policy.common.utils.resources.ResourceUtils; -import org.onap.policy.distribution.forwarding.PolicyForwardingException; -import org.onap.policy.distribution.forwarding.lifecycle.api.LifecycleApiPolicyForwarder; -import org.onap.policy.distribution.main.rest.aaf.AafDistributionFilter; - -/** - * The class for starting/stopping simulator for testing {@link LifecycleApiPolicyForwarder} . - * - * @author Ram Krishna Verma (ram.krishna.verma@est.tech) - */ -public class LifecycleApiSimulatorMain { - private RestServer restServer; - - /** - * Starts the simulator. - * - * @throws PolicyForwardingException if error occurs - * @throws CoderException if error occurs - */ - public void startLifecycycleApiSimulator() throws PolicyForwardingException, CoderException { - final StandardCoder standardCoder = new StandardCoder(); - final RestServerParameters restServerParameters = standardCoder.decode( - ResourceUtils.getResourceAsString("src/test/resources/parameters/RestServerParameters.json"), - RestServerParameters.class); - restServer = new RestServer(restServerParameters, AafDistributionFilter.class, - LifecycycleApiSimulatorEndpoint.class); - if (!restServer.start()) { - throw new PolicyForwardingException("Failed to start rest simulator. Check log for more details..."); - } - } - - /** - * Shut down Execution. - */ - public void stopLifecycycleApiSimulator() { - if (restServer != null) { - restServer.stop(); - } - } -} diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/LifecycycleApiSimulatorEndpoint.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/LifecycycleApiSimulatorEndpoint.java deleted file mode 100644 index f07605f0..00000000 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/testclasses/LifecycycleApiSimulatorEndpoint.java +++ /dev/null @@ -1,95 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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.xacml.pdp.testclasses; - -import io.swagger.annotations.ApiParam; - -import javax.ws.rs.Consumes; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.onap.policy.models.pap.concepts.PdpDeployPolicies; -import org.onap.policy.models.pap.concepts.PdpGroupDeployResponse; -import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; - -/** - * Class to provide rest end points for LifecycycleApiSimulator. - * - * @author Ram Krishna Verma (ram.krishna.verma@est.tech) - */ -@Path("/policy") -@Produces(MediaType.APPLICATION_JSON) -public class LifecycycleApiSimulatorEndpoint { - - /** - * Create policy type endpoint. - * - * @param body the post body - * @return the response object - */ - @POST - @Path("/api/v1/policytypes") - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - public Response createPolicyTypes(final ToscaServiceTemplate body) { - return Response.status(Response.Status.OK).entity(body).build(); - } - - /** - * Create policy endpoint. - * - * @param policyTypeId the policy type id - * @param policyTypeVersion the policy type version - * @param body the post body - * @return the response object - */ - @POST - @Path("/api/v1/policytypes/{policyTypeId}/versions/{policyTypeVersion}/policies") - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - public Response createPolicies(@PathParam("policyTypeId") final String policyTypeId, - @PathParam("policyTypeVersion") final String policyTypeVersion, - @ApiParam(value = "Entity body of policy", required = true) final ToscaServiceTemplate body) { - if ("onap.policies.controlloop.operational.ApexFailure".equals(policyTypeId)) { - return Response.status(Response.Status.NOT_FOUND).build(); - } else { - return Response.status(Response.Status.OK).entity(body).build(); - } - } - - /** - * Deploy policy endpoint. - * - * @param policies the post body - * @return the response object - */ - @POST - @Path("/pap/v1/pdps/policies") - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - public Response deployPolicies(final PdpDeployPolicies policies) { - return Response.status(Response.Status.OK).entity(new PdpGroupDeployResponse()).build(); - } -} -- cgit 1.2.3-korg