Merge "Fixed typo in SnapshotBackedWriteTransaction class"
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / osgi / NetconfOperationServiceFactoryImpl.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.controller.netconf.confignetconfconnector.osgi;
10
11 import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
12 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 import javax.management.MBeanServer;
17 import java.lang.management.ManagementFactory;
18
19 public class NetconfOperationServiceFactoryImpl implements NetconfOperationServiceFactory {
20
21     public static final int ATTEMPT_TIMEOUT_MS = 1000;
22     private static final int SILENT_ATTEMPTS = 30;
23
24     private final YangStoreService yangStoreService;
25     private final ConfigRegistryJMXClient jmxClient;
26
27     private static final Logger logger = LoggerFactory.getLogger(NetconfOperationServiceFactoryImpl.class);
28
29     public NetconfOperationServiceFactoryImpl(YangStoreService yangStoreService) {
30         this(yangStoreService, ManagementFactory.getPlatformMBeanServer());
31     }
32
33     public NetconfOperationServiceFactoryImpl(YangStoreService yangStoreService, MBeanServer mBeanServer) {
34         this.yangStoreService = yangStoreService;
35
36         ConfigRegistryJMXClient configRegistryJMXClient;
37         int i = 0;
38         // Config registry might not be present yet, but will be eventually
39         while(true) {
40
41             try {
42                 configRegistryJMXClient = new ConfigRegistryJMXClient(mBeanServer);
43                 break;
44             } catch (IllegalStateException e) {
45                 ++i;
46                 if (i > SILENT_ATTEMPTS) {
47                     logger.info("JMX client not created after {} attempts, still trying", i, e);
48                 } else {
49                     logger.debug("JMX client could not be created, reattempting, try {}", i, e);
50                 }
51                 try {
52                     Thread.sleep(ATTEMPT_TIMEOUT_MS);
53                 } catch (InterruptedException e1) {
54                     Thread.currentThread().interrupt();
55                     throw new IllegalStateException("Interrupted while reattempting connection", e1);
56                 }
57             }
58         }
59
60         jmxClient = configRegistryJMXClient;
61         if (i > SILENT_ATTEMPTS) {
62             logger.info("Created JMX client after {} attempts", i);
63         } else {
64             logger.debug("Created JMX client after {} attempts", i);
65         }
66     }
67
68     @Override
69     public NetconfOperationServiceImpl createService(String netconfSessionIdForReporting) {
70         try {
71             return new NetconfOperationServiceImpl(yangStoreService, jmxClient, netconfSessionIdForReporting);
72         } catch (YangStoreException e) {
73             throw new IllegalStateException(e);
74         }
75     }
76 }