2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2018 Ericsson. All rights reserved.
 
   4  * ================================================================================
 
   5  * Licensed under the Apache License, Version 2.0 (the "License");
 
   6  * you may not use this file except in compliance with the License.
 
   7  * You may obtain a copy of the License at
 
   9  *      http://www.apache.org/licenses/LICENSE-2.0
 
  11  * Unless required by applicable law or agreed to in writing, software
 
  12  * distributed under the License is distributed on an "AS IS" BASIS,
 
  13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  14  * See the License for the specific language governing permissions and
 
  15  * limitations under the License.
 
  17  * SPDX-License-Identifier: Apache-2.0
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.policy.distribution.forwarding.apex.pdp;
 
  23 import java.io.IOException;
 
  24 import java.io.InputStream;
 
  25 import java.util.Collection;
 
  27 import org.apache.commons.io.IOUtils;
 
  28 import org.onap.policy.apex.core.deployment.EngineServiceFacade;
 
  29 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
 
  30 import org.onap.policy.common.logging.flexlogger.FlexLogger;
 
  31 import org.onap.policy.common.logging.flexlogger.Logger;
 
  32 import org.onap.policy.common.parameters.ParameterService;
 
  33 import org.onap.policy.distribution.forwarding.PolicyForwarder;
 
  34 import org.onap.policy.distribution.forwarding.PolicyForwardingException;
 
  35 import org.onap.policy.distribution.forwarding.xacml.pdp.XacmlPdpPolicyForwarder;
 
  36 import org.onap.policy.distribution.model.Policy;
 
  37 import org.onap.policy.distribution.model.PolicyAsString;
 
  40  * This class provides an implementation of {@link PolicyForwarder} interface for forwarding the given policies to
 
  43  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
 
  45 public class ApexPdpPolicyForwarder implements PolicyForwarder {
 
  47     private static final Logger LOGGER = FlexLogger.getLogger(XacmlPdpPolicyForwarder.class);
 
  48     private static final String POLICY_TYPE = "APEX";
 
  50     private ApexPdpPolicyForwarderParameterGroup apexForwarderParameters;
 
  51     private EngineServiceFacade engineServiceFacade;
 
  57     public void configure(final String parameterGroupName) {
 
  58         apexForwarderParameters = ParameterService.get(parameterGroupName);
 
  60                 new EngineServiceFacade(apexForwarderParameters.getHostname(), apexForwarderParameters.getPort());
 
  68     public void forward(final Collection<Policy> policies) throws PolicyForwardingException {
 
  69         if (policies.size() > 1) {
 
  70             final String message = "More than one apex policy cannot be forwarded to an apex engine";
 
  71             LOGGER.debug(message);
 
  72             throw new PolicyForwardingException(message);
 
  75             final Policy policy = (Policy) policies.toArray()[0];
 
  76             if (policy.getClass().isAssignableFrom(PolicyAsString.class)
 
  77                     && policy.getPolicyType().equalsIgnoreCase(POLICY_TYPE)) {
 
  78                 forwardPolicy((PolicyAsString) policy);
 
  80                 final String message = "Ignoring the policy as it is not an apex-pdp policy";
 
  81                 LOGGER.debug(message);
 
  82                 throw new PolicyForwardingException(message);
 
  88      * Method to forward a given policy to apex-pdp.
 
  90      * @param apexPolicy the apex policy
 
  91      * @throws PolicyForwardingException if any exception occurs while forwarding policy
 
  93     private void forwardPolicy(final PolicyAsString apexPolicy) throws PolicyForwardingException {
 
  95             engineServiceFacade.init();
 
  96             final InputStream policyInputStream = IOUtils.toInputStream(apexPolicy.getPolicy(), "UTF-8");
 
  97             engineServiceFacade.deployModel(apexPolicy.getPolicyName(), policyInputStream,
 
  98                     apexForwarderParameters.isIgnoreConflicts(), apexForwarderParameters.isForceUpdate());
 
 100             LOGGER.debug("Sucessfully forwarded the policy to apex-pdp egine at "
 
 101                     + apexForwarderParameters.getHostname() + ":" + apexForwarderParameters.getPort());
 
 103         } catch (final ApexException | IOException exp) {
 
 104             final String message = "Error sending policy to apex-pdp engine at" + apexForwarderParameters.getHostname()
 
 105                     + ":" + apexForwarderParameters.getPort();
 
 106             LOGGER.error(message, exp);
 
 107             throw new PolicyForwardingException(message, exp);