Simplify esr-server project level.
[aai/esr-server.git] / esr-mgr / src / main / java / org / onap / aai / esr / ExtsysApp.java
1 /**
2  * Copyright 2016-2017 ZTE Corporation.
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.onap.aai.esr;
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
27 import org.glassfish.jersey.media.multipart.MultiPartFeature;
28 import org.onap.aai.esr.common.Config;
29 import org.onap.aai.esr.externalservice.msb.MsbHelper;
30 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public class ExtsysApp extends Application<ExtsysAppConfiguration> {
35
36   private static final Logger LOGGER = LoggerFactory.getLogger(ExtsysApp.class);
37
38   public static void main(String[] args) throws Exception {
39     new ExtsysApp().run(args);
40   }
41
42   @Override
43   public String getName() {
44     return "ONAP-ESR";
45   }
46
47
48   @Override
49   public void initialize(Bootstrap<ExtsysAppConfiguration> bootstrap) {
50     bootstrap.addBundle(new AssetsBundle("/api-doc", "/api-doc", "index.html", "api-doc"));
51   }
52
53   @Override
54   public void run(ExtsysAppConfiguration configuration, Environment environment) {
55     LOGGER.info("Start to initialize esr.");    
56     environment.jersey().packages("org.onap.aai.esr.resource");
57     environment.jersey().register(MultiPartFeature.class);
58     String MSB_IP=configuration.getMsbIp();
59     int MSB_Port=configuration.getMsbPort();
60     
61     initSwaggerConfig(environment, configuration);
62     Config.setConfigration(configuration);
63     MSBServiceClient msbClient = new MSBServiceClient(MSB_IP, MSB_Port);
64
65     MsbHelper helper = new MsbHelper(msbClient);
66     try {
67       helper.registerMsb();
68     } catch (Exception e) {
69       LOGGER.error("Register ESR to msb failed", e);
70     }
71     
72     LOGGER.info("Initialize extsys finished.");
73   }
74
75   /**
76    * initialize swagger configuration.
77    * 
78    * @param environment environment information
79    * @param configuration configuration
80    */
81   private void initSwaggerConfig(Environment environment, ExtsysAppConfiguration configuration) {
82     environment.jersey().register(new ApiListingResource());
83     environment.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
84
85     BeanConfig config = new BeanConfig();
86     config.setTitle("Open-o ExtSys Service rest API");
87     config.setVersion("1.0.0");
88     config.setResourcePackage("org.onap.aai.esr.resource");
89     // set rest api basepath in swagger
90     SimpleServerFactory simpleServerFactory =
91         (SimpleServerFactory) configuration.getServerFactory();
92     String basePath = simpleServerFactory.getApplicationContextPath();
93     String rootPath = simpleServerFactory.getJerseyRootPath();
94     rootPath = rootPath.substring(0, rootPath.indexOf("/*"));
95     basePath = basePath.equals("/") ? rootPath
96         : (new StringBuilder()).append(basePath).append(rootPath).toString();
97     config.setBasePath(basePath);
98     config.setScan(true);
99   }
100
101 }