diff options
Diffstat (limited to 'policy-management/src/main/java/org/openecomp/policy/drools/controller')
4 files changed, 1647 insertions, 0 deletions
diff --git a/policy-management/src/main/java/org/openecomp/policy/drools/controller/DroolsController.java b/policy-management/src/main/java/org/openecomp/policy/drools/controller/DroolsController.java new file mode 100644 index 00000000..72f8e0b2 --- /dev/null +++ b/policy-management/src/main/java/org/openecomp/policy/drools/controller/DroolsController.java @@ -0,0 +1,170 @@ +/*- + * ============LICENSE_START======================================================= + * policy-management + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.policy.drools.controller; + +import java.util.List; + +import org.openecomp.policy.drools.event.comm.TopicSink; +import org.openecomp.policy.drools.properties.Lockable; +import org.openecomp.policy.drools.properties.Startable; +import org.openecomp.policy.drools.protocol.coders.TopicCoderFilterConfiguration; + +/** + * Drools Controller is the abstractions that wraps the + * drools layer (policy-core) + */ +public interface DroolsController extends Startable, Lockable { + + /** + * No Group ID identifier + */ + public static final String NO_GROUP_ID = "NO-GROUP-ID"; + + /** + * No Artifact ID identifier + */ + public static final String NO_ARTIFACT_ID = "NO-ARTIFACT-ID"; + + /** + * No version identifier + */ + public static final String NO_VERSION = "NO-VERSION"; + + /** + * get group id + * @return group id + */ + public String getGroupId(); + + /** + * get artifact id + * @return artifact id + */ + public String getArtifactId(); + + /** + * get version + * @return version + */ + public String getVersion(); + + + /** + * return the policy session names + * + * @return policy session + */ + public List<String> getSessionNames(); + + /** + * offers an event to this controller for processing + * + * @param topic topic associated with the event + * @param event the event + * + * @return true if the operation was successful + */ + public boolean offer(String topic, String event); + + /** + * delivers "event" to "sink" + * + * @param sink destination + * @param event + * @return true if successful, false if a failure has occurred. + * @throws IllegalArgumentException when invalid or insufficient + * properties are provided + * @throws IllegalStateException when the engine is in a state where + * this operation is not permitted (ie. locked or stopped). + * @throws UnsupportedOperationException when the engine cannot deliver due + * to the functionality missing (ie. communication infrastructure + * not supported. + */ + public boolean deliver(TopicSink sink, Object event) + throws IllegalArgumentException, IllegalStateException, + UnsupportedOperationException; + + /** + * + * @return the most recent received events + */ + public Object[] getRecentSourceEvents(); + + /** + * + * @return the most recent delivered events + */ + public String[] getRecentSinkEvents(); + + /** + * Supports this encoder? + * + * @param encodedObject + * @return + */ + public boolean ownsCoder(Class<? extends Object> coderClass, int modelHash) throws IllegalStateException; + + /** + * fetches a class from the model + * + * @param className the class to fetch + * @return the actual class object, or null if not found + */ + public Class<?> fetchModelClass(String className) throws IllegalArgumentException; + + /** + * is this controller Smart? + */ + public boolean isBrained(); + + /** + * update the new version of the maven jar rules file + * + * @param newGroupId - new group id + * @param newArtifactId - new artifact id + * @param newVersion - new version + * @param decoderConfigurations - decoder configurations + * @param encoderConfigurations - encoder configurations + * + * @throws Exception from within drools libraries + * @throws LinkageError from within drools libraries + * @throws ArgumentException bad parameter passed in + */ + public void updateToVersion(String newGroupId, String newArtifactId, String newVersion, + List<TopicCoderFilterConfiguration> decoderConfigurations, + List<TopicCoderFilterConfiguration> encoderConfigurations) + throws IllegalArgumentException, LinkageError, Exception; + + + /** + * halts and permanently releases all resources + * @throws IllegalStateException + */ + public void halt() throws IllegalStateException; + + /** + * Factory to track and manage drools controllers + */ + public static final DroolsControllerFactory factory = + new IndexedDroolsControllerFactory(); + + +} diff --git a/policy-management/src/main/java/org/openecomp/policy/drools/controller/DroolsControllerFactory.java b/policy-management/src/main/java/org/openecomp/policy/drools/controller/DroolsControllerFactory.java new file mode 100644 index 00000000..d94e773c --- /dev/null +++ b/policy-management/src/main/java/org/openecomp/policy/drools/controller/DroolsControllerFactory.java @@ -0,0 +1,540 @@ +/*- + * ============LICENSE_START======================================================= + * policy-management + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.policy.drools.controller; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Properties; + +import org.openecomp.policy.drools.controller.internal.MavenDroolsController; +import org.openecomp.policy.drools.controller.internal.NullDroolsController; +import org.openecomp.policy.drools.event.comm.Topic; +import org.openecomp.policy.drools.event.comm.Topic.CommInfrastructure; +import org.openecomp.policy.common.logging.flexlogger.FlexLogger; +import org.openecomp.policy.common.logging.flexlogger.Logger; +import org.openecomp.policy.drools.event.comm.TopicSource; +import org.openecomp.policy.drools.event.comm.TopicSink; +import org.openecomp.policy.drools.properties.PolicyProperties; +import org.openecomp.policy.drools.protocol.coders.JsonProtocolFilter; +import org.openecomp.policy.drools.protocol.coders.TopicCoderFilterConfiguration; +import org.openecomp.policy.drools.protocol.coders.TopicCoderFilterConfiguration.CustomGsonCoder; +import org.openecomp.policy.drools.protocol.coders.TopicCoderFilterConfiguration.CustomJacksonCoder; +import org.openecomp.policy.drools.protocol.coders.TopicCoderFilterConfiguration.PotentialCoderFilter; +import org.openecomp.policy.drools.utils.Pair; + +/** + * Drools Controller Factory to manage controller creation, destruction, + * and retrieval for management interfaces + */ +public interface DroolsControllerFactory { + + /** + * Constructs a Drools Controller based on properties + * + * @param properties properties containing initialization parameters + * @param eventSources list of event sources + * @param eventSinks list of event sinks + * + * @return the instantiated Drools Controller + * @throws IllegalArgumentException with invalid parameters + * @throws LinkageError Failure to link rules and models in Drools Libraries + * @throws Exception Exception from Drools Libraries + */ + public DroolsController build(Properties properties, + List<? extends TopicSource> eventSources, + List<? extends TopicSink> eventSinks) + throws IllegalArgumentException, LinkageError, Exception; + + /** + * Explicit construction of a Drools Controller + * + * @param groupId maven group id of drools artifact + * @param artifactId maven artifact id of drools artifact + * @param version maven version id of drools artifact + * @param decoderConfigurations list of decoder configurations + * @param encoderConfigurations list of encoder configurations + * + * @return the instantiated Drools Controller + * @throws IllegalArgumentException with invalid parameters + * @throws LinkageError Failure to link rules and models in Drools Libraries + * @throws Exception Exception from Drools Libraries + */ + public DroolsController build(String groupId, + String artifactId, + String version, + List<TopicCoderFilterConfiguration> decoderConfigurations, + List<TopicCoderFilterConfiguration> encoderConfigurations) + throws IllegalArgumentException, LinkageError, Exception; + + /** + * Releases the Drools Controller from operation + * + * @param controller the Drools Controller to shut down + */ + public void shutdown(DroolsController controller); + + /** + * Disables all Drools Controllers from operation + */ + public void shutdown(); + + /** + * Destroys and releases resources for a Drools Controller + * + * @param controller the Drools Controller to destroy + */ + public void destroy(DroolsController controller); + + /** + * Destroys all Drools Controllers + */ + public void destroy(); + + /** + * Gets the Drools Controller associated with the maven group + * and artifact id + * + * @param groupId maven group id of drools artifact + * @param artifactId maven artifact id of drools artifact + * @param version maven version id of drools artifact + * + * @return the Drools Controller + * @throws IllegalArgumentException with invalid parameters + */ + public DroolsController get(String groupId, + String artifactId, + String version) + throws IllegalArgumentException; + + /** + * returns the current inventory of Drools Controllers + * + * @return a list of Drools Controllers + */ + public List<DroolsController> inventory(); +} + +/* ---------------- implementation -----------------*/ + +/** + * Factory of Drools Controllers indexed by the Maven coordinates + */ +class IndexedDroolsControllerFactory implements DroolsControllerFactory { + + /** + * logger + */ + private static Logger logger = FlexLogger.getLogger(MavenDroolsController.class); + + /** + * Policy Controller Name Index + */ + protected HashMap<String, DroolsController> droolsControllers = + new HashMap<String, DroolsController>(); + + /** + * Null Drools Controller + */ + protected NullDroolsController nullDroolsController = new NullDroolsController(); + + + public IndexedDroolsControllerFactory() { + + /* Add a NULL controller which will always be present in the hash */ + + DroolsController controller = new NullDroolsController(); + String controllerId = controller.getGroupId() + ":" + controller.getArtifactId(); + + synchronized(this) { + droolsControllers.put(controllerId, controller); + } + } + + /** + * {@inheritDoc} + */ + @Override + public DroolsController build(Properties properties, + List<? extends TopicSource> eventSources, + List<? extends TopicSink> eventSinks) + throws IllegalArgumentException, LinkageError, Exception { + + String groupId = properties.getProperty(PolicyProperties.RULES_GROUPID); + if (groupId == null || groupId.isEmpty()) + groupId = DroolsController.NO_GROUP_ID; + + String artifactId = properties.getProperty(PolicyProperties.RULES_ARTIFACTID); + if (artifactId == null || artifactId.isEmpty()) + artifactId = DroolsController.NO_ARTIFACT_ID; + + String version = properties.getProperty(PolicyProperties.RULES_VERSION); + if (version == null || version.isEmpty()) + version = DroolsController.NO_VERSION; + + List<TopicCoderFilterConfiguration> + topics2DecodedClasses2Filters = codersAndFilters(properties, eventSources); + + List<TopicCoderFilterConfiguration> + topics2EncodedClasses2Filters = codersAndFilters(properties, eventSinks); + + return this.build(groupId, artifactId, version, + topics2DecodedClasses2Filters, + topics2EncodedClasses2Filters); + } + + /** + * find out decoder classes and filters + * + * @param properties properties with information about decoders + * @param topicEntities topic sources + * @return list of topics, each with associated decoder classes, each + * with a list of associated filters + * @throws IllegalArgumentException invalid input data + */ + protected List<TopicCoderFilterConfiguration> codersAndFilters + (Properties properties, List<? extends Topic> topicEntities) + throws IllegalArgumentException { + + String PROPERTY_TOPIC_ENTITY_PREFIX; + + List<TopicCoderFilterConfiguration> + topics2DecodedClasses2Filters = + new ArrayList<TopicCoderFilterConfiguration>(); + + if (topicEntities.isEmpty()) + return topics2DecodedClasses2Filters; + + for (Topic topic: topicEntities) { + + /* source or sink ? ueb or dmaap? */ + boolean isSource = (topic instanceof TopicSource); + CommInfrastructure commInfra = topic.getTopicCommInfrastructure(); + if (commInfra == CommInfrastructure.UEB) { + if (isSource) { + PROPERTY_TOPIC_ENTITY_PREFIX = PolicyProperties.PROPERTY_UEB_SOURCE_TOPICS + "."; + } else { + PROPERTY_TOPIC_ENTITY_PREFIX = PolicyProperties.PROPERTY_UEB_SINK_TOPICS + "."; + } + } else if (commInfra == CommInfrastructure.DMAAP) { + if (isSource) { + PROPERTY_TOPIC_ENTITY_PREFIX = PolicyProperties.PROPERTY_DMAAP_SOURCE_TOPICS + "."; + } else { + PROPERTY_TOPIC_ENTITY_PREFIX = PolicyProperties.PROPERTY_DMAAP_SINK_TOPICS + "."; + } + } else { + throw new IllegalArgumentException("Invalid Communication Infrastructure: " + commInfra); + } + + // 1. first the topic + + String aTopic = topic.getTopic(); + + // 2. check if there is a custom decoder for this topic that the user prefers to use + // instead of the ones provided in the platform + + String customGson = properties.getProperty + (PROPERTY_TOPIC_ENTITY_PREFIX + + aTopic + + PolicyProperties.PROPERTY_TOPIC_EVENTS_CUSTOM_MODEL_CODER_GSON_SUFFIX); + + CustomGsonCoder customGsonCoder = null; + if (customGson != null && !customGson.isEmpty()) { + try { + customGsonCoder = new CustomGsonCoder(customGson); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } + } + + String customJackson = properties.getProperty + (PROPERTY_TOPIC_ENTITY_PREFIX + + aTopic + + PolicyProperties.PROPERTY_TOPIC_EVENTS_CUSTOM_MODEL_CODER_JACKSON_SUFFIX); + + CustomJacksonCoder customJacksonCoder = null; + if (customJackson != null && !customJackson.isEmpty()) { + try { + customJacksonCoder = new CustomJacksonCoder(customJackson); + } catch (IllegalArgumentException e) { + e.printStackTrace(); + } + } + + // 3. second the list of classes associated with each topic + + String eventClasses = + properties.getProperty(PROPERTY_TOPIC_ENTITY_PREFIX + aTopic + PolicyProperties.PROPERTY_TOPIC_EVENTS_SUFFIX); + + if (eventClasses == null || eventClasses.isEmpty()) { + // TODO warn + continue; + } + + List<PotentialCoderFilter> classes2Filters = new ArrayList<PotentialCoderFilter>(); + + List<String> aTopicClasses = + new ArrayList<String>(Arrays.asList(eventClasses.split("\\s*,\\s*"))); + + for (String aClass: aTopicClasses) { + + + // 4. third, for each coder class, get the list of field filters + + String filter = properties.getProperty + (PROPERTY_TOPIC_ENTITY_PREFIX + + aTopic + + PolicyProperties.PROPERTY_TOPIC_EVENTS_SUFFIX + + "." + aClass + + PolicyProperties.PROPERTY_TOPIC_EVENTS_FILTER_SUFFIX); + + List<Pair<String,String>> filters = new ArrayList<Pair<String,String>>(); + + if (filter == null || filter.isEmpty()) { + // 4. topic -> class -> with no filters + + JsonProtocolFilter protocolFilter = JsonProtocolFilter.fromRawFilters(filters); + PotentialCoderFilter class2Filters = + new PotentialCoderFilter(aClass, protocolFilter); + classes2Filters.add(class2Filters); + continue; + } + + // There are filters associated with the applicability of + // this class for decoding. + List<String> listOfFilters = + new ArrayList<String>(Arrays.asList(filter.split("\\s*,\\s*"))); + + for (String nameValue: listOfFilters) { + String fieldName; + String regexValue; + + String[] nameValueSplit = nameValue.split("\\s*=\\s*"); + if (nameValueSplit.length <= 0 || nameValueSplit.length > 2) { + // TODO warn + // skip + continue; + } + + if (nameValueSplit.length == 2) { + fieldName = nameValueSplit[0]; + regexValue = nameValueSplit[1]; + } else if (nameValueSplit.length == 1) { + fieldName = nameValueSplit[0]; + regexValue = null; + } else { + // unreachable + continue; + } + + filters.add(new Pair<String,String>(fieldName, regexValue)); + } + + JsonProtocolFilter protocolFilter = JsonProtocolFilter.fromRawFilters(filters); + PotentialCoderFilter class2Filters = + new PotentialCoderFilter(aClass, protocolFilter); + classes2Filters.add(class2Filters); + } + + TopicCoderFilterConfiguration topic2Classes2Filters = + new TopicCoderFilterConfiguration(aTopic,classes2Filters, customGsonCoder, customJacksonCoder); + topics2DecodedClasses2Filters.add(topic2Classes2Filters); + } + + return topics2DecodedClasses2Filters; + } + + /** + * {@inheritDoc} + * @param decoderConfiguration + */ + @Override + public DroolsController build(String newGroupId, + String newArtifactId, + String newVersion, + List<TopicCoderFilterConfiguration> decoderConfigurations, + List<TopicCoderFilterConfiguration> encoderConfigurations) + throws IllegalArgumentException, LinkageError, Exception { + + if (newGroupId == null || newArtifactId == null || newVersion == null || + newGroupId.isEmpty() || newArtifactId.isEmpty() || newVersion.isEmpty()) { + throw new IllegalArgumentException("Missing maven coordinates: " + + newGroupId + ":" + newArtifactId + ":" + + newVersion); + } + + String controllerId = newGroupId + ":" + newArtifactId; + DroolsController controllerCopy = null; + synchronized (this) { + /* + * The Null Drools Controller for no maven coordinates is always here + * so when no coordinates present, this is the return point + * + * assert (controllerCopy instanceof NullDroolsController) + */ + if (droolsControllers.containsKey(controllerId)) { + controllerCopy = droolsControllers.get(controllerId); + if (controllerCopy.getVersion().equalsIgnoreCase(newVersion)) { + return controllerCopy; + } + } + } + + if (controllerCopy != null) { + /* + * a controller keyed by group id + artifact id exists + * but with different version => version upgrade/downgrade + */ + + controllerCopy.updateToVersion(newGroupId, newArtifactId, newVersion, + decoderConfigurations, encoderConfigurations); + + return controllerCopy; + } + + /* new drools controller */ + + DroolsController controller = new MavenDroolsController + (newGroupId, newArtifactId, newVersion, + decoderConfigurations, + encoderConfigurations); + + synchronized(this) { + droolsControllers.put(controllerId, controller); + } + + return controller; + } + + /** + * {@inheritDoc} + */ + @Override + public void destroy(DroolsController controller) throws IllegalArgumentException { + unmanage(controller); + controller.halt(); + } + + /** + * {@inheritDoc} + */ + @Override + public void destroy() { + List<DroolsController> controllers = this.inventory(); + for (DroolsController controller: controllers) { + controller.halt(); + } + + synchronized(this) { + this.droolsControllers.clear(); + } + } + + /** + * unmanage the drools controller + * + * @param controller + * @return + * @throws IllegalArgumentException + */ + protected void unmanage(DroolsController controller) throws IllegalArgumentException { + if (controller == null) { + throw new IllegalArgumentException("No controller provided"); + } + + if (!controller.isBrained()) { + logger.info("Drools Controller is NOT OPERATIONAL - nothing to destroy"); + return; + } + + String controllerId = controller.getGroupId() + ":" + controller.getArtifactId(); + synchronized(this) { + if (!this.droolsControllers.containsKey(controllerId)) { + return; + } + + droolsControllers.remove(controllerId); + } + } + + /** + * {@inheritDoc} + */ + @Override + public void shutdown(DroolsController controller) throws IllegalArgumentException { + this.unmanage(controller); + controller.shutdown(); + } + + /** + * {@inheritDoc} + */ + @Override + public void shutdown() { + List<DroolsController> controllers = this.inventory(); + for (DroolsController controller: controllers) { + controller.shutdown(); + } + + synchronized(this) { + this.droolsControllers.clear(); + } + } + + /** + * {@inheritDoc} + */ + @Override + public DroolsController get(String groupId, + String artifactId, + String version) + throws IllegalArgumentException, IllegalStateException { + + if (groupId == null || artifactId == null || + groupId.isEmpty() || artifactId.isEmpty()) { + throw new IllegalArgumentException("Missing maven coordinates: " + + groupId + ":" + artifactId); + } + + String controllerId = groupId + ":" + artifactId; + + synchronized(this) { + if (this.droolsControllers.containsKey(controllerId)) { + return droolsControllers.get(controllerId); + } else { + throw new IllegalStateException("DroolController for " + + controllerId + " not found"); + } + } + } + + /** + * {@inheritDoc} + */ + @Override + public List<DroolsController> inventory() { + List<DroolsController> controllers = + new ArrayList<DroolsController>(this.droolsControllers.values()); + return controllers; + } + +} diff --git a/policy-management/src/main/java/org/openecomp/policy/drools/controller/internal/MavenDroolsController.java b/policy-management/src/main/java/org/openecomp/policy/drools/controller/internal/MavenDroolsController.java new file mode 100644 index 00000000..2c5708d3 --- /dev/null +++ b/policy-management/src/main/java/org/openecomp/policy/drools/controller/internal/MavenDroolsController.java @@ -0,0 +1,718 @@ +/*- + * ============LICENSE_START======================================================= + * policy-management + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.policy.drools.controller.internal; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.collections4.queue.CircularFifoQueue; + +import org.openecomp.policy.common.logging.eelf.MessageCodes; +import org.openecomp.policy.common.logging.flexlogger.FlexLogger; +import org.openecomp.policy.common.logging.flexlogger.Logger; +import org.openecomp.policy.drools.controller.DroolsController; +import org.openecomp.policy.drools.core.PolicyContainer; +import org.openecomp.policy.drools.core.PolicySession; +import org.openecomp.policy.drools.core.jmx.PdpJmx; +import org.openecomp.policy.drools.event.comm.TopicSink; +import org.openecomp.policy.drools.protocol.coders.EventProtocolCoder; +import org.openecomp.policy.drools.protocol.coders.JsonProtocolFilter; +import org.openecomp.policy.drools.protocol.coders.TopicCoderFilterConfiguration; +import org.openecomp.policy.drools.protocol.coders.TopicCoderFilterConfiguration.CustomGsonCoder; +import org.openecomp.policy.drools.protocol.coders.TopicCoderFilterConfiguration.CustomJacksonCoder; +import org.openecomp.policy.drools.protocol.coders.TopicCoderFilterConfiguration.PotentialCoderFilter; +import org.openecomp.policy.drools.utils.ReflectionUtil; +import com.fasterxml.jackson.annotation.JsonIgnore; + +/** + * Maven-based Drools Controller that interacts with the + * policy-core PolicyContainer and PolicySession to manage + * Drools containers instantiated using Maven. + */ +public class MavenDroolsController implements DroolsController { + + /** + * logger + */ + private static Logger logger = FlexLogger.getLogger(MavenDroolsController.class); + + /** + * Policy Container, the access object to the policy-core layer + */ + @JsonIgnore + protected final PolicyContainer policyContainer; + + /** + * alive status of this drools controller, + * reflects invocation of start()/stop() only + */ + protected volatile boolean alive = false; + + /** + * locked status of this drools controller, + * reflects if i/o drools related operations are permitted, + * more specifically: offer() and deliver(). + * It does not affect the ability to start and stop + * underlying drools infrastructure + */ + protected volatile boolean locked = false; + + /** + * list of topics, each with associated decoder classes, each + * with a list of associated filters. + */ + protected List<TopicCoderFilterConfiguration> decoderConfigurations; + + /** + * list of topics, each with associated encoder classes, each + * with a list of associated filters. + */ + protected List<TopicCoderFilterConfiguration> encoderConfigurations; + + /** + * recent source events processed + */ + protected final CircularFifoQueue<Object> recentSourceEvents = new CircularFifoQueue<Object>(10); + + /** + * recent sink events processed + */ + protected final CircularFifoQueue<String> recentSinkEvents = new CircularFifoQueue<String>(10); + + /** + * original Drools Model/Rules classloader hash + */ + protected int modelClassLoaderHash; + + /** + * Expanded version of the constructor + * + * @param groupId maven group id + * @param artifactId maven artifact id + * @param version maven version + * @param decoderConfiguration list of topic -> decoders -> filters mapping + * @param encoderConfiguration list of topic -> encoders -> filters mapping + * + * @throws IllegalArgumentException invalid arguments passed in + */ + public MavenDroolsController(String groupId, + String artifactId, + String version, + List<TopicCoderFilterConfiguration> decoderConfigurations, + List<TopicCoderFilterConfiguration> encoderConfigurations) + throws IllegalArgumentException { + + if (logger.isInfoEnabled()) + logger.info("DROOLS CONTROLLER: instantiation " + this + + " -> {" + groupId + ":" + artifactId + ":" + version + "}"); + + if (groupId == null || artifactId == null || version == null || + groupId.isEmpty() || artifactId.isEmpty() || version.isEmpty()) { + throw new IllegalArgumentException("Missing maven coordinates: " + + groupId + ":" + artifactId + ":" + + version); + } + + this.policyContainer= new PolicyContainer(groupId, artifactId, version); + this.init(decoderConfigurations, encoderConfigurations); + + if (logger.isInfoEnabled()) + logger.info("DROOLS CONTROLLER: instantiation completed " + this); + } + + /** + * init encoding/decoding configuration + * @param decoderConfiguration list of topic -> decoders -> filters mapping + * @param encoderConfiguration list of topic -> encoders -> filters mapping + */ + protected void init(List<TopicCoderFilterConfiguration> decoderConfigurations, + List<TopicCoderFilterConfiguration> encoderConfigurations) { + + this.decoderConfigurations = decoderConfigurations; + this.encoderConfigurations = encoderConfigurations; + + this.initCoders(decoderConfigurations, true); + this.initCoders(encoderConfigurations, false); + + this.modelClassLoaderHash = this.policyContainer.getClassLoader().hashCode(); + } + + /** + * {@inheritDoc} + */ + @Override + public void updateToVersion(String newGroupId, String newArtifactId, String newVersion, + List<TopicCoderFilterConfiguration> decoderConfigurations, + List<TopicCoderFilterConfiguration> encoderConfigurations) + throws IllegalArgumentException, LinkageError, Exception { + + if (logger.isInfoEnabled()) + logger.info("UPDATE-TO-VERSION: " + this + " -> {" + newGroupId + ":" + newArtifactId + ":" + newVersion + "}"); + + if (newGroupId == null || newArtifactId == null || newVersion == null || + newGroupId.isEmpty() || newArtifactId.isEmpty() || newVersion.isEmpty()) { + throw new IllegalArgumentException("Missing maven coordinates: " + + newGroupId + ":" + newArtifactId + ":" + + newVersion); + } + + if (newGroupId.equalsIgnoreCase(DroolsController.NO_GROUP_ID) || + newArtifactId.equalsIgnoreCase(DroolsController.NO_ARTIFACT_ID) || + newVersion.equalsIgnoreCase(DroolsController.NO_VERSION)) { + throw new IllegalArgumentException("BRAINLESS maven coordinates provided: " + + newGroupId + ":" + newArtifactId + ":" + + newVersion); + } + + if (newGroupId.equalsIgnoreCase(this.getGroupId()) && + newArtifactId.equalsIgnoreCase(this.getArtifactId()) && + newVersion.equalsIgnoreCase(this.getVersion())) { + logger.warn("Al in the right version: " + newGroupId + ":" + + newArtifactId + ":" + newVersion + " vs. " + this); + return; + } + + if (!newGroupId.equalsIgnoreCase(this.getGroupId()) || + !newArtifactId.equalsIgnoreCase(this.getArtifactId())) { + throw new IllegalArgumentException("Group ID and Artifact ID maven coordinates must be identical for the upgrade: " + + newGroupId + ":" + newArtifactId + ":" + + newVersion + " vs. " + this); + } + + /* upgrade */ + String messages = this.policyContainer.updateToVersion(newVersion); + if (logger.isWarnEnabled()) + logger.warn(this + "UPGRADE results: " + messages); + + /* + * If all sucessful (can load new container), now we can remove all coders from previous sessions + */ + this.removeCoders(); + + /* + * add the new coders + */ + this.init(decoderConfigurations, encoderConfigurations); + + if (logger.isInfoEnabled()) + logger.info("UPDATE-TO-VERSION: completed " + this); + } + + /** + * initialize decoders for all the topics supported by this controller + * Note this is critical to be done after the Policy Container is + * instantiated to be able to fetch the corresponding classes. + * + * @param decoderConfiguration list of topic -> decoders -> filters mapping + */ + protected void initCoders(List<TopicCoderFilterConfiguration> coderConfigurations, + boolean decoder) + throws IllegalArgumentException { + + if (logger.isInfoEnabled()) + logger.info("INIT-CODERS: " + this); + + if (coderConfigurations == null) { + return; + } + + + for (TopicCoderFilterConfiguration coderConfig: coderConfigurations) { + String topic = coderConfig.getTopic(); + + CustomGsonCoder customGsonCoder = coderConfig.getCustomGsonCoder(); + if (coderConfig.getCustomGsonCoder() != null && + coderConfig.getCustomGsonCoder().getClassContainer() != null && + !coderConfig.getCustomGsonCoder().getClassContainer().isEmpty()) { + + String customGsonCoderClass = coderConfig.getCustomGsonCoder().getClassContainer(); + if (!ReflectionUtil.isClass(this.policyContainer.getClassLoader(), + customGsonCoderClass)) { + logger.error(customGsonCoderClass + " cannot be retrieved"); + throw new IllegalArgumentException(customGsonCoderClass + " cannot be retrieved"); + } else { + if (logger.isInfoEnabled()) + logger.info("CLASS FETCHED " + customGsonCoderClass); + } + } + + CustomJacksonCoder customJacksonCoder = coderConfig.getCustomJacksonCoder(); + if (coderConfig.getCustomJacksonCoder() != null && + coderConfig.getCustomJacksonCoder().getClassContainer() != null && + !coderConfig.getCustomJacksonCoder().getClassContainer().isEmpty()) { + + String customJacksonCoderClass = coderConfig.getCustomJacksonCoder().getClassContainer(); + if (!ReflectionUtil.isClass(this.policyContainer.getClassLoader(), + customJacksonCoderClass)) { + logger.error(customJacksonCoderClass + " cannot be retrieved"); + throw new IllegalArgumentException(customJacksonCoderClass + " cannot be retrieved"); + } else { + if (logger.isInfoEnabled()) + logger.info("CLASS FETCHED " + customJacksonCoderClass); + } + } + + List<PotentialCoderFilter> coderFilters = coderConfig.getCoderFilters(); + if (coderFilters == null || coderFilters.isEmpty()) { + continue; + } + + for (PotentialCoderFilter coderFilter : coderFilters) { + String potentialCodedClass = coderFilter.getCodedClass(); + JsonProtocolFilter protocolFilter = coderFilter.getFilter(); + + if (!ReflectionUtil.isClass(this.policyContainer.getClassLoader(), + potentialCodedClass)) { + logger.error(potentialCodedClass + " cannot be retrieved"); + throw new IllegalArgumentException(potentialCodedClass + " cannot be retrieved"); + } else { + if (logger.isInfoEnabled()) + logger.info("CLASS FETCHED " + potentialCodedClass); + } + + if (decoder) + EventProtocolCoder.manager.addDecoder(this.getGroupId(), this.getArtifactId(), + topic, potentialCodedClass, protocolFilter, + customGsonCoder, + customJacksonCoder, + this.policyContainer.getClassLoader().hashCode()); + else + EventProtocolCoder.manager.addEncoder(this.getGroupId(), this.getArtifactId(), + topic, potentialCodedClass, protocolFilter, + customGsonCoder, + customJacksonCoder, + this.policyContainer.getClassLoader().hashCode()); + } + } + } + + + /** + * remove decoders. + */ + protected void removeDecoders() + throws IllegalArgumentException { + if (logger.isInfoEnabled()) + logger.info("REMOVE-DECODERS: " + this); + + if (this.decoderConfigurations == null) { + return; + } + + + for (TopicCoderFilterConfiguration coderConfig: decoderConfigurations) { + String topic = coderConfig.getTopic(); + EventProtocolCoder.manager.removeDecoders + (this.getGroupId(), this.getArtifactId(), topic); + } + } + + /** + * remove decoders. + */ + protected void removeEncoders() + throws IllegalArgumentException { + + if (logger.isInfoEnabled()) + logger.info("REMOVE-ENCODERS: " + this); + + if (this.encoderConfigurations == null) + return; + + + for (TopicCoderFilterConfiguration coderConfig: encoderConfigurations) { + String topic = coderConfig.getTopic(); + EventProtocolCoder.manager.removeEncoders + (this.getGroupId(), this.getArtifactId(), topic); + } + } + + + /** + * {@inheritDoc} + */ + @Override + public boolean ownsCoder(Class<? extends Object> coderClass, int modelHash) throws IllegalStateException { + if (!ReflectionUtil.isClass + (this.policyContainer.getClassLoader(), coderClass.getCanonicalName())) { + logger.error(this + coderClass.getCanonicalName() + " cannot be retrieved. "); + return false; + } + + if (modelHash == this.modelClassLoaderHash) { + if (logger.isInfoEnabled()) + logger.info(coderClass.getCanonicalName() + + this + " class loader matches original drools controller rules classloader " + + coderClass.getClassLoader()); + return true; + } else { + if (logger.isWarnEnabled()) + logger.warn(this + coderClass.getCanonicalName() + " class loaders don't match " + + coderClass.getClassLoader() + " vs " + + this.policyContainer.getClassLoader()); + return false; + } + } + + /** + * {@inheritDoc} + */ + @Override + public boolean start() { + + if (logger.isInfoEnabled()) + logger.info("START: " + this); + + synchronized (this) { + if (this.alive) + return true; + + this.alive = true; + } + + return this.policyContainer.start(); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean stop() { + + logger.info("STOP: " + this); + + synchronized (this) { + if (!this.alive) + return true; + + this.alive = false; + } + + return this.policyContainer.stop(); + } + + + /** + * {@inheritDoc} + */ + @Override + public void shutdown() throws IllegalStateException { + + if (logger.isInfoEnabled()) + logger.info(this + "SHUTDOWN"); + + try { + this.stop(); + this.removeCoders(); + } catch (Exception e) { + logger.error(MessageCodes.EXCEPTION_ERROR, e, "stop", this.toString()); + } finally { + this.policyContainer.shutdown(); + } + + } + + + /** + * {@inheritDoc} + */ + @Override + public void halt() throws IllegalStateException { + if (logger.isInfoEnabled()) + logger.info(this + "SHUTDOWN"); + + try { + this.stop(); + this.removeCoders(); + } catch (Exception e) { + logger.error(MessageCodes.EXCEPTION_ERROR, e, "halt", this.toString()); + } finally { + this.policyContainer.destroy(); + } + } + + /** + * removes this drools controllers and encoders and decoders from operation + */ + protected void removeCoders() { + + if (logger.isInfoEnabled()) + logger.info(this + "REMOVE-CODERS"); + + try { + this.removeDecoders(); + } catch (IllegalArgumentException e) { + logger.error(MessageCodes.EXCEPTION_ERROR, e, "removeDecoders", this.toString()); + } + + try { + this.removeEncoders(); + } catch (IllegalArgumentException e) { + logger.error(MessageCodes.EXCEPTION_ERROR, e, "removeEncoders", this.toString()); + } + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isAlive() { + return this.alive; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean offer(String topic, String event) { + + if (logger.isInfoEnabled()) + logger.info("OFFER: " + topic + ":" + event + " INTO " + this); + + if (this.locked) + return true; + + if (!this.alive) + return true; + + // 0. Check if the policy container has any sessions + + if (this.policyContainer.getPolicySessions().size() <= 0) { + // no sessions + return true; + } + + // 1. Now, check if this topic has a decoder: + + if (!EventProtocolCoder.manager.isDecodingSupported(this.getGroupId(), + this.getArtifactId(), + topic)) { + + logger.warn("DECODING-UNSUPPORTED: " + ":" + this.getGroupId() + + ":" + this.getArtifactId() + ":" + topic + " IN " + this); + return true; + } + + // 2. Decode + + Object anEvent; + try { + anEvent = EventProtocolCoder.manager.decode(this.getGroupId(), + this.getArtifactId(), + topic, + event); + } catch (Exception e) { + logger.error(MessageCodes.EXCEPTION_ERROR, e, + "DECODE:"+ this.getGroupId() + ":" + + this.getArtifactId() + ":" + topic + ":" + event, + this.toString()); + return true; + } + + synchronized(this.recentSourceEvents) { + this.recentSourceEvents.add(anEvent); + } + + // increment event count for Nagios monitoring + PdpJmx.getInstance().updateOccured(); + + // Broadcast + + if (logger.isInfoEnabled()) + logger.info(this + "BROADCAST-INJECT of " + event + " FROM " + topic + " INTO " + this.policyContainer.getName()); + + if (!this.policyContainer.insertAll(anEvent)) + logger.warn(this + "Failed to inject into PolicyContainer " + this.getSessionNames()); + + return true; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean deliver(TopicSink sink, Object event) + throws IllegalArgumentException, + IllegalStateException, + UnsupportedOperationException { + + if (logger.isInfoEnabled()) + logger.info(this + "DELIVER: " + event + " FROM " + this + " TO " + sink); + + if (sink == null) + throw new IllegalArgumentException + (this + " invalid sink"); + + if (event == null) + throw new IllegalArgumentException + (this + " invalid event"); + + if (this.locked) + throw new IllegalStateException + (this + " is locked"); + + if (!this.alive) + throw new IllegalStateException + (this + " is stopped"); + + String json = + EventProtocolCoder.manager.encode(sink.getTopic(), event, this); + + synchronized(this.recentSinkEvents) { + this.recentSinkEvents.add(json); + } + + return sink.send(json); + + } + + /** + * {@inheritDoc} + */ + @Override + public String getVersion() { + return this.policyContainer.getVersion(); + } + + /** + * {@inheritDoc} + */ + @Override + public String getArtifactId() { + return this.policyContainer.getArtifactId(); + } + + /** + * {@inheritDoc} + */ + @Override + public String getGroupId() { + return this.policyContainer.getGroupId(); + } + + /** + * @return the modelClassLoaderHash + */ + public int getModelClassLoaderHash() { + return modelClassLoaderHash; + } + + /** + * {@inheritDoc} + */ + @Override + public synchronized boolean lock() { + logger.info("LOCK: " + this); + + this.locked = true; + return true; + } + + /** + * {@inheritDoc} + */ + @Override + public synchronized boolean unlock() { + logger.info("UNLOCK: " + this); + + this.locked = false; + return true; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isLocked() { + return this.locked; + } + + @Override + public List<String> getSessionNames() { + List<String> sessionNames = new ArrayList<String>(); + try { + for (PolicySession session: this.policyContainer.getPolicySessions()) { + sessionNames.add(session.getFullName()); + } + } catch (Exception e) { + logger.warn(MessageCodes.EXCEPTION_ERROR, e, + "Can't retrieve POLICY-CORE sessions: " + e.getMessage(), + this.toString()); + sessionNames.add(e.getMessage()); + } + return sessionNames; + } + + /** + * {@inheritDoc} + */ + @Override + public Class<?> fetchModelClass(String className) throws IllegalStateException { + Class<?> modelClass = + ReflectionUtil.fetchClass(this.policyContainer.getClassLoader(), className); + return modelClass; + } + + /** + * @return the recentSourceEvents + */ + @Override + public Object[] getRecentSourceEvents() { + synchronized(this.recentSourceEvents) { + Object[] events = new Object[recentSourceEvents.size()]; + return recentSourceEvents.toArray(events); + } + } + + /** + * @return the recentSinkEvents + */ + @Override + public String[] getRecentSinkEvents() { + synchronized(this.recentSinkEvents) { + String[] events = new String[recentSinkEvents.size()]; + return recentSinkEvents.toArray(events); + } + } + + + /** + * {@inheritDoc} + */ + @Override + public boolean isBrained() { + return true; + } + + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("MavenDroolsController [policyContainer=") + .append((policyContainer != null) ? policyContainer.getName() : "NULL").append(":") + .append(", alive=") + .append(alive).append(", locked=").append(locked).append(", decoderConfigurations=") + .append(decoderConfigurations).append(", encoderConfigurations=").append(encoderConfigurations) + .append(", modelClassLoaderHash=").append(modelClassLoaderHash).append("]"); + return builder.toString(); + } + +} diff --git a/policy-management/src/main/java/org/openecomp/policy/drools/controller/internal/NullDroolsController.java b/policy-management/src/main/java/org/openecomp/policy/drools/controller/internal/NullDroolsController.java new file mode 100644 index 00000000..f0c0f474 --- /dev/null +++ b/policy-management/src/main/java/org/openecomp/policy/drools/controller/internal/NullDroolsController.java @@ -0,0 +1,219 @@ +/*- + * ============LICENSE_START======================================================= + * policy-management + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.policy.drools.controller.internal; + +import java.util.ArrayList; +import java.util.List; + +import org.openecomp.policy.drools.controller.DroolsController; +import org.openecomp.policy.drools.event.comm.TopicSink; +import org.openecomp.policy.drools.protocol.coders.TopicCoderFilterConfiguration; + +/** + * no-op Drools Controller + */ +public class NullDroolsController implements DroolsController { + + /** + * empty cached events + */ + protected static final String[] emptyRecentEvents = new String[0]; + + /** + * empty session names + */ + protected static final List<String> emptySessionNames = new ArrayList<String>(); + + /** + * {@inheritDoc} + */ + @Override + public boolean start() throws IllegalStateException { + return false; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean stop() throws IllegalStateException { + return false; + } + + /** + * {@inheritDoc} + */ + @Override + public void shutdown() throws IllegalStateException { + return; + } + + /** + * {@inheritDoc} + */ + @Override + public void halt() throws IllegalStateException { + return; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isAlive() { + return false; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean lock() { + return false; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean unlock() { + return false; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isLocked() { + return false; + } + + /** + * {@inheritDoc} + */ + @Override + public String getGroupId() { + return NO_GROUP_ID; + } + + /** + * {@inheritDoc} + */ + @Override + public String getArtifactId() { + return NO_ARTIFACT_ID; + } + + /** + * {@inheritDoc} + */ + @Override + public String getVersion() { + return NO_VERSION; + } + + /** + * {@inheritDoc} + */ + @Override + public List<String> getSessionNames() { + return new ArrayList<String>(); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean offer(String topic, String event) { + return false; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean deliver(TopicSink sink, Object event) + throws IllegalArgumentException, IllegalStateException, UnsupportedOperationException { + throw new IllegalArgumentException(this.getClass().getCanonicalName() + " invoked"); + } + + /** + * {@inheritDoc} + */ + @Override + public Object[] getRecentSourceEvents() { + return NullDroolsController.emptyRecentEvents; + } + + /** + * {@inheritDoc} + */ + @Override + public String[] getRecentSinkEvents() { + return NullDroolsController.emptyRecentEvents; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean ownsCoder(Class<? extends Object> coderClass, int modelHash) throws IllegalStateException { + throw new IllegalArgumentException(this.getClass().getCanonicalName() + " invoked"); + } + + /** + * {@inheritDoc} + */ + @Override + public Class<?> fetchModelClass(String className) throws IllegalArgumentException { + throw new IllegalArgumentException(this.getClass().getCanonicalName() + " invoked"); + } + + /** + * {@inheritDoc} + */ + @Override + public boolean isBrained() { + return false; + } + + /** + * {@inheritDoc} + */ + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("NullDroolsController []"); + return builder.toString(); + } + + /** + * {@inheritDoc} + */ + @Override + public void updateToVersion(String newGroupId, String newArtifactId, String newVersion, + List<TopicCoderFilterConfiguration> decoderConfigurations, + List<TopicCoderFilterConfiguration> encoderConfigurations) + throws IllegalArgumentException, LinkageError, Exception { + throw new IllegalArgumentException(this.getClass().getCanonicalName() + " invoked"); + } + +} |