Add junit coverage to TimedOutException class
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / main / java / org / onap / appc / listener / impl / ControllerImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.listener.impl;
26
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.Map;
30 import java.util.Set;
31 import java.util.concurrent.ArrayBlockingQueue;
32 import java.util.concurrent.ThreadPoolExecutor;
33 import java.util.concurrent.TimeUnit;
34
35 import org.apache.commons.lang3.concurrent.BasicThreadFactory;
36 import org.onap.appc.listener.Controller;
37 import org.onap.appc.listener.Listener;
38 import org.onap.appc.listener.ListenerProperties;
39
40 import com.att.eelf.configuration.EELFLogger;
41 import com.att.eelf.configuration.EELFManager;
42
43
44 /**
45  * A common implementation of a controller. This controller should not need to be modified to implement new listeners
46  *
47  */
48 public class ControllerImpl implements Controller {
49
50     private final EELFLogger LOG = EELFManager.getInstance().getLogger(ControllerImpl.class);
51
52     private int LISTENER_COUNT = 1;
53
54     private Map<ListenerProperties, Listener> listeners = null;
55
56     private ThreadPoolExecutor executor;
57
58     /**
59      * Creates a Controller with the set of listener properties which will be used to start listener threads.
60      *
61      * @param properties
62      *            A non null Set of ListenerProperties
63      */
64     public ControllerImpl(Set<ListenerProperties> properties) {
65         listeners = new HashMap<ListenerProperties, Listener>();
66         for (ListenerProperties props : properties) {
67             if (props.getClass() != null) {
68                 listeners.put(props, null);
69             } else {
70                 LOG.error(String.format(
71                     "The ListenerProperties %s has no Listener class associated with it and will not run.", props));
72                 properties.remove(props);
73             }
74         }
75
76         LISTENER_COUNT = properties.size();
77
78         // Only create executor if listeners are configured
79         if (LISTENER_COUNT > 0) {
80         executor = new ThreadPoolExecutor(LISTENER_COUNT, LISTENER_COUNT, 1, TimeUnit.SECONDS,
81             new ArrayBlockingQueue<Runnable>(LISTENER_COUNT));
82
83         // Custom Named thread factory
84         BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("Appc-Listener-%d").build();
85         executor.setThreadFactory(threadFactory);
86         }
87     }
88
89     @Override
90     public void start() {
91         LOG.info("Starting DMaaP Controller.");
92         for (ListenerProperties props : listeners.keySet()) {
93             try {
94                 if (props.isDisabled()) {
95                     LOG.warn(String.format("The listener %s is disabled and will not be run", props.getPrefix()));
96                 } else {
97                     Listener l = props.getListenerClass().getConstructor(ListenerProperties.class).newInstance(props);
98                     l.setListenerId(props.getPrefix());
99                     listeners.put(props, l);
100                     executor.execute(l);
101                 }
102             } catch (Exception e) {
103                 LOG.error(String.format("Exception while starting listener %s.", props), e);
104             }
105         }
106     }
107
108     @Override
109     public void stop(boolean stopNow) {
110         LOG.info("Stopping DMaaP Controller.");
111         Iterator<Listener> itr = listeners.values().iterator();
112         while (itr.hasNext()) {
113             Listener l = itr.next();
114             if (stopNow && l != null) {
115                 l.stopNow();
116             } else if(l!=null){
117                 l.stop();
118             }
119             itr.remove();
120         }
121         // disable new tasks from being submitted
122         if(executor != null) {
123             executor.shutdown();
124             int timeout=300;
125             try {
126                 if (!executor.awaitTermination(timeout, TimeUnit.SECONDS)) {
127                     LOG.error("Not all tasks completed execution after " + timeout + " seconds. " +
128                             "Attempting to stop all actively executing tasks.");
129                     executor.shutdownNow();
130                 }
131                 if (!executor.awaitTermination(timeout, TimeUnit.SECONDS)) {
132                     LOG.error("Could not terminate all tasks after " + (timeout*2) + " seconds.");
133                 }
134             } catch (InterruptedException e) {
135                 executor.shutdownNow();
136                 Thread.currentThread().interrupt();
137             }
138         }
139     }
140
141     @Override
142     public Map<ListenerProperties, Listener> getListeners() {
143         return listeners;
144     }
145 }