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