e510444adf91f5cf86a0fc62731f6c36faa7f53f
[bgpcep.git] / programming / impl / src / main / java / org / opendaylight / bgpcep / programming / impl / DefaultInstructionSchedulerFactory.java
1 /*
2  * Copyright (c) 2017 AT&T Intellectual Property. 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.bgpcep.programming.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.ThreadFactoryBuilder;
13 import io.netty.util.HashedWheelTimer;
14 import io.netty.util.Timer;
15 import java.util.concurrent.ExecutorService;
16 import java.util.concurrent.Executors;
17 import java.util.concurrent.ThreadFactory;
18 import javax.annotation.PreDestroy;
19 import javax.inject.Inject;
20 import javax.inject.Singleton;
21 import org.opendaylight.bgpcep.programming.spi.InstructionScheduler;
22 import org.opendaylight.bgpcep.programming.spi.InstructionSchedulerFactory;
23 import org.opendaylight.mdsal.binding.api.DataBroker;
24 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
25 import org.opendaylight.mdsal.binding.api.RpcProviderService;
26 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
27 import org.osgi.service.component.annotations.Activate;
28 import org.osgi.service.component.annotations.Component;
29 import org.osgi.service.component.annotations.Deactivate;
30 import org.osgi.service.component.annotations.Reference;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 @Component(immediate = true, service = InstructionSchedulerFactory.class)
35 @Singleton
36 public final class DefaultInstructionSchedulerFactory implements InstructionSchedulerFactory, AutoCloseable {
37     private static final Logger LOG = LoggerFactory.getLogger(DefaultInstructionSchedulerFactory.class);
38     private static final ThreadFactory THREAD_FACTORY = new ThreadFactoryBuilder()
39         .setNameFormat("programming-timer-%d")
40         .setDaemon(true)
41         .build();
42
43     private final ExecutorService exec = Executors.newSingleThreadExecutor();
44     private final Timer timer = new HashedWheelTimer(THREAD_FACTORY);
45     private final DataBroker dataProvider;
46     private final NotificationPublishService notifs;
47     private final RpcProviderService rpcProviderRegistry;
48     private final ClusterSingletonServiceProvider cssp;
49
50     @Inject
51     @Activate
52     public DefaultInstructionSchedulerFactory(@Reference final DataBroker dataProvider,
53             @Reference final RpcProviderService rpcProviderRegistry,
54             @Reference final NotificationPublishService notifs,
55             @Reference final ClusterSingletonServiceProvider cssp) {
56         this.dataProvider = requireNonNull(dataProvider);
57         this.notifs = requireNonNull(notifs);
58         this.rpcProviderRegistry = requireNonNull(rpcProviderRegistry);
59         this.cssp = requireNonNull(cssp);
60     }
61
62     @Override
63     public InstructionScheduler createInstructionScheduler(final String instructionId) {
64         LOG.info("Creating Instruction Scheduler {}.", instructionId);
65         return new DefaultInstructionScheduler(dataProvider, notifs, exec, rpcProviderRegistry, cssp, timer,
66             instructionId);
67     }
68
69     @Deactivate
70     @PreDestroy
71     @Override
72     public void close() {
73         // FIXME: This can have weird effects: should we keep track of all schedulers and refcount?
74         exec.shutdown();
75         timer.stop();
76     }
77 }