018da170ffa1cc3066d28fe63d365d6f6eae051d
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / utils / MsbRegister.java
1 /**
2  * Copyright 2017-2020 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.common.utils;
18
19 import org.apache.commons.lang3.StringUtils;
20 import org.jvnet.hk2.annotations.Service;
21 import org.onap.holmes.common.config.MicroServiceConfig;
22 import org.onap.holmes.common.exception.CorrelationException;
23 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
24 import org.onap.msb.sdk.discovery.entity.MicroServiceInfo;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import javax.ws.rs.client.Entity;
29 import javax.ws.rs.core.MediaType;
30 import java.util.concurrent.TimeUnit;
31
32 import static org.onap.holmes.common.utils.JerseyClient.PROTOCOL_HTTP;
33 import static org.onap.holmes.common.utils.JerseyClient.PROTOCOL_HTTPS;
34
35 @Service
36 public class MsbRegister {
37     private static final Logger log = LoggerFactory.getLogger(MsbRegister.class);
38
39     private JerseyClient client = JerseyClient.newInstance();
40
41     public MsbRegister() {
42     }
43
44     public void register2Msb(MicroServiceInfo msinfo) throws CorrelationException {
45         String[] msbAddrInfo = MicroServiceConfig.getMsbIpAndPort();
46
47         boolean isHttpsEnabled = StringUtils.isNotBlank(msbAddrInfo[1])
48                 && msbAddrInfo[1].equals("443");
49
50         log.info("Start to register Holmes Service to MSB...");
51         log.info("Registration information: {}", GsonUtil.beanToJson(msinfo));
52
53         MicroServiceFullInfo microServiceFullInfo = null;
54         int retry = 0;
55         int interval = 5;
56         while (null == microServiceFullInfo && retry < 20) {
57             try {
58                 log.info("Holmes Service Registration. Retry: " + retry++);
59
60                 microServiceFullInfo = client
61                         .header("Accept", MediaType.APPLICATION_JSON)
62                         .queryParam("createOrUpdate", true)
63                         .post(String.format("%s://%s:%s/api/microservices/v1/services",
64                                 isHttpsEnabled ? PROTOCOL_HTTPS : PROTOCOL_HTTP, msbAddrInfo[0], msbAddrInfo[1]),
65                                 Entity.entity(msinfo, MediaType.APPLICATION_JSON),
66                                 MicroServiceFullInfo.class);
67
68                 if (null == microServiceFullInfo) {
69                     log.warn(String.format("Failed to register the service to MSB. Sleep %ds and try again.", interval));
70                     threadSleep(TimeUnit.SECONDS.toSeconds(interval));
71                     interval += 5;
72                 } else {
73                     log.info("Registration succeeded!");
74                     break;
75                 }
76             } catch (Exception e) {
77                 log.warn("Unexpected exception: " + e.getMessage(), e);
78             }
79         }
80
81         if (null == microServiceFullInfo) {
82             throw new CorrelationException("Failed to register the service to MSB!");
83         }
84
85         log.info("Service registration completed.");
86     }
87
88     private void threadSleep(long second) {
89         log.info("Start sleeping...");
90         try {
91             TimeUnit.SECONDS.sleep(second);
92         } catch (InterruptedException error) {
93             log.error("thread sleep error message:" + error.getMessage(), error);
94             Thread.currentThread().interrupt();
95         }
96         log.info("Wake up.");
97     }
98 }