Upload the ESR server seed code.
[aai/esr-server.git] / esr-core / esr-mgr / src / main / java / org / onap / aai / esr / ExtsysApp.java
1 /**
2  * Copyright 2016 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.db.DataSourceFactory;
22 import io.dropwizard.hibernate.HibernateBundle;
23 import io.dropwizard.server.SimpleServerFactory;
24 import io.dropwizard.setup.Bootstrap;
25 import io.dropwizard.setup.Environment;
26 import io.swagger.jaxrs.config.BeanConfig;
27 import io.swagger.jaxrs.listing.ApiListingResource;
28
29 import org.glassfish.jersey.media.multipart.MultiPartFeature;
30 import org.onap.aai.esr.common.Config;
31 import org.onap.aai.esr.common.ServiceRegistrer;
32 import org.onap.aai.esr.dao.DaoManager;
33 import org.onap.aai.esr.entity.db.BaseData;
34 import org.onap.aai.esr.entity.db.EmsData;
35 import org.onap.aai.esr.entity.db.SdncData;
36 import org.onap.aai.esr.entity.db.VimData;
37 import org.onap.aai.esr.entity.db.VnfmData;
38 import org.onap.aai.esr.hibernate.HibernateBundleAgent;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class ExtsysApp extends Application<ExtsysAppConfiguration> {
43
44   private static final Logger LOGGER = LoggerFactory.getLogger(ExtsysApp.class);
45
46   public static void main(String[] args) throws Exception {
47     new ExtsysApp().run(args);
48   }
49
50   @Override
51   public String getName() {
52     return "ONAP-ESR";
53   }
54
55   private final HibernateBundleAgent bundle = new HibernateBundleAgent();
56
57   @Override
58   public void initialize(Bootstrap<ExtsysAppConfiguration> bootstrap) {
59     bootstrap.addBundle(new AssetsBundle("/api-doc", "/api-doc", "index.html", "api-doc"));
60     initDb(bootstrap);
61   }
62
63   private void initService() {
64     Thread registerExtsysService = new Thread(new ServiceRegistrer());
65     registerExtsysService.setName("register extsys service to Microservice Bus");
66     registerExtsysService.start();
67   }
68
69   private void initDb(Bootstrap<ExtsysAppConfiguration> bootstrap) {
70     bootstrap.addBundle(bundle);
71   }
72
73   @Override
74   public void run(ExtsysAppConfiguration configuration, Environment environment) {
75     LOGGER.info("Start to initialize extsys.");    
76     environment.jersey().packages("org.onap.aai.esr.resource");
77     environment.jersey().register(MultiPartFeature.class);
78     initSwaggerConfig(environment, configuration);
79     Config.setConfigration(configuration);
80     initService();
81     LOGGER.info("Initialize extsys finished.");
82   }
83
84   /**
85    * initialize swagger configuration.
86    * 
87    * @param environment environment information
88    * @param configuration configuration
89    */
90   private void initSwaggerConfig(Environment environment, ExtsysAppConfiguration configuration) {
91     environment.jersey().register(new ApiListingResource());
92     environment.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
93
94     BeanConfig config = new BeanConfig();
95     config.setTitle("Open-o ExtSys Service rest API");
96     config.setVersion("1.0.0");
97     config.setResourcePackage("org.onap.aai.esr.resource");
98     // set rest api basepath in swagger
99     SimpleServerFactory simpleServerFactory =
100         (SimpleServerFactory) configuration.getServerFactory();
101     String basePath = simpleServerFactory.getApplicationContextPath();
102     String rootPath = simpleServerFactory.getJerseyRootPath();
103     rootPath = rootPath.substring(0, rootPath.indexOf("/*"));
104     basePath = basePath.equals("/") ? rootPath
105         : (new StringBuilder()).append(basePath).append(rootPath).toString();
106     config.setBasePath(basePath);
107     config.setScan(true);
108   }
109
110 }