[feature/APPC-6]
[appc.git] / appc-adapters / appc-ansible-adapter / appc-ansible-adapter-bundle / src / main / java / org / openecomp / appc / adapter / ansible / AnsibleActivator.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.adapter.ansible;
24
25 import org.osgi.framework.BundleActivator;
26 import org.osgi.framework.BundleContext;
27 import org.osgi.framework.ServiceRegistration;
28
29 import org.openecomp.appc.Constants;
30 import org.openecomp.appc.adapter.ansible.impl.AnsibleAdapterImpl;
31 import org.openecomp.appc.configuration.Configuration;
32 import org.openecomp.appc.configuration.ConfigurationFactory;
33 import org.openecomp.appc.i18n.Msg;
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
36
37 /**
38  * This activator is used to initialize and terminate an instance of AnsibleAdapter class
39  * 
40  * Author : Ashwin Sridharan
41  * Date   : Oct 2016
42  */
43 public class AnsibleActivator implements BundleActivator {
44
45     /**
46      * The bundle registration
47      */
48     private ServiceRegistration registration = null;
49
50     /**
51      * The reference to the actual implementation object that implements the services
52      */
53     private AnsibleAdapter adapter;
54
55     /**
56      * The logger to be used
57      */
58     private static final EELFLogger logger = EELFManager.getInstance().getLogger(AnsibleActivator.class);
59
60     /**
61      * The configuration object used to configure this bundle
62      */
63     private Configuration configuration;
64
65     /**
66      * Called when this bundle is started so the Framework can perform the bundle-specific activities necessary to start
67      * this bundle. This method can be used to register services or to allocate any resources that this bundle needs.
68      * <p>
69      * This method must complete and return to its caller in a timely manner.
70      * </p>
71      * 
72      * @param context
73      *            The execution context of the bundle being started.
74      * @throws java.lang.Exception
75      *             If this method throws an exception, this bundle is marked as stopped and the Framework will remove
76      *             this bundle's listeners, unregister all services registered by this bundle, and release all services
77      *             used by this bundle.
78      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
79      */
80     @Override
81     public void start(BundleContext context) throws Exception {
82
83         logger.info("Starting bundle " + getName());
84         String appName = "APPC: ";
85         logger.info(Msg.COMPONENT_INITIALIZING, appName, "Ansible Adapter");
86         adapter = new AnsibleAdapterImpl();
87         
88         if (registration == null) {
89             logger.info(Msg.REGISTERING_SERVICE, appName, adapter.getAdapterName(),
90                         AnsibleAdapter.class.getSimpleName());
91             registration = context.registerService(AnsibleAdapter.class, adapter, null);
92         }
93
94         logger.info(Msg.COMPONENT_INITIALIZED, appName, "Ansible adapter");
95     }
96
97     /**
98      * Called when this bundle is stopped so the Framework can perform the bundle-specific activities necessary to stop
99      * the bundle. In general, this method should undo the work that the BundleActivator.start method started. There
100      * should be no active threads that were started by this bundle when this bundle returns. A stopped bundle must not
101      * call any Framework objects.
102      * <p>
103      * This method must complete and return to its caller in a timely manner.
104      * </p>
105      * 
106      * @param context
107      *            The execution context of the bundle being stopped.
108      * @throws java.lang.Exception
109      *             If this method throws an exception, the bundle is still marked as stopped, and the Framework will
110      *             remove the bundle's listeners, unregister all services registered by the bundle, and release all
111      *             services used by the bundle. *
112      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
113      */
114     @Override
115     public void stop(BundleContext context) throws Exception {
116         logger.info("Stopping bundle " + getName());
117
118         if (registration != null) {
119             String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
120             logger.info(Msg.COMPONENT_TERMINATING, appName, "Ansible adapter");
121             logger.info(Msg.UNREGISTERING_SERVICE, appName, adapter.getAdapterName());
122             registration.unregister();
123             registration = null;
124             logger.info(Msg.COMPONENT_TERMINATED, appName, "Ansible adapter");
125         }
126     }
127
128     public String getName() {
129         return "APPC Ansible Adapter";
130     }
131
132 }