summaryrefslogtreecommitdiffstats
path: root/holmes-actions/src/main/java
diff options
context:
space:
mode:
authorGuangrong Fu <fu.guangrong@zte.com.cn>2017-03-09 01:32:45 +0000
committerGerrit Code Review <gerrit@open-o.org>2017-03-09 01:32:45 +0000
commit82b2bd08a159aed50c6711cf41cb36b36112cb9d (patch)
treeb7d28555552e2f09f5199abc4efd1caf42fc815f /holmes-actions/src/main/java
parent1675d39c5a6a660b9dc4fc4c815bc344fc7d1d83 (diff)
parentd55c83c257914112016a9205b6ed81b73116a42a (diff)
Merge "Add msb register code"
Diffstat (limited to 'holmes-actions/src/main/java')
-rw-r--r--holmes-actions/src/main/java/org/openo/holmes/common/config/MicroServiceConfig.java36
-rw-r--r--holmes-actions/src/main/java/org/openo/holmes/common/utils/MSBRegisterUtil.java65
2 files changed, 101 insertions, 0 deletions
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