Update ves-agent dependency
[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 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;
39
40 import java.net.InetAddress;
41 import java.net.UnknownHostException;
42 import java.util.ArrayList;
43 import java.util.List;
44
45 public class EmsDriverApplication extends Application<EmsDriverConfiguration> {
46
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;
50
51     public static void main(String[] args) throws Exception {
52         logConfig();
53         log.info("EmsDriverApplication start");
54         new EmsDriverApplication().run(args);
55         log.info("EmsDriverApplication start sucess");
56     }
57
58     @Override
59     protected void bootstrapLogging() {
60     }
61     
62     public static void logConfig(){
63         PropertyConfigurator.configure(LOGCONFIG_PROPERTIES_LOCATION);
64     }
65     
66     @Override
67     public String getName() {
68         return "ems-driver";
69     }
70
71     @Override
72     public void initialize(Bootstrap<EmsDriverConfiguration> bootstrap) {
73         // nothing to do yet
74         context = new FileSystemXmlApplicationContext("file:" + Constant.SYS_CFG + "spring.xml");
75         bootstrap.addBundle(new AssetsBundle("/api-doc", "/api-doc", "index.html", "api-doc"));
76     }
77
78     @Override
79     public void run(EmsDriverConfiguration configuration, Environment environment) {
80         // register CommandResource
81         environment.jersey().register(new CommandResource());
82         MsbConfiguration.setMsbAddress(configuration.getMsbAddress());
83         //MSB register
84         String registerFlag = configuration.getAutoServiceRegister();
85         if ("false".equalsIgnoreCase(registerFlag)) {
86             this.msbRegisteEmsDriverService(configuration);
87         }
88         //Start workThread
89         this.startThread();
90         initSwaggerConfig(environment, configuration);
91     }
92
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);
98             if (thread == null) {
99                 log.error(threadName + "Thread start error,system exit");
100                 System.exit(1);
101             } else {
102                 thread.setName(threadName);
103                 thread.start();
104             }
105
106         }
107     }
108
109     // init swagger
110     private void initSwaggerConfig(Environment environment, EmsDriverConfiguration configuration) {
111         environment.jersey().register(new ApiListingResource());
112         environment.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
113
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);
126     }
127
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();
133         String ip = "";
134         try {
135             ip = InetAddress.getLocalHost().getHostAddress();
136         } catch (UnknownHostException e) {
137             log.error("Unable to get host ip: ", e);
138         }
139         if ("".equals(ip)) {
140             ip = connector.getBindHost();
141         }
142         serviceNode.setIp(ip);
143         serviceNode.setPort(String.valueOf(connector.getPort()));
144         serviceNode.setTtl(0);
145
146         List<ServiceNodeVo> nodeList = new ArrayList<>();
147         nodeList.add(serviceNode);
148         registerVo.setServiceName("emsdriver");
149         registerVo.setUrl("/api/emsdriver/v1");
150         registerVo.setNodes(nodeList);
151
152         MsbRestServiceProxy.registerService(registerVo);
153         log.info("register vfc-emsdriver service to msb finished.");
154
155     }
156
157
158 }