2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Properties;
29 import org.onap.policy.common.endpoints.event.comm.Topic;
30 import org.onap.policy.common.endpoints.event.comm.TopicEndpoint;
31 import org.onap.policy.common.endpoints.event.comm.TopicListener;
32 import org.onap.policy.common.endpoints.event.comm.TopicSink;
33 import org.onap.policy.common.endpoints.event.comm.TopicSource;
34 import org.onap.policy.drools.controller.DroolsController;
35 import org.onap.policy.drools.controller.DroolsControllerFactory;
36 import org.onap.policy.drools.features.PolicyControllerFeatureAPI;
37 import org.onap.policy.drools.persistence.SystemPersistence;
38 import org.onap.policy.drools.properties.DroolsProperties;
39 import org.onap.policy.drools.protocol.configuration.DroolsConfiguration;
40 import org.onap.policy.drools.system.PolicyController;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * This implementation of the Policy Controller merely aggregates and tracks for management purposes
46 * all underlying resources that this controller depends upon.
48 public class AggregatedPolicyController implements PolicyController, TopicListener {
53 private static final Logger logger = LoggerFactory.getLogger(AggregatedPolicyController.class);
56 * identifier for this policy controller.
58 private final String name;
61 * Abstracted Event Sources List regardless communication technology.
63 private final List<? extends TopicSource> sources;
66 * Abstracted Event Sinks List regardless communication technology.
68 private final List<? extends TopicSink> sinks;
71 * Mapping topics to sinks.
74 private final HashMap<String, TopicSink> topic2Sinks = new HashMap<>();
77 * Is this Policy Controller running (alive) ? reflects invocation of start()/stop() only.
79 private volatile boolean alive;
82 * Is this Policy Controller locked ? reflects if i/o controller related operations and start
83 * are permitted, more specifically: start(), deliver() and onTopicEvent(). It does not affect
84 * the ability to stop the underlying drools infrastructure
86 private volatile boolean locked;
89 * Policy Drools Controller.
91 private volatile DroolsController droolsController;
94 * Properties used to initialize controller.
96 private final Properties properties;
99 * Constructor version mainly used for bootstrapping at initialization time a policy engine
102 * @param name controller name
105 * @throws IllegalArgumentException when invalid arguments are provided
107 public AggregatedPolicyController(String name, Properties properties) {
112 * 1. Register read topics with network infrastructure (ueb, dmaap, rest) 2. Register write
113 * topics with network infrastructure (ueb, dmaap, rest) 3. Register with drools
117 // Create/Reuse Readers/Writers for all event sources endpoints
119 this.sources = getEndpointManager().addTopicSources(properties);
120 this.sinks = getEndpointManager().addTopicSinks(properties);
122 initDrools(properties);
125 /* persist new properties */
126 getPersistenceManager().storeController(name, properties);
127 this.properties = properties;
131 * initialize drools layer.
133 * @throws IllegalArgumentException if invalid parameters are passed in
135 private void initDrools(Properties properties) {
137 // Register with drools infrastructure
138 this.droolsController = getDroolsFactory().build(properties, sources, sinks);
139 } catch (Exception | LinkageError e) {
140 logger.error("{}: cannot init-drools because of {}", this, e.getMessage(), e);
141 throw new IllegalArgumentException(e);
148 * @throws IllegalArgumentException if invalid parameters are passed in
150 private void initSinks() {
151 this.topic2Sinks.clear();
152 for (TopicSink sink : sinks) {
153 this.topic2Sinks.put(sink.getTopic(), sink);
161 public boolean updateDrools(DroolsConfiguration newDroolsConfiguration) {
163 DroolsConfiguration oldDroolsConfiguration = new DroolsConfiguration(this.droolsController.getArtifactId(),
164 this.droolsController.getGroupId(), this.droolsController.getVersion());
166 if (oldDroolsConfiguration.getGroupId().equalsIgnoreCase(newDroolsConfiguration.getGroupId())
167 && oldDroolsConfiguration.getArtifactId().equalsIgnoreCase(newDroolsConfiguration.getArtifactId())
168 && oldDroolsConfiguration.getVersion().equalsIgnoreCase(newDroolsConfiguration.getVersion())) {
169 logger.warn("{}: cannot update-drools: identical configuration {} vs {}", this, oldDroolsConfiguration,
170 newDroolsConfiguration);
175 /* Drools Controller created, update initialization properties for restarts */
177 this.properties.setProperty(DroolsProperties.RULES_GROUPID, newDroolsConfiguration.getGroupId());
178 this.properties.setProperty(DroolsProperties.RULES_ARTIFACTID, newDroolsConfiguration.getArtifactId());
179 this.properties.setProperty(DroolsProperties.RULES_VERSION, newDroolsConfiguration.getVersion());
181 getPersistenceManager().storeController(name, this.properties);
183 this.initDrools(this.properties);
185 /* set drools controller to current locked status */
187 if (this.isLocked()) {
188 this.droolsController.lock();
190 this.droolsController.unlock();
193 /* set drools controller to current alive status */
195 if (this.isAlive()) {
196 this.droolsController.start();
198 this.droolsController.stop();
201 } catch (IllegalArgumentException e) {
202 logger.error("{}: cannot update-drools because of {}", this, e.getMessage(), e);
213 public String getName() {
221 public boolean start() {
222 logger.info("{}: start", this);
224 for (PolicyControllerFeatureAPI feature : getProviders()) {
226 if (feature.beforeStart(this)) {
229 } catch (Exception e) {
230 logger.error("{}: feature {} before-start failure because of {}", this, feature.getClass().getName(),
235 if (this.isLocked()) {
236 throw new IllegalStateException("Policy Controller " + name + " is locked");
239 synchronized (this) {
247 final boolean success = this.droolsController.start();
249 // register for events
251 for (TopicSource source : sources) {
252 source.register(this);
255 for (TopicSink sink : sinks) {
258 } catch (Exception e) {
259 logger.error("{}: cannot start {} because of {}", this, sink, e.getMessage(), e);
263 for (PolicyControllerFeatureAPI feature : getProviders()) {
265 if (feature.afterStart(this)) {
268 } catch (Exception e) {
269 logger.error("{}: feature {} after-start failure because of {}", this, feature.getClass().getName(),
281 public boolean stop() {
282 logger.info("{}: stop", this);
284 for (PolicyControllerFeatureAPI feature : getProviders()) {
286 if (feature.beforeStop(this)) {
289 } catch (Exception e) {
290 logger.error("{}: feature {} before-stop failure because of {}", this, feature.getClass().getName(),
295 /* stop regardless locked state */
297 synchronized (this) {
305 // 1. Stop registration
307 for (TopicSource source : sources) {
308 source.unregister(this);
311 boolean success = this.droolsController.stop();
313 for (PolicyControllerFeatureAPI feature : getProviders()) {
315 if (feature.afterStop(this)) {
318 } catch (Exception e) {
319 logger.error("{}: feature {} after-stop failure because of {}", this, feature.getClass().getName(),
331 public void shutdown() {
332 logger.info("{}: shutdown", this);
334 for (PolicyControllerFeatureAPI feature : getProviders()) {
336 if (feature.beforeShutdown(this)) {
339 } catch (Exception e) {
340 logger.error("{}: feature {} before-shutdown failure because of {}", this, feature.getClass().getName(),
347 getDroolsFactory().shutdown(this.droolsController);
349 for (PolicyControllerFeatureAPI feature : getProviders()) {
351 if (feature.afterShutdown(this)) {
354 } catch (Exception e) {
355 logger.error("{}: feature {} after-shutdown failure because of {}", this, feature.getClass().getName(),
366 logger.info("{}: halt", this);
368 for (PolicyControllerFeatureAPI feature : getProviders()) {
370 if (feature.beforeHalt(this)) {
373 } catch (Exception e) {
374 logger.error("{}: feature {} before-halt failure because of {}", this, feature.getClass().getName(),
380 getDroolsFactory().destroy(this.droolsController);
381 getPersistenceManager().deleteController(this.name);
383 for (PolicyControllerFeatureAPI feature : getProviders()) {
385 if (feature.afterHalt(this)) {
388 } catch (Exception e) {
389 logger.error("{}: feature {} after-halt failure because of {}", this, feature.getClass().getName(),
399 public void onTopicEvent(Topic.CommInfrastructure commType, String topic, String event) {
401 logger.debug("{}: event offered from {}:{}: {}", this, commType, topic, event);
403 for (PolicyControllerFeatureAPI feature : getProviders()) {
405 if (feature.beforeOffer(this, commType, topic, event)) {
408 } catch (Exception e) {
409 logger.error("{}: feature {} before-offer failure because of {}", this, feature.getClass().getName(),
422 boolean success = this.droolsController.offer(topic, event);
424 for (PolicyControllerFeatureAPI feature : getProviders()) {
426 if (feature.afterOffer(this, commType, topic, event, success)) {
429 } catch (Exception e) {
430 logger.error("{}: feature {} after-offer failure because of {}", this, feature.getClass().getName(),
440 public boolean deliver(Topic.CommInfrastructure commType, String topic, Object event) {
442 logger.debug("{}: deliver event to {}:{}: {}", this, commType, topic, event);
444 for (PolicyControllerFeatureAPI feature : getProviders()) {
446 if (feature.beforeDeliver(this, commType, topic, event)) {
449 } catch (Exception e) {
450 logger.error("{}: feature {} before-deliver failure because of {}", this, feature.getClass().getName(),
455 if (topic == null || topic.isEmpty()) {
456 throw new IllegalArgumentException("Invalid Topic");
460 throw new IllegalArgumentException("Invalid Event");
463 if (!this.isAlive()) {
464 throw new IllegalStateException("Policy Engine is stopped");
467 if (this.isLocked()) {
468 throw new IllegalStateException("Policy Engine is locked");
471 if (!this.topic2Sinks.containsKey(topic)) {
472 logger.warn("{}: cannot deliver event because the sink {}:{} is not registered: {}", this, commType, topic,
474 throw new IllegalArgumentException("Unsupported topic " + topic + " for delivery");
477 boolean success = this.droolsController.deliver(this.topic2Sinks.get(topic), event);
479 for (PolicyControllerFeatureAPI feature : getProviders()) {
481 if (feature.afterDeliver(this, commType, topic, event, success)) {
484 } catch (Exception e) {
485 logger.error("{}: feature {} after-deliver failure because of {}", this, feature.getClass().getName(),
497 public boolean isAlive() {
505 public boolean lock() {
506 logger.info("{}: lock", this);
508 for (PolicyControllerFeatureAPI feature : getProviders()) {
510 if (feature.beforeLock(this)) {
513 } catch (Exception e) {
514 logger.error("{}: feature {} before-lock failure because of {}", this, feature.getClass().getName(),
519 synchronized (this) {
527 // it does not affect associated sources/sinks, they are
528 // autonomous entities
530 boolean success = this.droolsController.lock();
532 for (PolicyControllerFeatureAPI feature : getProviders()) {
534 if (feature.afterLock(this)) {
537 } catch (Exception e) {
538 logger.error("{}: feature {} after-lock failure because of {}", this, feature.getClass().getName(),
550 public boolean unlock() {
552 logger.info("{}: unlock", this);
554 for (PolicyControllerFeatureAPI feature : getProviders()) {
556 if (feature.beforeUnlock(this)) {
559 } catch (Exception e) {
560 logger.error("{}: feature {} before-unlock failure because of {}", this, feature.getClass().getName(),
565 synchronized (this) {
573 boolean success = this.droolsController.unlock();
575 for (PolicyControllerFeatureAPI feature : getProviders()) {
577 if (feature.afterUnlock(this)) {
580 } catch (Exception e) {
581 logger.error("{}: feature {} after-unlock failure because of {}", this, feature.getClass().getName(),
593 public boolean isLocked() {
601 public List<? extends TopicSource> getTopicSources() {
609 public List<? extends TopicSink> getTopicSinks() {
617 public DroolsController getDrools() {
618 return this.droolsController;
627 public Properties getProperties() {
628 return this.properties;
632 public String toString() {
633 return "AggregatedPolicyController [name=" + name + ", alive=" + alive
634 + ", locked=" + locked + ", droolsController=" + droolsController + "]";
637 // the following methods may be overridden by junit tests
639 protected SystemPersistence getPersistenceManager() {
640 return SystemPersistence.manager;
643 protected TopicEndpoint getEndpointManager() {
644 return TopicEndpoint.manager;
647 protected DroolsControllerFactory getDroolsFactory() {
648 return DroolsController.factory;
651 protected List<PolicyControllerFeatureAPI> getProviders() {
652 return PolicyControllerFeatureAPI.providers.getList();