812ee4800df7635cf746b4ebb0dff5171142162d
[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.TimeUnit;
34
35 import static org.onap.holmes.common.utils.CommonUtils.getEnv;
36 import static org.onap.holmes.common.utils.CommonUtils.isIpAddress;
37
38 @Service
39 public class Initializer {
40     private static final Logger logger = LoggerFactory.getLogger(Initializer.class);
41     private static boolean readyForMsbReg = false;
42     private MsbRegister msbRegister;
43
44     @Inject
45     public Initializer(MsbRegister msbRegister) {
46         this.msbRegister = msbRegister;
47     }
48
49     @PostConstruct
50     private void init() {
51         waitUntilReady();
52         try {
53             msbRegister.register2Msb(createMicroServiceInfo());
54         } catch (CorrelationException e) {
55             logger.error(e.getMessage(), e);
56         }
57     }
58
59     private void waitUntilReady() {
60         int count = 1;
61         while(!readyForMsbReg) {
62             if (count > 20) {
63                 break;
64             }
65             int interval = 5 * count++;
66             logger.info("Not ready for MSB registration. Try again after {} seconds...", interval);
67             try {
68                 TimeUnit.SECONDS.sleep(interval);
69             } catch (InterruptedException e) {
70                 logger.info(e.getMessage(), e);
71             }
72         }
73     }
74
75     public static void setReadyForMsbReg(boolean readyForMsbReg) {
76         Initializer.readyForMsbReg = readyForMsbReg;
77     }
78
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"));
93         node.setPort("9101");
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");*/
101
102         nodes.add(node);
103         msinfo.setNodes(nodes);
104         return msinfo;
105     }
106 }