f71c6aeef0a169a15200167defd8a74761b1fbf1
[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.io.IOException;
24 import java.io.InputStream;
25 import java.util.Collection;
26
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;
38
39 /**
40  * This class provides an implementation of {@link PolicyForwarder} interface for forwarding the given policies to
41  * apex-pdp.
42  *
43  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
44  */
45 public class ApexPdpPolicyForwarder implements PolicyForwarder {
46
47     private static final Logger LOGGER = FlexLogger.getLogger(XacmlPdpPolicyForwarder.class);
48     private static final String POLICY_TYPE = "APEX";
49
50     private ApexPdpPolicyForwarderParameterGroup apexForwarderParameters;
51     private EngineServiceFacade engineServiceFacade;
52
53     /**
54      * {@inheritDoc}.
55      */
56     @Override
57     public void configure(final String parameterGroupName) {
58         apexForwarderParameters = ParameterService.get(parameterGroupName);
59         engineServiceFacade =
60                 new EngineServiceFacade(apexForwarderParameters.getHostname(), apexForwarderParameters.getPort());
61
62     }
63
64     /**
65      * {@inheritDoc}.
66      */
67     @Override
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);
73
74         } else {
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);
79             } else {
80                 final String message = "Ignoring the policy as it is not an apex-pdp policy";
81                 LOGGER.debug(message);
82                 throw new PolicyForwardingException(message);
83             }
84         }
85     }
86
87     /**
88      * Method to forward a given policy to apex-pdp.
89      *
90      * @param apexPolicy the apex policy
91      * @throws PolicyForwardingException if any exception occurs while forwarding policy
92      */
93     private void forwardPolicy(final PolicyAsString apexPolicy) throws PolicyForwardingException {
94         try {
95             engineServiceFacade.init();
96             final InputStream policyInputStream = IOUtils.toInputStream(apexPolicy.getPolicy(), "UTF-8");
97             engineServiceFacade.deployModel(apexPolicy.getPolicyName(), policyInputStream,
98                     apexForwarderParameters.isIgnoreConflicts(), apexForwarderParameters.isForceUpdate());
99
100             LOGGER.debug("Sucessfully forwarded the policy to apex-pdp egine at "
101                     + apexForwarderParameters.getHostname() + ":" + apexForwarderParameters.getPort());
102
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);
108         }
109     }
110 }
111