Eliminate GlobalNetconfSshScheduledExecutor
[netconf.git] / netconf / netconf-config / src / main / java / org / opendaylight / netconf / config / GlobalNetconfConfiguration.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 com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
13 import java.util.ArrayList;
14 import java.util.Map;
15 import org.osgi.framework.FrameworkUtil;
16 import org.osgi.service.component.ComponentFactory;
17 import org.osgi.service.component.ComponentInstance;
18 import org.osgi.service.component.annotations.Activate;
19 import org.osgi.service.component.annotations.Component;
20 import org.osgi.service.component.annotations.Deactivate;
21 import org.osgi.service.component.annotations.Modified;
22 import org.osgi.service.component.annotations.Reference;
23 import org.osgi.service.metatype.annotations.Designate;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Component instantiating global NETCONF resources.
29  */
30 @Component(service = { }, configurationPid = "org.opendaylight.netconf.config")
31 @Designate(ocd = Configuration.class)
32 public final class GlobalNetconfConfiguration {
33     private static final Logger LOG = LoggerFactory.getLogger(GlobalNetconfConfiguration.class);
34
35     private final ComponentFactory<GlobalNetconfProcessingExecutor> processingFactory;
36
37     private GlobalNetconfThreadFactory threadFactory;
38     private ComponentInstance<GlobalNetconfProcessingExecutor> processingExecutor;
39     private Map<String, ?> processingProps;
40
41     @Activate
42     public GlobalNetconfConfiguration(
43             @Reference(target = "(component.factory=" + GlobalNetconfProcessingExecutor.FACTORY_NAME + ")")
44             final ComponentFactory<GlobalNetconfProcessingExecutor> processingFactory,
45             final Configuration configuration) {
46         this.processingFactory = requireNonNull(processingFactory);
47
48         threadFactory = new GlobalNetconfThreadFactory(configuration.name$_$prefix());
49         processingProps = GlobalNetconfProcessingExecutor.props(threadFactory, configuration);
50         processingExecutor = processingFactory.newInstance(FrameworkUtil.asDictionary(processingProps));
51         LOG.info("Global NETCONF configuration pools started");
52     }
53
54     @Modified
55     void modified(final Configuration configuration) {
56         final var newNamePrefix = configuration.name$_$prefix();
57         if (!threadFactory.getNamePrefix().equals(newNamePrefix)) {
58             threadFactory = new GlobalNetconfThreadFactory(newNamePrefix);
59             processingProps = null;
60             LOG.debug("Forcing restart of all executors");
61         }
62
63         // We want to instantiate new services before we dispose old ones, so
64         final var toDispose = new ArrayList<ComponentInstance<?>>();
65
66         final var newProcessingProps = GlobalNetconfProcessingExecutor.props(threadFactory, configuration);
67         if (!newProcessingProps.equals(processingProps)) {
68             processingProps = newProcessingProps;
69             toDispose.add(processingExecutor);
70             processingExecutor = processingFactory.newInstance(FrameworkUtil.asDictionary(processingProps));
71             LOG.debug("Processing executor restarted with {}", processingProps);
72         }
73
74         toDispose.forEach(ComponentInstance::dispose);
75     }
76
77     @Deactivate
78     void deactivate() {
79         processingExecutor.dispose();
80         processingExecutor = null;
81         threadFactory = null;
82         LOG.info("Global NETCONF configuration pools stopped");
83     }
84
85     static <T> T extractProp(final Map<String, ?> properties, final String key, final Class<T> valueType) {
86         return valueType.cast(verifyNotNull(properties.get(requireNonNull(key))));
87     }
88 }