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