8b8528fb6ef6a750e91fc2a81a43d2c85c0c627a
[policy/distribution.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.distribution.forwarding.apex.pdp;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Collection;
27
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;
37
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * This class provides an implementation of {@link PolicyForwarder} interface for forwarding the given policies to
43  * apex-pdp.
44  *
45  * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
46  */
47 public class ApexPdpPolicyForwarder implements PolicyForwarder {
48
49     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpPolicyForwarder.class);
50     private static final String POLICY_TYPE = "APEX";
51
52     private ApexPdpPolicyForwarderParameterGroup apexForwarderParameters;
53     private EngineServiceFacade engineServiceFacade;
54
55     /**
56      * {@inheritDoc}.
57      */
58     @Override
59     public void configure(final String parameterGroupName) {
60         apexForwarderParameters = ParameterService.get(parameterGroupName);
61         engineServiceFacade =
62                 new EngineServiceFacade(apexForwarderParameters.getHostname(), apexForwarderParameters.getPort());
63
64     }
65
66     /**
67      * {@inheritDoc}.
68      */
69     @Override
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);
79         } else {
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);
84             } else {
85                 final String message = "Ignoring the policy as it is not an apex-pdp policy";
86                 LOGGER.debug(message);
87                 throw new PolicyForwardingException(message);
88             }
89         }
90     }
91
92     /**
93      * Method to forward a given policy to apex-pdp.
94      *
95      * @param apexPolicy the apex policy
96      * @throws PolicyForwardingException if any exception occurs while forwarding policy
97      */
98     private void forwardPolicy(final PolicyAsString apexPolicy) throws PolicyForwardingException {
99         try {
100             engineServiceFacade.init();
101             final InputStream policyInputStream = IOUtils.toInputStream(apexPolicy.getPolicy(), "UTF-8");
102             engineServiceFacade.deployModel(apexPolicy.getPolicyName(), policyInputStream,
103                     apexForwarderParameters.isIgnoreConflicts(), apexForwarderParameters.isForceUpdate());
104
105             LOGGER.debug("Sucessfully forwarded the policy to apex-pdp egine at {}:{}",
106                     apexForwarderParameters.getHostname(), apexForwarderParameters.getPort());
107
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);
113         }
114     }
115 }
116