Create seed codes of nfvo
[vfc/nfvo/driver/sfc.git] / zte / sfc-driver / sfc-driver / src / main / java / org / openo / sfc / SfcDriver.java
1 /**
2  * Copyright 2016 [ZTE] and others.
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 package org.openo.sfc;
17
18 import com.fasterxml.jackson.annotation.JsonInclude;
19 import io.dropwizard.Application;
20 import io.dropwizard.assets.AssetsBundle;
21 import io.dropwizard.server.SimpleServerFactory;
22 import io.dropwizard.setup.Bootstrap;
23 import io.dropwizard.setup.Environment;
24 import io.swagger.jaxrs.config.BeanConfig;
25 import io.swagger.jaxrs.listing.ApiListingResource;
26 import org.openo.sfc.health.ConsoleHealthCheck;
27 import org.openo.sfc.resources.DriverResource;
28 import org.openo.sfc.service.ConfigInfo;
29 import org.openo.sfc.resources.MsbServiceRegister;
30 import org.openo.sfc.utils.SfcConst;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class SfcDriver extends Application<SfcDriverConfig> {
35
36     private static final Logger LOGGER = LoggerFactory.getLogger(SfcDriver.class);
37
38     public static void main(String[] args) throws Exception {
39         new SfcDriver().run(args);
40     }
41
42     @Override
43     public String getName() {
44         return SfcConst.SERVICE_NAME;
45     }
46
47     @Override
48     public void initialize(Bootstrap<SfcDriverConfig> bootstrap) {
49         bootstrap.addBundle(new AssetsBundle("/iui", "/", "index.html", "iui"));
50         bootstrap.addBundle(new AssetsBundle("/api-doc", "/api-doc", "index.html", "api-doc"));
51     }
52
53     @Override
54     public void run(SfcDriverConfig configuration,
55                     Environment environment) {
56         final DriverResource driverResource = new DriverResource();
57         final ConsoleHealthCheck healthCheck =
58                 new ConsoleHealthCheck(configuration.getTemplate());
59         environment.healthChecks().register("template", healthCheck);
60         environment.jersey().register(driverResource);
61         ConfigInfo.setConfig(configuration);
62
63         registerService();
64         initSwaggerConfig(environment, configuration);
65     }
66
67
68     private void initSwaggerConfig(Environment environment, SfcDriverConfig configuration) {
69         environment.jersey().register(new ApiListingResource());
70         environment.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
71
72         BeanConfig config = new BeanConfig();
73         config.setTitle(" Console Service rest API");
74         config.setVersion("1.0.0");
75         config.setResourcePackage("org.openo.sfc.resources");
76         //swagger rest api basepath
77         SimpleServerFactory simpleServerFactory = (SimpleServerFactory) configuration.getServerFactory();
78         String basePath = simpleServerFactory.getApplicationContextPath();
79
80         basePath = basePath.endsWith("/") ? basePath : (new StringBuilder()).append(basePath).append('/').toString();
81         basePath = basePath + "service";
82         LOGGER.info("getApplicationContextPath: " + basePath);
83         config.setBasePath(basePath);
84         config.setScan(true);
85     }
86
87     private void registerService()
88     {
89         Thread msbRegisterThread = new Thread(new MsbServiceRegister());
90         msbRegisterThread.setName("Register Service 2 MSB");
91         msbRegisterThread.start();
92     }
93
94 }