Updated the repo to support JDK 17
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / msb / MsbRegister.java
1 /**
2  * Copyright 2017-2023 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.msb;
18
19 import jakarta.ws.rs.client.Entity;
20 import jakarta.ws.rs.core.MediaType;
21 import lombok.Setter;
22 import org.apache.commons.lang3.StringUtils;
23 import org.onap.holmes.common.config.MicroServiceConfig;
24 import org.onap.holmes.common.exception.CorrelationException;
25 import org.onap.holmes.common.msb.entity.MicroServiceFullInfo;
26 import org.onap.holmes.common.msb.entity.MicroServiceInfo;
27 import org.onap.holmes.common.utils.GsonUtil;
28 import org.onap.holmes.common.utils.JerseyClient;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.stereotype.Service;
32
33 import java.util.concurrent.TimeUnit;
34
35 import static org.onap.holmes.common.utils.JerseyClient.PROTOCOL_HTTP;
36 import static org.onap.holmes.common.utils.JerseyClient.PROTOCOL_HTTPS;
37
38 @Service
39 public class MsbRegister {
40     private static final Logger log = LoggerFactory.getLogger(MsbRegister.class);
41
42     private JerseyClient client = JerseyClient.newInstance();
43
44     @Setter
45     private int totalRetryTimes = 20;
46
47     @Setter
48     private int interval = 20;
49
50     public MsbRegister() {
51     }
52
53     public void register2Msb(MicroServiceInfo msinfo) throws CorrelationException {
54         String[] msbAddrInfo = MicroServiceConfig.getMsbIpAndPort();
55
56         boolean isHttpsEnabled = StringUtils.isNotBlank(msbAddrInfo[1])
57                 && msbAddrInfo[1].equals("443");
58
59         log.info("Start to register Holmes Service to MSB...");
60         log.info("Registration information: {}", GsonUtil.beanToJson(msinfo));
61
62         MicroServiceFullInfo microServiceFullInfo = null;
63         int retry = 0;
64         while (null == microServiceFullInfo && retry < totalRetryTimes) {
65             int time = interval * ++retry;
66             try {
67                 log.info("Holmes Service Registration. Times: " + retry);
68                 microServiceFullInfo = client
69                         .header("Accept", MediaType.APPLICATION_JSON)
70                         .queryParam("createOrUpdate", true)
71                         .post(String.format("%s://%s:%s/api/microservices/v1/services",
72                                         isHttpsEnabled ? PROTOCOL_HTTPS : PROTOCOL_HTTP, msbAddrInfo[0], msbAddrInfo[1]),
73                                 Entity.entity(msinfo, MediaType.APPLICATION_JSON),
74                                 MicroServiceFullInfo.class);
75
76                 if (null == microServiceFullInfo) {
77                     retry(time);
78                 } else {
79                     log.info("Registration succeeded!");
80                     break;
81                 }
82             } catch (Exception e) {
83                 log.warn("Unexpected exception: " + e.getMessage(), e);
84                 retry(time);
85             }
86         }
87
88         if (null == microServiceFullInfo) {
89             throw new CorrelationException("Failed to register the service to MSB!");
90         }
91
92         log.info("Service registration completed.");
93     }
94
95     private void retry(int intervalInSecond) {
96         log.warn(String.format("Failed to register the service to MSB. Sleep %ds and try again.", intervalInSecond));
97         threadSleep(TimeUnit.SECONDS.toSeconds(intervalInSecond));
98     }
99
100     private void threadSleep(long second) {
101         log.info("Start sleeping...");
102         try {
103             TimeUnit.SECONDS.sleep(second);
104         } catch (InterruptedException error) {
105             log.error("thread sleep error message:" + error.getMessage(), error);
106             Thread.currentThread().interrupt();
107         }
108         log.info("Wake up.");
109     }
110 }