c39de60c08f4849f247cce8a99aaaeffda45437b
[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
34 import static org.onap.holmes.common.utils.CommonUtils.getEnv;
35 import static org.onap.holmes.common.utils.CommonUtils.isIpAddress;
36
37 @Service
38 public class Initializer {
39     private static final Logger logger = LoggerFactory.getLogger(Initializer.class);
40     private MsbRegister msbRegister;
41
42     @Inject
43     public Initializer(MsbRegister msbRegister) {
44         this.msbRegister = msbRegister;
45     }
46
47     @PostConstruct
48     private void init() {
49         try {
50             msbRegister.register2Msb(createMicroServiceInfo());
51         } catch (CorrelationException e) {
52             logger.error(e.getMessage(), e);
53         }
54     }
55
56     private MicroServiceInfo createMicroServiceInfo() {
57         String[] serviceIpAndPort = MicroServiceConfig.getMicroServiceIpAndPort();
58         MicroServiceInfo msinfo = new MicroServiceInfo();
59         msinfo.setServiceName("holmes-rule-mgmt");
60         msinfo.setVersion("v1");
61         msinfo.setUrl("/api/holmes-rule-mgmt/v1");
62         msinfo.setPath("/api/holmes-rule-mgmt/v1");
63         msinfo.setProtocol("REST");
64         msinfo.setVisualRange("0|1");
65         msinfo.setLb_policy("round-robin");
66         msinfo.setEnable_ssl(CommonUtils.isHttpsEnabled());
67         Set<Node> nodes = new HashSet<>();
68         Node node = new Node();
69         node.setIp(isIpAddress(serviceIpAndPort[0]) ? serviceIpAndPort[0] : getEnv("HOLMES_RULE_MGMT_SERVICE_HOST"));
70         node.setPort("9101");
71         /* Following codes will cause an unregistration from MSB (due to MSB malfunction), comment them for now
72         String msbAddrTemplate = (CommonUtils.isHttpsEnabled() ? "https" : "http")
73                 + "://%s:%s/api/holmes-rule-mgmt/v1/healthcheck";
74         node.setCheckType("HTTP");
75         node.setCheckUrl(String.format(msbAddrTemplate, serviceIpAndPort[0], "9101"));
76         node.setCheckTimeOut("60s");
77         node.setCheckInterval("60s");*/
78
79         nodes.add(node);
80         msinfo.setNodes(nodes);
81         return msinfo;
82     }
83 }