Release Artifact 1.3.1
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / utils / MSBRegisterUtil.java
1 /**\r
2  * Copyright 2017-2020 ZTE Corporation.\r
3  * <p>\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  * <p>\r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  * <p>\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 lombok.extern.slf4j.Slf4j;\r
20 import org.jvnet.hk2.annotations.Service;\r
21 import org.onap.holmes.common.config.MicroServiceConfig;\r
22 import org.onap.holmes.common.exception.CorrelationException;\r
23 import org.onap.msb.sdk.discovery.common.RouteException;\r
24 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;\r
25 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;\r
26 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;\r
27 \r
28 @Slf4j\r
29 @Service\r
30 public class MSBRegisterUtil {\r
31 \r
32     public void register2Msb(MicroServiceInfo msinfo) throws CorrelationException {\r
33         String[] msbAddrInfo = MicroServiceConfig.getMsbIpAndPort();\r
34         MSBServiceClient msbClient = new MSBServiceClient(msbAddrInfo[0],\r
35                 Integer.parseInt(msbAddrInfo[1]));\r
36 \r
37         log.info("Start to register Holmes Service to MSB...");\r
38         MicroServiceFullInfo microServiceFullInfo = null;\r
39         int retry = 0;\r
40         while (null == microServiceFullInfo && retry < 20) {\r
41             log.info("Holmes Service Registration. Retry: " + retry);\r
42             retry++;\r
43             try {\r
44                 microServiceFullInfo = msbClient.registerMicroServiceInfo(msinfo, false);\r
45             } catch (RouteException e) {\r
46 \r
47             }\r
48 \r
49             if (null == microServiceFullInfo) {\r
50                 log.warn("Failed to register the service to MSB. Sleep 30s and try again.");\r
51                 threadSleep(30000);\r
52             } else {\r
53                 log.info("Registration succeeded!");\r
54                 break;\r
55             }\r
56         }\r
57 \r
58         if (null == microServiceFullInfo) {\r
59             throw new CorrelationException("Failed to register the service to MSB!");\r
60         }\r
61 \r
62         log.info("Service registration completed.");\r
63     }\r
64 \r
65     private void threadSleep(int second) {\r
66         log.info("Start sleeping...");\r
67         try {\r
68             Thread.sleep(second);\r
69         } catch (InterruptedException error) {\r
70             log.error("thread sleep error message:" + error.getMessage(), error);\r
71             Thread.currentThread().interrupt();\r
72         }\r
73         log.info("Wake up.");\r
74     }\r
75 }