Trying to fix the healthcheck problem
[holmes/engine-management.git] / engine-d / src / main / java / org / onap / holmes / engine / 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.engine;
18
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;
27
28 import javax.annotation.PostConstruct;
29 import javax.inject.Inject;
30 import java.util.HashSet;
31 import java.util.Set;
32 import java.util.concurrent.Executors;
33 import java.util.concurrent.TimeUnit;
34
35 import static org.onap.holmes.common.config.MicroServiceConfig.POD_IP;
36 import static org.onap.holmes.common.config.MicroServiceConfig.getMicroServiceIpAndPort;
37 import static org.onap.holmes.common.utils.CommonUtils.getEnv;
38 import static org.onap.holmes.common.utils.CommonUtils.isIpAddress;
39
40 @Service
41 public class Initializer {
42     private static final Logger logger = LoggerFactory.getLogger(Initializer.class);
43     private volatile static boolean readyForMsbReg = false;
44     private MsbRegister msbRegister;
45
46     @Inject
47     public Initializer(MsbRegister msbRegister) {
48         this.msbRegister = msbRegister;
49     }
50
51     @PostConstruct
52     private void init() {
53         Executors.newSingleThreadExecutor().execute(() -> {
54             waitUntilReady();
55             try {
56                 msbRegister.register2Msb(createMicroServiceInfo());
57             } catch (CorrelationException e) {
58                 logger.error(e.getMessage(), e);
59             }
60         });
61     }
62
63     private void waitUntilReady() {
64         int count = 1;
65         while (!readyForMsbReg) {
66             if (count > 20) {
67                 break;
68             }
69             int interval = 5 * count++;
70             logger.info("Not ready for MSB registration. Try again after {} seconds...", interval);
71             try {
72                 TimeUnit.SECONDS.sleep(interval);
73             } catch (InterruptedException e) {
74                 logger.info(e.getMessage(), e);
75             }
76         }
77     }
78
79     public static void setReadyForMsbReg(boolean readyForMsbReg) {
80         Initializer.readyForMsbReg = readyForMsbReg;
81     }
82
83     private MicroServiceInfo createMicroServiceInfo() {
84         String[] serviceIpAndPort = getMicroServiceIpAndPort();
85         MicroServiceInfo msinfo = new MicroServiceInfo();
86         msinfo.setServiceName("holmes-engine-mgmt");
87         msinfo.setVersion("v1");
88         msinfo.setUrl("/api/holmes-engine-mgmt/v1");
89         msinfo.setPath("/api/holmes-engine-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(POD_IP));
97         node.setPort("9102");
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-engine-mgmt/v1/healthcheck";
101         node.setCheckType("HTTP");
102         node.setCheckUrl(String.format(msbAddrTemplate, serviceAddrInfo[0], "9102"));
103         node.setCheckTimeOut("60s");
104         node.setCheckInterval("60s");
105         */
106         nodes.add(node);
107         msinfo.setNodes(nodes);
108         return msinfo;
109     }
110 }