Add ApiPath.empty()
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / OSGiNorthbound.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.restconf.nb.rfc8040;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.Map;
13 import org.opendaylight.restconf.nb.rfc8040.streams.DefaultPingExecutor;
14 import org.opendaylight.restconf.nb.rfc8040.streams.DefaultRestconfStreamServletFactory;
15 import org.opendaylight.restconf.nb.rfc8040.streams.StreamsConfiguration;
16 import org.opendaylight.restconf.server.mdsal.MdsalRestconfStreamRegistry;
17 import org.osgi.framework.FrameworkUtil;
18 import org.osgi.service.component.ComponentFactory;
19 import org.osgi.service.component.ComponentInstance;
20 import org.osgi.service.component.annotations.Activate;
21 import org.osgi.service.component.annotations.Component;
22 import org.osgi.service.component.annotations.Deactivate;
23 import org.osgi.service.component.annotations.Modified;
24 import org.osgi.service.component.annotations.Reference;
25 import org.osgi.service.metatype.annotations.AttributeDefinition;
26 import org.osgi.service.metatype.annotations.Designate;
27 import org.osgi.service.metatype.annotations.ObjectClassDefinition;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Component managing global RESTCONF northbound configuration.
33  */
34 @Component(service = { }, configurationPid = "org.opendaylight.restconf.nb.rfc8040")
35 @Designate(ocd = OSGiNorthbound.Configuration.class)
36 public final class OSGiNorthbound {
37     @ObjectClassDefinition
38     public @interface Configuration {
39         @AttributeDefinition(min = "0", max = "" + StreamsConfiguration.MAXIMUM_FRAGMENT_LENGTH_LIMIT)
40         int maximum$_$fragment$_$length() default 0;
41
42         @AttributeDefinition(min = "0")
43         int heartbeat$_$interval() default 10000;
44
45         @AttributeDefinition(min = "1")
46         int idle$_$timeout() default 30000;
47
48         @AttributeDefinition(min = "1")
49         String ping$_$executor$_$name$_$prefix() default DefaultPingExecutor.DEFAULT_NAME_PREFIX;
50
51         // FIXME: this is a misnomer: it specifies the core pool size, i.e. minimum thread count, the maximum is set to
52         //        Integer.MAX_VALUE, which is not what we want
53         @AttributeDefinition(min = "0")
54         int max$_$thread$_$count() default DefaultPingExecutor.DEFAULT_CORE_POOL_SIZE;
55
56         @Deprecated(since = "7.0.0", forRemoval = true)
57         @AttributeDefinition
58         boolean use$_$sse() default true;
59
60         @AttributeDefinition(name = "{+restconf}", description = """
61             The value of RFC8040 {+restconf} URI template, pointing to the root resource. Must not end with '/'.""")
62         String restconf() default "rests";
63     }
64
65     private static final Logger LOG = LoggerFactory.getLogger(OSGiNorthbound.class);
66
67     private final ComponentFactory<MdsalRestconfStreamRegistry> registryFactory;
68     private final ComponentFactory<DefaultRestconfStreamServletFactory> servletFactoryFactory;
69
70     private ComponentInstance<MdsalRestconfStreamRegistry> registry;
71     @Deprecated(since = "7.0.0", forRemoval = true)
72     private boolean useSSE;
73
74     private ComponentInstance<DefaultRestconfStreamServletFactory> servletFactory;
75     private Map<String, ?> servletProps;
76
77     @Activate
78     public OSGiNorthbound(
79             @Reference(target = "(component.factory=" + DefaultRestconfStreamServletFactory.FACTORY_NAME + ")")
80             final ComponentFactory<DefaultRestconfStreamServletFactory> servletFactoryFactory,
81             @Reference(target = "(component.factory=" + MdsalRestconfStreamRegistry.FACTORY_NAME + ")")
82             final ComponentFactory<MdsalRestconfStreamRegistry> registryFactory, final Configuration configuration) {
83         this.registryFactory = requireNonNull(registryFactory);
84         this.servletFactoryFactory = requireNonNull(servletFactoryFactory);
85
86         useSSE = configuration.use$_$sse();
87         registry = registryFactory.newInstance(FrameworkUtil.asDictionary(MdsalRestconfStreamRegistry.props(useSSE)));
88
89         servletProps = DefaultRestconfStreamServletFactory.props(configuration.restconf(), registry.getInstance(),
90             useSSE,
91             new StreamsConfiguration(configuration.maximum$_$fragment$_$length(),
92                 configuration.idle$_$timeout(), configuration.heartbeat$_$interval()),
93             configuration.ping$_$executor$_$name$_$prefix(), configuration.max$_$thread$_$count());
94         servletFactory = servletFactoryFactory.newInstance(FrameworkUtil.asDictionary(servletProps));
95
96         LOG.info("Global RESTCONF northbound pools started");
97     }
98
99     @Modified
100     void modified(final Configuration configuration) {
101         final var newUseSSE = configuration.use$_$sse();
102         if (newUseSSE != useSSE) {
103             useSSE = newUseSSE;
104             registry.dispose();
105             registry = registryFactory.newInstance(FrameworkUtil.asDictionary(
106                 MdsalRestconfStreamRegistry.props(useSSE)));
107             LOG.debug("ListenersBroker restarted with {}", newUseSSE ? "SSE" : "Websockets");
108         }
109
110         final var newServletProps = DefaultRestconfStreamServletFactory.props(configuration.restconf(),
111             registry.getInstance(), useSSE,
112             new StreamsConfiguration(configuration.maximum$_$fragment$_$length(),
113                 configuration.idle$_$timeout(), configuration.heartbeat$_$interval()),
114             configuration.ping$_$executor$_$name$_$prefix(), configuration.max$_$thread$_$count());
115         if (!newServletProps.equals(servletProps)) {
116             servletProps = newServletProps;
117             servletFactory.dispose();
118             servletFactory = servletFactoryFactory.newInstance(FrameworkUtil.asDictionary(servletProps));
119             LOG.debug("RestconfStreamServletFactory restarted with {}", servletProps);
120         }
121
122         LOG.debug("Applied {}", configuration);
123     }
124
125     @Deactivate
126     void deactivate() {
127         servletFactory.dispose();
128         servletFactory = null;
129         registry.dispose();
130         registry = null;
131         LOG.info("Global RESTCONF northbound pools stopped");
132     }
133 }