summaryrefslogtreecommitdiffstats
path: root/holmes-actions/src/main/java/org/onap/holmes/common/utils/MSBRegisterUtil.java
blob: 877a8240376225376efae46dc9f7cfea438a3c0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
 * Copyright 2017 ZTE Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.holmes.common.utils;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

import lombok.extern.slf4j.Slf4j;
import org.jvnet.hk2.annotations.Service;
import org.onap.holmes.common.config.MicroServiceConfig;
import org.onap.holmes.common.exception.CorrelationException;
import org.onap.msb.sdk.discovery.common.RouteException;
import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;

@Slf4j
@Service
public class MSBRegisterUtil {

    public void register2Msb(MicroServiceInfo msinfo) throws CorrelationException {
        String[] msbAddrInfo = MicroServiceConfig.getMsbIpAndPort();
        MSBServiceClient msbClient = new MSBServiceClient(msbAddrInfo[0],
                Integer.parseInt(msbAddrInfo[1]));

        log.info("Start to register Holmes Service to MSB...");
        MicroServiceFullInfo microServiceFullInfo = null;
        int retry = 0;
        while (null == microServiceFullInfo && retry < 20) {
            log.info("Holmes Service Registration. Retry: " + retry);
            retry++;
            try {
                microServiceFullInfo = msbClient.registerMicroServiceInfo(msinfo, false);
            } catch (RouteException e) {

            }

            if (null == microServiceFullInfo) {
                log.warn("Failed to register the service to MSB. Sleep 30s and try again.");
                threadSleep(30000);
            } else {
                log.info("Registration succeeded!");
                break;
            }
        }

        if (null == microServiceFullInfo) {
            throw new CorrelationException("Failed to register the service to MSB!");
        }

        log.info("Service registration completed.");
    }

    private void threadSleep(int second) {
        log.info("Start sleeping...");
        try {
            Thread.sleep(second);
        } catch (InterruptedException error) {
            log.error("thread sleep error message:" + error.getMessage(), error);
            Thread.currentThread().interrupt();
        }
        log.info("Wake up.");
    }
}