Modify package structure and pom file
[vfc/nfvo/driver/ems.git] / ems / boco / src / main / java / org / onap / vfc / nfvo / emsdriver / northbound / service / 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.northbound.service;
17
18 import io.dropwizard.Application;
19 import io.dropwizard.jetty.HttpConnectorFactory;
20 import io.dropwizard.server.DefaultServerFactory;
21 import io.dropwizard.setup.Bootstrap;
22 import io.dropwizard.setup.Environment;
23
24 import java.net.InetAddress;
25 import java.net.UnknownHostException;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.onap.vfc.nfvo.emsdriver.serviceregister.MsbConfiguration;
32 import org.onap.vfc.nfvo.emsdriver.serviceregister.MsbRestServiceProxy;
33 import org.onap.vfc.nfvo.emsdriver.serviceregister.model.MsbRegisterVo;
34 import org.onap.vfc.nfvo.emsdriver.serviceregister.model.ServiceNodeVo;
35
36 public class EmsDriverApplication extends Application<EmsDriverConfiguration> {
37         
38         protected static Log log = LogFactory.getLog(EmsDriverApplication.class);
39         
40         public static void main(String[] args) throws Exception {
41         new EmsDriverApplication().run(args);
42     }
43
44     @Override
45     public String getName() {
46         return "ems-driver";
47     }
48
49     @Override
50     public void initialize(Bootstrap<EmsDriverConfiguration> bootstrap) {
51         // nothing to do yet
52     }
53
54     @Override
55     public void run(EmsDriverConfiguration configuration,Environment environment) {
56         // register CommandResource
57         environment.jersey().register(new CommandResource());
58         
59         
60         MsbConfiguration.setMsbAddress(configuration.getMsbAddress());
61         //MSB register
62         this.msbRegisteEmsDriverService(configuration);
63     }
64
65         private void msbRegisteEmsDriverService(EmsDriverConfiguration configuration) {
66                 DefaultServerFactory defaultServerFactory = (DefaultServerFactory)configuration.getServerFactory();
67         HttpConnectorFactory connector = (HttpConnectorFactory)defaultServerFactory.getAdminConnectors().get(0);
68                 MsbRegisterVo registerVo = new MsbRegisterVo();
69                 ServiceNodeVo serviceNode = new ServiceNodeVo();
70                 String ip = "";
71                 try {
72                         ip = InetAddress.getLocalHost().getHostAddress();
73                 } catch (UnknownHostException e) {
74                         log.error("Unable to get host ip: " + e.getMessage());
75                 }
76                 if(ip.equals("")){
77                         ip = connector.getBindHost();
78                 }
79                 serviceNode.setIp(ip);
80                 serviceNode.setPort(String.valueOf(connector.getPort()));
81                 serviceNode.setTtl(0);
82                 
83                 List<ServiceNodeVo> nodeList =  new ArrayList<ServiceNodeVo>();
84                 nodeList.add(serviceNode);
85                 registerVo.setServiceName("emsdriver");
86                 registerVo.setUrl("/api/emsdriver/v1");
87                 registerVo.setNodes(nodeList);
88                 
89                 MsbRestServiceProxy.registerService(registerVo);
90                 log.info("register monitor-umc service to msb finished.");
91                 
92         }
93 }