Sonar fixes in "appc-netconf-adapter-bundle"
[appc.git] / appc-adapters / appc-netconf-adapter / appc-netconf-adapter-bundle / src / main / java / org / onap / appc / adapter / netconf / AppcNetconfAdapterActivator.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.netconf;
26
27 import org.onap.appc.adapter.netconf.internal.NetconfDataAccessServiceImpl;
28 import org.onap.ccsdk.sli.core.dblib.DbLibService;
29 import org.osgi.framework.BundleActivator;
30 import org.osgi.framework.BundleContext;
31 import org.osgi.framework.ServiceReference;
32 import org.osgi.framework.ServiceRegistration;
33
34 public class AppcNetconfAdapterActivator implements BundleActivator {
35
36     /**
37      * The bundle registration
38      */
39     private ServiceRegistration registration = null;
40     private ServiceRegistration reporterRegistration = null;
41     private ServiceRegistration factoryRegistration = null;
42     private ServiceRegistration dbRegistration = null;
43
44     /**
45      * Called when this bundle is started so the Framework can perform the bundle-specific activities necessary to start
46      * this bundle. This method can be used to register services or to allocate any resources that this bundle needs.
47      * <p>
48      * This method must complete and return to its caller in a timely manner.
49      * </p>
50      *
51      * @param context
52      *            The execution context of the bundle being started.
53      * @throws java.lang.Exception
54      *             If this method throws an exception, this bundle is marked as stopped and the Framework will remove
55      *             this bundle's listeners, unregister all services registered by this bundle, and release all services
56      *             used by this bundle.
57      * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
58      */
59     @Override
60     public void start(BundleContext context) throws Exception {
61         if (registration == null) {
62             /*
63             * The reference to the actual implementation object that implements the services
64             */
65             NetconfClientFactory clientFactory = new NetconfClientFactory();                
66             factoryRegistration = context.registerService(NetconfClientFactory.class, clientFactory, null);
67             NetconfDataAccessService dataAccessService = new NetconfDataAccessServiceImpl();
68             //set dblib service
69             ServiceReference sref = context.getServiceReference(DbLibService.class.getName());
70             dataAccessService.setDbLibService((DbLibService)context.getService(sref));
71             ///////////////////////////////////
72             factoryRegistration = context.registerService(NetconfDataAccessService.class, dataAccessService, null);
73         }
74     }
75
76     /**
77      * Called when this bundle is stopped so the Framework can perform the bundle-specific activities necessary to stop
78      * the bundle. In general, this method should undo the work that the BundleActivator.start method started. There
79      * should be no active threads that were started by this bundle when this bundle returns. A stopped bundle must not
80      * call any Framework objects.
81      * <p>
82      * This method must complete and return to its caller in a timely manner.
83      * </p>
84      *
85      * @param context
86      *            The execution context of the bundle being stopped.
87      * @throws java.lang.Exception
88      *             If this method throws an exception, the bundle is still marked as stopped, and the Framework will
89      *             remove the bundle's listeners, unregister all services registered by the bundle, and release all
90      *             services used by the bundle. *
91      * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
92      */
93     @Override
94     public void stop(BundleContext context) throws Exception {
95         if (registration != null) {
96             registration.unregister();
97             registration = null;
98         }
99         if (reporterRegistration != null) {
100             reporterRegistration.unregister();
101             reporterRegistration = null;
102         }
103
104         if (null != factoryRegistration) {
105             factoryRegistration.unregister();
106             factoryRegistration = null;
107         }
108
109         if (dbRegistration != null) {
110             dbRegistration.unregister();
111             dbRegistration = null;
112         }
113     }
114 }