2 * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
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
8 package org.opendaylight.bgpcep.programming.impl;
10 import static java.util.Objects.requireNonNull;
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;
34 @Component(immediate = true, service = InstructionSchedulerFactory.class)
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")
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;
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);
63 public InstructionScheduler createInstructionScheduler(final String instructionId) {
64 LOG.info("Creating Instruction Scheduler {}.", instructionId);
65 return new DefaultInstructionScheduler(dataProvider, notifs, exec, rpcProviderRegistry, cssp, timer,
73 // FIXME: This can have weird effects: should we keep track of all schedulers and refcount?