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 com.fasterxml.jackson.annotation.JsonInclude;
19 import io.dropwizard.Application;
20 import io.dropwizard.assets.AssetsBundle;
21 import io.dropwizard.jetty.HttpConnectorFactory;
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 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.onap.vfc.nfvo.emsdriver.commons.constant.Constant;
30 import org.onap.vfc.nfvo.emsdriver.commons.utils.DriverThread;
31 import org.onap.vfc.nfvo.emsdriver.northbound.service.CommandResource;
32 import org.onap.vfc.nfvo.emsdriver.serviceregister.MsbConfiguration;
33 import org.onap.vfc.nfvo.emsdriver.serviceregister.MsbRestServiceProxy;
34 import org.onap.vfc.nfvo.emsdriver.serviceregister.model.MsbRegisterVo;
35 import org.onap.vfc.nfvo.emsdriver.serviceregister.model.ServiceNodeVo;
36 import org.springframework.context.ApplicationContext;
37 import org.springframework.context.support.FileSystemXmlApplicationContext;
39 import java.net.InetAddress;
40 import java.net.UnknownHostException;
41 import java.util.ArrayList;
42 import java.util.List;
44 public class EmsDriverApplication extends Application<EmsDriverConfiguration> {
46 protected static Log log = LogFactory.getLog(EmsDriverApplication.class);
47 private ApplicationContext context = null;
49 public static void main(String[] args) throws Exception {
50 log.info("EmsDriverApplication start");
51 new EmsDriverApplication().run(args);
52 log.info("EmsDriverApplication start sucess");
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);
99 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 ="/".equals(basePath) ? 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<>();
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.");