/*
 * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.controller.netconf.confignetconfconnector.osgi;

import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
import org.opendaylight.controller.config.yang.store.api.YangStoreException;
import org.opendaylight.controller.config.yang.store.api.YangStoreService;
import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.management.MBeanServer;
import java.lang.management.ManagementFactory;

public class NetconfOperationServiceFactoryImpl implements NetconfOperationServiceFactory {

    public static final int ATTEMPT_TIMEOUT_MS = 1000;

    private final YangStoreService yangStoreService;
    private final ConfigRegistryJMXClient jmxClient;

    private static final Logger logger = LoggerFactory.getLogger(NetconfOperationServiceFactoryImpl.class);

    public NetconfOperationServiceFactoryImpl(YangStoreService yangStoreService) {
        this(yangStoreService, ManagementFactory.getPlatformMBeanServer());
    }

    public NetconfOperationServiceFactoryImpl(YangStoreService yangStoreService, MBeanServer mBeanServer) {
        this.yangStoreService = yangStoreService;

        // Config registry might not be present yet, but will be eventually
        while(true) {

            final ConfigRegistryJMXClient configRegistryJMXClient;
            try {
                configRegistryJMXClient = new ConfigRegistryJMXClient(mBeanServer);
            } catch (IllegalStateException e) {
                logger.debug("Jmx client could not be created, reattempting");
                try {
                    Thread.sleep(ATTEMPT_TIMEOUT_MS);
                } catch (InterruptedException e1) {
                    Thread.currentThread().interrupt();
                    throw new RuntimeException(e1);
                }
                continue;
            }

            jmxClient = configRegistryJMXClient;
            break;
        }
    }

    @Override
    public NetconfOperationServiceImpl createService(long netconfSessionId, String netconfSessionIdForReporting) {
        try {
            return new NetconfOperationServiceImpl(yangStoreService, jmxClient, netconfSessionIdForReporting);
        } catch (YangStoreException e) {
            throw new IllegalStateException(e);
        }
    }
}

@@ -977,16 +977,67 @@
  = 1000;
+%0A    private static final int SILENT_ATTEMPTS = 30;
 %0A%0A    pr
@@ -1571,16 +1571,92 @@
 rvice;%0A%0A
+        ConfigRegistryJMXClient configRegistryJMXClient;%0A        int i = 0;%0A
         
@@ -1750,75 +1750,8 @@
  %7B%0A%0A
-            final ConfigRegistryJMXClient configRegistryJMXClient;%0A
     
@@ -1848,16 +1848,39 @@
 erver);%0A
+                break;%0A
         
@@ -1911,32 +1911,222 @@
 eException e) %7B%0A
+                ++i;%0A                if (i %3E SILENT_ATTEMPTS) %7B%0A                    logger.info(%22JMX client not created after %7B%7D attempts, still trying%22, i, e);%0A                %7D else %7B%0A    
                 
@@ -2140,18 +2140,18 @@
 debug(%22J
-mx
+MX
  client 
@@ -2188,11 +2188,43 @@
 ting
-%22);
+, try %7B%7D%22, i, e);%0A                %7D
 %0A   
@@ -2451,16 +2451,61 @@
 ception(
+%22Interrupted while reattempting connection%22, 
 e1);%0A   
@@ -2535,26 +2535,10 @@
     
-    continue;%0A    
+%7D%0A
     
@@ -2552,20 +2552,16 @@
         
-  
-  
 jmxClien
@@ -2593,33 +2593,203 @@
 nt;%0A        
-    break
+if (i %3E SILENT_ATTEMPTS) %7B%0A            logger.info(%22Created JMX client after %7B%7D attempts%22, i);%0A        %7D else %7B%0A            logger.debug(%22Created JMX client after %7B%7D attempts%22, i)
 ;%0A        %7D%0A

Improve client logging