Introduce MSB Java SDK
[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 register(ServiceRegisterEntity entity) throws IOException {\r
39         log.info("Start register Holmes Service to MSB...");\r
40         boolean flag = false;\r
41         int retry = 0;\r
42         while (!flag && retry < 20) {\r
43             log.info("Holmes Service Registration. Retry: " + retry);\r
44             retry++;\r
45             flag = innerRegister(entity);\r
46             if (!flag) {\r
47                 log.warn("Failed to register the service to MSB. Sleep 30s and try again.");\r
48                 threadSleep(30000);\r
49             } else {\r
50                 log.info("Registration succeeded!");\r
51                 break;\r
52             }\r
53         }\r
54         log.info("Service registration completed.");\r
55     }\r
56 \r
57     private boolean innerRegister(ServiceRegisterEntity entity) {\r
58         try {\r
59             log.info("msbServerAddr:" + MicroServiceConfig.getMsbServerAddr());\r
60             log.info("entity:" + entity);\r
61             MicroserviceBusRest resourceserviceproxy = ConsumerFactory.createConsumer(\r
62                     MicroServiceConfig.getMsbServerAddr(), MicroserviceBusRest.class);\r
63             resourceserviceproxy.registerServce("false", entity);\r
64         } catch (Exception error) {\r
65             log.error("Micro-service registration failed!" + error.getMessage(), error);\r
66             return false;\r
67         }\r
68         return true;\r
69     }\r
70 \r
71     public void register2Msb(MicroServiceInfo msinfo) throws CorrelationException {\r
72         MSBServiceClient msbClient = new MSBServiceClient(MicroServiceConfig.getMsbServerIp(),\r
73                 MicroServiceConfig.getMsbServerPort());\r
74 \r
75         log.info("Start register Holmes Service to MSB...");\r
76         MicroServiceFullInfo microServiceFullInfo = null;\r
77         int retry = 0;\r
78         while (null == microServiceFullInfo && retry < 20) {\r
79             log.info("Holmes Service Registration. Retry: " + retry);\r
80             retry++;\r
81             try {\r
82                 microServiceFullInfo = msbClient.registerMicroServiceInfo(msinfo, false);\r
83             } catch (RouteException e) {\r
84 \r
85             }\r
86 \r
87             if (null == microServiceFullInfo) {\r
88                 log.warn("Failed to register the service to MSB. Sleep 30s and try again.");\r
89                 threadSleep(30000);\r
90             } else {\r
91                 log.info("Registration succeeded!");\r
92                 break;\r
93             }\r
94         }\r
95 \r
96         if (null == microServiceFullInfo) {\r
97             throw new CorrelationException("Failed to register the service to MSB!");\r
98         }\r
99 \r
100         log.info("Service registration completed.");\r
101     }\r
102 \r
103     private void threadSleep(int second) {\r
104         log.info("Start sleeping...");\r
105         try {\r
106             Thread.sleep(second);\r
107         } catch (InterruptedException error) {\r
108             log.error("thread sleep error message:" + error.getMessage(), error);\r
109             Thread.currentThread().interrupt();\r
110         }\r
111         log.info("Wake up.");\r
112     }\r
113 }