2 * Copyright 2017-2018 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.jvnet.hk2.annotations.Service;
20 import org.onap.holmes.common.config.MicroServiceConfig;
21 import org.onap.holmes.common.exception.CorrelationException;
22 import org.onap.holmes.common.utils.CommonUtils;
23 import org.onap.holmes.common.utils.MsbRegister;
24 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
25 import org.onap.msb.sdk.discovery.entity.Node;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import javax.annotation.PostConstruct;
30 import javax.inject.Inject;
31 import java.util.HashSet;
33 import java.util.concurrent.TimeUnit;
35 import static org.onap.holmes.common.utils.CommonUtils.getEnv;
36 import static org.onap.holmes.common.utils.CommonUtils.isIpAddress;
39 public class Initializer {
40 private static final Logger logger = LoggerFactory.getLogger(Initializer.class);
41 private static boolean readyForMsbReg = false;
42 private MsbRegister msbRegister;
45 public Initializer(MsbRegister msbRegister) {
46 this.msbRegister = msbRegister;
53 msbRegister.register2Msb(createMicroServiceInfo());
54 } catch (CorrelationException e) {
55 logger.error(e.getMessage(), e);
59 private void waitUntilReady() {
61 while(!readyForMsbReg) {
65 int interval = 5 * count++;
66 logger.info("Not ready for MSB registration. Try again after {} seconds...", interval);
68 TimeUnit.SECONDS.sleep(interval);
69 } catch (InterruptedException e) {
70 logger.info(e.getMessage(), e);
75 public static void setReadyForMsbReg(boolean readyForMsbReg) {
76 Initializer.readyForMsbReg = readyForMsbReg;
79 private MicroServiceInfo createMicroServiceInfo() {
80 String[] serviceIpAndPort = MicroServiceConfig.getMicroServiceIpAndPort();
81 MicroServiceInfo msinfo = new MicroServiceInfo();
82 msinfo.setServiceName("holmes-rule-mgmt");
83 msinfo.setVersion("v1");
84 msinfo.setUrl("/api/holmes-rule-mgmt/v1");
85 msinfo.setPath("/api/holmes-rule-mgmt/v1");
86 msinfo.setProtocol("REST");
87 msinfo.setVisualRange("0|1");
88 msinfo.setLb_policy("round-robin");
89 msinfo.setEnable_ssl(CommonUtils.isHttpsEnabled());
90 Set<Node> nodes = new HashSet<>();
91 Node node = new Node();
92 node.setIp(isIpAddress(serviceIpAndPort[0]) ? serviceIpAndPort[0] : getEnv("HOLMES_RULE_MGMT_SERVICE_HOST"));
94 /* Following codes will cause an unregistration from MSB (due to MSB malfunction), comment them for now
95 String msbAddrTemplate = (CommonUtils.isHttpsEnabled() ? "https" : "http")
96 + "://%s:%s/api/holmes-rule-mgmt/v1/healthcheck";
97 node.setCheckType("HTTP");
98 node.setCheckUrl(String.format(msbAddrTemplate, serviceIpAndPort[0], "9101"));
99 node.setCheckTimeOut("60s");
100 node.setCheckInterval("60s");*/
103 msinfo.setNodes(nodes);