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.DefaultServerFactory;
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;
28 import java.net.InetAddress;
29 import java.net.UnknownHostException;
30 import java.util.ArrayList;
31 import java.util.List;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.onap.vfc.nfvo.emsdriver.commons.constant.Constant;
36 import org.onap.vfc.nfvo.emsdriver.commons.utils.DriverThread;
37 import org.onap.vfc.nfvo.emsdriver.northbound.service.CommandResource;
38 import org.onap.vfc.nfvo.emsdriver.serviceregister.MsbConfiguration;
39 import org.onap.vfc.nfvo.emsdriver.serviceregister.MsbRestServiceProxy;
40 import org.onap.vfc.nfvo.emsdriver.serviceregister.model.MsbRegisterVo;
41 import org.onap.vfc.nfvo.emsdriver.serviceregister.model.ServiceNodeVo;
42 import org.springframework.context.ApplicationContext;
43 import org.springframework.context.support.FileSystemXmlApplicationContext;
45 import com.fasterxml.jackson.annotation.JsonInclude;
47 public class EmsDriverApplication extends Application<EmsDriverConfiguration> {
49 protected static Log log = LogFactory.getLog(EmsDriverApplication.class);
50 public static ApplicationContext context = null;
52 public static void main(String[] args) throws Exception {
53 new EmsDriverApplication().run(args);
57 public String getName() {
62 public void initialize(Bootstrap<EmsDriverConfiguration> bootstrap) {
64 context = new FileSystemXmlApplicationContext("file:" + Constant.SYS_CFG+ "spring.xml");
65 bootstrap.addBundle(new AssetsBundle("/api-doc", "/api-doc", "index.html", "api-doc"));
69 public void run(EmsDriverConfiguration configuration,Environment environment) {
70 // register CommandResource
71 environment.jersey().register(new CommandResource());
72 MsbConfiguration.setMsbAddress(configuration.getMsbAddress());
74 String registerFlag = configuration.getAutoServiceRegister();
75 if(registerFlag.equalsIgnoreCase("false")){
76 this.msbRegisteEmsDriverService(configuration);
80 initSwaggerConfig(environment, configuration);
83 private void startThread(){
84 String[] allThreadName = context.getBeanNamesForType(DriverThread.class);
85 log.info("worker num :" + allThreadName.length);
86 for (String threadName : allThreadName) {
87 DriverThread thread = (DriverThread) context.getBean(threadName);
89 log.error(threadName + "Thread start error,system exit");
92 thread.setName(threadName);
97 private void initSwaggerConfig(Environment environment, EmsDriverConfiguration configuration)
99 environment.jersey().register(new ApiListingResource());
100 environment.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
102 BeanConfig config = new BeanConfig();
103 config.setTitle(" Console Service rest API");
104 config.setVersion("1.0.0");
105 config.setResourcePackage("org.onap.vfc.nfvo.emsdriver.northbound.service");
106 //swagger rest api basepath
107 SimpleServerFactory simpleServerFactory = (SimpleServerFactory)configuration.getServerFactory();
108 String basePath = simpleServerFactory.getApplicationContextPath();
109 String rootPath = simpleServerFactory.getJerseyRootPath().get();
110 rootPath = rootPath.substring(0, rootPath.indexOf("/*"));
111 basePath = basePath.equals("/") ? rootPath : (new StringBuilder()).append(basePath).append(rootPath).toString();
112 config.setBasePath(basePath);
113 config.setScan(true);
116 private void msbRegisteEmsDriverService(EmsDriverConfiguration configuration) {
117 SimpleServerFactory simpleServerFactory = (SimpleServerFactory)configuration.getServerFactory();
118 HttpConnectorFactory connector = (HttpConnectorFactory)simpleServerFactory.getConnector();
119 MsbRegisterVo registerVo = new MsbRegisterVo();
120 ServiceNodeVo serviceNode = new ServiceNodeVo();
123 ip = InetAddress.getLocalHost().getHostAddress();
124 } catch (UnknownHostException e) {
125 log.error("Unable to get host ip: " + e.getMessage());
128 ip = connector.getBindHost();
130 serviceNode.setIp(ip);
131 serviceNode.setPort(String.valueOf(connector.getPort()));
132 serviceNode.setTtl(0);
134 List<ServiceNodeVo> nodeList = new ArrayList<ServiceNodeVo>();
135 nodeList.add(serviceNode);
136 registerVo.setServiceName("emsdriver");
137 registerVo.setUrl("/api/emsdriver/v1");
138 registerVo.setNodes(nodeList);
140 MsbRestServiceProxy.registerService(registerVo);
141 log.info("register vfc-emsdriver service to msb finished.");