aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/CatalogApp.java
blob: 4764c7b485e027d6faefd8d8d36ec24e3cfe6d75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
 * Copyright 2016 [ZTE] and others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openo.commontosca.catalog;

import com.fasterxml.jackson.annotation.JsonInclude;

import io.dropwizard.Application;
import io.dropwizard.assets.AssetsBundle;
import io.dropwizard.db.DataSourceFactory;
import io.dropwizard.hibernate.HibernateBundle;
import io.dropwizard.migrations.MigrationsBundle;
import io.dropwizard.server.SimpleServerFactory;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import io.swagger.jaxrs.config.BeanConfig;
import io.swagger.jaxrs.listing.ApiListingResource;

import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.openo.commontosca.catalog.common.Config;
import org.openo.commontosca.catalog.common.HttpServerAddrConfig;
import org.openo.commontosca.catalog.common.HttpServerPathConfig;
import org.openo.commontosca.catalog.common.MsbAddrConfig;
import org.openo.commontosca.catalog.common.ServiceRegistrer;
import org.openo.commontosca.catalog.common.YmalParseBaseAddrConfig;
import org.openo.commontosca.catalog.db.dao.DaoManager;
import org.openo.commontosca.catalog.db.entity.NodeTemplateData;
import org.openo.commontosca.catalog.db.entity.PackageData;
import org.openo.commontosca.catalog.db.entity.ServiceTemplateData;
import org.openo.commontosca.catalog.db.entity.ServiceTemplateMappingData;
import org.openo.commontosca.catalog.health.ConsoleHealthCheck;
import org.openo.commontosca.catalog.resources.PackageResource;
import org.openo.commontosca.catalog.resources.TemplateResource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class CatalogApp extends Application<CatalogAppConfiguration> {

  private static final Logger LOGGER = LoggerFactory.getLogger(CatalogApp.class);

  public static void main(String[] args) throws Exception {
    new CatalogApp().run(args);
  }

  @Override
  public String getName() {
    return "OPENO-Catalog";
  }

  private final HibernateBundle<CatalogAppConfiguration> bundle =
      new HibernateBundle<CatalogAppConfiguration>(ServiceTemplateData.class, PackageData.class,
          NodeTemplateData.class, ServiceTemplateMappingData.class) {
        @Override
        public DataSourceFactory getDataSourceFactory(CatalogAppConfiguration configuration) {
          return configuration.getDataSourceFactory();
        }
      };

  @Override
  public void initialize(Bootstrap<CatalogAppConfiguration> bootstrap) {
    bootstrap.addBundle(new AssetsBundle("/api-doc", "/api-doc", "index.html", "api-doc"));
    initDb(bootstrap);
  }

  private void initDao() {
    DaoManager.getInstance().setSessionFactory(bundle.getSessionFactory());
  }

  private void initDb(Bootstrap<CatalogAppConfiguration> bootstrap) {
    bootstrap.addBundle(bundle);
    bootstrap.addBundle(new MigrationsBundle<CatalogAppConfiguration>() {
      @Override
      public DataSourceFactory getDataSourceFactory(CatalogAppConfiguration configuration) {
        return configuration.getDataSourceFactory();
      }
    });
  }

  @Override
  public void run(CatalogAppConfiguration configuration, Environment environment) {
    LOGGER.info("Start to initialize catalogue.");
    MsbAddrConfig.setMsbAddress(configuration.getMsbServerAddr());
    HttpServerAddrConfig.setHttpServerAddress(configuration.getHttpServerAddr());
    HttpServerPathConfig.setHttpServerPath(configuration.getHttpServerPath());
    YmalParseBaseAddrConfig.setYmalParseBaseAddr(configuration.getYamlParseAddr());
    initDao();
    final ConsoleHealthCheck healthCheck = new ConsoleHealthCheck(configuration.getTemplate());
    environment.healthChecks().register("template", healthCheck);

    environment.jersey().register(new PackageResource());
    environment.jersey().register(new TemplateResource());
    // environment.jersey().register(new VNFHostImageResource());
    // environment.jersey().register(new VNFSoftwareVersionResource());

    // register rest interface
    environment.jersey().packages("org.openo.commontosca.catalog.resources");
    // upload file by inputstream need to register MultiPartFeature
    environment.jersey().register(MultiPartFeature.class);

    initSwaggerConfig(environment, configuration);
    //    initCometd(environment);
    Config.setConfigration(configuration);
    initService();
    LOGGER.info("Initialize catalogue finished.");
  }

  /**
   * initialize swagger configuration.
   * 
   * @param environment environment information
   * @param configuration catalogue configuration
   */
  private void initSwaggerConfig(Environment environment, CatalogAppConfiguration configuration) {
    environment.jersey().register(new ApiListingResource());
    environment.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);

    BeanConfig config = new BeanConfig();
    config.setTitle("Open-o Catalog Service rest API");
    config.setVersion("1.0.0");
    config.setResourcePackage("org.openo.commontosca.catalog.resources");
    // set rest api basepath in swagger
    SimpleServerFactory simpleServerFactory =
        (SimpleServerFactory) configuration.getServerFactory();
    String basePath = simpleServerFactory.getApplicationContextPath();
    String rootPath = simpleServerFactory.getJerseyRootPath();
    rootPath = rootPath.substring(0, rootPath.indexOf("/*"));
    basePath =
        basePath.equals("/") ? rootPath : (new StringBuilder()).append(basePath).append(rootPath)
            .toString();
    config.setBasePath(basePath);
    config.setScan(true);
  }

  private void initService() {
    Thread registerCatalogService = new Thread(new ServiceRegistrer());
    registerCatalogService.setName("register catalog service to Microservice Bus");
    registerCatalogService.start();
  }

//   /**
//   * initialize cometd server.
//   * 
//   * @param environment environment information
//   */
//  private void initCometd(Environment environment) {
//    // add filter
//    environment.getApplicationContext().addFilter(CrossOriginFilter.class,
//        "/api/nsoccataloguenotification/v1/*",
//        EnumSet.of(DispatcherType.REQUEST, DispatcherType.ERROR));
//    // add servlet
//    environment.getApplicationContext()
//        .addServlet("org.cometd.server.CometDServlet", "/api/nsoccataloguenotification/v1/*")
//        .setInitOrder(1);
//    // add servlet
//    environment.getApplicationContext()
//        .addServlet("CometdServlet", "/api/nsoccataloguenotification/v1").setInitOrder(2);
//  }
}