2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 
   4  *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
 
   5  * ================================================================================
 
   6  * Licensed under the Apache License, Version 2.0 (the "License");
 
   7  * you may not use this file except in compliance with the License.
 
   8  * You may obtain a copy of the License at
 
  10  *      http://www.apache.org/licenses/LICENSE-2.0
 
  12  * Unless required by applicable law or agreed to in writing, software
 
  13  * distributed under the License is distributed on an "AS IS" BASIS,
 
  14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  15  * See the License for the specific language governing permissions and
 
  16  * limitations under the License.
 
  18  * SPDX-License-Identifier: Apache-2.0
 
  19  * ============LICENSE_END=========================================================
 
  22 package org.onap.policy.apex.service.engine.event.impl;
 
  24 import org.onap.policy.apex.service.engine.event.ApexEventConsumer;
 
  25 import org.onap.policy.apex.service.engine.event.ApexEventException;
 
  26 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
 
  27 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
 
  28 import org.slf4j.ext.XLogger;
 
  29 import org.slf4j.ext.XLoggerFactory;
 
  32  * This factory class creates event consumers of various technology types for Apex engines.
 
  34  * @author Liam Fallon (liam.fallon@ericsson.com)
 
  36 public class EventConsumerFactory {
 
  37     // The logger for this class
 
  38     private static final XLogger LOGGER = XLoggerFactory.getXLogger(EventConsumerFactory.class);
 
  41      * Create an event consumer of the required type for the specified consumer technology.
 
  43      * @param name the name of the consumer
 
  44      * @param consumerParameters The parameters for the Apex engine, we use the technology type of the required consumer
 
  45      * @return the event consumer
 
  46      * @throws ApexEventException on errors creating the Apex event consumer
 
  48     public ApexEventConsumer createConsumer(final String name, final EventHandlerParameters consumerParameters)
 
  49             throws ApexEventException {
 
  50         // Get the carrier technology parameters
 
  51         final CarrierTechnologyParameters technologyParameters = consumerParameters.getCarrierTechnologyParameters();
 
  53         // Get the class for the event consumer using reflection
 
  54         final String consumerPluginClass = technologyParameters.getEventConsumerPluginClass();
 
  55         Object consumerPluginObject = null;
 
  57             consumerPluginObject = Class.forName(consumerPluginClass).getDeclaredConstructor().newInstance();
 
  58         } catch (final Exception e) {
 
  59             final String errorMessage = "could not create an Apex event consumer for \"" + name
 
  60                     + "\" for the carrier technology \"" + technologyParameters.getLabel()
 
  61                     + "\", specified event consumer plugin class \"" + consumerPluginClass + "\" not found";
 
  62             LOGGER.error(errorMessage, e);
 
  63             throw new ApexEventException(errorMessage, e);
 
  66         // Check the class is an event consumer
 
  67         if (!(consumerPluginObject instanceof ApexEventConsumer)) {
 
  68             final String errorMessage = "could not create an Apex event consumer \"" + name
 
  69                     + "\" for the carrier technology \"" + technologyParameters.getLabel()
 
  70                     + "\", specified event consumer plugin class \"" + consumerPluginClass
 
  71                     + "\" is not an instance of \"" + ApexEventConsumer.class.getName() + "\"";
 
  72             LOGGER.error(errorMessage);
 
  73             throw new ApexEventException(errorMessage);
 
  76         return (ApexEventConsumer) consumerPluginObject;