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.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.apache.log4j.PropertyConfigurator;
30 import org.onap.vfc.nfvo.emsdriver.commons.constant.Constant;
31 import org.onap.vfc.nfvo.emsdriver.commons.utils.DriverThread;
32 import org.onap.vfc.nfvo.emsdriver.northbound.service.CommandResource;
33 import org.onap.vfc.nfvo.emsdriver.serviceregister.MsbConfiguration;
34 import org.onap.vfc.nfvo.emsdriver.serviceregister.MsbRestServiceProxy;
35 import org.onap.vfc.nfvo.emsdriver.serviceregister.model.MsbRegisterVo;
36 import org.onap.vfc.nfvo.emsdriver.serviceregister.model.ServiceNodeVo;
37 import org.springframework.context.ApplicationContext;
38 import org.springframework.context.support.FileSystemXmlApplicationContext;
40 import java.net.InetAddress;
41 import java.net.UnknownHostException;
42 import java.util.ArrayList;
43 import java.util.List;
45 public class EmsDriverApplication extends Application<EmsDriverConfiguration> {
47 protected static Logger log = LoggerFactory.getLogger(EmsDriverApplication.class);
48 public static String LOGCONFIG_PROPERTIES_LOCATION = Constant.SYS_CFG + "log4j.properties";
49 private ApplicationContext context = null;
51 public static void main(String[] args) throws Exception {
53 log.info("EmsDriverApplication start");
54 new EmsDriverApplication().run(args);
55 log.info("EmsDriverApplication start sucess");
59 protected void bootstrapLogging() {
62 public static void logConfig(){
63 PropertyConfigurator.configure(LOGCONFIG_PROPERTIES_LOCATION);
67 public String getName() {
72 public void initialize(Bootstrap<EmsDriverConfiguration> bootstrap) {
74 context = new FileSystemXmlApplicationContext("file:" + Constant.SYS_CFG + "spring.xml");
75 bootstrap.addBundle(new AssetsBundle("/api-doc", "/api-doc", "index.html", "api-doc"));
79 public void run(EmsDriverConfiguration configuration, Environment environment) {
80 // register CommandResource
81 environment.jersey().register(new CommandResource());
82 MsbConfiguration.setMsbAddress(configuration.getMsbAddress());
84 String registerFlag = configuration.getAutoServiceRegister();
85 if ("false".equalsIgnoreCase(registerFlag)) {
86 this.msbRegisteEmsDriverService(configuration);
90 initSwaggerConfig(environment, configuration);
93 private void startThread() {
94 String[] allThreadName = context.getBeanNamesForType(DriverThread.class);
95 log.info("worker num :" + allThreadName.length);
96 for (String threadName : allThreadName) {
97 DriverThread thread = (DriverThread) context.getBean(threadName);
99 log.error(threadName + "Thread start error,system exit");
102 thread.setName(threadName);
110 private void initSwaggerConfig(Environment environment, EmsDriverConfiguration configuration) {
111 environment.jersey().register(new ApiListingResource());
112 environment.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
114 BeanConfig config = new BeanConfig();
115 config.setTitle(" Console Service rest API");
116 config.setVersion("1.0.0");
117 config.setResourcePackage("org.onap.vfc.nfvo.emsdriver.northbound.service");
118 //swagger rest api basepath
119 SimpleServerFactory simpleServerFactory = (SimpleServerFactory) configuration.getServerFactory();
120 String basePath = simpleServerFactory.getApplicationContextPath();
121 String rootPath = simpleServerFactory.getJerseyRootPath().get();
122 rootPath = rootPath.substring(0, rootPath.indexOf("/*"));
123 basePath ="/".equals(basePath) ? rootPath : (new StringBuilder()).append(basePath).append(rootPath).toString();
124 config.setBasePath(basePath);
125 config.setScan(true);
128 private void msbRegisteEmsDriverService(EmsDriverConfiguration configuration) {
129 SimpleServerFactory simpleServerFactory = (SimpleServerFactory) configuration.getServerFactory();
130 HttpConnectorFactory connector = (HttpConnectorFactory) simpleServerFactory.getConnector();
131 MsbRegisterVo registerVo = new MsbRegisterVo();
132 ServiceNodeVo serviceNode = new ServiceNodeVo();
135 ip = InetAddress.getLocalHost().getHostAddress();
136 } catch (UnknownHostException e) {
137 log.error("Unable to get host ip: ", e);
140 ip = connector.getBindHost();
142 serviceNode.setIp(ip);
143 serviceNode.setPort(String.valueOf(connector.getPort()));
144 serviceNode.setTtl(0);
146 List<ServiceNodeVo> nodeList = new ArrayList<>();
147 nodeList.add(serviceNode);
148 registerVo.setServiceName("emsdriver");
149 registerVo.setUrl("/api/emsdriver/v1");
150 registerVo.setNodes(nodeList);
152 MsbRestServiceProxy.registerService(registerVo);
153 log.info("register vfc-emsdriver service to msb finished.");