Modify the Service Addr Query Logic
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / utils / MSBRegisterUtil.java
1 /**\r
2  * Copyright 2017 ZTE Corporation.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package org.onap.holmes.common.utils;\r
18 \r
19 import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;\r
20 \r
21 import lombok.extern.slf4j.Slf4j;\r
22 import org.jvnet.hk2.annotations.Service;\r
23 import org.onap.holmes.common.config.MicroServiceConfig;\r
24 import org.onap.holmes.common.exception.CorrelationException;\r
25 import org.onap.msb.sdk.discovery.common.RouteException;\r
26 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;\r
27 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;\r
28 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;\r
29 \r
30 @Slf4j\r
31 @Service\r
32 public class MSBRegisterUtil {\r
33 \r
34     public void register2Msb(MicroServiceInfo msinfo) throws CorrelationException {\r
35         String[] msbAddrInfo = MicroServiceConfig.getMsbIpAndPort();\r
36         MSBServiceClient msbClient = new MSBServiceClient(msbAddrInfo[0],\r
37                 Integer.parseInt(msbAddrInfo[1]));\r
38 \r
39         log.info("Start to register Holmes Service to MSB...");\r
40         MicroServiceFullInfo microServiceFullInfo = null;\r
41         int retry = 0;\r
42         while (null == microServiceFullInfo && retry < 20) {\r
43             log.info("Holmes Service Registration. Retry: " + retry);\r
44             retry++;\r
45             try {\r
46                 microServiceFullInfo = msbClient.registerMicroServiceInfo(msinfo, false);\r
47             } catch (RouteException e) {\r
48 \r
49             }\r
50 \r
51             if (null == microServiceFullInfo) {\r
52                 log.warn("Failed to register the service to MSB. Sleep 30s and try again.");\r
53                 threadSleep(30000);\r
54             } else {\r
55                 log.info("Registration succeeded!");\r
56                 break;\r
57             }\r
58         }\r
59 \r
60         if (null == microServiceFullInfo) {\r
61             throw new CorrelationException("Failed to register the service to MSB!");\r
62         }\r
63 \r
64         log.info("Service registration completed.");\r
65     }\r
66 \r
67     private void threadSleep(int second) {\r
68         log.info("Start sleeping...");\r
69         try {\r
70             Thread.sleep(second);\r
71         } catch (InterruptedException error) {\r
72             log.error("thread sleep error message:" + error.getMessage(), error);\r
73             Thread.currentThread().interrupt();\r
74         }\r
75         log.info("Wake up.");\r
76     }\r
77 }