From: Guangrong Fu Date: Thu, 9 Mar 2017 01:32:45 +0000 (+0000) Subject: Merge "Add msb register code" X-Git-Tag: release/mercury~18 X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=82b2bd08a159aed50c6711cf41cb36b36112cb9d;hp=1675d39c5a6a660b9dc4fc4c815bc344fc7d1d83;p=holmes%2Fcommon.git Merge "Add msb register code" --- diff --git a/holmes-actions/src/main/java/org/openo/holmes/common/config/MicroServiceConfig.java b/holmes-actions/src/main/java/org/openo/holmes/common/config/MicroServiceConfig.java new file mode 100644 index 0000000..ba55562 --- /dev/null +++ b/holmes-actions/src/main/java/org/openo/holmes/common/config/MicroServiceConfig.java @@ -0,0 +1,36 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openo.holmes.common.config; + +public class MicroServiceConfig { + + private static String getProperty(String name) { + String value = System.getenv(name); + if (value == null) { + value = System.getProperty(name); + } + return value; + } + + public static String getMsbServerAddr() { + return getProperty("MSB_ADDR"); + } + + public static String getServiceIp() { + return getProperty("SERVICE_IP"); + } + +} diff --git a/holmes-actions/src/main/java/org/openo/holmes/common/utils/MSBRegisterUtil.java b/holmes-actions/src/main/java/org/openo/holmes/common/utils/MSBRegisterUtil.java new file mode 100644 index 0000000..49dff92 --- /dev/null +++ b/holmes-actions/src/main/java/org/openo/holmes/common/utils/MSBRegisterUtil.java @@ -0,0 +1,65 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openo.holmes.common.utils; + +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ByteArrayEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.jvnet.hk2.annotations.Service; +import org.openo.holmes.common.api.entity.ServiceRegisterEntity; +import org.openo.holmes.common.config.MicroServiceConfig; +import org.openo.holmes.common.constant.AlarmConst; + +@Slf4j +@Service +public class MSBRegisterUtil { + + public boolean register(ServiceRegisterEntity entity) throws IOException { + CloseableHttpClient httpClient = HttpClients.createDefault(); + try { + ObjectMapper mapper = new ObjectMapper(); + String content = mapper.writeValueAsString(entity); + HttpPost httpPost = new HttpPost("http://" + MicroServiceConfig.getMsbServerAddr() + + ":8086/openoapi/microservices/v1/services?createOrUpdate=false"); + if (StringUtils.isNotEmpty(content)) { + httpPost.setEntity(new ByteArrayEntity(content.getBytes())); + } + HttpResponse response; + try { + response = httpClient.execute(httpPost); + } catch (Exception e) { + log.warn("Registering the service to the bus failure", e); + return false; + } + if (response.getStatusLine().getStatusCode() == AlarmConst.RESPONSE_STATUS_OK) { + log.info("Registration successful service to the bus :" + response.getEntity()); + return true; + } else { + log.warn("Registering the service to the bus failure"); + return false; + } + } finally { + httpClient.close(); + } + } +} \ No newline at end of file