Migrate from DW to Springboot
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / Initializer.java
1 /**
2  * Copyright 2017-2022 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.onap.holmes.common.config.MicroServiceConfig;
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 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;
31
32 import java.util.HashSet;
33 import java.util.Set;
34 import java.util.concurrent.Executors;
35 import java.util.concurrent.TimeUnit;
36
37 import static org.onap.holmes.common.utils.CommonUtils.getEnv;
38 import static org.onap.holmes.common.utils.CommonUtils.isIpAddress;
39
40 @Component
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;
45
46     @Autowired
47     public Initializer(MsbRegister msbRegister) {
48         this.msbRegister = msbRegister;
49     }
50
51     @Override
52     public void run(ApplicationArguments args) {
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 = 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"));
97         node.setPort("9101");
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");*/
105
106         nodes.add(node);
107         msinfo.setNodes(nodes);
108         return msinfo;
109     }
110 }