Convert mdsal-netconf-impl to OSGi DS
[netconf.git] / netconf / mdsal-netconf-impl / src / main / java / org / opendaylight / netconf / impl / mdsal / OSGiNetconfServer.java
diff --git a/netconf/mdsal-netconf-impl/src/main/java/org/opendaylight/netconf/impl/mdsal/OSGiNetconfServer.java b/netconf/mdsal-netconf-impl/src/main/java/org/opendaylight/netconf/impl/mdsal/OSGiNetconfServer.java
new file mode 100644 (file)
index 0000000..e4bb3ba
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.netconf.impl.mdsal;
+
+import static com.google.common.base.Verify.verifyNotNull;
+import static java.util.Objects.requireNonNull;
+
+import io.netty.channel.EventLoopGroup;
+import io.netty.util.Timer;
+import java.util.Map;
+import org.opendaylight.controller.config.threadpool.ScheduledThreadPool;
+import org.opendaylight.netconf.impl.NetconfServerSessionNegotiatorFactory;
+import org.opendaylight.netconf.impl.ServerChannelInitializer;
+import org.opendaylight.netconf.impl.SessionIdProvider;
+import org.opendaylight.netconf.impl.osgi.AggregatedNetconfOperationServiceFactory;
+import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.service.component.ComponentFactory;
+import org.osgi.service.component.ComponentInstance;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.metatype.annotations.AttributeDefinition;
+import org.osgi.service.metatype.annotations.Designate;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
+
+@Component(service = { }, configurationPid = "org.opendaylight.netconf.impl")
+@Designate(ocd = OSGiNetconfServer.Configuration.class)
+public final class OSGiNetconfServer {
+    @ObjectClassDefinition
+    public @interface Configuration {
+        @AttributeDefinition(min = "0")
+        long connection$_$timeout$_$millis() default 20000;
+        @AttributeDefinition
+        long monitoring$_$update$_$interval() default 6;
+    }
+
+    private final AggregatedNetconfOperationServiceFactory mappers = new AggregatedNetconfOperationServiceFactory();
+    private final ComponentInstance<DefaultNetconfMonitoringService> monitoring;
+    private final ComponentInstance<DefaultNetconfServerDispatcher> dispatcher;
+
+    @Activate
+    public OSGiNetconfServer(
+            @Reference(target = "(component.factory=" + DefaultNetconfMonitoringService.FACTORY_NAME + ")")
+            final ComponentFactory<DefaultNetconfMonitoringService> monitoringFactory,
+            @Reference(target = "(component.factory=" + DefaultNetconfServerDispatcher.FACTORY_NAME + ")")
+            final ComponentFactory<DefaultNetconfServerDispatcher> dispatcherFactory,
+            @Reference(target = "(" + NetconfMapperAggregator.OSGI_TYPE + ")")
+            final NetconfOperationServiceFactory mapperAggregatorRegistry,
+            @Reference(target = "(type=global-netconf-ssh-scheduled-executor)")
+            final ScheduledThreadPool sshScheduledExecutor,
+            @Reference(target = "(type=global-boss-group)") final EventLoopGroup bossGroup,
+            @Reference(target = "(type=global-boss-group)") final EventLoopGroup workerGroup,
+            @Reference(target = "(type=global-timer)") final Timer timer,
+            @Reference final SessionIdProvider sessionIdProvider,
+            final Configuration configuration) {
+        mappers.onAddNetconfOperationServiceFactory(mapperAggregatorRegistry);
+        monitoring = monitoringFactory.newInstance(FrameworkUtil.asDictionary(DefaultNetconfMonitoringService.props(
+            mapperAggregatorRegistry, sshScheduledExecutor, configuration.monitoring$_$update$_$interval())));
+        dispatcher = dispatcherFactory.newInstance(FrameworkUtil.asDictionary(DefaultNetconfServerDispatcher.props(
+            bossGroup, workerGroup, new ServerChannelInitializer(new NetconfServerSessionNegotiatorFactory(timer,
+                mappers, sessionIdProvider, configuration.connection$_$timeout$_$millis(),
+                monitoring.getInstance())))));
+    }
+
+    @Deactivate
+    public void deactivate() {
+        dispatcher.dispose();
+        monitoring.dispose();
+        mappers.close();
+    }
+
+    static <T> T extractProp(final Map<String, ?> properties, final String key, final Class<T> valueType) {
+        return valueType.cast(verifyNotNull(properties.get(requireNonNull(key))));
+    }
+}