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