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