78ac59b3712f39bb35b2166982723c6577097ee4
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / utils / 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.utils;
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.msb.sdk.discovery.entity.MicroServiceFullInfo;
26 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.stereotype.Service;
30
31 import java.util.concurrent.TimeUnit;
32
33 import static org.onap.holmes.common.utils.JerseyClient.PROTOCOL_HTTP;
34 import static org.onap.holmes.common.utils.JerseyClient.PROTOCOL_HTTPS;
35
36 @Service
37 public class MsbRegister {
38     private static final Logger log = LoggerFactory.getLogger(MsbRegister.class);
39
40     private JerseyClient client = JerseyClient.newInstance();
41
42     @Setter
43     private int totalRetryTimes = 20;
44
45     @Setter
46     private int interval = 20;
47
48     public MsbRegister() {
49     }
50
51     public void register2Msb(MicroServiceInfo msinfo) throws CorrelationException {
52         String[] msbAddrInfo = MicroServiceConfig.getMsbIpAndPort();
53
54         boolean isHttpsEnabled = StringUtils.isNotBlank(msbAddrInfo[1])
55                 && msbAddrInfo[1].equals("443");
56
57         log.info("Start to register Holmes Service to MSB...");
58         log.info("Registration information: {}", GsonUtil.beanToJson(msinfo));
59
60         MicroServiceFullInfo microServiceFullInfo = null;
61         int retry = 0;
62         while (null == microServiceFullInfo && retry < totalRetryTimes) {
63             int time  = interval * ++retry;
64             try {
65                 log.info("Holmes Service Registration. Times: " + retry);
66                 microServiceFullInfo = client
67                         .header("Accept", MediaType.APPLICATION_JSON)
68                         .queryParam("createOrUpdate", true)
69                         .post(String.format("%s://%s:%s/api/microservices/v1/services",
70                                 isHttpsEnabled ? PROTOCOL_HTTPS : PROTOCOL_HTTP, msbAddrInfo[0], msbAddrInfo[1]),
71                                 Entity.entity(msinfo, MediaType.APPLICATION_JSON),
72                                 MicroServiceFullInfo.class);
73
74                 if (null == microServiceFullInfo) {
75                     retry(time);
76                 } else {
77                     log.info("Registration succeeded!");
78                     break;
79                 }
80             } catch (Exception e) {
81                 log.warn("Unexpected exception: " + e.getMessage(), e);
82                 retry(time);
83             }
84         }
85
86         if (null == microServiceFullInfo) {
87             throw new CorrelationException("Failed to register the service to MSB!");
88         }
89
90         log.info("Service registration completed.");
91     }
92
93     private void retry(int intervalInSecond) {
94         log.warn(String.format("Failed to register the service to MSB. Sleep %ds and try again.", intervalInSecond));
95         threadSleep(TimeUnit.SECONDS.toSeconds(intervalInSecond));
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 }