2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-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.system.internal;
23 import com.fasterxml.jackson.annotation.JsonIgnore;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Properties;
29 import java.util.stream.Collectors;
30 import org.onap.policy.common.endpoints.event.comm.Topic;
31 import org.onap.policy.common.endpoints.event.comm.TopicEndpoint;
32 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
33 import org.onap.policy.common.endpoints.event.comm.TopicListener;
34 import org.onap.policy.common.endpoints.event.comm.TopicSink;
35 import org.onap.policy.common.endpoints.event.comm.TopicSource;
36 import org.onap.policy.common.gson.annotation.GsonJsonIgnore;
37 import org.onap.policy.common.utils.services.FeatureApiUtils;
38 import org.onap.policy.drools.controller.DroolsController;
39 import org.onap.policy.drools.controller.DroolsControllerConstants;
40 import org.onap.policy.drools.controller.DroolsControllerFactory;
41 import org.onap.policy.drools.features.PolicyControllerFeatureApi;
42 import org.onap.policy.drools.features.PolicyControllerFeatureApiConstants;
43 import org.onap.policy.drools.persistence.SystemPersistence;
44 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
45 import org.onap.policy.drools.properties.DroolsPropertyConstants;
46 import org.onap.policy.drools.protocol.configuration.DroolsConfiguration;
47 import org.onap.policy.drools.system.PolicyController;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 * This implementation of the Policy Controller merely aggregates and tracks for management purposes
54 * all underlying resources that this controller depends upon.
56 public class AggregatedPolicyController implements PolicyController, TopicListener {
58 private static final String BEFORE_OFFER_FAILURE = "{}: feature {} before-offer failure because of {}";
59 private static final String AFTER_OFFER_FAILURE = "{}: feature {} after-offer failure because of {}";
64 private static final Logger logger = LoggerFactory.getLogger(AggregatedPolicyController.class);
67 * identifier for this policy controller.
69 private final String name;
72 * Abstracted Event Sources List regardless communication technology.
74 private final List<TopicSource> sources;
77 * Abstracted Event Sinks List regardless communication technology.
79 private final List<TopicSink> sinks;
82 * Mapping topics to sinks.
86 private final HashMap<String, TopicSink> topic2Sinks = new HashMap<>();
89 * Is this Policy Controller running (alive) ? reflects invocation of start()/stop() only.
91 private volatile boolean alive;
94 * Is this Policy Controller locked ? reflects if i/o controller related operations and start
95 * are permitted, more specifically: start(), deliver() and onTopicEvent(). It does not affect
96 * the ability to stop the underlying drools infrastructure
98 private volatile boolean locked;
101 * Policy Drools Controller.
103 private volatile DroolsController droolsController;
106 * Properties used to initialize controller.
108 private final Properties properties;
113 private List<ToscaPolicyTypeIdentifier> policyTypes;
116 * Constructor version mainly used for bootstrapping at initialization time a policy engine
119 * @param name controller name
122 * @throws IllegalArgumentException when invalid arguments are provided
124 public AggregatedPolicyController(String name, Properties properties) {
129 * 1. Register read topics with network infrastructure (ueb, dmaap, rest) 2. Register write
130 * topics with network infrastructure (ueb, dmaap, rest) 3. Register with drools
134 // Create/Reuse Readers/Writers for all event sources endpoints
136 this.sources = getEndpointManager().addTopicSources(properties);
137 this.sinks = getEndpointManager().addTopicSinks(properties);
139 initDrools(properties);
142 /* persist new properties */
143 getPersistenceManager().storeController(name, properties);
144 this.properties = properties;
146 this.policyTypes = getPolicyTypesFromProperties();
150 public List<ToscaPolicyTypeIdentifier> getPolicyTypes() {
151 if (!policyTypes.isEmpty()) {
155 return droolsController
156 .getBaseDomainNames()
158 .map(d -> new ToscaPolicyTypeIdentifier(d,
159 DroolsPropertyConstants.DEFAULT_CONTROLLER_POLICY_TYPE_VERSION))
160 .collect(Collectors.toList());
163 protected List<ToscaPolicyTypeIdentifier> getPolicyTypesFromProperties() {
164 List<ToscaPolicyTypeIdentifier> policyTypeIds = new ArrayList<>();
166 String ptiPropValue = properties.getProperty(DroolsPropertyConstants.PROPERTY_CONTROLLER_POLICY_TYPES);
167 if (ptiPropValue == null) {
168 return policyTypeIds;
171 List<String> ptiPropList = new ArrayList<>(Arrays.asList(ptiPropValue.split("\\s*,\\s*")));
172 for (String pti : ptiPropList) {
173 String[] ptv = pti.split(":");
174 if (ptv.length == 1) {
175 policyTypeIds.add(new ToscaPolicyTypeIdentifier(ptv[0],
176 DroolsPropertyConstants.DEFAULT_CONTROLLER_POLICY_TYPE_VERSION));
177 } else if (ptv.length == 2) {
178 policyTypeIds.add(new ToscaPolicyTypeIdentifier(ptv[0], ptv[1]));
182 return policyTypeIds;
186 * initialize drools layer.
188 * @throws IllegalArgumentException if invalid parameters are passed in
190 private void initDrools(Properties properties) {
192 // Register with drools infrastructure
193 this.droolsController = getDroolsFactory().build(properties, sources, sinks);
194 } catch (Exception | LinkageError e) {
195 logger.error("{}: cannot init-drools because of {}", this, e.getMessage(), e);
196 throw new IllegalArgumentException(e);
203 * @throws IllegalArgumentException if invalid parameters are passed in
205 private void initSinks() {
206 this.topic2Sinks.clear();
207 for (TopicSink sink : sinks) {
208 this.topic2Sinks.put(sink.getTopic(), sink);
216 public boolean updateDrools(DroolsConfiguration newDroolsConfiguration) {
218 DroolsConfiguration oldDroolsConfiguration = new DroolsConfiguration(this.droolsController.getArtifactId(),
219 this.droolsController.getGroupId(), this.droolsController.getVersion());
221 if (oldDroolsConfiguration.getGroupId().equalsIgnoreCase(newDroolsConfiguration.getGroupId())
222 && oldDroolsConfiguration.getArtifactId().equalsIgnoreCase(newDroolsConfiguration.getArtifactId())
223 && oldDroolsConfiguration.getVersion().equalsIgnoreCase(newDroolsConfiguration.getVersion())) {
224 logger.warn("{}: cannot update-drools: identical configuration {} vs {}", this, oldDroolsConfiguration,
225 newDroolsConfiguration);
229 if (droolsController.isBrained()
230 && (newDroolsConfiguration.getArtifactId() == null
231 || DroolsControllerConstants.NO_ARTIFACT_ID.equals(newDroolsConfiguration.getArtifactId()))) {
232 DroolsControllerConstants.getFactory().destroy(this.droolsController);
236 /* Drools Controller created, update initialization properties for restarts */
238 this.properties.setProperty(DroolsPropertyConstants.RULES_GROUPID, newDroolsConfiguration.getGroupId());
239 this.properties.setProperty(DroolsPropertyConstants.RULES_ARTIFACTID,
240 newDroolsConfiguration.getArtifactId());
241 this.properties.setProperty(DroolsPropertyConstants.RULES_VERSION, newDroolsConfiguration.getVersion());
243 getPersistenceManager().storeController(name, this.properties);
245 this.initDrools(this.properties);
247 /* set drools controller to current locked status */
249 if (this.isLocked()) {
250 this.droolsController.lock();
252 this.droolsController.unlock();
255 /* set drools controller to current alive status */
257 if (this.isAlive()) {
258 this.droolsController.start();
260 this.droolsController.stop();
263 } catch (IllegalArgumentException e) {
264 logger.error("{}: cannot update-drools because of {}", this, e.getMessage(), e);
275 public String getName() {
283 public boolean start() {
284 logger.info("{}: start", this);
286 if (FeatureApiUtils.apply(getProviders(),
287 feature -> feature.beforeStart(this),
288 (feature, ex) -> logger.error("{}: feature {} before-start failure because of {}", this,
289 feature.getClass().getName(), ex.getMessage(), ex))) {
293 if (this.isLocked()) {
294 throw new IllegalStateException("Policy Controller " + name + " is locked");
297 synchronized (this) {
305 final boolean success = this.droolsController.start();
307 // register for events
309 for (TopicSource source : sources) {
310 source.register(this);
313 for (TopicSink sink : sinks) {
316 } catch (Exception e) {
317 logger.error("{}: cannot start {} because of {}", this, sink, e.getMessage(), e);
321 FeatureApiUtils.apply(getProviders(),
322 feature -> feature.afterStart(this),
323 (feature, ex) -> logger.error("{}: feature {} after-start failure because of {}", this,
324 feature.getClass().getName(), ex.getMessage(), ex));
333 public boolean stop() {
334 logger.info("{}: stop", this);
336 if (FeatureApiUtils.apply(getProviders(),
337 feature -> feature.beforeStop(this),
338 (feature, ex) -> logger.error("{}: feature {} before-stop failure because of {}", this,
339 feature.getClass().getName(), ex.getMessage(), ex))) {
343 /* stop regardless locked state */
345 synchronized (this) {
353 // 1. Stop registration
355 for (TopicSource source : sources) {
356 source.unregister(this);
359 boolean success = this.droolsController.stop();
361 FeatureApiUtils.apply(getProviders(),
362 feature -> feature.afterStop(this),
363 (feature, ex) -> logger.error("{}: feature {} after-stop failure because of {}", this,
364 feature.getClass().getName(), ex.getMessage(), ex));
373 public void shutdown() {
374 logger.info("{}: shutdown", this);
376 if (FeatureApiUtils.apply(getProviders(),
377 feature -> feature.beforeShutdown(this),
378 (feature, ex) -> logger.error("{}: feature {} before-shutdown failure because of {}", this,
379 feature.getClass().getName(), ex.getMessage(), ex))) {
385 getDroolsFactory().shutdown(this.droolsController);
387 FeatureApiUtils.apply(getProviders(),
388 feature -> feature.afterShutdown(this),
389 (feature, ex) -> logger.error("{}: feature {} after-shutdown failure because of {}", this,
390 feature.getClass().getName(), ex.getMessage(), ex));
398 logger.info("{}: halt", this);
400 if (FeatureApiUtils.apply(getProviders(),
401 feature -> feature.beforeHalt(this),
402 (feature, ex) -> logger.error("{}: feature {} before-halt failure because of {}", this,
403 feature.getClass().getName(), ex.getMessage(), ex))) {
408 getDroolsFactory().destroy(this.droolsController);
409 getPersistenceManager().deleteController(this.name);
411 FeatureApiUtils.apply(getProviders(),
412 feature -> feature.afterHalt(this),
413 (feature, ex) -> logger.error("{}: feature {} after-halt failure because of {}", this,
414 feature.getClass().getName(), ex.getMessage(), ex));
421 public void onTopicEvent(Topic.CommInfrastructure commType, String topic, String event) {
422 logger.debug("{}: raw event offered from {}:{}: {}", this, commType, topic, event);
428 if (FeatureApiUtils.apply(getProviders(),
429 feature -> feature.beforeOffer(this, commType, topic, event),
430 (feature, ex) -> logger.error(BEFORE_OFFER_FAILURE, this,
431 feature.getClass().getName(), ex.getMessage(), ex))) {
435 boolean success = this.droolsController.offer(topic, event);
437 FeatureApiUtils.apply(getProviders(),
438 feature -> feature.afterOffer(this, commType, topic, event, success),
439 (feature, ex) -> logger.error(AFTER_OFFER_FAILURE, this,
440 feature.getClass().getName(), ex.getMessage(), ex));
444 public <T> boolean offer(T event) {
445 logger.debug("{}: event offered: {}", this, event);
451 if (FeatureApiUtils.apply(getProviders(),
452 feature -> feature.beforeOffer(this, event),
453 (feature, ex) -> logger.error(BEFORE_OFFER_FAILURE, this,
454 feature.getClass().getName(), ex.getMessage(), ex))) {
458 boolean success = this.droolsController.offer(event);
460 FeatureApiUtils.apply(getProviders(),
461 feature -> feature.afterOffer(this, event, success),
462 (feature, ex) -> logger.error(AFTER_OFFER_FAILURE, this,
463 feature.getClass().getName(), ex.getMessage(), ex));
468 private boolean skipOffer() {
469 return isLocked() || !isAlive();
476 public boolean deliver(Topic.CommInfrastructure commType, String topic, Object event) {
478 logger.debug("{}: deliver event to {}:{}: {}", this, commType, topic, event);
480 if (FeatureApiUtils.apply(getProviders(),
481 feature -> feature.beforeDeliver(this, commType, topic, event),
482 (feature, ex) -> logger.error("{}: feature {} before-deliver failure because of {}", this,
483 feature.getClass().getName(), ex.getMessage(), ex))) {
487 if (topic == null || topic.isEmpty()) {
488 throw new IllegalArgumentException("Invalid Topic");
492 throw new IllegalArgumentException("Invalid Event");
495 if (!this.isAlive()) {
496 throw new IllegalStateException("Policy Engine is stopped");
499 if (this.isLocked()) {
500 throw new IllegalStateException("Policy Engine is locked");
503 if (!this.topic2Sinks.containsKey(topic)) {
504 logger.warn("{}: cannot deliver event because the sink {}:{} is not registered: {}", this, commType, topic,
506 throw new IllegalArgumentException("Unsupported topic " + topic + " for delivery");
509 boolean success = this.droolsController.deliver(this.topic2Sinks.get(topic), event);
511 FeatureApiUtils.apply(getProviders(),
512 feature -> feature.afterDeliver(this, commType, topic, event, success),
513 (feature, ex) -> logger.error("{}: feature {} after-deliver failure because of {}", this,
514 feature.getClass().getName(), ex.getMessage(), ex));
523 public boolean isAlive() {
531 public boolean lock() {
532 logger.info("{}: lock", this);
534 if (FeatureApiUtils.apply(getProviders(),
535 feature -> feature.beforeLock(this),
536 (feature, ex) -> logger.error("{}: feature {} before-lock failure because of {}", this,
537 feature.getClass().getName(), ex.getMessage(), ex))) {
541 synchronized (this) {
549 // it does not affect associated sources/sinks, they are
550 // autonomous entities
552 boolean success = this.droolsController.lock();
554 FeatureApiUtils.apply(getProviders(),
555 feature -> feature.afterLock(this),
556 (feature, ex) -> logger.error("{}: feature {} after-lock failure because of {}", this,
557 feature.getClass().getName(), ex.getMessage(), ex));
566 public boolean unlock() {
568 logger.info("{}: unlock", this);
570 if (FeatureApiUtils.apply(getProviders(),
571 feature -> feature.beforeUnlock(this),
572 (feature, ex) -> logger.error("{}: feature {} before-unlock failure because of {}", this,
573 feature.getClass().getName(), ex.getMessage(), ex))) {
577 synchronized (this) {
585 boolean success = this.droolsController.unlock();
587 FeatureApiUtils.apply(getProviders(),
588 feature -> feature.afterUnlock(this),
589 (feature, ex) -> logger.error("{}: feature {} after-unlock failure because of {}", this,
590 feature.getClass().getName(), ex.getMessage(), ex));
599 public boolean isLocked() {
607 public List<TopicSource> getTopicSources() {
615 public List<TopicSink> getTopicSinks() {
623 public DroolsController getDrools() {
624 return this.droolsController;
634 public Properties getProperties() {
635 return this.properties;
639 public String toString() {
640 return "AggregatedPolicyController [name=" + name + ", alive=" + alive
641 + ", locked=" + locked + ", droolsController=" + droolsController + "]";
644 // the following methods may be overridden by junit tests
646 protected SystemPersistence getPersistenceManager() {
647 return SystemPersistenceConstants.getManager();
650 protected TopicEndpoint getEndpointManager() {
651 return TopicEndpointManager.getManager();
654 protected DroolsControllerFactory getDroolsFactory() {
655 return DroolsControllerConstants.getFactory();
658 protected List<PolicyControllerFeatureApi> getProviders() {
659 return PolicyControllerFeatureApiConstants.getProviders().getList();