add msb config
[usecase-ui/server.git] / server / src / main / java / org / onap / usecaseui / server / UsecaseuiServerApplication.java
1 /*
2  * Copyright (C) 2017 CMCC, Inc. and others. All rights reserved.
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.usecaseui.server;
17
18 import org.onap.msb.sdk.discovery.common.RouteException;
19 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
20 import org.onap.msb.sdk.discovery.entity.Node;
21 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
22 import org.onap.usecaseui.server.util.RestfulServices;
23 import org.springframework.boot.SpringApplication;
24 import org.springframework.boot.autoconfigure.SpringBootApplication;
25 import org.springframework.context.annotation.Bean;
26 import org.springframework.context.annotation.ComponentScan;
27 import org.springframework.web.client.RestTemplate;
28
29 import java.net.InetAddress;
30 import java.net.UnknownHostException;
31 import java.util.HashSet;
32 import java.util.Set;
33
34 @SpringBootApplication
35 @ComponentScan(basePackages = "org.onap.usecaseui.server")
36 public class UsecaseuiServerApplication {
37
38     @Bean
39     public RestTemplate getRestTemplate(){
40         return new RestTemplate();
41     }
42     
43         public static void main(String[] args) {
44                 SpringApplication.run(UsecaseuiServerApplication.class, args);
45         String msbUrl = RestfulServices.getMsbAddress();
46         if (msbUrl.contains(":")) {
47             String[] ipAndPort = msbUrl.split(":");
48             MSBServiceClient msbClient = new MSBServiceClient(ipAndPort[0], Integer.parseInt(ipAndPort[1]));
49
50             MicroServiceInfo msinfo = new MicroServiceInfo();
51             msinfo.setServiceName("usecase-ui-server");
52             msinfo.setVersion("v1");
53             msinfo.setUrl("/api/usecaseui/server/v1");
54             msinfo.setProtocol("REST");
55             msinfo.setVisualRange("0|1");
56
57             try {
58                 Set<Node> nodes = new HashSet<>();
59                 Node node1 = new Node();
60                 node1.setIp(InetAddress.getLocalHost().getHostAddress());
61                 node1.setPort("8082");
62                 nodes.add(node1);
63                 msinfo.setNodes(nodes);
64                 msbClient.registerMicroServiceInfo(msinfo, false);
65             } catch (UnknownHostException e) {
66                 e.printStackTrace();
67             } catch (RouteException e) {
68                 e.printStackTrace();
69             }
70         }
71         }
72         
73 }