2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.drools.lifecycle;
23 import java.util.Collections;
24 import java.util.List;
26 import java.util.Properties;
27 import java.util.stream.Collectors;
29 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
30 import org.onap.policy.common.endpoints.event.comm.TopicSink;
31 import org.onap.policy.common.endpoints.event.comm.TopicSource;
32 import org.onap.policy.common.gson.annotation.GsonJsonIgnore;
33 import org.onap.policy.common.utils.coder.CoderException;
34 import org.onap.policy.drools.domain.models.controller.ControllerCustomSerialization;
35 import org.onap.policy.drools.domain.models.controller.ControllerEvent;
36 import org.onap.policy.drools.domain.models.controller.ControllerPolicy;
37 import org.onap.policy.drools.domain.models.controller.ControllerProperties;
38 import org.onap.policy.drools.domain.models.controller.ControllerSinkTopic;
39 import org.onap.policy.drools.domain.models.controller.ControllerSourceTopic;
40 import org.onap.policy.drools.properties.DroolsPropertyConstants;
41 import org.onap.policy.drools.system.PolicyController;
42 import org.onap.policy.drools.system.PolicyControllerConstants;
43 import org.onap.policy.drools.system.PolicyEngineConstants;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 public class PolicyTypeNativeDroolsController implements PolicyTypeController {
50 private static final Logger logger = LoggerFactory.getLogger(PolicyTypeNativeDroolsController.class);
53 protected final ToscaPolicyTypeIdentifier policyType;
56 protected final LifecycleFsm fsm;
58 public PolicyTypeNativeDroolsController(LifecycleFsm fsm, ToscaPolicyTypeIdentifier policyType) {
59 this.policyType = policyType;
64 public boolean deploy(ToscaPolicy policy) {
65 Properties controllerProps = new Properties();
66 ControllerPolicy controllerPolicy = toDomainPolicy(policy);
67 if (controllerPolicy == null) {
71 ControllerProperties controllerConfig = controllerPolicy.getProperties();
73 configControllerName(controllerConfig, controllerProps);
76 configControllerSources(controllerConfig, controllerProps)
77 && configControllerSinks(controllerConfig, controllerProps)
78 && configControllerCustom(controllerConfig, controllerProps);
84 PolicyController controller;
87 PolicyEngineConstants.getManager()
88 .createPolicyController(controllerConfig.getControllerName(), controllerProps);
89 } catch (RuntimeException e) {
90 logger.warn("failed deploy (cannot create controller) for policy: {}", policy, e);
96 } catch (RuntimeException e) {
97 logger.warn("failed deploy (cannot start controller) for policy: {}", policy, e);
98 PolicyEngineConstants.getManager().removePolicyController(controller);
106 public boolean undeploy(ToscaPolicy policy) {
108 ControllerPolicy nativePolicy = fsm.getDomainMaker().convertTo(policy, ControllerPolicy.class);
109 PolicyEngineConstants.getManager()
110 .removePolicyController(nativePolicy.getProperties().getControllerName());
112 } catch (RuntimeException | CoderException e) {
113 logger.warn("failed undeploy of policy: {}", policy);
118 private ControllerPolicy toDomainPolicy(ToscaPolicy policy) {
119 ControllerPolicy nativePolicy = null;
121 nativePolicy = fsm.getDomainMaker().convertTo(policy, ControllerPolicy.class);
122 ControllerProperties config = nativePolicy.getProperties();
124 /* check for duplicates */
126 if (isDups(sourceTopics(config.getSourceTopics()))
127 || isDups(sinkTopics(config.getSinkTopics()))) {
128 logger.warn("there are duplicated topics in policy {}", policy);
132 /* check for non-existance of the controller - throws IAE if there's not */
134 PolicyControllerConstants.getFactory().get(nativePolicy.getProperties().getControllerName());
136 } catch (CoderException e) {
137 logger.warn("failed deploy of policy (invalid): {}", policy);
139 } catch (IllegalArgumentException e) {
141 logger.trace("proceeding with the deploy of native controller policy: {}", policy);
147 private void configControllerName(ControllerProperties controllerConfig, Properties controllerProps) {
149 .setProperty(DroolsPropertyConstants.PROPERTY_CONTROLLER_NAME, controllerConfig.getControllerName());
152 private boolean configControllerSources(ControllerProperties controllerConfig, Properties controllerProps) {
153 if (controllerConfig.getSourceTopics() == null) {
157 for (ControllerSourceTopic configSourceTopic : controllerConfig.getSourceTopics()) {
158 List<TopicSource> sources =
159 TopicEndpointManager.getManager().getTopicSources(List.of(configSourceTopic.getTopicName()));
160 if (sources.size() != 1) {
161 logger.warn("Topic {} is not present or ambigous {}", configSourceTopic.getTopicName(), sources);
165 configSourceTopic(sources.get(0), configSourceTopic, controllerProps);
170 private void configSourceTopic(TopicSource topic, ControllerSourceTopic configTopic, Properties controllerProps) {
171 String configCommPrefix = topic.getTopicCommInfrastructure().name().toLowerCase() + ".source";
172 configTopic(configCommPrefix, topic.getTopic(), configTopic.getEvents(), controllerProps);
175 private boolean configControllerSinks(ControllerProperties controllerConfig, Properties controllerProps) {
176 if (controllerConfig.getSinkTopics() == null) {
180 for (ControllerSinkTopic configSinkTopic : controllerConfig.getSinkTopics()) {
181 List<TopicSink> sinks =
182 TopicEndpointManager.getManager().getTopicSinks(List.of(configSinkTopic.getTopicName()));
183 if (sinks.size() != 1) {
184 logger.warn("Topic {} is not present or ambigous {}", configSinkTopic.getTopicName(), sinks);
188 configSinkTopic(sinks.get(0), configSinkTopic, controllerProps);
193 private void configSinkTopic(TopicSink topic, ControllerSinkTopic configTopic, Properties controllerProps) {
194 String configCommPrefix = topic.getTopicCommInfrastructure().name().toLowerCase() + ".sink";
195 configTopic(configCommPrefix, topic.getTopic(), configTopic.getEvents(), controllerProps);
198 private void configTopic(
199 String configCommPrefix, String topicName, List<ControllerEvent> events, Properties controllerProps) {
200 String configTopicPrefix = configCommPrefix + ".topics." + topicName;
201 configTopics(configCommPrefix, topicName, controllerProps);
202 for (ControllerEvent configEvent : events) {
203 configEvent(configTopicPrefix, configEvent, controllerProps);
207 private void configTopics(String propPrefix, String topicName, Properties controllerProps) {
208 String topicsPropKey = propPrefix + ".topics";
209 configTopicItemList(topicsPropKey, topicName, controllerProps);
212 private void configEvent(String propPrefix, ControllerEvent configEvent, Properties controllerProps) {
213 String eventPropPrefix = propPrefix + ".events";
214 if (configEvent.getEventFilter() != null) {
215 controllerProps.setProperty(
216 eventPropPrefix + "." + configEvent.getEventClass() + ".filter", configEvent.getEventFilter());
218 if (configEvent.getCustomSerialization() != null) {
219 configSerialization(eventPropPrefix, configEvent.getCustomSerialization(), controllerProps);
221 configTopicItemList(eventPropPrefix, configEvent.getEventClass(), controllerProps);
224 private void configTopicItemList(String itemPrefix, String item, Properties controllerProps) {
225 String itemValue = controllerProps.getProperty(itemPrefix);
226 if (itemValue == null) {
227 controllerProps.setProperty(itemPrefix, item);
229 controllerProps.setProperty(itemPrefix, itemValue + "," + item);
233 private void configSerialization(
234 String propPrefix, ControllerCustomSerialization configCustom, Properties controllerProps) {
235 String customPropPrefix = propPrefix + ".custom.gson";
236 controllerProps.setProperty(
237 customPropPrefix, configCustom.getCustomSerializerClass() + "," + configCustom.getJsonParser());
240 private boolean configControllerCustom(ControllerProperties controllerConfig, Properties controllerProps) {
241 Map<String, String> configCustom = controllerConfig.getCustomConfig();
242 if (configCustom != null && !configCustom.isEmpty()) {
243 controllerProps.putAll(configCustom);
249 private <T> boolean isDups(List<T> items) {
250 return items.size() != items.stream().distinct().count();
253 private List<String> sourceTopics(List<ControllerSourceTopic> sourceTopics) {
254 if (sourceTopics == null) {
255 return Collections.emptyList();
258 return sourceTopics.stream()
259 .map(ControllerSourceTopic::getTopicName)
260 .collect(Collectors.toList());
263 private List<String> sinkTopics(List<ControllerSinkTopic> sinkTopics) {
264 if (sinkTopics == null) {
265 return Collections.emptyList();
268 return sinkTopics.stream()
269 .map(ControllerSinkTopic::getTopicName)
270 .collect(Collectors.toList());