Remove all powermock(ito) usages in appc-dg-util
[appc.git] / appc-dg-util / appc-dg-util-bundle / src / main / java / org / onap / appc / dg / util / impl / AAIServiceFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018 Nokia
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.appc.dg.util.impl;
22
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25 import com.att.eelf.i18n.EELFResourceManager;
26 import org.onap.appc.i18n.Msg;
27 import org.onap.ccsdk.sli.adaptors.aai.AAIService;
28 import org.osgi.framework.Bundle;
29 import org.osgi.framework.BundleContext;
30 import org.osgi.framework.FrameworkUtil;
31 import org.osgi.framework.ServiceReference;
32
33 public class AAIServiceFactory {
34
35     private static final EELFLogger logger = EELFManager.getInstance().getLogger(AAIServiceFactory.class);
36
37     private FrameworkUtilWrapper frameworkUtilWrapper = new FrameworkUtilWrapper();
38
39     public AAIService getAAIService() {
40         BundleContext bctx = frameworkUtilWrapper.getBundle(AAIService.class).getBundleContext();
41         // Get AAIadapter reference
42         ServiceReference sref = bctx.getServiceReference(AAIService.class.getName());
43         if (sref != null) {
44             logger.info("AAIService from bundlecontext");
45             return (AAIService) bctx.getService(sref);
46
47         } else {
48             logger.info("AAIService error from bundlecontext");
49             logger.error(EELFResourceManager.format(Msg.AAI_CONNECTION_FAILED, "AAIService"));
50         }
51         return null;
52     }
53
54     static class FrameworkUtilWrapper {
55
56         Bundle getBundle(Class<?> clazz) {
57             return FrameworkUtil.getBundle(clazz);
58         }
59     }
60 }