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