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.util.Collection;
 
  25 import org.onap.policy.apex.core.deployment.EngineServiceFacade;
 
  26 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
 
  27 import org.onap.policy.common.logging.flexlogger.FlexLogger;
 
  28 import org.onap.policy.common.logging.flexlogger.Logger;
 
  29 import org.onap.policy.common.parameters.ParameterService;
 
  30 import org.onap.policy.distribution.forwarding.PolicyForwarder;
 
  31 import org.onap.policy.distribution.forwarding.PolicyForwardingException;
 
  32 import org.onap.policy.distribution.forwarding.xacml.pdp.XacmlPdpPolicyForwarder;
 
  33 import org.onap.policy.distribution.model.ApexPdpPolicy;
 
  34 import org.onap.policy.distribution.model.Policy;
 
  37  * This class provides an implementation of {@link PolicyForwarder} interface for forwarding the given policies to
 
  40  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
 
  42 public class ApexPdpPolicyForwarder implements PolicyForwarder {
 
  44     private static final Logger LOGGER = FlexLogger.getLogger(XacmlPdpPolicyForwarder.class);
 
  46     private ApexPdpPolicyForwarderParameterGroup apexForwarderParameters;
 
  47     private EngineServiceFacade engineServiceFacade;
 
  53     public void configure(final String parameterGroupName) {
 
  54         apexForwarderParameters = ParameterService.get(parameterGroupName);
 
  56                 new EngineServiceFacade(apexForwarderParameters.getHostname(), apexForwarderParameters.getPort());
 
  64     public void forward(final Collection<Policy> policies) throws PolicyForwardingException {
 
  65         if (policies.size() > 1) {
 
  66             final String message = "More than one apex policy cannot be forwarded to an apex engine";
 
  67             LOGGER.debug(message);
 
  68             throw new PolicyForwardingException(message);
 
  71             final Policy policy = (Policy) policies.toArray()[0];
 
  72             if (policy.getClass().isAssignableFrom(ApexPdpPolicy.class)) {
 
  73                 forwardPolicy((ApexPdpPolicy) policy);
 
  75                 final String message = "Ignoring the policy as it is not an apex-pdp policy";
 
  76                 LOGGER.debug(message);
 
  77                 throw new PolicyForwardingException(message);
 
  83      * Method to forward a given policy to apex-pdp.
 
  85      * @param apexPolicy the apex policy
 
  86      * @throws PolicyForwardingException if any exception occurs while forwarding policy
 
  88     private void forwardPolicy(final ApexPdpPolicy apexPolicy) throws PolicyForwardingException {
 
  90             engineServiceFacade.init();
 
  91             engineServiceFacade.deployModel(apexPolicy.getPolicyName(), apexPolicy.getPolicyInputStream(),
 
  92                     apexForwarderParameters.isIgnoreConflicts(), apexForwarderParameters.isForceUpdate());
 
  94             LOGGER.debug("Sucessfully forwarded the policy to apex-pdp egine at "
 
  95                     + apexForwarderParameters.getHostname() + ":" + apexForwarderParameters.getPort());
 
  97         } catch (final ApexException exp) {
 
  98             final String message = "Error sending policy to apex-pdp engine at" + apexForwarderParameters.getHostname()
 
  99                     + ":" + apexForwarderParameters.getPort();
 
 100             LOGGER.error(message, exp);
 
 101             throw new PolicyForwardingException(message, exp);