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.engine;
19 import org.jvnet.hk2.annotations.Service;
20 import org.onap.holmes.common.exception.CorrelationException;
21 import org.onap.holmes.common.utils.CommonUtils;
22 import org.onap.holmes.common.utils.MsbRegister;
23 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
24 import org.onap.msb.sdk.discovery.entity.Node;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import javax.annotation.PostConstruct;
29 import javax.inject.Inject;
30 import java.util.HashSet;
32 import java.util.concurrent.Executors;
33 import java.util.concurrent.TimeUnit;
35 import static org.onap.holmes.common.config.MicroServiceConfig.getMicroServiceIpAndPort;
36 import static org.onap.holmes.common.utils.CommonUtils.getEnv;
37 import static org.onap.holmes.common.utils.CommonUtils.isIpAddress;
40 public class Initializer {
41 private static final Logger logger = LoggerFactory.getLogger(Initializer.class);
42 private volatile static boolean readyForMsbReg = false;
43 private MsbRegister msbRegister;
46 public Initializer(MsbRegister msbRegister) {
47 this.msbRegister = msbRegister;
52 Executors.newSingleThreadExecutor().execute(() -> {
55 msbRegister.register2Msb(createMicroServiceInfo());
56 } catch (CorrelationException e) {
57 logger.error(e.getMessage(), e);
62 private void waitUntilReady() {
64 while (!readyForMsbReg) {
68 int interval = 5 * count++;
69 logger.info("Not ready for MSB registration. Try again after {} seconds...", interval);
71 TimeUnit.SECONDS.sleep(interval);
72 } catch (InterruptedException e) {
73 logger.info(e.getMessage(), e);
78 public static void setReadyForMsbReg(boolean readyForMsbReg) {
79 Initializer.readyForMsbReg = readyForMsbReg;
82 private MicroServiceInfo createMicroServiceInfo() {
83 String[] serviceIpAndPort = getMicroServiceIpAndPort();
84 MicroServiceInfo msinfo = new MicroServiceInfo();
85 msinfo.setServiceName("holmes-engine-mgmt");
86 msinfo.setVersion("v1");
87 msinfo.setUrl("/api/holmes-engine-mgmt/v1");
88 msinfo.setPath("/api/holmes-engine-mgmt/v1");
89 msinfo.setProtocol("REST");
90 msinfo.setVisualRange("0|1");
91 msinfo.setLb_policy("round-robin");
92 msinfo.setEnable_ssl(CommonUtils.isHttpsEnabled());
93 Set<Node> nodes = new HashSet<>();
94 Node node = new Node();
95 node.setIp(isIpAddress(serviceIpAndPort[0]) ? serviceIpAndPort[0] : getEnv("HOLMES_ENGINE_MGMT_SERVICE_HOST"));
97 /* Following codes will cause an unregistration from MSB (due to MSB malfunction), comment them for now
98 String msbAddrTemplate = (CommonUtils.isHttpsEnabled() ? "https" : "http")
99 + "://%s:%s/api/holmes-engine-mgmt/v1/healthcheck";
100 node.setCheckType("HTTP");
101 node.setCheckUrl(String.format(msbAddrTemplate, serviceAddrInfo[0], "9102"));
102 node.setCheckTimeOut("60s");
103 node.setCheckInterval("60s");
106 msinfo.setNodes(nodes);