996b887d2c8b9e4504e0de49117fd2d87703128e
[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.models.tosca.authorative.concepts.ToscaEntity;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
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 = LoggerFactory.getLogger(ApexPdpPolicyForwarder.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<ToscaEntity> policies) throws PolicyForwardingException {
69         if (policies.isEmpty()) {
70             final String message = "No apex policy to be forwarded to an apex engine";
71             LOGGER.debug(message);
72             throw new PolicyForwardingException(message);
73         } else if (policies.size() > 1) {
74             final String message = "More than one apex policy cannot be forwarded to an apex engine";
75             LOGGER.debug(message);
76             throw new PolicyForwardingException(message);
77         } else {
78             final ToscaEntity policy = (ToscaEntity) policies.toArray()[0];
79             if (policy instanceof ToscaPolicy) {
80                 final ToscaPolicy toscaPolicy = (ToscaPolicy) policy;
81                 if (POLICY_TYPE.equalsIgnoreCase(toscaPolicy.getType())) {
82                     forwardPolicy(toscaPolicy);
83                 } else {
84                     final String message = "Ignoring the policy as it is not an apex-pdp policy";
85                     LOGGER.debug(message);
86                     throw new PolicyForwardingException(message);
87                 }
88             } else {
89                 final String message = "Ignoring the policy as it is not of type ToscaPolicy";
90                 LOGGER.debug(message);
91                 throw new PolicyForwardingException(message);
92             }
93         }
94     }
95
96     /**
97      * Method to forward a given policy to apex-pdp.
98      *
99      * @param apexPolicy the apex policy
100      * @throws PolicyForwardingException if any exception occurs while forwarding policy
101      */
102     private void forwardPolicy(final ToscaPolicy apexPolicy) throws PolicyForwardingException {
103         try {
104             engineServiceFacade.init();
105             final InputStream policyInputStream = IOUtils.toInputStream(apexPolicy.toString(), "UTF-8");
106             engineServiceFacade.deployModel(apexPolicy.getName(), policyInputStream,
107                     apexForwarderParameters.isIgnoreConflicts(), apexForwarderParameters.isForceUpdate());
108
109             LOGGER.debug("Sucessfully forwarded the policy to apex-pdp egine at {}:{}",
110                     apexForwarderParameters.getHostname(), apexForwarderParameters.getPort());
111
112         } catch (final ApexException | IOException exp) {
113             final String message = "Error sending policy to apex-pdp engine at" + apexForwarderParameters.getHostname()
114                     + ":" + apexForwarderParameters.getPort();
115             LOGGER.error(message, exp);
116             throw new PolicyForwardingException(message, exp);
117         }
118     }
119 }
120