Issue-ID:VFC-384
[vfc/nfvo/driver/ems.git] / ems / boco / src / main / java / org / onap / vfc / nfvo / emsdriver / EmsDriverApplication.java
1 /**
2  * Copyright 2017 BOCO Corporation.  CMCC Technologies Co., Ltd
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.vfc.nfvo.emsdriver;
17
18 import io.dropwizard.Application;
19 import io.dropwizard.assets.AssetsBundle;
20 import io.dropwizard.jetty.HttpConnectorFactory;
21 import io.dropwizard.server.DefaultServerFactory;
22 import io.dropwizard.server.SimpleServerFactory;
23 import io.dropwizard.setup.Bootstrap;
24 import io.dropwizard.setup.Environment;
25 import io.swagger.jaxrs.config.BeanConfig;
26 import io.swagger.jaxrs.listing.ApiListingResource;
27
28 import java.net.InetAddress;
29 import java.net.UnknownHostException;
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.onap.vfc.nfvo.emsdriver.commons.constant.Constant;
36 import org.onap.vfc.nfvo.emsdriver.commons.utils.DriverThread;
37 import org.onap.vfc.nfvo.emsdriver.northbound.service.CommandResource;
38 import org.onap.vfc.nfvo.emsdriver.serviceregister.MsbConfiguration;
39 import org.onap.vfc.nfvo.emsdriver.serviceregister.MsbRestServiceProxy;
40 import org.onap.vfc.nfvo.emsdriver.serviceregister.model.MsbRegisterVo;
41 import org.onap.vfc.nfvo.emsdriver.serviceregister.model.ServiceNodeVo;
42 import org.springframework.context.ApplicationContext;
43 import org.springframework.context.support.FileSystemXmlApplicationContext;
44
45 import com.fasterxml.jackson.annotation.JsonInclude;
46
47 public class EmsDriverApplication extends Application<EmsDriverConfiguration> {
48         
49         protected static Log log = LogFactory.getLog(EmsDriverApplication.class);
50         public static ApplicationContext context = null;
51         
52         public static void main(String[] args) throws Exception {
53         new EmsDriverApplication().run(args);
54     }
55
56     @Override
57     public String getName() {
58         return "ems-driver";
59     }
60
61     @Override
62     public void initialize(Bootstrap<EmsDriverConfiguration> bootstrap) {
63         // nothing to do yet
64         context = new FileSystemXmlApplicationContext("file:" + Constant.SYS_CFG+ "spring.xml");
65         bootstrap.addBundle(new AssetsBundle("/api-doc", "/api-doc", "index.html", "api-doc"));
66     }
67
68     @Override
69     public void run(EmsDriverConfiguration configuration,Environment environment) {
70         // register CommandResource
71         environment.jersey().register(new CommandResource());
72         MsbConfiguration.setMsbAddress(configuration.getMsbAddress());
73         //MSB register
74         String registerFlag = configuration.getAutoServiceRegister();
75         if(registerFlag.equalsIgnoreCase("false")){
76                 this.msbRegisteEmsDriverService(configuration);
77         }
78         //Start workThread
79         this.startThread();
80         initSwaggerConfig(environment, configuration);
81     }
82     
83     private void startThread(){
84         String[] allThreadName = context.getBeanNamesForType(DriverThread.class);
85                 log.info("worker num :" + allThreadName.length);
86                 for (String threadName : allThreadName) {
87                         DriverThread thread = (DriverThread) context.getBean(threadName);
88                         if (thread == null) {
89                                 log.error(threadName + "Thread start error,system exit");
90                                 System.exit(1);
91                         }
92                         thread.setName(threadName);
93                         thread.start();
94                 }
95     }
96     // init swagger
97     private void initSwaggerConfig(Environment environment, EmsDriverConfiguration configuration)
98     {
99         environment.jersey().register(new ApiListingResource());
100         environment.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
101
102         BeanConfig config = new BeanConfig();
103         config.setTitle(" Console Service rest API");
104         config.setVersion("1.0.0");
105         config.setResourcePackage("org.onap.vfc.nfvo.emsdriver.northbound.service");
106         //swagger rest api basepath
107         SimpleServerFactory simpleServerFactory = (SimpleServerFactory)configuration.getServerFactory();
108         String basePath = simpleServerFactory.getApplicationContextPath();
109         String rootPath = simpleServerFactory.getJerseyRootPath().get();
110         rootPath = rootPath.substring(0, rootPath.indexOf("/*"));
111         basePath = basePath.equals("/") ? rootPath : (new StringBuilder()).append(basePath).append(rootPath).toString();
112         config.setBasePath(basePath);
113         config.setScan(true);    
114     }
115     
116         private void msbRegisteEmsDriverService(EmsDriverConfiguration configuration) {
117                 SimpleServerFactory simpleServerFactory = (SimpleServerFactory)configuration.getServerFactory();
118                 HttpConnectorFactory connector = (HttpConnectorFactory)simpleServerFactory.getConnector();
119                 MsbRegisterVo registerVo = new MsbRegisterVo();
120                 ServiceNodeVo serviceNode = new ServiceNodeVo();
121                 String ip = "";
122                 try {
123                         ip = InetAddress.getLocalHost().getHostAddress();
124                 } catch (UnknownHostException e) {
125                         log.error("Unable to get host ip: " + e.getMessage());
126                 }
127                 if(ip.equals("")){
128                         ip = connector.getBindHost();
129                 }
130                 serviceNode.setIp(ip);
131                 serviceNode.setPort(String.valueOf(connector.getPort()));
132                 serviceNode.setTtl(0);
133                 
134                 List<ServiceNodeVo> nodeList =  new ArrayList<ServiceNodeVo>();
135                 nodeList.add(serviceNode);
136                 registerVo.setServiceName("emsdriver");
137                 registerVo.setUrl("/api/emsdriver/v1");
138                 registerVo.setNodes(nodeList);
139                 
140                 MsbRestServiceProxy.registerService(registerVo);
141                 log.info("register vfc-emsdriver service to msb finished.");
142                 
143         }
144         
145         
146 }