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