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