2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 
   6  * Copyright (C) 2017 Amdocs
 
   7  * ================================================================================
 
   8  * Licensed under the Apache License, Version 2.0 (the "License");
 
   9  * you may not use this file except in compliance with the License.
 
  10  * You may obtain a copy of the License at
 
  12  *      http://www.apache.org/licenses/LICENSE-2.0
 
  14  * Unless required by applicable law or agreed to in writing, software
 
  15  * distributed under the License is distributed on an "AS IS" BASIS,
 
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  17  * See the License for the specific language governing permissions and
 
  18  * limitations under the License.
 
  19  * ============LICENSE_END=========================================================
 
  20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 
  23 package org.openecomp.appc.listener.impl;
 
  25 import java.util.HashMap;
 
  26 import java.util.Iterator;
 
  29 import java.util.concurrent.ArrayBlockingQueue;
 
  30 import java.util.concurrent.ThreadPoolExecutor;
 
  31 import java.util.concurrent.TimeUnit;
 
  33 import org.apache.commons.lang3.concurrent.BasicThreadFactory;
 
  34 import org.openecomp.appc.listener.Controller;
 
  35 import org.openecomp.appc.listener.Listener;
 
  36 import org.openecomp.appc.listener.ListenerProperties;
 
  38 import com.att.eelf.configuration.EELFLogger;
 
  39 import com.att.eelf.configuration.EELFManager;
 
  43  * A common implementation of a controller. This controller should not need to be modified to implement new listeners
 
  46 public class ControllerImpl implements Controller {
 
  48     private final EELFLogger LOG = EELFManager.getInstance().getLogger(ControllerImpl.class);
 
  50     private int LISTENER_COUNT = 1;
 
  52     private Map<ListenerProperties, Listener> listeners = null;
 
  54     private ThreadPoolExecutor executor;
 
  57      * Creates a Controller with the set of listener properties which will be used to start listener threads.
 
  60      *            A non null Set of ListenerProperties
 
  62     public ControllerImpl(Set<ListenerProperties> properties) {
 
  63         listeners = new HashMap<ListenerProperties, Listener>();
 
  64         for (ListenerProperties props : properties) {
 
  65             if (props.getClass() != null) {
 
  66                 listeners.put(props, null);
 
  68                 LOG.error(String.format(
 
  69                     "The ListenerProperties %s has no Listener class associated with it and will not run.", props));
 
  73         LISTENER_COUNT = properties.size();
 
  75         executor = new ThreadPoolExecutor(LISTENER_COUNT, LISTENER_COUNT, 1, TimeUnit.SECONDS,
 
  76             new ArrayBlockingQueue<Runnable>(LISTENER_COUNT));
 
  78         // Custom Named thread factory
 
  79         BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("Appc-Listener-%d").build();
 
  80         executor.setThreadFactory(threadFactory);
 
  85         LOG.info("Starting DMaaP Controller.");
 
  86         for (ListenerProperties props : listeners.keySet()) {
 
  88                 if (props.isDisabled()) {
 
  89                     LOG.warn(String.format("The listener %s is disabled and will not be run", props.getPrefix()));
 
  91                     Listener l = props.getListenerClass().getConstructor(ListenerProperties.class).newInstance(props);
 
  92                     l.setListenerId(props.getPrefix());
 
  93                     listeners.put(props, l);
 
  96             } catch (Exception e) {
 
  98                 LOG.error(String.format("Exception while starting listener %s.", props), e);
 
 104     public void stop(boolean stopNow) {
 
 105         LOG.info("Stopping DMaaP Controller.");
 
 106         Iterator<Listener> itr = listeners.values().iterator();
 
 107         while (itr.hasNext()) {
 
 108             Listener l = itr.next();
 
 118             executor.awaitTermination(10, TimeUnit.SECONDS);
 
 119         } catch (InterruptedException e) {
 
 124     public Map<ListenerProperties, Listener> getListeners() {