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 com.fasterxml.jackson.annotation.JsonIgnore;
24 import java.util.Collections;
25 import java.util.List;
27 import java.util.Properties;
28 import java.util.stream.Collectors;
30 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
31 import org.onap.policy.common.endpoints.event.comm.TopicSink;
32 import org.onap.policy.common.endpoints.event.comm.TopicSource;
33 import org.onap.policy.common.gson.annotation.GsonJsonIgnore;
34 import org.onap.policy.common.utils.coder.CoderException;
35 import org.onap.policy.drools.domain.models.controller.ControllerCustomSerialization;
36 import org.onap.policy.drools.domain.models.controller.ControllerEvent;
37 import org.onap.policy.drools.domain.models.controller.ControllerPolicy;
38 import org.onap.policy.drools.domain.models.controller.ControllerProperties;
39 import org.onap.policy.drools.domain.models.controller.ControllerSinkTopic;
40 import org.onap.policy.drools.domain.models.controller.ControllerSourceTopic;
41 import org.onap.policy.drools.properties.DroolsPropertyConstants;
42 import org.onap.policy.drools.system.PolicyController;
43 import org.onap.policy.drools.system.PolicyControllerConstants;
44 import org.onap.policy.drools.system.PolicyEngineConstants;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public class PolicyTypeNativeDroolsController implements PolicyTypeController {
51 private static final Logger logger = LoggerFactory.getLogger(PolicyTypeNativeDroolsController.class);
54 protected final ToscaPolicyTypeIdentifier policyType;
58 protected final transient LifecycleFsm fsm;
60 public PolicyTypeNativeDroolsController(LifecycleFsm fsm, ToscaPolicyTypeIdentifier policyType) {
61 this.policyType = policyType;
66 public boolean deploy(ToscaPolicy policy) {
67 Properties controllerProps = new Properties();
68 ControllerPolicy controllerPolicy = toDomainPolicy(policy);
69 if (controllerPolicy == null) {
73 ControllerProperties controllerConfig = controllerPolicy.getProperties();
76 configControllerName(controllerConfig, controllerProps)
77 && configControllerSources(controllerConfig, controllerProps)
78 && configControllerSinks(controllerConfig, controllerProps)
79 && configControllerCustom(controllerConfig, controllerProps);
85 PolicyController controller;
88 PolicyEngineConstants.getManager()
89 .createPolicyController(controllerConfig.getControllerName(), controllerProps);
90 } catch (RuntimeException e) {
91 logger.warn("failed deploy (cannot create controller) for policy: {}", policy, e);
97 } catch (RuntimeException e) {
98 logger.warn("failed deploy (cannot start ontroller) for policy: {}", policy, e);
99 PolicyEngineConstants.getManager().removePolicyController(controller);
107 public boolean undeploy(ToscaPolicy policy) {
109 ControllerPolicy nativePolicy = fsm.getDomainMaker().convertTo(policy, ControllerPolicy.class);
110 PolicyEngineConstants.getManager()
111 .removePolicyController(nativePolicy.getProperties().getControllerName());
113 } catch (RuntimeException | CoderException e) {
114 logger.warn("failed undeploy of policy: {}", policy);
119 private ControllerPolicy toDomainPolicy(ToscaPolicy policy) {
120 ControllerPolicy nativePolicy = null;
122 nativePolicy = fsm.getDomainMaker().convertTo(policy, ControllerPolicy.class);
123 ControllerProperties config = nativePolicy.getProperties();
125 /* check for duplicates */
127 if (isDups(sourceTopics(config.getSourceTopics()))
128 || isDups(sinkTopics(config.getSinkTopics()))) {
129 logger.warn("there are duplicated topics in policy {}", policy);
133 /* check for non-existance of the controller - throws IAE if there's not */
135 PolicyControllerConstants.getFactory().get(nativePolicy.getProperties().getControllerName());
137 } catch (CoderException e) {
138 logger.warn("failed deploy of policy (invalid): {}", policy);
140 } catch (IllegalArgumentException e) {
142 logger.trace("proceeding with the deploy of native controller policy: {}", policy);
148 private boolean configControllerName(ControllerProperties controllerConfig, Properties controllerProps) {
150 .setProperty(DroolsPropertyConstants.PROPERTY_CONTROLLER_NAME, controllerConfig.getControllerName());
154 private boolean configControllerSources(ControllerProperties controllerConfig, Properties controllerProps) {
155 if (controllerConfig.getSourceTopics() == null) {
159 for (ControllerSourceTopic configSourceTopic : controllerConfig.getSourceTopics()) {
160 List<TopicSource> sources =
161 TopicEndpointManager.getManager().getTopicSources(List.of(configSourceTopic.getTopicName()));
162 if (sources.size() != 1) {
163 logger.warn("Topic {} is not present or ambigous {}", configSourceTopic.getTopicName(), sources);
167 configSourceTopic(sources.get(0), configSourceTopic, controllerProps);
172 private void configSourceTopic(TopicSource topic, ControllerSourceTopic configTopic, Properties controllerProps) {
173 String configCommPrefix = topic.getTopicCommInfrastructure().name().toLowerCase() + ".source";
174 configTopic(configCommPrefix, topic.getTopic(), configTopic.getEvents(), controllerProps);
177 private boolean configControllerSinks(ControllerProperties controllerConfig, Properties controllerProps) {
178 if (controllerConfig.getSinkTopics() == null) {
182 for (ControllerSinkTopic configSinkTopic : controllerConfig.getSinkTopics()) {
183 List<TopicSink> sinks =
184 TopicEndpointManager.getManager().getTopicSinks(List.of(configSinkTopic.getTopicName()));
185 if (sinks.size() != 1) {
186 logger.warn("Topic {} is not present or ambigous {}", configSinkTopic.getTopicName(), sinks);
190 configSinkTopic(sinks.get(0), configSinkTopic, controllerProps);
195 private void configSinkTopic(TopicSink topic, ControllerSinkTopic configTopic, Properties controllerProps) {
196 String configCommPrefix = topic.getTopicCommInfrastructure().name().toLowerCase() + ".sink";
197 configTopic(configCommPrefix, topic.getTopic(), configTopic.getEvents(), controllerProps);
200 private void configTopic(
201 String configCommPrefix, String topicName, List<ControllerEvent> events, Properties controllerProps) {
202 String configTopicPrefix = configCommPrefix + "." + topicName;
203 configTopics(configCommPrefix, topicName, controllerProps);
204 for (ControllerEvent configEvent : events) {
205 configEvent(configTopicPrefix, configEvent, controllerProps);
209 private void configTopics(String propPrefix, String topicName, Properties controllerProps) {
210 String topicsPropKey = propPrefix + ".topics";
211 configTopicItemList(topicsPropKey, topicName, controllerProps);
214 private void configEvent(String propPrefix, ControllerEvent configEvent, Properties controllerProps) {
215 String eventPropPrefix = propPrefix + ".events";
216 controllerProps.setProperty(eventPropPrefix, configEvent.getEventClass());
217 if (configEvent.getEventFilter() != null) {
218 controllerProps.setProperty(
219 eventPropPrefix + "." + configEvent.getEventClass() + ".filter", configEvent.getEventFilter());
221 if (configEvent.getCustomSerialization() != null) {
222 configSerialization(eventPropPrefix, configEvent.getCustomSerialization(), controllerProps);
224 configTopicItemList(eventPropPrefix, configEvent.getEventClass(), controllerProps);
227 private void configTopicItemList(String itemPrefix, String item, Properties controllerProps) {
228 if (controllerProps.getProperty(itemPrefix) == null) {
229 controllerProps.setProperty(itemPrefix, item);
231 controllerProps.setProperty(itemPrefix, "," + item);
235 private void configSerialization(
236 String propPrefix, ControllerCustomSerialization configCustom, Properties controllerProps) {
237 String customPropPrefix = propPrefix + ".custom.gson";
238 controllerProps.setProperty(
239 customPropPrefix, configCustom.getCustomSerializerClass() + "," + configCustom.getJsonParser());
242 private boolean configControllerCustom(ControllerProperties controllerConfig, Properties controllerProps) {
243 Map<String, String> configCustom = controllerConfig.getCustomConfig();
244 if (configCustom == null || configCustom.isEmpty()) {
248 controllerProps.putAll(configCustom);
252 private <T> boolean isDups(List<T> items) {
253 return items.size() != items.stream().distinct().count();
256 private List<String> sourceTopics(List<ControllerSourceTopic> sourceTopics) {
257 if (sourceTopics == null) {
258 return Collections.EMPTY_LIST;
261 return sourceTopics.stream()
262 .map(ControllerSourceTopic::getTopicName)
263 .collect(Collectors.toList());
266 private List<String> sinkTopics(List<ControllerSinkTopic> sinkTopics) {
267 if (sinkTopics == null) {
268 return Collections.EMPTY_LIST;
271 return sinkTopics.stream()
272 .map(ControllerSinkTopic::getTopicName)
273 .collect(Collectors.toList());