5f9d1d05e1e8f9d7dc738adf9d2cd60169bbc440
[appc.git] /
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.onap.appc.Constants;
28 import org.onap.appc.adapter.ansible.impl.AnsibleAdapterImpl;
29 import org.onap.appc.configuration.Configuration;
30 import org.onap.appc.configuration.ConfigurationFactory;
31 import org.onap.appc.i18n.Msg;
32 import org.osgi.framework.BundleActivator;
33 import org.osgi.framework.BundleContext;
34 import org.osgi.framework.ServiceRegistration;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37
38 /**
39  * This activator is used to initialize and terminate an instance of AnsibleAdapter class
40  */
41 public class AnsibleActivator implements BundleActivator {
42
43     private static final String ANSIBLE_ADAPTER = "Ansible Adapter";
44     private static final String BUNDLE_NAME = "APPC Ansible Adapter";
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 final EELFLogger logger = EELFManager.getInstance().getLogger(AnsibleActivator.class);
59
60     /**
61      * The configuration object used to configure this bundle
62      */
63     private final Configuration configuration = ConfigurationFactory.getConfiguration();
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 The execution context of the bundle being started.
73      * @throws java.lang.Exception If this method throws an exception, this bundle is marked as stopped and the
74      *         Framework will remove this bundle's listeners, unregister all services registered
75      *         by this bundle, and release all services used by this bundle.
76      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
77      */
78     @Override
79     public void start(BundleContext context) throws Exception {
80
81         logger.info("Starting bundle " + BUNDLE_NAME);
82         String appName = "APPC: ";
83         logger.info(Msg.COMPONENT_INITIALIZING, appName, ANSIBLE_ADAPTER);
84         adapter = new AnsibleAdapterImpl();
85
86         if (registration == null) {
87             logger.info(Msg.REGISTERING_SERVICE, appName, adapter.getAdapterName(),
88                     AnsibleAdapter.class.getSimpleName());
89             registration = context.registerService(AnsibleAdapter.class, adapter, null);
90         }
91
92         logger.info(Msg.COMPONENT_INITIALIZED, appName, ANSIBLE_ADAPTER);
93     }
94
95     /**
96      * Called when this bundle is stopped so the Framework can perform the bundle-specific activities necessary to stop
97      * the bundle. In general, this method should undo the work that the BundleActivator.start method started. There
98      * should be no active threads that were started by this bundle when this bundle returns. A stopped bundle must not
99      * call any Framework objects.
100      * <p>
101      * This method must complete and return to its caller in a timely manner.
102      * </p>
103      *
104      * @param context The execution context of the bundle being stopped.
105      * @throws java.lang.Exception If this method throws an exception, the bundle is still marked as stopped, and the
106      *         Framework will remove the bundle's listeners, unregister all services registered
107      *         by the bundle, and release all services used by the bundle.
108      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
109      */
110     @Override
111     public void stop(BundleContext context) throws Exception {
112         logger.info("Stopping bundle " + BUNDLE_NAME);
113
114         if (registration != null) {
115             String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
116             logger.info(Msg.COMPONENT_TERMINATING, appName, ANSIBLE_ADAPTER);
117             logger.info(Msg.UNREGISTERING_SERVICE, appName, adapter.getAdapterName());
118             registration.unregister();
119             registration = null;
120             logger.info(Msg.COMPONENT_TERMINATED, appName, ANSIBLE_ADAPTER);
121         }
122     }
123 }