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