Add junit coverage to TimedOutException class
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / main / java / org / onap / appc / listener / AppcEventListenerActivator.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;
26
27 import org.onap.appc.configuration.Configuration;
28 import org.onap.appc.configuration.ConfigurationFactory;
29 import org.onap.appc.listener.impl.ControllerImpl;
30
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
33 import org.osgi.framework.BundleActivator;
34 import org.osgi.framework.BundleContext;
35 import org.osgi.framework.ServiceRegistration;
36
37 import java.util.HashSet;
38 import java.util.Properties;
39 import java.util.Set;
40
41 /**
42  * This activator is used to initialize and terminate the dmaap listener controller and pool(s)
43  * <p>
44  * The DMaaP listener is responsible for listening to a topic on the Universal Event Bus and reading in messages that
45  * conform to the DCAE message format for APPC. These messages will then be parsed and passed along to the APPC Provider
46  * to take action on. The listener will also send messages out on DMaaP during critical phases. The messages sent out
47  * will have a status of:
48  * <ul>
49  * <li><i>PENDING</i> - The listener has read the message off of DMaaP and has put it in the queue to be processed</li>
50  * <li><i>ACTIVE</i> - The listener has begun actually processing the request and is waiting on the appc provider to
51  * complete the request</li>
52  * <li><i>SUCCESS</i> or <i>FAILURE</i> - The listener has gotten a response back from the appc provider. If it is a
53  * FAILURE, a message should also be included</li>
54  * </ul>
55  * </p>
56  * <p>
57  * Activation of the bundle will provision 1 controller that in turn will provision 1 (or in the future more) listener
58  * to interact with DMaaP. Each listener will have a queue of messages read off of DMaaP and a thread pool of workers to
59  * process them. This worker is responsible for contacting appc provider to perform the action
60  * </p>
61  * <p>
62  * When the bundle is deactivated, the stopNow() method is called and the thread pool is emptied and all remaining jobs
63  * are orphaned. Alternatively stop() could be called which would allow all remaining jobs in the queue to complete at
64  * the cost of longer run time.
65  * </p>
66  * 
67  * @since Aug 30, 2015
68  * @version $Id$
69  */
70 public class AppcEventListenerActivator implements BundleActivator {
71
72     /**
73      * The bundle registration
74      */
75     private ServiceRegistration registration = null;
76
77     /**
78      * The configuration object
79      */
80     private Configuration configuration;
81
82     /**
83      * The reference to the actual implementation object that implements the services
84      */
85     private Controller adapter;
86
87     /**
88      * The logger to be used
89      */
90     private final EELFLogger LOG = EELFManager.getInstance().getLogger(AppcEventListenerActivator.class);
91
92     /**
93      * Called when this bundle is started so the Framework can perform the bundle-specific activities necessary to start
94      * this bundle. This method can be used to register services or to allocate any resources that this bundle needs.
95      * <p>
96      * This method must complete and return to its caller in a timely manner.
97      * </p>
98      *
99      * @param ctx
100      *            The execution context of the bundle being started.
101      * @throws java.lang.Exception
102      *             If this method throws an exception, this bundle is marked as stopped and the Framework will remove
103      *             this bundle's listeners, unregister all services registered by this bundle, and release all services
104      *             used by this bundle.
105      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
106      */
107     @Override
108     public void start(BundleContext ctx) throws Exception {
109         LOG.info("Starting Bundle " + getName());
110
111         Properties props = getProperties();
112
113         Set<ListenerProperties> listeners = new HashSet<>();
114
115         // Configure event listener for the demo use case
116         ListenerProperties demoProps = new ListenerProperties("appc.demo", props);
117         // Only add the listener if properties are set
118         if (!demoProps.getProperties().isEmpty()) {
119             demoProps.setListenerClass(org.onap.appc.listener.demo.impl.ListenerImpl.class);
120             listeners.add(demoProps);
121         }
122
123
124         ListenerProperties clLCMProps = new ListenerProperties("appc.LCM", props);
125         // Only add the listener if properties are set
126         if (!clLCMProps.getProperties().isEmpty()) {
127             clLCMProps.setListenerClass(org.onap.appc.listener.LCM.impl.ListenerImpl.class);
128             listeners.add(clLCMProps);
129         }
130
131
132         // Configure the OAM properties
133         String oamPropKeyPrefix = "appc.OAM";
134         ListenerProperties oamProps  = new ListenerProperties(oamPropKeyPrefix, props);
135         // Only add the listener if properties are set and enabled is true
136         if (!oamProps.getProperties().isEmpty() && isAppcOamPropsListenerEnabled(oamProps)) {
137             oamProps.setListenerClass(org.onap.appc.listener.LCM.impl.ListenerImpl.class);
138             listeners.add(oamProps);
139         } else {
140             LOG.warn(String.format("The listener %s is disabled and will not be run", oamPropKeyPrefix));
141         }
142
143         adapter = new ControllerImpl(listeners);
144         if (ctx != null && registration == null) {
145             LOG.info("Registering service DMaaP Controller");
146             registration = ctx.registerService(Controller.class, adapter, null);
147         }
148         adapter.start();
149
150         LOG.info("DMaaP Listener started successfully");
151     }
152
153     /**
154      * Called when this bundle is stopped so the Framework can perform the bundle-specific activities necessary to stop
155      * the bundle. In general, this method should undo the work that the BundleActivator.start method started. There
156      * should be no active threads that were started by this bundle when this bundle returns. A stopped bundle must not
157      * call any Framework objects.
158      * <p>
159      * This method must complete and return to its caller in a timely manner.
160      * </p>
161      *
162      * @param ctx
163      *            The execution context of the bundle being stopped.
164      * @throws java.lang.Exception
165      *             If this method throws an exception, the bundle is still marked as stopped, and the Framework will
166      *             remove the bundle's listeners, unregister all services registered by the bundle, and release all
167      *             services used by the bundle. *
168      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
169      */
170     @Override
171     public void stop(BundleContext ctx) throws Exception {
172         boolean stopNow = true;
173         LOG.info("Stopping DMaaP Listener. StopNow=" + stopNow);
174         adapter.stop(stopNow);
175         if (registration != null) {
176             registration.unregister();
177             registration = null;
178         }
179         LOG.info("DMaaP Listener stopped successfully");
180     }
181
182     public String getName() {
183         return "DMaaP Listener";
184     }
185
186     /**
187      * Check if AppcOam props disable listener or not
188      *
189      * @param listenerProperties of ListenerProperties objext
190      * @return false only if AppcOam disabled key is defined and equal to true. Otherwise, return true.
191      */
192     private boolean isAppcOamPropsListenerEnabled(ListenerProperties listenerProperties) {
193         final Properties appcOamProperties = listenerProperties.getProperties();
194
195         boolean result;
196         if (appcOamProperties == null) {
197             result = true;
198         } else {
199             result = !Boolean.parseBoolean(appcOamProperties.getProperty(
200                     ListenerProperties.KEYS.DISABLED.getPropertySuffix()));
201         }
202
203         return result;
204     }
205     
206     /**
207      * Get properties from configuration
208      */
209     Properties getProperties() {
210         configuration = ConfigurationFactory.getConfiguration();
211         return configuration.getProperties();
212     }
213     
214 }