Initial code drop of netconf protocol implementation
[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
25     private final YangStoreService yangStoreService;
26     private final ConfigRegistryJMXClient jmxClient;
27
28     private static final Logger logger = LoggerFactory.getLogger(NetconfOperationServiceFactoryImpl.class);
29
30     public NetconfOperationServiceFactoryImpl(YangStoreService yangStoreService) {
31         this(yangStoreService, ManagementFactory.getPlatformMBeanServer());
32     }
33
34     public NetconfOperationServiceFactoryImpl(YangStoreService yangStoreService, MBeanServer mBeanServer) {
35         this.yangStoreService = yangStoreService;
36
37         // Config registry might not be present yet, but will be eventually
38         while(true) {
39
40             final ConfigRegistryJMXClient configRegistryJMXClient;
41             try {
42                 configRegistryJMXClient = new ConfigRegistryJMXClient(mBeanServer);
43             } catch (IllegalStateException e) {
44                 logger.debug("Jmx client could not be created, reattempting");
45                 try {
46                     Thread.sleep(ATTEMPT_TIMEOUT_MS);
47                 } catch (InterruptedException e1) {
48                     throw new RuntimeException(e1);
49                 }
50                 continue;
51             }
52
53             jmxClient = configRegistryJMXClient;
54             break;
55         }
56     }
57
58     @Override
59     public NetconfOperationServiceImpl createService(long netconfSessionId, String netconfSessionIdForReporting) {
60         try {
61             return new NetconfOperationServiceImpl(yangStoreService, jmxClient, netconfSessionIdForReporting);
62         } catch (YangStoreException e) {
63             throw new IllegalStateException(e);
64         }
65     }
66 }