dabf2033f1cf3c58029dacfba95b4f5140cb9885
[vfc/nfvo/driver/ems.git] / ems / boco / src / main / java / org / onap / vfc / nfvo / emsdriver / EmsDriverApplication.java
1 /**
2  * Copyright 2017 BOCO Corporation.  CMCC Technologies Co., Ltd
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.vfc.nfvo.emsdriver;
17
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;
24
25 import java.net.InetAddress;
26 import java.net.UnknownHostException;
27 import java.util.ArrayList;
28 import java.util.List;
29
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;
41
42 public class EmsDriverApplication extends Application<EmsDriverConfiguration> {
43         
44         protected static Log log = LogFactory.getLog(EmsDriverApplication.class);
45         public static ApplicationContext context = null;
46         
47         public static void main(String[] args) throws Exception {
48         new EmsDriverApplication().run(args);
49     }
50
51     @Override
52     public String getName() {
53         return "ems-driver";
54     }
55
56     @Override
57     public void initialize(Bootstrap<EmsDriverConfiguration> bootstrap) {
58         // nothing to do yet
59         context = new FileSystemXmlApplicationContext("file:" + Constant.SYS_CFG+ "spring.xml");
60     }
61
62     @Override
63     public void run(EmsDriverConfiguration configuration,Environment environment) {
64         // register CommandResource
65         environment.jersey().register(new CommandResource());
66         MsbConfiguration.setMsbAddress(configuration.getMsbAddress());
67         //MSB register
68         String registerFlag = configuration.getAutoServiceRegister();
69         if(registerFlag.equalsIgnoreCase("false")){
70                 this.msbRegisteEmsDriverService(configuration);
71         }
72         //Start workThread
73         this.startThread();
74     }
75     
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);
81                         if (thread == null) {
82                                 log.error(threadName + "Thread start error,system exit");
83                                 System.exit(1);
84                         }
85                         thread.setName(threadName);
86                         thread.start();
87                 }
88     }
89
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();
95                 String ip = "";
96                 try {
97                         ip = InetAddress.getLocalHost().getHostAddress();
98                 } catch (UnknownHostException e) {
99                         log.error("Unable to get host ip: " + e.getMessage());
100                 }
101                 if(ip.equals("")){
102                         ip = connector.getBindHost();
103                 }
104                 serviceNode.setIp(ip);
105                 serviceNode.setPort(String.valueOf(connector.getPort()));
106                 serviceNode.setTtl(0);
107                 
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);
113                 
114                 MsbRestServiceProxy.registerService(registerVo);
115                 log.info("register vfc-emsdriver service to msb finished.");
116                 
117         }
118         
119         
120 }