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