2 * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others. 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.netconf.config;
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
13 import java.util.ArrayList;
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;
28 * Component instantiating global NETCONF resources.
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);
35 private final ComponentFactory<GlobalNetconfProcessingExecutor> processingFactory;
37 private GlobalNetconfThreadFactory threadFactory;
38 private ComponentInstance<GlobalNetconfProcessingExecutor> processingExecutor;
39 private Map<String, ?> processingProps;
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);
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");
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");
63 // We want to instantiate new services before we dispose old ones, so
64 final var toDispose = new ArrayList<ComponentInstance<?>>();
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);
74 toDispose.forEach(ComponentInstance::dispose);
79 processingExecutor.dispose();
80 processingExecutor = null;
82 LOG.info("Global NETCONF configuration pools stopped");
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))));