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