bugfix - fixed the healthcheck problem
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / Initializer.java
1 /**
2  * Copyright 2017-2018 ZTE Corporation.
3  * <p>
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
7  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
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.
15  */
16
17 package org.onap.holmes.rulemgt;
18
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;
28
29 import javax.annotation.PostConstruct;
30 import javax.inject.Inject;
31 import java.util.HashSet;
32 import java.util.Set;
33 import java.util.concurrent.Executors;
34 import java.util.concurrent.TimeUnit;
35
36 import static org.onap.holmes.common.utils.CommonUtils.getEnv;
37 import static org.onap.holmes.common.utils.CommonUtils.isIpAddress;
38
39 @Service
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;
44
45     @Inject
46     public Initializer(MsbRegister msbRegister) {
47         this.msbRegister = msbRegister;
48     }
49
50     @PostConstruct
51     private void init() {
52         Executors.newSingleThreadExecutor().execute(() -> {
53             waitUntilReady();
54             try {
55                 msbRegister.register2Msb(createMicroServiceInfo());
56             } catch (CorrelationException e) {
57                 logger.error(e.getMessage(), e);
58             }
59         });
60     }
61
62     private void waitUntilReady() {
63         int count = 1;
64         while (!readyForMsbReg) {
65             if (count > 20) {
66                 break;
67             }
68             int interval = 5 * count++;
69             logger.info("Not ready for MSB registration. Try again after {} seconds...", interval);
70             try {
71                 TimeUnit.SECONDS.sleep(interval);
72             } catch (InterruptedException e) {
73                 logger.info(e.getMessage(), e);
74             }
75         }
76     }
77
78     public static void setReadyForMsbReg(boolean readyForMsbReg) {
79         Initializer.readyForMsbReg = readyForMsbReg;
80     }
81
82     private MicroServiceInfo createMicroServiceInfo() {
83         String[] serviceIpAndPort = MicroServiceConfig.getMicroServiceIpAndPort();
84         MicroServiceInfo msinfo = new MicroServiceInfo();
85         msinfo.setServiceName("holmes-rule-mgmt");
86         msinfo.setVersion("v1");
87         msinfo.setUrl("/api/holmes-rule-mgmt/v1");
88         msinfo.setPath("/api/holmes-rule-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_RULE_MGMT_SERVICE_HOST"));
96         node.setPort("9101");
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-rule-mgmt/v1/healthcheck";
100         node.setCheckType("HTTP");
101         node.setCheckUrl(String.format(msbAddrTemplate, serviceIpAndPort[0], "9101"));
102         node.setCheckTimeOut("60s");
103         node.setCheckInterval("60s");*/
104
105         nodes.add(node);
106         msinfo.setNodes(nodes);
107         return msinfo;
108     }
109 }