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