Fixed MSB Registration Failure
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / utils / MsbRegister.java
1 /**
2  * Copyright 2017-2020 ZTE Corporation.
3  * <p>
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.holmes.common.utils;
18
19 import org.apache.commons.lang3.StringUtils;
20 import org.eclipse.jetty.http.HttpStatus;
21 import org.jvnet.hk2.annotations.Service;
22 import org.onap.holmes.common.config.MicroServiceConfig;
23 import org.onap.holmes.common.exception.CorrelationException;
24 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
25 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import javax.inject.Inject;
30 import javax.ws.rs.client.Client;
31 import javax.ws.rs.client.Entity;
32 import javax.ws.rs.client.WebTarget;
33 import javax.ws.rs.core.MediaType;
34 import javax.ws.rs.core.Response;
35 import java.util.concurrent.TimeUnit;
36
37 import static org.onap.holmes.common.utils.JerseyClient.PROTOCOL_HTTP;
38 import static org.onap.holmes.common.utils.JerseyClient.PROTOCOL_HTTPS;
39
40 @Service
41 public class MsbRegister {
42     private static final Logger log = LoggerFactory.getLogger(MsbRegister.class);
43
44     private JerseyClient jerseyClient;
45
46     @Inject
47     public MsbRegister(JerseyClient jerseyClient) {
48         this.jerseyClient = jerseyClient;
49     }
50
51     public void register2Msb(MicroServiceInfo msinfo) throws CorrelationException {
52         String[] msbAddrInfo = MicroServiceConfig.getMsbIpAndPort();
53         boolean isHttpsEnabled = StringUtils.isNotBlank(msbAddrInfo[1])
54                 && msbAddrInfo[1].equals("443");
55
56         Client client = jerseyClient.client(isHttpsEnabled);
57         WebTarget target = client.target(String.format("%s://%s:%s/api/microservices/v1/services",
58                 isHttpsEnabled ? PROTOCOL_HTTPS : PROTOCOL_HTTP, msbAddrInfo[0], msbAddrInfo[1]));
59
60         log.info("Start to register Holmes Service to MSB...");
61
62         MicroServiceFullInfo microServiceFullInfo = null;
63         int retry = 0;
64         int interval = 5;
65         while (null == microServiceFullInfo && retry < 20) {
66             log.info("Holmes Service Registration. Retry: " + retry++);
67
68             Response response = target.queryParam("createOrUpdate", true)
69                     .request(MediaType.APPLICATION_JSON)
70                     .post(Entity.entity(msinfo, MediaType.APPLICATION_JSON));
71
72             if (response != null) {
73                 String ret = response.readEntity(String.class);
74                 int statusCode = response.getStatus();
75                 log.info(String.format("=========MSB REG=========\nStatus Code: %d\nInformation: %s", statusCode, ret));
76                 if (HttpStatus.isSuccess(statusCode)) {
77                     microServiceFullInfo = GsonUtil.jsonToBean(ret, MicroServiceFullInfo.class);
78                 }
79             }
80
81             if (null == microServiceFullInfo) {
82                 log.warn(String.format("Failed to register the service to MSB. Sleep %ds and try again.", interval));
83                 threadSleep(TimeUnit.SECONDS.toSeconds(interval));
84                 interval += 5;
85             } else {
86                 log.info("Registration succeeded!");
87                 break;
88             }
89         }
90
91         if (null == microServiceFullInfo) {
92             throw new CorrelationException("Failed to register the service to MSB!");
93         }
94
95         log.info("Service registration completed.");
96     }
97
98     private void threadSleep(long second) {
99         log.info("Start sleeping...");
100         try {
101             TimeUnit.SECONDS.sleep(second);
102         } catch (InterruptedException error) {
103             log.error("thread sleep error message:" + error.getMessage(), error);
104             Thread.currentThread().interrupt();
105         }
106         log.info("Wake up.");
107     }
108 }