Moving all files to root directory
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / main / java / org / openecomp / appc / listener / impl / ControllerImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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  */
21
22 package org.openecomp.appc.listener.impl;
23
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.concurrent.ArrayBlockingQueue;
29 import java.util.concurrent.ThreadPoolExecutor;
30 import java.util.concurrent.TimeUnit;
31
32 import org.apache.commons.lang3.concurrent.BasicThreadFactory;
33 import org.openecomp.appc.listener.Controller;
34 import org.openecomp.appc.listener.Listener;
35 import org.openecomp.appc.listener.ListenerProperties;
36
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
39
40
41 /**
42  * A common implementation of a controller. This controller should not need to be modified to implement new listeners
43  *
44  */
45 public class ControllerImpl implements Controller {
46
47     private final EELFLogger LOG = EELFManager.getInstance().getLogger(ControllerImpl.class);
48
49     private int LISTENER_COUNT = 1;
50
51     private Map<ListenerProperties, Listener> listeners = null;
52
53     private ThreadPoolExecutor executor;
54
55     /**
56      * Creates a Controller with the set of listener properties which will be used to start listener threads.
57      *
58      * @param properties
59      *            A non null Set of ListenerProperties
60      */
61     public ControllerImpl(Set<ListenerProperties> properties) {
62         listeners = new HashMap<ListenerProperties, Listener>();
63         for (ListenerProperties props : properties) {
64             if (props.getClass() != null) {
65                 listeners.put(props, null);
66             } else {
67                 LOG.error(String.format(
68                     "The ListenerProperties %s has no Listener class associated with it and will not run.", props));
69             }
70         }
71
72         LISTENER_COUNT = properties.size();
73
74         executor = new ThreadPoolExecutor(LISTENER_COUNT, LISTENER_COUNT, 1, TimeUnit.SECONDS,
75             new ArrayBlockingQueue<Runnable>(LISTENER_COUNT));
76
77         // Custom Named thread factory
78         BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("Appc-Listener-%d").build();
79         executor.setThreadFactory(threadFactory);
80     }
81
82     @Override
83     public void start() {
84         LOG.info("Starting DMaaP Controller.");
85         for (ListenerProperties props : listeners.keySet()) {
86             try {
87                 if (props.isDisabled()) {
88                     LOG.warn(String.format("The listener %s is disabled and will not be run", props.getPrefix()));
89                 } else {
90                     Listener l = props.getListenerClass().getConstructor(ListenerProperties.class).newInstance(props);
91                     l.setListenerId(props.getPrefix());
92                     listeners.put(props, l);
93                     executor.execute(l);
94                 }
95             } catch (Exception e) {
96                 e.printStackTrace();
97                 LOG.error(String.format("Exception while starting listener %s.", props), e);
98             }
99         }
100     }
101
102     @Override
103     public void stop(boolean stopNow) {
104         LOG.info("Stopping DMaaP Controller.");
105         Iterator<Listener> itr = listeners.values().iterator();
106         while (itr.hasNext()) {
107             Listener l = itr.next();
108             if (stopNow) {
109                 l.stopNow();
110             } else {
111                 l.stop();
112             }
113             itr.remove();
114         }
115         executor.shutdown();
116         try {
117             executor.awaitTermination(10, TimeUnit.SECONDS);
118         } catch (InterruptedException e) {
119         }
120     }
121
122     @Override
123     public Map<ListenerProperties, Listener> getListeners() {
124         return listeners;
125     }
126 }