Applying license changes to all files
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / main / java / org / openecomp / appc / listener / impl / ControllerImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.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.openecomp.appc.listener.Controller;
37 import org.openecomp.appc.listener.Listener;
38 import org.openecomp.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             }
73         }
74
75         LISTENER_COUNT = properties.size();
76
77         executor = new ThreadPoolExecutor(LISTENER_COUNT, LISTENER_COUNT, 1, TimeUnit.SECONDS,
78             new ArrayBlockingQueue<Runnable>(LISTENER_COUNT));
79
80         // Custom Named thread factory
81         BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("Appc-Listener-%d").build();
82         executor.setThreadFactory(threadFactory);
83     }
84
85     @Override
86     public void start() {
87         LOG.info("Starting DMaaP Controller.");
88         for (ListenerProperties props : listeners.keySet()) {
89             try {
90                 if (props.isDisabled()) {
91                     LOG.warn(String.format("The listener %s is disabled and will not be run", props.getPrefix()));
92                 } else {
93                     Listener l = props.getListenerClass().getConstructor(ListenerProperties.class).newInstance(props);
94                     l.setListenerId(props.getPrefix());
95                     listeners.put(props, l);
96                     executor.execute(l);
97                 }
98             } catch (Exception e) {
99                 e.printStackTrace();
100                 LOG.error(String.format("Exception while starting listener %s.", props), e);
101             }
102         }
103     }
104
105     @Override
106     public void stop(boolean stopNow) {
107         LOG.info("Stopping DMaaP Controller.");
108         Iterator<Listener> itr = listeners.values().iterator();
109         while (itr.hasNext()) {
110             Listener l = itr.next();
111             if (stopNow) {
112                 l.stopNow();
113             } else {
114                 l.stop();
115             }
116             itr.remove();
117         }
118         executor.shutdown();
119         try {
120             executor.awaitTermination(10, TimeUnit.SECONDS);
121         } catch (InterruptedException e) {
122         }
123     }
124
125     @Override
126     public Map<ListenerProperties, Listener> getListeners() {
127         return listeners;
128     }
129 }