Eliminate GlobalNetconfSshScheduledExecutor
[netconf.git] / netconf / netconf-config / src / main / java / org / opendaylight / netconf / config / GlobalNetconfProcessingExecutor.java
1 /*
2  * Copyright (c) 2023 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.netconf.config;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.Map;
13 import java.util.concurrent.TimeUnit;
14 import javax.annotation.PreDestroy;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.opendaylight.controller.config.threadpool.ThreadPool;
18 import org.opendaylight.controller.config.threadpool.util.FlexibleThreadPoolWrapper;
19 import org.osgi.service.component.annotations.Activate;
20 import org.osgi.service.component.annotations.Component;
21 import org.osgi.service.component.annotations.Deactivate;
22
23 @Singleton
24 @Component(factory = GlobalNetconfProcessingExecutor.FACTORY_NAME, service = ThreadPool.class)
25 public final class GlobalNetconfProcessingExecutor extends FlexibleThreadPoolWrapper {
26     public static final String OSGI_TYPE = "global-netconf-processing-executor";
27     public static final int DEFAULT_MIN_THREAD_COUNT = 1;
28     public static final int DEFAULT_MAX_THREAD_COUNT = 4;
29     public static final long DEFAULT_KEEPALIVE_MILLIS = 600000;
30
31     // OSGi DS Component Factory name
32     static final String FACTORY_NAME = "org.opendaylight.netconf.config.GlobalNetconfProcessingExecutor";
33
34     private static final String PROP_KEEPALIVE = ".keepAlive";
35     private static final String PROP_MIN_THREAD_COUNT = ".minThreadCount";
36     private static final String PROP_MAX_THREAD_COUNT = ".maxThreadCount";
37     private static final String PROP_THREAD_FACTORY = ".threadFactory";
38
39     public GlobalNetconfProcessingExecutor(final GlobalNetconfThreadFactory threadFactory, final int minThreadCount,
40             final int maxThreadCount, final long keepAliveMillis) {
41         super(minThreadCount, maxThreadCount, keepAliveMillis, TimeUnit.MILLISECONDS, threadFactory);
42     }
43
44     @Inject
45     public GlobalNetconfProcessingExecutor(final GlobalNetconfThreadFactory threadFactory) {
46         this(threadFactory, DEFAULT_MIN_THREAD_COUNT, DEFAULT_MAX_THREAD_COUNT, DEFAULT_KEEPALIVE_MILLIS);
47     }
48
49     @Activate
50     public GlobalNetconfProcessingExecutor(final Map<String, ?> properties) {
51         this(GlobalNetconfConfiguration.extractProp(properties, PROP_THREAD_FACTORY, GlobalNetconfThreadFactory.class),
52             GlobalNetconfConfiguration.extractProp(properties, PROP_MIN_THREAD_COUNT, Integer.class),
53             GlobalNetconfConfiguration.extractProp(properties, PROP_MAX_THREAD_COUNT, Integer.class),
54             GlobalNetconfConfiguration.extractProp(properties, PROP_KEEPALIVE, Long.class));
55     }
56
57     @Override
58     @PreDestroy
59     @Deactivate
60     public void close() {
61         super.close();
62     }
63
64     static Map<String, ?> props(final GlobalNetconfThreadFactory threadFactory, final Configuration configuration) {
65         return Map.of(
66             "type", OSGI_TYPE,
67             PROP_THREAD_FACTORY, requireNonNull(threadFactory),
68             PROP_KEEPALIVE, configuration.keep$_$alive$_$millis$_$flexible$_$thread$_$pool(),
69             PROP_MIN_THREAD_COUNT, configuration.min$_$thread$_$count$_$flexible$_$thread$_$pool(),
70             PROP_MAX_THREAD_COUNT, configuration.max$_$thread$_$count$_$flexible$_$thread$_$pool());
71     }
72 }