Modify config Module impls to co-exist with blueprint
[controller.git] / opendaylight / config / netty-threadgroup-config / src / main / java / org / opendaylight / controller / config / yang / netty / threadgroup / NettyThreadgroupModule.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 /**
10 * Generated file
11
12 * Generated from: yang module name: nsos-threadpool  yang module local name: netty-threadgroup-fixed
13 * Generated by: org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator
14 * Generated at: Fri Nov 08 08:31:45 CET 2013
15 *
16 * Do not modify this file unless it is present under src/main directory
17 */
18 package org.opendaylight.controller.config.yang.netty.threadgroup;
19
20 import com.google.common.reflect.AbstractInvocationHandler;
21 import com.google.common.reflect.Reflection;
22 import io.netty.channel.EventLoopGroup;
23 import java.lang.reflect.Method;
24 import org.opendaylight.controller.config.api.JmxAttributeValidationException;
25 import org.opendaylight.controller.config.api.osgi.WaitingServiceTracker;
26 import org.osgi.framework.BundleContext;
27
28 /**
29 *
30 */
31 public final class NettyThreadgroupModule extends org.opendaylight.controller.config.yang.netty.threadgroup.AbstractNettyThreadgroupModule
32 {
33     private BundleContext bundleContext;
34
35     public NettyThreadgroupModule(org.opendaylight.controller.config.api.ModuleIdentifier name, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
36         super(name, dependencyResolver);
37     }
38
39     public NettyThreadgroupModule(org.opendaylight.controller.config.api.ModuleIdentifier name, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, NettyThreadgroupModule oldModule, java.lang.AutoCloseable oldInstance) {
40         super(name, dependencyResolver, oldModule, oldInstance);
41     }
42
43     @Override
44     public void validate(){
45         if(getThreadCount()!=null) {
46             JmxAttributeValidationException.checkCondition(getThreadCount() > 0, "value must be greater than 0",
47                     threadCountJmxAttribute);
48         }
49     }
50
51     @Override
52     public AutoCloseable createInstance() {
53         // The service is provided via blueprint so wait for and return it here for backwards compatibility.
54         String typeFilter = String.format("(type=%s)", getIdentifier().getInstanceName());
55         final WaitingServiceTracker<EventLoopGroup> tracker = WaitingServiceTracker.create(
56                 EventLoopGroup.class, bundleContext, typeFilter);
57         final EventLoopGroup group = tracker.waitForService(WaitingServiceTracker.FIVE_MINUTES);
58
59         return Reflection.newProxy(AutoCloseableEventLoopGroupInterface.class, new AbstractInvocationHandler() {
60             @Override
61             protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
62                 if (method.getName().equals("close")) {
63                     tracker.close();
64                     return null;
65                 } else {
66                     return method.invoke(group, args);
67                 }
68             }
69         });
70     }
71
72     public void setBundleContext(BundleContext bundleContext) {
73         this.bundleContext = bundleContext;
74     }
75
76     private static interface AutoCloseableEventLoopGroupInterface extends EventLoopGroup, AutoCloseable {
77     }
78 }