2 * Copyright 2017-2023 ZTE Corporation.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.holmes.common.msb;
19 import jakarta.ws.rs.client.Entity;
20 import jakarta.ws.rs.core.MediaType;
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;
33 import java.util.concurrent.TimeUnit;
35 import static org.onap.holmes.common.utils.JerseyClient.PROTOCOL_HTTP;
36 import static org.onap.holmes.common.utils.JerseyClient.PROTOCOL_HTTPS;
39 public class MsbRegister {
40 private static final Logger log = LoggerFactory.getLogger(MsbRegister.class);
42 private JerseyClient client = JerseyClient.newInstance();
45 private int totalRetryTimes = 20;
48 private int interval = 20;
50 public MsbRegister() {
53 public void register2Msb(MicroServiceInfo msinfo) throws CorrelationException {
54 String[] msbAddrInfo = MicroServiceConfig.getMsbIpAndPort();
56 boolean isHttpsEnabled = StringUtils.isNotBlank(msbAddrInfo[1])
57 && msbAddrInfo[1].equals("443");
59 log.info("Start to register Holmes Service to MSB...");
60 log.info("Registration information: {}", GsonUtil.beanToJson(msinfo));
62 MicroServiceFullInfo microServiceFullInfo = null;
64 while (null == microServiceFullInfo && retry < totalRetryTimes) {
65 int time = interval * ++retry;
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);
76 if (null == microServiceFullInfo) {
79 log.info("Registration succeeded!");
82 } catch (Exception e) {
83 log.warn("Unexpected exception: " + e.getMessage(), e);
88 if (null == microServiceFullInfo) {
89 throw new CorrelationException("Failed to register the service to MSB!");
92 log.info("Service registration completed.");
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));
100 private void threadSleep(long second) {
101 log.info("Start sleeping...");
103 TimeUnit.SECONDS.sleep(second);
104 } catch (InterruptedException error) {
105 log.error("thread sleep error message:" + error.getMessage(), error);
106 Thread.currentThread().interrupt();
108 log.info("Wake up.");