ChefActivator JUnits
[appc.git] / appc-adapters / appc-chef-adapter / appc-chef-adapter-bundle / src / main / java / org / onap / appc / adapter / chef / ChefActivator.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.chef;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.onap.appc.Constants;
30 import org.onap.appc.adapter.chef.impl.ChefAdapterImpl;
31 import org.onap.appc.configuration.Configuration;
32 import org.onap.appc.configuration.ConfigurationFactory;
33 import org.onap.appc.i18n.Msg;
34 import org.osgi.framework.BundleActivator;
35 import org.osgi.framework.BundleContext;
36 import org.osgi.framework.ServiceRegistration;
37
38 /**
39  * This activator is used to initialize and terminate the connection pool to one or more providers.
40  * <p>
41  * The CDP abstraction layer supports multiple types of providers, with each provider supporting multiple tenants. The
42  * "connection" to a specific tenant on a specific provider is represented by a "context" object. These context objects
43  * are authenticated to a specific tenant on the provider, but can be reused from one request to another. Contexts are
44  * slow to set up and are resource intensive, so they are cached. However, the contexts for a specific tenant on a
45  * specific provider must be cached separately.
46  * </p>
47  * <p>
48  * Activation of the bundle creates an empty cache which is organized first by provider type, then by tenant name, with
49  * the contents being an empty pool of contexts for that provider/tenant combination. The pool is created on first use,
50  * and retained for as long as the bundle is active.
51  * </p>
52  * <p>
53  * When the bundle is deactivated, the cache is torn down with all contexts being closed.
54  * </p>
55  */
56 public class ChefActivator implements BundleActivator {
57
58     private static final EELFLogger logger = EELFManager.getInstance().getLogger(ChefActivator.class);
59     private static final String NAME = "APPC Chef Adapter";
60     private static final String ADAPTER_NAME = "CHEF adapter";
61
62     /**
63      * The bundle registration
64      */
65     private ServiceRegistration registration = null;
66
67     /**
68      * The reference to the actual implementation object that implements the services
69      */
70     private ChefAdapter adapter;
71
72     /**
73      * The configuration object used to configure this bundle
74      */
75     private Configuration configuration = ConfigurationFactory.getConfiguration();
76
77     /**
78      * Called when this bundle is started so the Framework can perform the bundle-specific activities necessary to start
79      * this bundle. This method can be used to register services or to allocate any resources that this bundle needs.
80      * <p>
81      * This method must complete and return to its caller in a timely manner.
82      * </p>
83      *
84      * @param context
85      *            The execution context of the bundle being started.
86      * @throws java.lang.Exception
87      *             If this method throws an exception, this bundle is marked as stopped and the Framework will remove
88      *             this bundle's listeners, unregister all services registered by this bundle, and release all services
89      *             used by this bundle.
90      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
91      */
92     @Override
93     public void start(BundleContext context) throws Exception {
94         logger.info("Starting bundle " + NAME);
95
96         String appName = readApplicationNameFromProperties();
97         logger.info(Msg.COMPONENT_INITIALIZING, appName, ADAPTER_NAME);
98         adapter = new ChefAdapterImpl();
99         if (registration == null) {
100             logger.info(Msg.REGISTERING_SERVICE, appName, ADAPTER_NAME,
101                 ChefAdapter.class.getSimpleName());
102             registration = context.registerService(ChefAdapter.class, adapter, null);
103         }
104
105         logger.info(Msg.COMPONENT_INITIALIZED, appName, ADAPTER_NAME);
106     }
107
108     /**
109      * Called when this bundle is stopped so the Framework can perform the bundle-specific activities necessary to stop
110      * the bundle. In general, this method should undo the work that the BundleActivator.start method started. There
111      * should be no active threads that were started by this bundle when this bundle returns. A stopped bundle must not
112      * call any Framework objects.
113      * <p>
114      * This method must complete and return to its caller in a timely manner.
115      * </p>
116      *
117      * @param context
118      *            The execution context of the bundle being stopped.
119      * @throws java.lang.Exception
120      *             If this method throws an exception, the bundle is still marked as stopped, and the Framework will
121      *             remove the bundle's listeners, unregister all services registered by the bundle, and release all
122      *             services used by the bundle. *
123      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
124      */
125     @Override
126     public void stop(BundleContext context) throws Exception {
127         logger.info("Stopping bundle " + NAME);
128
129         if (registration != null) {
130             String appName = readApplicationNameFromProperties();
131             logger.info(Msg.COMPONENT_TERMINATING, appName, ADAPTER_NAME);
132             logger.info(Msg.UNREGISTERING_SERVICE, appName, ADAPTER_NAME);
133             registration.unregister();
134             registration = null;
135             logger.info(Msg.COMPONENT_TERMINATED, appName, ADAPTER_NAME);
136         }
137     }
138
139     private String readApplicationNameFromProperties() {
140         return configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
141     }
142 }