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