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.rulemgt;
19 import org.onap.holmes.common.config.MicroServiceConfig;
20 import org.onap.holmes.common.exception.CorrelationException;
21 import org.onap.holmes.common.msb.MsbRegister;
22 import org.onap.holmes.common.msb.entity.MicroServiceInfo;
23 import org.onap.holmes.common.msb.entity.Node;
24 import org.onap.holmes.common.utils.CommonUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.boot.ApplicationArguments;
29 import org.springframework.boot.ApplicationRunner;
30 import org.springframework.stereotype.Component;
32 import java.util.HashSet;
34 import java.util.concurrent.Executors;
35 import java.util.concurrent.TimeUnit;
37 import static org.onap.holmes.common.utils.CommonUtils.getEnv;
38 import static org.onap.holmes.common.utils.CommonUtils.isIpAddress;
41 public class Initializer implements ApplicationRunner {
42 private static final Logger logger = LoggerFactory.getLogger(Initializer.class);
43 private volatile static boolean readyForMsbReg = false;
44 private MsbRegister msbRegister;
47 public Initializer(MsbRegister msbRegister) {
48 this.msbRegister = msbRegister;
52 public void run(ApplicationArguments args) {
53 Executors.newSingleThreadExecutor().execute(() -> {
56 msbRegister.register2Msb(createMicroServiceInfo());
57 } catch (CorrelationException e) {
58 logger.error(e.getMessage(), e);
63 private void waitUntilReady() {
65 while (!readyForMsbReg) {
69 int interval = 5 * count++;
70 logger.info("Not ready for MSB registration. Try again after {} seconds...", interval);
72 TimeUnit.SECONDS.sleep(interval);
73 } catch (InterruptedException e) {
74 logger.info(e.getMessage(), e);
79 public static void setReadyForMsbReg(boolean readyForMsbReg) {
80 Initializer.readyForMsbReg = readyForMsbReg;
83 private MicroServiceInfo createMicroServiceInfo() {
84 String[] serviceIpAndPort = MicroServiceConfig.getMicroServiceIpAndPort();
85 MicroServiceInfo msinfo = new MicroServiceInfo();
86 msinfo.setServiceName("holmes-rule-mgmt");
87 msinfo.setVersion("v1");
88 msinfo.setUrl("/api/holmes-rule-mgmt/v1");
89 msinfo.setPath("/api/holmes-rule-mgmt/v1");
90 msinfo.setProtocol("REST");
91 msinfo.setVisualRange("0|1");
92 msinfo.setLb_policy("round-robin");
93 msinfo.setEnable_ssl(CommonUtils.isHttpsEnabled());
94 Set<Node> nodes = new HashSet<>();
95 Node node = new Node();
96 node.setIp(isIpAddress(serviceIpAndPort[0]) ? serviceIpAndPort[0] : getEnv("HOLMES_RULE_MGMT_SERVICE_HOST"));
98 /* Following codes will cause an unregistration from MSB (due to MSB malfunction), comment them for now
99 String msbAddrTemplate = (CommonUtils.isHttpsEnabled() ? "https" : "http")
100 + "://%s:%s/api/holmes-rule-mgmt/v1/healthcheck";
101 node.setCheckType("HTTP");
102 node.setCheckUrl(String.format(msbAddrTemplate, serviceIpAndPort[0], "9101"));
103 node.setCheckTimeOut("60s");
104 node.setCheckInterval("60s");*/
107 msinfo.setNodes(nodes);