Integrate netconf-mapping-api into netconf-server
[netconf.git] / netconf / mdsal-netconf-impl / src / main / java / org / opendaylight / netconf / impl / mdsal / OSGiNetconfServer.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.impl.mdsal;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
13 import io.netty.channel.EventLoopGroup;
14 import io.netty.util.Timer;
15 import java.util.Map;
16 import org.opendaylight.controller.config.threadpool.ScheduledThreadPool;
17 import org.opendaylight.netconf.server.NetconfServerSessionNegotiatorFactory;
18 import org.opendaylight.netconf.server.ServerChannelInitializer;
19 import org.opendaylight.netconf.server.api.SessionIdProvider;
20 import org.opendaylight.netconf.server.api.operations.NetconfOperationServiceFactory;
21 import org.opendaylight.netconf.server.osgi.AggregatedNetconfOperationServiceFactory;
22 import org.osgi.framework.FrameworkUtil;
23 import org.osgi.service.component.ComponentFactory;
24 import org.osgi.service.component.ComponentInstance;
25 import org.osgi.service.component.annotations.Activate;
26 import org.osgi.service.component.annotations.Component;
27 import org.osgi.service.component.annotations.Deactivate;
28 import org.osgi.service.component.annotations.Reference;
29 import org.osgi.service.metatype.annotations.AttributeDefinition;
30 import org.osgi.service.metatype.annotations.Designate;
31 import org.osgi.service.metatype.annotations.ObjectClassDefinition;
32
33 @Component(service = { }, configurationPid = "org.opendaylight.netconf.impl")
34 @Designate(ocd = OSGiNetconfServer.Configuration.class)
35 public final class OSGiNetconfServer {
36     @ObjectClassDefinition
37     public @interface Configuration {
38         @AttributeDefinition(min = "0")
39         long connection$_$timeout$_$millis() default 20000;
40         @AttributeDefinition
41         long monitoring$_$update$_$interval() default 6;
42     }
43
44     private final AggregatedNetconfOperationServiceFactory mappers = new AggregatedNetconfOperationServiceFactory();
45     private final ComponentInstance<DefaultNetconfMonitoringService> monitoring;
46     private final ComponentInstance<DefaultNetconfServerDispatcher> dispatcher;
47
48     @Activate
49     public OSGiNetconfServer(
50             @Reference(target = "(component.factory=" + DefaultNetconfMonitoringService.FACTORY_NAME + ")")
51             final ComponentFactory<DefaultNetconfMonitoringService> monitoringFactory,
52             @Reference(target = "(component.factory=" + DefaultNetconfServerDispatcher.FACTORY_NAME + ")")
53             final ComponentFactory<DefaultNetconfServerDispatcher> dispatcherFactory,
54             @Reference(target = "(" + NetconfMapperAggregator.OSGI_TYPE + ")")
55             final NetconfOperationServiceFactory mapperAggregatorRegistry,
56             @Reference(target = "(type=global-netconf-ssh-scheduled-executor)")
57             final ScheduledThreadPool sshScheduledExecutor,
58             @Reference(target = "(type=global-boss-group)") final EventLoopGroup bossGroup,
59             @Reference(target = "(type=global-boss-group)") final EventLoopGroup workerGroup,
60             @Reference(target = "(type=global-timer)") final Timer timer,
61             @Reference final SessionIdProvider sessionIdProvider,
62             final Configuration configuration) {
63         mappers.onAddNetconfOperationServiceFactory(mapperAggregatorRegistry);
64         monitoring = monitoringFactory.newInstance(FrameworkUtil.asDictionary(DefaultNetconfMonitoringService.props(
65             mapperAggregatorRegistry, sshScheduledExecutor, configuration.monitoring$_$update$_$interval())));
66         dispatcher = dispatcherFactory.newInstance(FrameworkUtil.asDictionary(DefaultNetconfServerDispatcher.props(
67             bossGroup, workerGroup, new ServerChannelInitializer(new NetconfServerSessionNegotiatorFactory(timer,
68                 mappers, sessionIdProvider, configuration.connection$_$timeout$_$millis(),
69                 monitoring.getInstance())))));
70     }
71
72     @Deactivate
73     public void deactivate() {
74         dispatcher.dispose();
75         monitoring.dispose();
76         mappers.close();
77     }
78
79     static <T> T extractProp(final Map<String, ?> properties, final String key, final Class<T> valueType) {
80         return valueType.cast(verifyNotNull(properties.get(requireNonNull(key))));
81     }
82 }