8b3841900f9b107fd510406f18bf7c190751abd3
[holmes/common.git] / holmes-actions / src / main / java / org / openo / 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.openo.holmes.common.utils;\r
18 \r
19 import com.fasterxml.jackson.databind.ObjectMapper;\r
20 import java.io.IOException;\r
21 import lombok.extern.slf4j.Slf4j;\r
22 import org.apache.commons.lang3.StringUtils;\r
23 import org.apache.http.HttpResponse;\r
24 import org.apache.http.client.methods.HttpPost;\r
25 import org.apache.http.client.methods.HttpRequestBase;\r
26 import org.apache.http.entity.ByteArrayEntity;\r
27 import org.apache.http.impl.client.CloseableHttpClient;\r
28 import org.apache.http.impl.client.HttpClients;\r
29 import org.apache.http.util.EntityUtils;\r
30 import org.jvnet.hk2.annotations.Service;\r
31 import org.openo.holmes.common.api.entity.ServiceRegisterEntity;\r
32 import org.openo.holmes.common.config.MicroServiceConfig;\r
33 import org.openo.holmes.common.constant.AlarmConst;\r
34 \r
35 @Slf4j\r
36 @Service\r
37 public class MSBRegisterUtil {\r
38 \r
39     public void register(ServiceRegisterEntity entity) throws IOException {\r
40             log.info("start inventory micro service register");\r
41             boolean flag = false;\r
42             int retry = 0;\r
43             while (!flag && retry < 20) {\r
44                 log.info("inventory micro service register.retry:" + retry);\r
45                 retry++;\r
46                 flag = inner_register(entity);\r
47                 if (!flag) {\r
48                     log.warn("micro service register failed, sleep 30S and try again.");\r
49                     threadSleep(30000);\r
50                 } else {\r
51                     log.info("micro service register success!");\r
52                     break;\r
53                 }\r
54             }\r
55             log.info("holmes micro service register end.");\r
56     }\r
57 \r
58     private void setHeader(HttpRequestBase httpRequestBase) {\r
59         httpRequestBase.setHeader("Content-Type", "text/html;charset=UTF-8");\r
60         httpRequestBase.setHeader("Accept", "application/json");\r
61         httpRequestBase.setHeader("Content-Type", "application/json");\r
62     }\r
63 \r
64     private boolean inner_register(ServiceRegisterEntity entity) {\r
65         CloseableHttpClient httpClient = HttpClients.createDefault();\r
66         try {\r
67             ObjectMapper mapper = new ObjectMapper();\r
68             String content = mapper.writeValueAsString(entity);\r
69             HttpPost httpPost = new HttpPost(\r
70                     MicroServiceConfig.getMsbServerAddr() + "/api/microservices/v1/services?createOrUpdate=false");\r
71             if (StringUtils.isNotEmpty(content)) {\r
72                 httpPost.setEntity(new ByteArrayEntity(content.getBytes()));\r
73             }\r
74             this.setHeader(httpPost);\r
75             HttpResponse response;\r
76             try {\r
77                 response = httpClient.execute(httpPost);\r
78             } catch (Exception e) {\r
79                 log.warn("Registering the service to the bus failure", e);\r
80                 return false;\r
81             }\r
82             if (response.getStatusLine().getStatusCode() == AlarmConst.MICRO_SERVICE_STATUS_SUCCESS) {\r
83                 log.info("Registration successful service to the bus :" + EntityUtils.toString(response.getEntity()));\r
84                 return true;\r
85             } else {\r
86                 log.warn(\r
87                         "Registering the service to the bus failure:" + response.getStatusLine().getStatusCode() + " " +\r
88                                 response.getStatusLine().getReasonPhrase());\r
89                 return false;\r
90             }\r
91         } catch (IOException e) {\r
92             log.warn("ServiceRegisterEntity:" + entity + " parse failed",e);\r
93         } finally {\r
94             try {\r
95                 httpClient.close();\r
96             } catch (IOException e) {\r
97                 log.warn("At the time of registering service httpclient close failure",e);\r
98             }\r
99         }\r
100         return false;\r
101     }\r
102 \r
103     private void threadSleep(int second) {\r
104         log.info("start sleep ....");\r
105         try {\r
106             Thread.sleep(second);\r
107         } catch (InterruptedException error) {\r
108             log.error("thread sleep error.errorMsg:" + error.getMessage());\r
109         }\r
110         log.info("sleep end .");\r
111     }\r
112 }