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.jetty.HttpConnectorFactory;
20 import io.dropwizard.server.DefaultServerFactory;
21 import io.dropwizard.server.SimpleServerFactory;
22 import io.dropwizard.setup.Bootstrap;
23 import io.dropwizard.setup.Environment;
25 import java.net.InetAddress;
26 import java.net.UnknownHostException;
27 import java.util.ArrayList;
28 import java.util.List;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.onap.vfc.nfvo.emsdriver.commons.constant.Constant;
33 import org.onap.vfc.nfvo.emsdriver.commons.utils.DriverThread;
34 import org.onap.vfc.nfvo.emsdriver.northbound.service.CommandResource;
35 import org.onap.vfc.nfvo.emsdriver.serviceregister.MsbConfiguration;
36 import org.onap.vfc.nfvo.emsdriver.serviceregister.MsbRestServiceProxy;
37 import org.onap.vfc.nfvo.emsdriver.serviceregister.model.MsbRegisterVo;
38 import org.onap.vfc.nfvo.emsdriver.serviceregister.model.ServiceNodeVo;
39 import org.springframework.context.ApplicationContext;
40 import org.springframework.context.support.FileSystemXmlApplicationContext;
42 public class EmsDriverApplication extends Application<EmsDriverConfiguration> {
44 protected static Log log = LogFactory.getLog(EmsDriverApplication.class);
45 public static ApplicationContext context = null;
47 public static void main(String[] args) throws Exception {
48 new EmsDriverApplication().run(args);
52 public String getName() {
57 public void initialize(Bootstrap<EmsDriverConfiguration> bootstrap) {
59 context = new FileSystemXmlApplicationContext("file:" + Constant.SYS_CFG+ "spring.xml");
63 public void run(EmsDriverConfiguration configuration,Environment environment) {
64 // register CommandResource
65 environment.jersey().register(new CommandResource());
66 MsbConfiguration.setMsbAddress(configuration.getMsbAddress());
68 String registerFlag = configuration.getAutoServiceRegister();
69 if(registerFlag.equalsIgnoreCase("false")){
70 this.msbRegisteEmsDriverService(configuration);
76 private void startThread(){
77 String[] allThreadName = context.getBeanNamesForType(DriverThread.class);
78 log.info("worker num :" + allThreadName.length);
79 for (String threadName : allThreadName) {
80 DriverThread thread = (DriverThread) context.getBean(threadName);
82 log.error(threadName + "Thread start error,system exit");
85 thread.setName(threadName);
90 private void msbRegisteEmsDriverService(EmsDriverConfiguration configuration) {
91 SimpleServerFactory simpleServerFactory = (SimpleServerFactory)configuration.getServerFactory();
92 HttpConnectorFactory connector = (HttpConnectorFactory)simpleServerFactory.getConnector();
93 MsbRegisterVo registerVo = new MsbRegisterVo();
94 ServiceNodeVo serviceNode = new ServiceNodeVo();
97 ip = InetAddress.getLocalHost().getHostAddress();
98 } catch (UnknownHostException e) {
99 log.error("Unable to get host ip: " + e.getMessage());
102 ip = connector.getBindHost();
104 serviceNode.setIp(ip);
105 serviceNode.setPort(String.valueOf(connector.getPort()));
106 serviceNode.setTtl(0);
108 List<ServiceNodeVo> nodeList = new ArrayList<ServiceNodeVo>();
109 nodeList.add(serviceNode);
110 registerVo.setServiceName("emsdriver");
111 registerVo.setUrl("/api/emsdriver/v1");
112 registerVo.setNodes(nodeList);
114 MsbRestServiceProxy.registerService(registerVo);
115 log.info("register vfc-emsdriver service to msb finished.");