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