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