cb2eb708acf0eb1e5214809fee4f957b2b02097e
[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     @Activate
40     public OSGiGlobalTimer(final Config config) {
41         delegate = HashedWheelTimerCloseable.newInstance(config.tickDuration(), config.ticksPerWheel());
42         LOG.info("Global Netty timer started");
43     }
44
45     @Deactivate
46     void deactivate() {
47         delegate.stop();
48         delegate = null;
49         LOG.info("Global Netty timer stopped");
50     }
51
52     @Override
53     public Timeout newTimeout(final TimerTask task, final long delay, final TimeUnit unit) {
54         return delegate.newTimeout(task, delay, unit);
55     }
56
57     @Override
58     public Set<Timeout> stop() {
59         LOG.warn("Attepted to stop global timer", new Throwable());
60         return Set.of();
61     }
62 }