2 * Copyright 2017 BOCO Corporation. CMCC Technologies Co., Ltd
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.vfc.nfvo.emsdriver;
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;
27 import java.net.InetAddress;
28 import java.net.UnknownHostException;
29 import java.util.ArrayList;
30 import java.util.List;
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;
44 import com.fasterxml.jackson.annotation.JsonInclude;
46 public class EmsDriverApplication extends Application<EmsDriverConfiguration> {
48 protected static Log log = LogFactory.getLog(EmsDriverApplication.class);
49 private ApplicationContext context = null;
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");
58 public String getName() {
63 public void initialize(Bootstrap<EmsDriverConfiguration> bootstrap) {
65 context = new FileSystemXmlApplicationContext("file:" + Constant.SYS_CFG+ "spring.xml");
66 bootstrap.addBundle(new AssetsBundle("/api-doc", "/api-doc", "index.html", "api-doc"));
70 public void run(EmsDriverConfiguration configuration,Environment environment) {
71 // register CommandResource
72 environment.jersey().register(new CommandResource());
73 MsbConfiguration.setMsbAddress(configuration.getMsbAddress());
75 String registerFlag = configuration.getAutoServiceRegister();
76 if("false".equalsIgnoreCase(registerFlag)){
77 this.msbRegisteEmsDriverService(configuration);
81 initSwaggerConfig(environment, configuration);
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);
90 log.error(threadName + "Thread start error,system exit");
93 thread.setName(threadName);
100 private void initSwaggerConfig(Environment environment, EmsDriverConfiguration configuration)
102 environment.jersey().register(new ApiListingResource());
103 environment.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
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);
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();
126 ip = InetAddress.getLocalHost().getHostAddress();
127 } catch (UnknownHostException e) {
128 log.error("Unable to get host ip: ",e);
131 ip = connector.getBindHost();
133 serviceNode.setIp(ip);
134 serviceNode.setPort(String.valueOf(connector.getPort()));
135 serviceNode.setTtl(0);
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);
143 MsbRestServiceProxy.registerService(registerVo);
144 log.info("register vfc-emsdriver service to msb finished.");