Merge of new rebased code
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / main / java / org / openecomp / appc / listener / AppcEventListenerActivator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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  */
21
22 package org.openecomp.appc.listener;
23
24 import org.openecomp.appc.configuration.Configuration;
25 import org.openecomp.appc.configuration.ConfigurationFactory;
26 import org.openecomp.appc.listener.impl.ControllerImpl;
27
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import org.osgi.framework.BundleActivator;
31 import org.osgi.framework.BundleContext;
32 import org.osgi.framework.ServiceRegistration;
33
34 import java.util.HashSet;
35 import java.util.Properties;
36 import java.util.Set;
37
38 /**
39  * This activator is used to initialize and terminate the dmaap listener controller and pool(s)
40  * <p>
41  * The DMaaP listener is responsible for listening to a topic on the Universal Event Bus and reading in messages that
42  * conform to the DCAE message format for APPC. These messages will then be parsed and passed along to the APPC Provider
43  * to take action on. The listener will also send messages out on DMaaP during critical phases. The messages sent out will
44  * have a status of:
45  * <ul>
46  * <li><i>PENDING</i> - The listener has read the message off of DMaaP and has put it in the queue to be processed</li>
47  * <li><i>ACTIVE</i> - The listener has begun actually processing the request and is waiting on the appc provider to
48  * complete the request</li>
49  * <li><i>SUCCESS</i> or <i>FAILURE</i> - The listener has gotten a response back from the appc provider. If it is a
50  * FAILURE, a message should also be included</li>
51  * </ul>
52  * </p>
53  * <p>
54  * Activation of the bundle will provision 1 controller that in turn will provision 1 (or in the future more) listener
55  * to interact with DMaaP. Each listener will have a queue of messages read off of DMaaP and a thread pool of workers to
56  * process them. This worker is responsible for contacting appc provider to perform the action
57  * </p>
58  * <p>
59  * When the bundle is deactivated, the stopNow() method is called and the thread pool is emptied and all remaining jobs
60  * are orphaned. Alternatively stop() could be called which would allow all remaining jobs in the queue to complete at
61  * the cost of longer run time.
62  * </p>
63  * 
64  * @since Aug 30, 2015
65  * @version $Id$
66  */
67 public class AppcEventListenerActivator implements BundleActivator {
68
69     /**
70      * The bundle registration
71      */
72     private ServiceRegistration registration = null;
73
74     /**
75      * The configuration object
76      */
77     private Configuration configuration;
78
79     /**
80      * The bundle context
81      */
82     private static BundleContext context;
83
84     /**
85      * The reference to the actual implementation object that implements the services
86      */
87     private Controller adapter;
88
89     /**
90      * The logger to be used
91      */
92     private final EELFLogger LOG = EELFManager.getInstance().getLogger(AppcEventListenerActivator.class);
93
94     /**
95      * Called when this bundle is started so the Framework can perform the bundle-specific activities necessary to start
96      * this bundle. This method can be used to register services or to allocate any resources that this bundle needs.
97      * <p>
98      * This method must complete and return to its caller in a timely manner.
99      * </p>
100      *
101      * @param ctx
102      *            The execution context of the bundle being started.
103      * @throws java.lang.Exception
104      *             If this method throws an exception, this bundle is marked as stopped and the Framework will remove
105      *             this bundle's listeners, unregister all services registered by this bundle, and release all services
106      *             used by this bundle.
107      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
108      */
109     @Override
110     public void start(BundleContext ctx) throws Exception {
111         LOG.info("Starting Bundle " + getName());
112
113         context = ctx;
114
115         configuration = ConfigurationFactory.getConfiguration();
116
117         Properties props = configuration.getProperties();
118
119         Set<ListenerProperties> listeners = new HashSet<ListenerProperties>();
120
121         // Configure event listener for the demo use case
122         ListenerProperties demoProps = new ListenerProperties("appc.demo", props);
123         demoProps.setListenerClass(org.openecomp.appc.listener.demo.impl.ListenerImpl.class);
124         listeners.add(demoProps);
125
126         // ===========================================================================                                                                          
127
128         adapter = new ControllerImpl(listeners);
129         if (ctx != null && registration == null) {
130             LOG.info("Registering service DMaaP Controller");
131             registration = ctx.registerService(Controller.class, adapter, null);
132         }
133         adapter.start();
134
135         LOG.info("DMaaP Listener started successfully");
136     }
137
138     /**
139      * Called when this bundle is stopped so the Framework can perform the bundle-specific activities necessary to stop
140      * the bundle. In general, this method should undo the work that the BundleActivator.start method started. There
141      * should be no active threads that were started by this bundle when this bundle returns. A stopped bundle must not
142      * call any Framework objects.
143      * <p>
144      * This method must complete and return to its caller in a timely manner.
145      * </p>
146      *
147      * @param ctx
148      *            The execution context of the bundle being stopped.
149      * @throws java.lang.Exception
150      *             If this method throws an exception, the bundle is still marked as stopped, and the Framework will
151      *             remove the bundle's listeners, unregister all services registered by the bundle, and release all
152      *             services used by the bundle. *
153      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
154      */
155     @Override
156     public void stop(BundleContext ctx) throws Exception {
157         boolean stopNow = true;
158         LOG.info("Stopping DMaaP Listener. StopNow=" + stopNow);
159         adapter.stop(stopNow);
160         if (registration != null) {
161             registration.unregister();
162             registration = null;
163         }
164         LOG.info("DMaaP Listener stopped successfully");
165     }
166
167     public String getName() {
168         return "DMaaP Listener";
169     }
170
171 }