fix ServiceHandler SpotBugs false positives
[transportpce.git] / tests / honeynode / 2.2.1 / restconf / src / main / java / io / fd / honeycomb / northbound / restconf / JettyServerStarter.java
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.northbound.restconf;
18
19 import static io.fd.honeycomb.northbound.restconf.RestconfModule.RESTCONF_HTTP;
20 import static io.fd.honeycomb.northbound.restconf.RestconfModule.RESTCONF_HTTPS;
21
22 import com.google.inject.Inject;
23 import com.google.inject.name.Named;
24 import io.fd.honeycomb.binding.init.ProviderTrait;
25
26 import io.fd.honeycomb.data.init.ShutdownHandler;
27 import javax.annotation.Nullable;
28
29 import org.eclipse.jetty.server.Server;
30 import org.eclipse.jetty.server.ServerConnector;
31 import org.opendaylight.netconf.sal.rest.api.RestConnector;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 class JettyServerStarter extends ProviderTrait<JettyServerStarter.RestconfJettyServer> {
36
37     private static final Logger LOG = LoggerFactory.getLogger(JettyServerStarter.class);
38
39     @Inject
40     private Server server;
41
42     // injecting all connectors to make sure that server is started after they are added
43     @Inject
44     private RestConnector connector;
45
46     // if HTTP is disabled, null will be injected
47     @Nullable
48     @Inject(optional = true)
49     @Named(RESTCONF_HTTP)
50     private ServerConnector httpConnectorInit;
51
52     // if HTTPS is disabled, null will be injected
53     @Nullable
54     @Inject(optional = true)
55     @Named(RESTCONF_HTTPS)
56     private ServerConnector httpsConnectorInit;
57
58     @Inject
59     private ShutdownHandler shutdownHandler;
60
61     @Override
62     protected RestconfJettyServer create() {
63         final RestconfJettyServer jettyServer = new RestconfJettyServer(server);
64         shutdownHandler.register(RestconfJettyServer.class.getCanonicalName(), jettyServer);
65         jettyServer.start();
66         return jettyServer;
67     }
68
69     final class RestconfJettyServer implements AutoCloseable {
70         private final Server server;
71
72         private RestconfJettyServer(final Server server) {
73             this.server = server;
74         }
75
76         private void start() {
77             try {
78                 LOG.info("Starting RESTCONF Jetty server");
79                 server.start();
80                 LOG.info("RESTCONF Jetty server successfully started");
81             } catch (Exception e) {
82                 LOG.error("Unable to start RESTCONF Jetty server", e);
83                 throw new IllegalStateException("Unable to start RESTCONF Jetty server", e);
84             }
85         }
86
87         @Override
88         public void close() throws Exception {
89             LOG.info("Stopping RESTCONF Jetty server");
90             server.stop();
91             server.destroy();
92             LOG.info("RESTCONF Jetty server successfully stopped");
93         }
94     }
95 }