Merge "Address comments from blueprint patches"
[netconf.git] / netconf / netconf-util / src / main / java / org / opendaylight / netconf / util / osgi / NetconfConfigUtil.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.netconf.util.osgi;
10
11 import java.util.Collection;
12 import java.util.Optional;
13 import org.osgi.framework.BundleContext;
14 import org.osgi.framework.InvalidSyntaxException;
15 import org.osgi.framework.ServiceReference;
16 import org.osgi.service.cm.ManagedService;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public final class NetconfConfigUtil {
21     private static final Logger LOG = LoggerFactory.getLogger(NetconfConfigUtil.class);
22
23     private NetconfConfigUtil() {
24     }
25
26     public static Optional<NetconfConfiguration> getNetconfConfigurationService(BundleContext bundleContext) {
27         final Collection<ServiceReference<ManagedService>> serviceReferences;
28         try {
29             serviceReferences = bundleContext.getServiceReferences(ManagedService.class, null);
30             for (final ServiceReference<ManagedService> serviceReference : serviceReferences) {
31                 ManagedService service = bundleContext.getService(serviceReference);
32                 if (service instanceof NetconfConfiguration){
33                     return Optional.of((NetconfConfiguration) service);
34                 }
35             }
36         } catch (InvalidSyntaxException e) {
37             LOG.error("Unable to retrieve references for ManagedService: {}", e);
38         }
39         LOG.error("Unable to retrieve NetconfConfiguration service. Not found. Bundle netconf-util probably failed to load.");
40         return Optional.empty();
41     }
42 }