1a603f04ef9573549ca6bef3c72874eba57608a9
[policy/distribution.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distribution.forwarding.apex.pdp;
22
23 import java.util.Collection;
24
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;
35
36 /**
37  * This class provides an implementation of {@link PolicyForwarder} interface for forwarding the given policies to
38  * apex-pdp.
39  *
40  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
41  */
42 public class ApexPdpPolicyForwarder implements PolicyForwarder {
43
44     private static final Logger LOGGER = FlexLogger.getLogger(XacmlPdpPolicyForwarder.class);
45
46     private ApexPdpPolicyForwarderParameterGroup apexForwarderParameters;
47     private EngineServiceFacade engineServiceFacade;
48
49     /**
50      * {@inheritDoc}.
51      */
52     @Override
53     public void configure(final String parameterGroupName) {
54         apexForwarderParameters = ParameterService.get(parameterGroupName);
55         engineServiceFacade =
56                 new EngineServiceFacade(apexForwarderParameters.getHostname(), apexForwarderParameters.getPort());
57
58     }
59
60     /**
61      * {@inheritDoc}.
62      */
63     @Override
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);
69
70         } else {
71             final Policy policy = (Policy) policies.toArray()[0];
72             if (policy.getClass().isAssignableFrom(ApexPdpPolicy.class)) {
73                 forwardPolicy((ApexPdpPolicy) policy);
74             } else {
75                 final String message = "Ignoring the policy as it is not an apex-pdp policy";
76                 LOGGER.debug(message);
77                 throw new PolicyForwardingException(message);
78             }
79         }
80     }
81
82     /**
83      * Method to forward a given policy to apex-pdp.
84      *
85      * @param apexPolicy the apex policy
86      * @throws PolicyForwardingException if any exception occurs while forwarding policy
87      */
88     private void forwardPolicy(final ApexPdpPolicy apexPolicy) throws PolicyForwardingException {
89         try {
90             engineServiceFacade.init();
91             engineServiceFacade.deployModel(apexPolicy.getPolicyName(), apexPolicy.getPolicyInputStream(),
92                     apexForwarderParameters.isIgnoreConflicts(), apexForwarderParameters.isForceUpdate());
93
94             LOGGER.debug("Sucessfully forwarded the policy to apex-pdp egine at "
95                     + apexForwarderParameters.getHostname() + ":" + apexForwarderParameters.getPort());
96
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);
102         }
103     }
104 }
105