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 new EmsDriverApplication().run(args);
56 public String getName() {
61 public void initialize(Bootstrap<EmsDriverConfiguration> bootstrap) {
63 context = new FileSystemXmlApplicationContext("file:" + Constant.SYS_CFG+ "spring.xml");
64 bootstrap.addBundle(new AssetsBundle("/api-doc", "/api-doc", "index.html", "api-doc"));
68 public void run(EmsDriverConfiguration configuration,Environment environment) {
69 // register CommandResource
70 environment.jersey().register(new CommandResource());
71 MsbConfiguration.setMsbAddress(configuration.getMsbAddress());
73 String registerFlag = configuration.getAutoServiceRegister();
74 if("false".equalsIgnoreCase(registerFlag)){
75 this.msbRegisteEmsDriverService(configuration);
79 initSwaggerConfig(environment, configuration);
82 private void startThread(){
83 String[] allThreadName = context.getBeanNamesForType(DriverThread.class);
84 log.info("worker num :" + allThreadName.length);
85 for (String threadName : allThreadName) {
86 DriverThread thread = (DriverThread) context.getBean(threadName);
88 log.error(threadName + "Thread start error,system exit");
91 thread.setName(threadName);
98 private void initSwaggerConfig(Environment environment, EmsDriverConfiguration configuration)
100 environment.jersey().register(new ApiListingResource());
101 environment.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
103 BeanConfig config = new BeanConfig();
104 config.setTitle(" Console Service rest API");
105 config.setVersion("1.0.0");
106 config.setResourcePackage("org.onap.vfc.nfvo.emsdriver.northbound.service");
107 //swagger rest api basepath
108 SimpleServerFactory simpleServerFactory = (SimpleServerFactory)configuration.getServerFactory();
109 String basePath = simpleServerFactory.getApplicationContextPath();
110 String rootPath = simpleServerFactory.getJerseyRootPath().get();
111 rootPath = rootPath.substring(0, rootPath.indexOf("/*"));
112 basePath = basePath.equals("/") ? rootPath : (new StringBuilder()).append(basePath).append(rootPath).toString();
113 config.setBasePath(basePath);
114 config.setScan(true);
117 private void msbRegisteEmsDriverService(EmsDriverConfiguration configuration) {
118 SimpleServerFactory simpleServerFactory = (SimpleServerFactory)configuration.getServerFactory();
119 HttpConnectorFactory connector = (HttpConnectorFactory)simpleServerFactory.getConnector();
120 MsbRegisterVo registerVo = new MsbRegisterVo();
121 ServiceNodeVo serviceNode = new ServiceNodeVo();
124 ip = InetAddress.getLocalHost().getHostAddress();
125 } catch (UnknownHostException e) {
126 log.error("Unable to get host ip: ",e);
129 ip = connector.getBindHost();
131 serviceNode.setIp(ip);
132 serviceNode.setPort(String.valueOf(connector.getPort()));
133 serviceNode.setTtl(0);
135 List<ServiceNodeVo> nodeList = new ArrayList<ServiceNodeVo>();
136 nodeList.add(serviceNode);
137 registerVo.setServiceName("emsdriver");
138 registerVo.setUrl("/api/emsdriver/v1");
139 registerVo.setNodes(nodeList);
141 MsbRestServiceProxy.registerService(registerVo);
142 log.info("register vfc-emsdriver service to msb finished.");