Fix the bug about register MSB.
[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
19 import org.onap.aai.esr.common.MsbConfig;
20 import org.onap.aai.esr.externalservice.msb.MsbHelper;
21 import org.onap.aai.esr.resource.EmsManager;
22 import org.onap.aai.esr.resource.ServiceTest;
23 import org.onap.aai.esr.resource.ThirdpartySdncManager;
24 import org.onap.aai.esr.resource.VimManager;
25 import org.onap.aai.esr.resource.VnfmManager;
26 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import io.dropwizard.Application;
30 import io.dropwizard.server.SimpleServerFactory;
31 import io.dropwizard.setup.Environment;
32 import io.swagger.jaxrs.config.BeanConfig;
33 import io.swagger.jaxrs.listing.ApiListingResource;
34
35 import com.fasterxml.jackson.annotation.JsonInclude;
36
37 public class ExtsysApp extends Application<ExtsysAppConfiguration> {
38
39     private static final Logger LOGGER = LoggerFactory.getLogger(ExtsysApp.class);
40
41     public static void main(String[] args) throws Exception {
42         new ExtsysApp().run(args);
43     }
44
45     @Override
46     public String getName() {
47         return "ONAP-ESR";
48     }
49
50     @Override
51     public void run(ExtsysAppConfiguration configuration, Environment environment) {
52         LOGGER.info("Start to initialize esr.");
53         MsbConfig.setMsbServerAddr(configuration.getMsbServerAddr());
54         MsbConfig.setMsbDiscoveryIp(configuration.getMsbDiscoveryIp());
55         MsbConfig.setMsbDiscoveryPort(configuration.getMsbDiscoveryPort());
56         environment.jersey().register(new EmsManager());
57         environment.jersey().register(new ThirdpartySdncManager());
58         environment.jersey().register(new VimManager());
59         environment.jersey().register(new VnfmManager());
60         environment.jersey().register(new ServiceTest());
61         initSwaggerConfig(environment, configuration);
62         if ("false".equals(configuration.getRegistByHand())) {
63             String MSB_IP = configuration.getMsbDiscoveryIp();
64             Integer MSB_Port = Integer.valueOf(configuration.getMsbDiscoveryPort());
65             MSBServiceClient msbClient = new MSBServiceClient(MSB_IP, MSB_Port);
66             MsbHelper helper = new MsbHelper(msbClient);
67             try {
68                 helper.registerMsb();
69                 LOGGER.info("Register esr-server to msb by java-sdk finished");
70             } catch (Exception e) {
71                 LOGGER.error("Register esr-server to msb by java-sdk failed", e);
72             }
73         }
74         LOGGER.info("Initialize extsys finished.");
75     }
76     
77     /**
78      * initialize swagger configuration.
79      * 
80      * @param environment environment information
81      * @param configuration catalogue configuration
82      */
83     private void initSwaggerConfig(Environment environment, ExtsysAppConfiguration configuration) {
84         environment.jersey().register(new ApiListingResource());
85         environment.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
86
87         BeanConfig config = new BeanConfig();
88         config.setTitle("ONAP ESR Service rest API");
89         config.setVersion("1.0.0");
90         config.setResourcePackage("org.onap.aai.esr.resource");
91         // set rest api basepath in swagger
92         SimpleServerFactory simpleServerFactory =
93                 (SimpleServerFactory) configuration.getServerFactory();
94         String basePath = simpleServerFactory.getApplicationContextPath();
95         String rootPath = simpleServerFactory.getJerseyRootPath().get();
96         rootPath = rootPath.substring(0, rootPath.indexOf("/*"));
97         basePath =
98                 basePath.equals("/") ? rootPath : (new StringBuilder()).append(basePath)
99                         .append(rootPath).toString();
100         LOGGER.info("getApplicationContextPath: " + basePath);
101         config.setBasePath(basePath);
102         config.setScan(true);
103     }
104     
105 }