952ac5f903276cb93d52c2ed862b37bf55d37a45
[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.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.onap.vfc.nfvo.emsdriver.commons.constant.Constant;
30 import org.onap.vfc.nfvo.emsdriver.commons.utils.DriverThread;
31 import org.onap.vfc.nfvo.emsdriver.northbound.service.CommandResource;
32 import org.onap.vfc.nfvo.emsdriver.serviceregister.MsbConfiguration;
33 import org.onap.vfc.nfvo.emsdriver.serviceregister.MsbRestServiceProxy;
34 import org.onap.vfc.nfvo.emsdriver.serviceregister.model.MsbRegisterVo;
35 import org.onap.vfc.nfvo.emsdriver.serviceregister.model.ServiceNodeVo;
36 import org.springframework.context.ApplicationContext;
37 import org.springframework.context.support.FileSystemXmlApplicationContext;
38
39 import java.net.InetAddress;
40 import java.net.UnknownHostException;
41 import java.util.ArrayList;
42 import java.util.List;
43
44 public class EmsDriverApplication extends Application<EmsDriverConfiguration> {
45
46     protected static Log log = LogFactory.getLog(EmsDriverApplication.class);
47     private ApplicationContext context = null;
48
49     public static void main(String[] args) throws Exception {
50         log.info("EmsDriverApplication start");
51         new EmsDriverApplication().run(args);
52         log.info("EmsDriverApplication start sucess");
53     }
54
55     @Override
56     public String getName() {
57         return "ems-driver";
58     }
59
60     @Override
61     public void initialize(Bootstrap<EmsDriverConfiguration> bootstrap) {
62         // nothing to do yet
63         context = new FileSystemXmlApplicationContext("file:" + Constant.SYS_CFG + "spring.xml");
64         bootstrap.addBundle(new AssetsBundle("/api-doc", "/api-doc", "index.html", "api-doc"));
65     }
66
67     @Override
68     public void run(EmsDriverConfiguration configuration, Environment environment) {
69         // register CommandResource
70         environment.jersey().register(new CommandResource());
71         MsbConfiguration.setMsbAddress(configuration.getMsbAddress());
72         //MSB register
73         String registerFlag = configuration.getAutoServiceRegister();
74         if ("false".equalsIgnoreCase(registerFlag)) {
75             this.msbRegisteEmsDriverService(configuration);
76         }
77         //Start workThread
78         this.startThread();
79         initSwaggerConfig(environment, configuration);
80     }
81
82     private void startThread() {
83         String[] allThreadName = context.getBeanNamesForType(DriverThread.class);
84         log.info("worker num :" + allThreadName.length);
85         for (String threadName : allThreadName) {
86             DriverThread thread = (DriverThread) context.getBean(threadName);
87             if (thread == null) {
88                 log.error(threadName + "Thread start error,system exit");
89                 System.exit(1);
90             } else {
91                 thread.setName(threadName);
92                 thread.start();
93             }
94
95         }
96     }
97
98     // init swagger
99     private void initSwaggerConfig(Environment environment, EmsDriverConfiguration configuration) {
100         environment.jersey().register(new ApiListingResource());
101         environment.getObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);
102
103         BeanConfig config = new BeanConfig();
104         config.setTitle(" Console Service rest API");
105         config.setVersion("1.0.0");
106         config.setResourcePackage("org.onap.vfc.nfvo.emsdriver.northbound.service");
107         //swagger rest api basepath
108         SimpleServerFactory simpleServerFactory = (SimpleServerFactory) configuration.getServerFactory();
109         String basePath = simpleServerFactory.getApplicationContextPath();
110         String rootPath = simpleServerFactory.getJerseyRootPath().get();
111         rootPath = rootPath.substring(0, rootPath.indexOf("/*"));
112         basePath ="/".equals(basePath) ? rootPath : (new StringBuilder()).append(basePath).append(rootPath).toString();
113         config.setBasePath(basePath);
114         config.setScan(true);
115     }
116
117     private void msbRegisteEmsDriverService(EmsDriverConfiguration configuration) {
118         SimpleServerFactory simpleServerFactory = (SimpleServerFactory) configuration.getServerFactory();
119         HttpConnectorFactory connector = (HttpConnectorFactory) simpleServerFactory.getConnector();
120         MsbRegisterVo registerVo = new MsbRegisterVo();
121         ServiceNodeVo serviceNode = new ServiceNodeVo();
122         String ip = "";
123         try {
124             ip = InetAddress.getLocalHost().getHostAddress();
125         } catch (UnknownHostException e) {
126             log.error("Unable to get host ip: ", e);
127         }
128         if ("".equals(ip)) {
129             ip = connector.getBindHost();
130         }
131         serviceNode.setIp(ip);
132         serviceNode.setPort(String.valueOf(connector.getPort()));
133         serviceNode.setTtl(0);
134
135         List<ServiceNodeVo> nodeList = new ArrayList<>();
136         nodeList.add(serviceNode);
137         registerVo.setServiceName("emsdriver");
138         registerVo.setUrl("/api/emsdriver/v1");
139         registerVo.setNodes(nodeList);
140
141         MsbRestServiceProxy.registerService(registerVo);
142         log.info("register vfc-emsdriver service to msb finished.");
143
144     }
145
146
147 }