Merge "Migrate NetconfHelloMessage's use of Optional"
[netconf.git] / netconf / netconf-monitoring / src / main / java / org / opendaylight / netconf / monitoring / osgi / NetconfMonitoringActivator.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.monitoring.osgi;
10
11 import java.util.Collections;
12 import java.util.Set;
13 import org.opendaylight.netconf.api.capability.Capability;
14 import org.opendaylight.netconf.api.monitoring.CapabilityListener;
15 import org.opendaylight.netconf.mapping.api.NetconfOperationService;
16 import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
17 import org.osgi.framework.BundleActivator;
18 import org.osgi.framework.BundleContext;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class NetconfMonitoringActivator implements BundleActivator {
23
24     private static final Logger LOG = LoggerFactory.getLogger(NetconfMonitoringActivator.class);
25
26     private NetconfMonitoringServiceTracker monitor;
27
28     @Override
29     public void start(final BundleContext context) {
30         monitor = new NetconfMonitoringServiceTracker(context);
31         monitor.open();
32     }
33
34     @SuppressWarnings("checkstyle:IllegalCatch")
35     @Override
36     public void stop(final BundleContext context) {
37         if (monitor != null) {
38             try {
39                 monitor.close();
40             } catch (final Exception e) {
41                 LOG.warn("Ignoring exception while closing {}", monitor, e);
42             }
43         }
44     }
45
46     public static class NetconfMonitoringOperationServiceFactory implements NetconfOperationServiceFactory,
47             AutoCloseable {
48
49         private final NetconfMonitoringOperationService operationService;
50
51         private static final AutoCloseable AUTO_CLOSEABLE = () -> {
52             // NOOP
53         };
54
55         public NetconfMonitoringOperationServiceFactory(final NetconfMonitoringOperationService operationService) {
56             this.operationService = operationService;
57         }
58
59         @Override
60         public NetconfOperationService createService(final String netconfSessionIdForReporting) {
61             return operationService;
62         }
63
64         @Override
65         public Set<Capability> getCapabilities() {
66             return Collections.emptySet();
67         }
68
69         @Override
70         public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
71             return AUTO_CLOSEABLE;
72         }
73
74         @Override
75         public void close() {}
76     }
77 }