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.controller.internal;
23 import com.fasterxml.jackson.annotation.JsonIgnore;
24 import com.fasterxml.jackson.annotation.JsonProperty;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.List;
30 import org.apache.commons.collections4.queue.CircularFifoQueue;
31 import org.drools.core.ClassObjectFilter;
32 import org.kie.api.definition.KiePackage;
33 import org.kie.api.definition.rule.Query;
34 import org.kie.api.runtime.KieSession;
35 import org.kie.api.runtime.rule.FactHandle;
36 import org.kie.api.runtime.rule.QueryResults;
37 import org.kie.api.runtime.rule.QueryResultsRow;
38 import org.onap.policy.common.endpoints.event.comm.TopicSink;
39 import org.onap.policy.drools.controller.DroolsController;
40 import org.onap.policy.drools.core.PolicyContainer;
41 import org.onap.policy.drools.core.PolicySession;
42 import org.onap.policy.drools.core.jmx.PdpJmx;
43 import org.onap.policy.drools.features.DroolsControllerFeatureAPI;
44 import org.onap.policy.drools.protocol.coders.EventProtocolCoder;
45 import org.onap.policy.drools.protocol.coders.JsonProtocolFilter;
46 import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration;
47 import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration.CustomGsonCoder;
48 import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration.CustomJacksonCoder;
49 import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration.PotentialCoderFilter;
50 import org.onap.policy.drools.utils.ReflectionUtil;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
55 * Maven-based Drools Controller that interacts with the
56 * policy-core PolicyContainer and PolicySession to manage
57 * Drools containers instantiated using Maven.
59 public class MavenDroolsController implements DroolsController {
64 private static Logger logger = LoggerFactory.getLogger(MavenDroolsController.class);
67 * Policy Container, the access object to the policy-core layer.
70 protected final PolicyContainer policyContainer;
73 * alive status of this drools controller,
74 * reflects invocation of start()/stop() only.
76 protected volatile boolean alive = false;
79 * locked status of this drools controller,
80 * reflects if i/o drools related operations are permitted,
81 * more specifically: offer() and deliver().
82 * It does not affect the ability to start and stop
83 * underlying drools infrastructure
85 protected volatile boolean locked = false;
88 * list of topics, each with associated decoder classes, each
89 * with a list of associated filters.
91 protected List<TopicCoderFilterConfiguration> decoderConfigurations;
94 * list of topics, each with associated encoder classes, each
95 * with a list of associated filters.
97 protected List<TopicCoderFilterConfiguration> encoderConfigurations;
100 * recent source events processed.
102 protected final CircularFifoQueue<Object> recentSourceEvents = new CircularFifoQueue<>(10);
105 * recent sink events processed.
107 protected final CircularFifoQueue<String> recentSinkEvents = new CircularFifoQueue<>(10);
110 * original Drools Model/Rules classloader hash.
112 protected int modelClassLoaderHash;
115 * Expanded version of the constructor.
117 * @param groupId maven group id
118 * @param artifactId maven artifact id
119 * @param version maven version
120 * @param decoderConfigurations list of topic -> decoders -> filters mapping
121 * @param encoderConfigurations list of topic -> encoders -> filters mapping
123 * @throws IllegalArgumentException invalid arguments passed in
125 public MavenDroolsController(String groupId,
128 List<TopicCoderFilterConfiguration> decoderConfigurations,
129 List<TopicCoderFilterConfiguration> encoderConfigurations) {
131 logger.info("drools-controller instantiation [{}:{}:{}]", groupId, artifactId, version);
133 if (groupId == null || groupId.isEmpty()) {
134 throw new IllegalArgumentException("Missing maven group-id coordinate");
137 if (artifactId == null || artifactId.isEmpty()) {
138 throw new IllegalArgumentException("Missing maven artifact-id coordinate");
141 if (version == null || version.isEmpty()) {
142 throw new IllegalArgumentException("Missing maven version coordinate");
145 this.policyContainer = new PolicyContainer(groupId, artifactId, version);
146 this.init(decoderConfigurations, encoderConfigurations);
148 logger.debug("{}: instantiation completed ", this);
152 * init encoding/decoding configuration.
154 * @param decoderConfigurations list of topic -> decoders -> filters mapping
155 * @param encoderConfigurations list of topic -> encoders -> filters mapping
157 protected void init(List<TopicCoderFilterConfiguration> decoderConfigurations,
158 List<TopicCoderFilterConfiguration> encoderConfigurations) {
160 this.decoderConfigurations = decoderConfigurations;
161 this.encoderConfigurations = encoderConfigurations;
163 this.initCoders(decoderConfigurations, true);
164 this.initCoders(encoderConfigurations, false);
166 this.modelClassLoaderHash = this.policyContainer.getClassLoader().hashCode();
170 public void updateToVersion(String newGroupId, String newArtifactId, String newVersion,
171 List<TopicCoderFilterConfiguration> decoderConfigurations,
172 List<TopicCoderFilterConfiguration> encoderConfigurations)
173 throws LinkageError {
175 logger.info("updating version -> [{}:{}:{}]", newGroupId, newArtifactId, newVersion);
177 if (newGroupId == null || newGroupId.isEmpty()) {
178 throw new IllegalArgumentException("Missing maven group-id coordinate");
181 if (newArtifactId == null || newArtifactId.isEmpty()) {
182 throw new IllegalArgumentException("Missing maven artifact-id coordinate");
185 if (newVersion == null || newVersion.isEmpty()) {
186 throw new IllegalArgumentException("Missing maven version coordinate");
189 if (newGroupId.equalsIgnoreCase(DroolsController.NO_GROUP_ID)
190 || newArtifactId.equalsIgnoreCase(DroolsController.NO_ARTIFACT_ID)
191 || newVersion.equalsIgnoreCase(DroolsController.NO_VERSION)) {
192 throw new IllegalArgumentException("BRAINLESS maven coordinates provided: "
193 + newGroupId + ":" + newArtifactId + ":"
197 if (newGroupId.equalsIgnoreCase(this.getGroupId())
198 && newArtifactId.equalsIgnoreCase(this.getArtifactId())
199 && newVersion.equalsIgnoreCase(this.getVersion())) {
200 logger.warn("Al in the right version: " + newGroupId + ":"
201 + newArtifactId + ":" + newVersion + " vs. " + this);
205 if (!newGroupId.equalsIgnoreCase(this.getGroupId())
206 || !newArtifactId.equalsIgnoreCase(this.getArtifactId())) {
207 throw new IllegalArgumentException(
208 "Group ID and Artifact ID maven coordinates must be identical for the upgrade: "
209 + newGroupId + ":" + newArtifactId + ":"
210 + newVersion + " vs. " + this);
214 String messages = this.policyContainer.updateToVersion(newVersion);
215 if (logger.isWarnEnabled()) {
216 logger.warn("{} UPGRADE results: {}", this, messages);
220 * If all sucessful (can load new container), now we can remove all coders from previous sessions
227 this.init(decoderConfigurations, encoderConfigurations);
229 if (logger.isInfoEnabled()) {
230 logger.info("UPDATE-TO-VERSION: completed " + this);
235 * initialize decoders for all the topics supported by this controller
236 * Note this is critical to be done after the Policy Container is
237 * instantiated to be able to fetch the corresponding classes.
239 * @param coderConfigurations list of topic -> decoders -> filters mapping
241 protected void initCoders(List<TopicCoderFilterConfiguration> coderConfigurations,
244 if (logger.isInfoEnabled()) {
245 logger.info("INIT-CODERS: " + this);
248 if (coderConfigurations == null) {
253 for (TopicCoderFilterConfiguration coderConfig: coderConfigurations) {
254 String topic = coderConfig.getTopic();
256 CustomGsonCoder customGsonCoder = coderConfig.getCustomGsonCoder();
257 if (coderConfig.getCustomGsonCoder() != null
258 && coderConfig.getCustomGsonCoder().getClassContainer() != null
259 && !coderConfig.getCustomGsonCoder().getClassContainer().isEmpty()) {
261 String customGsonCoderClass = coderConfig.getCustomGsonCoder().getClassContainer();
262 if (!ReflectionUtil.isClass(this.policyContainer.getClassLoader(),
263 customGsonCoderClass)) {
264 throw makeRetrieveEx(customGsonCoderClass);
266 if (logger.isInfoEnabled()) {
267 logClassFetched(customGsonCoderClass);
272 CustomJacksonCoder customJacksonCoder = coderConfig.getCustomJacksonCoder();
273 if (coderConfig.getCustomJacksonCoder() != null
274 && coderConfig.getCustomJacksonCoder().getClassContainer() != null
275 && !coderConfig.getCustomJacksonCoder().getClassContainer().isEmpty()) {
277 String customJacksonCoderClass = coderConfig.getCustomJacksonCoder().getClassContainer();
278 if (!ReflectionUtil.isClass(this.policyContainer.getClassLoader(),
279 customJacksonCoderClass)) {
280 throw makeRetrieveEx(customJacksonCoderClass);
282 if (logger.isInfoEnabled()) {
283 logClassFetched(customJacksonCoderClass);
288 List<PotentialCoderFilter> coderFilters = coderConfig.getCoderFilters();
289 if (coderFilters == null || coderFilters.isEmpty()) {
293 for (PotentialCoderFilter coderFilter : coderFilters) {
294 String potentialCodedClass = coderFilter.getCodedClass();
295 JsonProtocolFilter protocolFilter = coderFilter.getFilter();
297 if (!ReflectionUtil.isClass(this.policyContainer.getClassLoader(),
298 potentialCodedClass)) {
299 throw makeRetrieveEx(potentialCodedClass);
301 if (logger.isInfoEnabled()) {
302 logClassFetched(potentialCodedClass);
307 EventProtocolCoder.manager.addDecoder(this.getGroupId(), this.getArtifactId(),
308 topic, potentialCodedClass, protocolFilter,
311 this.policyContainer.getClassLoader().hashCode());
313 EventProtocolCoder.manager.addEncoder(this.getGroupId(), this.getArtifactId(),
314 topic, potentialCodedClass, protocolFilter,
317 this.policyContainer.getClassLoader().hashCode());
324 * Logs an error and makes an exception for an item that cannot be retrieved.
325 * @param itemName the item to retrieve
326 * @return a new exception
328 private IllegalArgumentException makeRetrieveEx(String itemName) {
329 logger.error("{} cannot be retrieved", itemName);
330 return new IllegalArgumentException(itemName + " cannot be retrieved");
334 * Logs the name of the class that was fetched.
335 * @param className class name fetched
337 private void logClassFetched(String className) {
338 logger.info("CLASS FETCHED {}", className);
345 protected void removeDecoders() {
346 if (logger.isInfoEnabled()) {
347 logger.info("REMOVE-DECODERS: {}", this);
350 if (this.decoderConfigurations == null) {
355 for (TopicCoderFilterConfiguration coderConfig: decoderConfigurations) {
356 String topic = coderConfig.getTopic();
357 EventProtocolCoder.manager.removeDecoders(this.getGroupId(), this.getArtifactId(), topic);
364 protected void removeEncoders() {
366 if (logger.isInfoEnabled()) {
367 logger.info("REMOVE-ENCODERS: {}", this);
370 if (this.encoderConfigurations == null) {
374 for (TopicCoderFilterConfiguration coderConfig: encoderConfigurations) {
375 String topic = coderConfig.getTopic();
376 EventProtocolCoder.manager.removeEncoders(this.getGroupId(), this.getArtifactId(), topic);
382 public boolean ownsCoder(Class<? extends Object> coderClass, int modelHash) {
383 if (!ReflectionUtil.isClass(this.policyContainer.getClassLoader(), coderClass.getCanonicalName())) {
384 logger.error("{}{} cannot be retrieved. ", this, coderClass.getCanonicalName());
388 if (modelHash == this.modelClassLoaderHash) {
389 if (logger.isInfoEnabled()) {
390 logger.info(coderClass.getCanonicalName()
391 + this + " class loader matches original drools controller rules classloader "
392 + coderClass.getClassLoader());
396 if (logger.isWarnEnabled()) {
397 logger.warn(this + coderClass.getCanonicalName() + " class loaders don't match "
398 + coderClass.getClassLoader() + " vs "
399 + this.policyContainer.getClassLoader());
406 public boolean start() {
408 if (logger.isInfoEnabled()) {
409 logger.info("START: {}", this);
412 synchronized (this) {
419 return this.policyContainer.start();
423 public boolean stop() {
425 logger.info("STOP: {}", this);
427 synchronized (this) {
434 return this.policyContainer.stop();
438 public void shutdown() {
439 logger.info("{}: SHUTDOWN", this);
444 } catch (Exception e) {
445 logger.error("{} SHUTDOWN FAILED because of {}", this, e.getMessage(), e);
447 this.policyContainer.shutdown();
454 logger.info("{}: HALT", this);
459 } catch (Exception e) {
460 logger.error("{} HALT FAILED because of {}", this, e.getMessage(), e);
462 this.policyContainer.destroy();
467 * removes this drools controllers and encoders and decoders from operation.
469 protected void removeCoders() {
470 logger.info("{}: REMOVE-CODERS", this);
473 this.removeDecoders();
474 } catch (IllegalArgumentException e) {
475 logger.error("{} REMOVE-DECODERS FAILED because of {}", this, e.getMessage(), e);
479 this.removeEncoders();
480 } catch (IllegalArgumentException e) {
481 logger.error("{} REMOVE-ENCODERS FAILED because of {}", this, e.getMessage(), e);
486 public boolean isAlive() {
491 public boolean offer(String topic, String event) {
492 logger.debug("{}: OFFER: {} <- {}", this, topic, event);
501 // 0. Check if the policy container has any sessions
503 if (this.policyContainer.getPolicySessions().isEmpty()) {
508 // 1. Now, check if this topic has a decoder:
510 if (!EventProtocolCoder.manager.isDecodingSupported(this.getGroupId(),
511 this.getArtifactId(),
514 logger.warn("{}: DECODING-UNSUPPORTED {}:{}:{}", this,
515 topic, this.getGroupId(), this.getArtifactId());
523 anEvent = EventProtocolCoder.manager.decode(this.getGroupId(),
524 this.getArtifactId(),
527 } catch (UnsupportedOperationException uoe) {
528 logger.debug("{}: DECODE FAILED: {} <- {} because of {}", this, topic,
529 event, uoe.getMessage(), uoe);
531 } catch (Exception e) {
532 logger.warn("{}: DECODE FAILED: {} <- {} because of {}", this, topic,
533 event, e.getMessage(), e);
537 synchronized (this.recentSourceEvents) {
538 this.recentSourceEvents.add(anEvent);
541 // increment event count for Nagios monitoring
542 PdpJmx.getInstance().updateOccured();
546 if (logger.isInfoEnabled()) {
547 logger.info("{} BROADCAST-INJECT of {} FROM {} INTO {}",
548 this, event, topic, this.policyContainer.getName());
551 for (DroolsControllerFeatureAPI feature : DroolsControllerFeatureAPI.providers.getList()) {
553 if (feature.beforeInsert(this, anEvent)) {
556 } catch (Exception e) {
557 logger.error("{}: feature {} before-insert failure because of {}",
558 this, feature.getClass().getName(), e.getMessage(), e);
562 boolean successInject = this.policyContainer.insertAll(anEvent);
563 if (!successInject) {
564 logger.warn(this + "Failed to inject into PolicyContainer {}", this.getSessionNames());
567 for (DroolsControllerFeatureAPI feature : DroolsControllerFeatureAPI.providers.getList()) {
569 if (feature.afterInsert(this, anEvent, successInject)) {
572 } catch (Exception e) {
573 logger.error("{}: feature {} after-insert failure because of {}",
574 this, feature.getClass().getName(), e.getMessage(), e);
582 public boolean deliver(TopicSink sink, Object event) {
584 if (logger.isInfoEnabled()) {
585 logger.info("{}DELIVER: {} FROM {} TO {}", this, event, this, sink);
589 throw new IllegalArgumentException(this + " invalid sink");
593 throw new IllegalArgumentException(this + " invalid event");
597 throw new IllegalStateException(this + " is locked");
601 throw new IllegalStateException(this + " is stopped");
605 EventProtocolCoder.manager.encode(sink.getTopic(), event, this);
607 synchronized (this.recentSinkEvents) {
608 this.recentSinkEvents.add(json);
611 return sink.send(json);
616 public String getVersion() {
617 return this.policyContainer.getVersion();
621 public String getArtifactId() {
622 return this.policyContainer.getArtifactId();
626 public String getGroupId() {
627 return this.policyContainer.getGroupId();
631 * Get model class loader hash.
633 * @return the modelClassLoaderHash
635 public int getModelClassLoaderHash() {
636 return modelClassLoaderHash;
640 public synchronized boolean lock() {
641 logger.info("LOCK: {}", this);
648 public synchronized boolean unlock() {
649 logger.info("UNLOCK: {}", this);
656 public boolean isLocked() {
662 public PolicyContainer getContainer() {
663 return this.policyContainer;
666 @JsonProperty("sessions")
668 public List<String> getSessionNames() {
669 return getSessionNames(true);
675 * @param abbreviated true for the short form, otherwise the long form
676 * @return session names
678 protected List<String> getSessionNames(boolean abbreviated) {
679 List<String> sessionNames = new ArrayList<>();
681 for (PolicySession session: this.policyContainer.getPolicySessions()) {
683 sessionNames.add(session.getName());
685 sessionNames.add(session.getFullName());
688 } catch (Exception e) {
689 logger.warn("Can't retrieve CORE sessions: " + e.getMessage(), e);
690 sessionNames.add(e.getMessage());
695 @JsonProperty("sessionCoordinates")
697 public List<String> getCanonicalSessionNames() {
698 return getSessionNames(false);
702 * provides the underlying core layer container sessions.
704 * @return the attached Policy Container
706 protected List<PolicySession> getSessions() {
707 List<PolicySession> sessions = new ArrayList<>();
708 sessions.addAll(this.policyContainer.getPolicySessions());
713 * provides the underlying core layer container session with name sessionName.
715 * @param sessionName session name
716 * @return the attached Policy Container
717 * @throws IllegalArgumentException when an invalid session name is provided
718 * @throws IllegalStateException when the drools controller is in an invalid state
720 protected PolicySession getSession(String sessionName) {
721 if (sessionName == null || sessionName.isEmpty()) {
722 throw new IllegalArgumentException("A Session Name must be provided");
725 List<PolicySession> sessions = this.getSessions();
726 for (PolicySession session : sessions) {
727 if (sessionName.equals(session.getName()) || sessionName.equals(session.getFullName())) {
732 throw invalidSessNameEx(sessionName);
735 private IllegalArgumentException invalidSessNameEx(String sessionName) {
736 return new IllegalArgumentException("Invalid Session Name: " + sessionName);
740 public Map<String,Integer> factClassNames(String sessionName) {
741 if (sessionName == null || sessionName.isEmpty()) {
742 throw invalidSessNameEx(sessionName);
745 Map<String,Integer> classNames = new HashMap<>();
747 PolicySession session = getSession(sessionName);
748 KieSession kieSession = session.getKieSession();
750 Collection<FactHandle> facts = session.getKieSession().getFactHandles();
751 for (FactHandle fact : facts) {
753 String className = kieSession.getObject(fact).getClass().getName();
754 if (classNames.containsKey(className)) {
755 classNames.put(className, classNames.get(className) + 1);
757 classNames.put(className, 1);
759 } catch (Exception e) {
760 logger.warn("Object cannot be retrieved from fact {}", fact, e);
768 public long factCount(String sessionName) {
769 if (sessionName == null || sessionName.isEmpty()) {
770 throw invalidSessNameEx(sessionName);
773 PolicySession session = getSession(sessionName);
774 return session.getKieSession().getFactCount();
778 public List<Object> facts(String sessionName, String className, boolean delete) {
779 if (sessionName == null || sessionName.isEmpty()) {
780 throw invalidSessNameEx(sessionName);
783 if (className == null || className.isEmpty()) {
784 throw new IllegalArgumentException("Invalid Class Name: " + className);
788 ReflectionUtil.fetchClass(this.policyContainer.getClassLoader(), className);
789 if (factClass == null) {
790 throw new IllegalArgumentException("Class cannot be fetched in model's classloader: " + className);
793 PolicySession session = getSession(sessionName);
794 KieSession kieSession = session.getKieSession();
796 List<Object> factObjects = new ArrayList<>();
798 Collection<FactHandle> factHandles = kieSession.getFactHandles(new ClassObjectFilter(factClass));
799 for (FactHandle factHandle : factHandles) {
801 factObjects.add(kieSession.getObject(factHandle));
803 kieSession.delete(factHandle);
805 } catch (Exception e) {
806 logger.warn("Object cannot be retrieved from fact {}", factHandle, e);
814 public List<Object> factQuery(String sessionName, String queryName, String queriedEntity,
815 boolean delete, Object... queryParams) {
816 if (sessionName == null || sessionName.isEmpty()) {
817 throw invalidSessNameEx(sessionName);
820 if (queryName == null || queryName.isEmpty()) {
821 throw new IllegalArgumentException("Invalid Query Name: " + queryName);
824 if (queriedEntity == null || queriedEntity.isEmpty()) {
825 throw new IllegalArgumentException("Invalid Queried Entity: " + queriedEntity);
828 PolicySession session = getSession(sessionName);
829 KieSession kieSession = session.getKieSession();
831 boolean found = false;
832 for (KiePackage kiePackage : kieSession.getKieBase().getKiePackages()) {
833 for (Query q : kiePackage.getQueries()) {
834 if (q.getName() != null && q.getName().equals(queryName)) {
841 throw new IllegalArgumentException("Invalid Query Name: " + queryName);
844 List<Object> factObjects = new ArrayList<>();
846 QueryResults queryResults = kieSession.getQueryResults(queryName, queryParams);
847 for (QueryResultsRow row : queryResults) {
849 factObjects.add(row.get(queriedEntity));
851 kieSession.delete(row.getFactHandle(queriedEntity));
853 } catch (Exception e) {
854 logger.warn("Object cannot be retrieved from row: {}", row, e);
862 public Class<?> fetchModelClass(String className) {
863 return ReflectionUtil.fetchClass(this.policyContainer.getClassLoader(), className);
867 * Get recent source events.
869 * @return the recentSourceEvents
872 public Object[] getRecentSourceEvents() {
873 synchronized (this.recentSourceEvents) {
874 Object[] events = new Object[recentSourceEvents.size()];
875 return recentSourceEvents.toArray(events);
880 * Get recent sink events.
882 * @return the recentSinkEvents
885 public String[] getRecentSinkEvents() {
886 synchronized (this.recentSinkEvents) {
887 String[] events = new String[recentSinkEvents.size()];
888 return recentSinkEvents.toArray(events);
893 public boolean isBrained() {
899 public String toString() {
900 StringBuilder builder = new StringBuilder();
902 .append("MavenDroolsController [policyContainer=")
903 .append((policyContainer != null) ? policyContainer.getName() : "NULL")
908 .append(", modelClassLoaderHash=")
909 .append(modelClassLoaderHash)
911 return builder.toString();