2 * Copyright (c) 2017 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.northbound.restconf;
19 import static io.fd.honeycomb.northbound.restconf.RestconfModule.RESTCONF_HTTP;
20 import static io.fd.honeycomb.northbound.restconf.RestconfModule.RESTCONF_HTTPS;
22 import com.google.inject.Inject;
23 import com.google.inject.name.Named;
24 import io.fd.honeycomb.binding.init.ProviderTrait;
26 import io.fd.honeycomb.data.init.ShutdownHandler;
27 import javax.annotation.Nullable;
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;
35 class JettyServerStarter extends ProviderTrait<JettyServerStarter.RestconfJettyServer> {
37 private static final Logger LOG = LoggerFactory.getLogger(JettyServerStarter.class);
40 private Server server;
42 // injecting all connectors to make sure that server is started after they are added
44 private RestConnector connector;
46 // if HTTP is disabled, null will be injected
48 @Inject(optional = true)
50 private ServerConnector httpConnectorInit;
52 // if HTTPS is disabled, null will be injected
54 @Inject(optional = true)
55 @Named(RESTCONF_HTTPS)
56 private ServerConnector httpsConnectorInit;
59 private ShutdownHandler shutdownHandler;
62 protected RestconfJettyServer create() {
63 final RestconfJettyServer jettyServer = new RestconfJettyServer(server);
64 shutdownHandler.register(RestconfJettyServer.class.getCanonicalName(), jettyServer);
69 final class RestconfJettyServer implements AutoCloseable {
70 private final Server server;
72 private RestconfJettyServer(final Server server) {
76 private void start() {
78 LOG.info("Starting RESTCONF Jetty server");
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);
88 public void close() throws Exception {
89 LOG.info("Stopping RESTCONF Jetty server");
92 LOG.info("RESTCONF Jetty server successfully stopped");