c9e0c40da4d52d645ab41322cd1117625de3f4d1
[sdc/sdc-workflow-designer.git] /
1 /**
2  * Copyright (c) 2017-2018 ZTE Corporation.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the Apache License, Version 2.0
5  * and the Eclipse Public License v1.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     ZTE - initial API and implementation and/or initial documentation
11  */
12
13 package org.onap.sdc.workflowdesigner;
14
15 import org.onap.sdc.workflowdesigner.config.AdapterType;
16 import org.onap.sdc.workflowdesigner.config.AppConfig;
17 import org.onap.sdc.workflowdesigner.resources.ExtendActivityResource;
18 import org.onap.sdc.workflowdesigner.resources.WorkflowModelerResource;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import com.fasterxml.jackson.annotation.JsonInclude;
23
24 import io.dropwizard.Application;
25 import io.dropwizard.assets.AssetsBundle;
26 import io.dropwizard.server.SimpleServerFactory;
27 import io.dropwizard.setup.Bootstrap;
28 import io.dropwizard.setup.Environment;
29 import io.swagger.jaxrs.config.BeanConfig;
30 import io.swagger.jaxrs.listing.ApiListingResource;
31
32 public class WorkflowDesignerApp extends Application<WorkflowDesignerConfiguration> {
33   private static final Logger LOGGER = LoggerFactory.getLogger(WorkflowDesignerApp.class);
34
35   public static void main(String[] args) throws Exception {
36     new WorkflowDesignerApp().run(args);
37   }
38
39   @Override
40   public String getName() {
41     return "Workflow Designer";
42   }
43
44   @Override
45   public void initialize(Bootstrap<WorkflowDesignerConfiguration> bootstrap) {
46     bootstrap.addBundle(new AssetsBundle("/api-doc", "/api-doc", "index.html", "api-doc"));
47     bootstrap.addBundle(new AssetsBundle("/workflow-modeler", "/workflow-modeler", "index.html",
48         "workflow-modeler"));
49     bootstrap.addBundle(new AssetsBundle("/workflow-modeler", "/", "index.html", "ng"));
50   }
51
52   @Override
53   public void run(WorkflowDesignerConfiguration configuration, Environment environment) {
54     LOGGER.info("Start to initialize Workflow Designer.");
55
56     saveAppConfig(configuration);
57
58     environment.jersey().register(new WorkflowModelerResource());
59     environment.jersey().register(new ExtendActivityResource());
60
61     // register rest interface
62     environment.jersey().packages("org.onap.sdc.workflowdesigner.resources");
63
64     initSwaggerConfig(environment, configuration);
65
66     LOGGER.info("Initialize catalogue finished.");
67   }
68
69   /**
70    * @param configuration
71    */
72   private void saveAppConfig(WorkflowDesignerConfiguration configuration) {
73     AppConfig.setAdapterType(AdapterType.valueOf(configuration.getAdapterType()));
74     AppConfig.setSdcServiceProxy(configuration.getSdcServiceProxy());
75     AppConfig.setActivitySpecServiceProxy(configuration.getActivitySpecServiceProxy());
76   }
77
78   /**
79    * initialize swagger configuration.
80    * 
81    * @param environment environment information
82    * @param configuration catalogue configuration
83    */
84   private void initSwaggerConfig(Environment environment,
85       WorkflowDesignerConfiguration configuration) {
86     environment.jersey().register(new ApiListingResource());
87     environment.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
88
89     BeanConfig config = new BeanConfig();
90     config.setTitle("Workflow Designer rest API");
91     config.setVersion("1.0.0");
92     config.setResourcePackage("org.onap.sdc.workflowdesigner.resources");
93
94     // set rest api basepath in swagger
95     SimpleServerFactory simpleServerFactory =
96         (SimpleServerFactory) configuration.getServerFactory();
97     String basePath = simpleServerFactory.getApplicationContextPath();
98     String rootPath = simpleServerFactory.getJerseyRootPath().get();
99     rootPath = rootPath.substring(0, rootPath.indexOf("/*"));
100     basePath = basePath.equals("/") ? rootPath
101         : (new StringBuilder()).append(basePath).append(rootPath).toString();
102     config.setBasePath(basePath);
103     config.setScan(true);
104   }
105
106 }