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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.distribution.forwarding.apex.pdp;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Collection;
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;
40 * This class provides an implementation of {@link PolicyForwarder} interface for forwarding the given policies to
43 * @author Ram Krishna Verma (ram.krishna.verma@ericsson.com)
45 public class ApexPdpPolicyForwarder implements PolicyForwarder {
47 private static final Logger LOGGER = LoggerFactory.getLogger(ApexPdpPolicyForwarder.class);
48 private static final String POLICY_TYPE = "APEX";
50 private ApexPdpPolicyForwarderParameterGroup apexForwarderParameters;
51 private EngineServiceFacade engineServiceFacade;
57 public void configure(final String parameterGroupName) {
58 apexForwarderParameters = ParameterService.get(parameterGroupName);
60 new EngineServiceFacade(apexForwarderParameters.getHostname(), apexForwarderParameters.getPort());
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);
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);
84 final String message = "Ignoring the policy as it is not an apex-pdp policy";
85 LOGGER.debug(message);
86 throw new PolicyForwardingException(message);
89 final String message = "Ignoring the policy as it is not of type ToscaPolicy";
90 LOGGER.debug(message);
91 throw new PolicyForwardingException(message);
97 * Method to forward a given policy to apex-pdp.
99 * @param apexPolicy the apex policy
100 * @throws PolicyForwardingException if any exception occurs while forwarding policy
102 private void forwardPolicy(final ToscaPolicy apexPolicy) throws PolicyForwardingException {
104 engineServiceFacade.init();
105 final InputStream policyInputStream = IOUtils.toInputStream(apexPolicy.toString(), "UTF-8");
106 engineServiceFacade.deployModel(apexPolicy.getName(), policyInputStream,
107 apexForwarderParameters.isIgnoreConflicts(), apexForwarderParameters.isForceUpdate());
109 LOGGER.debug("Sucessfully forwarded the policy to apex-pdp egine at {}:{}",
110 apexForwarderParameters.getHostname(), apexForwarderParameters.getPort());
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);