1cd481e42fd401f463803d0f761bb2e578365a29
[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 com.eclipsesource.jaxrs.consumer.ConsumerFactory;\r
22 import java.io.IOException;\r
23 import lombok.extern.slf4j.Slf4j;\r
24 import org.jvnet.hk2.annotations.Service;\r
25 import org.onap.holmes.common.api.entity.ServiceRegisterEntity;\r
26 import org.onap.holmes.common.config.MicroServiceConfig;\r
27 import org.onap.holmes.common.exception.CorrelationException;\r
28 import org.onap.holmes.common.msb.MicroserviceBusRest;\r
29 import org.onap.msb.sdk.discovery.common.RouteException;\r
30 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;\r
31 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;\r
32 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;\r
33 \r
34 @Slf4j\r
35 @Service\r
36 public class MSBRegisterUtil {\r
37 \r
38     public void register2Msb(MicroServiceInfo msinfo) throws CorrelationException {\r
39         String[] msbAddrInfo = MicroServiceConfig.getMsbAddrInfo();\r
40         MSBServiceClient msbClient = new MSBServiceClient(msbAddrInfo[0],\r
41                 Integer.parseInt(msbAddrInfo[1]));\r
42 \r
43         log.info("Start to register Holmes Service to MSB...");\r
44         MicroServiceFullInfo microServiceFullInfo = null;\r
45         int retry = 0;\r
46         while (null == microServiceFullInfo && retry < 20) {\r
47             log.info("Holmes Service Registration. Retry: " + retry);\r
48             retry++;\r
49             try {\r
50                 microServiceFullInfo = msbClient.registerMicroServiceInfo(msinfo, false);\r
51             } catch (RouteException e) {\r
52 \r
53             }\r
54 \r
55             if (null == microServiceFullInfo) {\r
56                 log.warn("Failed to register the service to MSB. Sleep 30s and try again.");\r
57                 threadSleep(30000);\r
58             } else {\r
59                 log.info("Registration succeeded!");\r
60                 break;\r
61             }\r
62         }\r
63 \r
64         if (null == microServiceFullInfo) {\r
65             throw new CorrelationException("Failed to register the service to MSB!");\r
66         }\r
67 \r
68         log.info("Service registration completed.");\r
69     }\r
70 \r
71     private void threadSleep(int second) {\r
72         log.info("Start sleeping...");\r
73         try {\r
74             Thread.sleep(second);\r
75         } catch (InterruptedException error) {\r
76             log.error("thread sleep error message:" + error.getMessage(), error);\r
77             Thread.currentThread().interrupt();\r
78         }\r
79         log.info("Wake up.");\r
80     }\r
81 }