Convert netty-timer-config to OSGi DS
[controller.git] / opendaylight / config / netty-timer-config / src / main / java / org / opendaylight / controller / config / yang / netty / timer / OSGiGlobalTimer.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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 package org.opendaylight.controller.config.yang.netty.timer;
9
10 import io.netty.util.Timeout;
11 import io.netty.util.Timer;
12 import io.netty.util.TimerTask;
13 import java.util.Set;
14 import java.util.concurrent.TimeUnit;
15 import org.osgi.service.component.annotations.Activate;
16 import org.osgi.service.component.annotations.Component;
17 import org.osgi.service.component.annotations.Deactivate;
18 import org.osgi.service.metatype.annotations.AttributeDefinition;
19 import org.osgi.service.metatype.annotations.Designate;
20 import org.osgi.service.metatype.annotations.ObjectClassDefinition;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 @Component(immediate = true, configurationPid = "org.opendaylight.netty.timer", property = "type=global-timer")
25 @Designate(ocd = OSGiGlobalTimer.Config.class)
26 public final class OSGiGlobalTimer implements Timer {
27     @ObjectClassDefinition
28     public @interface Config {
29         @AttributeDefinition(name = "tick-duration")
30         long tickDuration() default 0;
31         @AttributeDefinition(name = "ticks-per-wheel")
32         int ticksPerWheel() default 0;
33     }
34
35     private static final Logger LOG = LoggerFactory.getLogger(OSGiGlobalTimer.class);
36
37     private Timer delegate;
38
39     @Override
40     public Timeout newTimeout(final TimerTask task, final long delay, final TimeUnit unit) {
41         return delegate.newTimeout(task, delay, unit);
42     }
43
44     @Override
45     public Set<Timeout> stop() {
46         return delegate.stop();
47     }
48
49     @Activate
50     void activate(final Config config) {
51         delegate = HashedWheelTimerCloseable.newInstance(config.tickDuration(), config.ticksPerWheel());
52         LOG.info("Global Netty timer started");
53     }
54
55     @Deactivate
56     void deactivate() {
57         delegate.stop();
58         LOG.info("Global Netty timer stopped");
59     }
60 }