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