Add Honeynode emulator for device221
[transportpce.git] / tests / honeynode221 / 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 javax.annotation.Nullable;
27
28 import org.eclipse.jetty.server.Server;
29 import org.eclipse.jetty.server.ServerConnector;
30 import org.opendaylight.netconf.sal.rest.api.RestConnector;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 class JettyServerStarter extends ProviderTrait<JettyServerStarter.ServerInit> {
35
36     private static final Logger LOG = LoggerFactory.getLogger(JettyServerStarter.class);
37
38     @Inject
39     private Server server;
40
41     // injecting all connectors to make sure that server is started after they are added
42     @Inject
43     private RestConnector connector;
44
45     // if HTTP is disabled, null will be injected
46     @Nullable
47     @Inject(optional = true)
48     @Named(RESTCONF_HTTP)
49     private ServerConnector httpConnectorInit;
50
51     // if HTTPS is disabled, null will be injected
52     @Nullable
53     @Inject(optional = true)
54     @Named(RESTCONF_HTTPS)
55     private ServerConnector httpsConnectorInit;
56
57     @Override
58     protected ServerInit create() {
59         try {
60             LOG.info("Starting RESTCONF Jetty server");
61             server.start();
62             LOG.info("RESTCONF Jetty server successfully started");
63         } catch (Exception e) {
64             LOG.error("Unable to start RESTCONF Jetty server", e);
65             throw new IllegalStateException("Unable to start RESTCONF Jetty server", e);
66         }
67
68         return new ServerInit() {
69         };
70     }
71
72     interface ServerInit {
73     }
74 }