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