Updated holmes-actions to 1.3.5
[holmes/rule-management.git] / rulemgt / src / main / java / org / onap / holmes / rulemgt / bolt / enginebolt / EngineService.java
index f3e8d5c..4b6e248 100644 (file)
@@ -1,12 +1,12 @@
 /**\r
- * Copyright 2017 ZTE Corporation.\r
- *\r
+ * Copyright 2017-2021 ZTE Corporation.\r
+ * <p>\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
  * You may obtain a copy of the License at\r
- *\r
- *     http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
+ * <p>\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * <p>\r
  * Unless required by applicable law or agreed to in writing, software\r
  * distributed under the License is distributed on an "AS IS" BASIS,\r
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
  */\r
 package org.onap.holmes.rulemgt.bolt.enginebolt;\r
 \r
-import com.fasterxml.jackson.databind.ObjectMapper;\r
-import java.io.IOException;\r
-import javax.ws.rs.client.Client;\r
-import javax.ws.rs.client.ClientBuilder;\r
-import javax.ws.rs.client.Entity;\r
-import javax.ws.rs.client.WebTarget;\r
-import javax.ws.rs.core.MediaType;\r
-import javax.ws.rs.core.Response;\r
-import lombok.extern.slf4j.Slf4j;\r
-import org.glassfish.jersey.client.ClientConfig;\r
 import org.jvnet.hk2.annotations.Service;\r
+import org.onap.holmes.common.utils.CommonUtils;\r
+import org.onap.holmes.common.utils.JerseyClient;\r
 import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;\r
 import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;\r
-import org.onap.holmes.rulemgt.constant.RuleMgtConstant;\r
-import org.onap.holmes.common.config.MicroServiceConfig;\r
 \r
-@Slf4j\r
+import javax.ws.rs.client.Entity;\r
+import javax.ws.rs.core.MediaType;\r
+\r
+import static org.onap.holmes.rulemgt.constant.RuleMgtConstant.ENGINE_PATH;\r
+\r
 @Service\r
 public class EngineService {\r
 \r
-    protected Response delete(String packageName) throws IOException {\r
-        Client client = createClient();\r
-        WebTarget webTarget = client\r
-                .target(MicroServiceConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH + "/" + packageName);\r
-        return webTarget.request(MediaType.APPLICATION_JSON).delete();\r
+    private static final String PORT = "9102";\r
+    private static final String SEP = "//";\r
+    private static final String COLON = ":";\r
+\r
+    protected boolean delete(String packageName, String ip) {\r
+        return JerseyClient.newInstance()\r
+                .path(packageName)\r
+                .delete(getUrl(ip)) != null;\r
+    }\r
+\r
+    protected boolean check(CorrelationCheckRule4Engine correlationCheckRule4Engine, String ip) {\r
+        return JerseyClient.newInstance()\r
+                .header("Accept", MediaType.APPLICATION_JSON)\r
+                .post(getUrl(ip), Entity.json(correlationCheckRule4Engine)) != null;\r
     }\r
 \r
-    private Client createClient() {\r
-        ClientConfig clientConfig = new ClientConfig();\r
-        return ClientBuilder.newClient(clientConfig);\r
+    protected String deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine, String ip) {\r
+        return JerseyClient.newInstance()\r
+                .header("Accept", MediaType.APPLICATION_JSON)\r
+                .put(getUrl(ip), Entity.json(correlationDeployRule4Engine));\r
     }\r
 \r
-    protected Response check(CorrelationCheckRule4Engine correlationCheckRule4Engine)\r
-            throws IOException {\r
-        Client client = createClient();\r
-        ObjectMapper mapper = new ObjectMapper();\r
-        String content = mapper.writeValueAsString(correlationCheckRule4Engine);\r
-        WebTarget webTarget = client.target(MicroServiceConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH);\r
-        return webTarget.request(MediaType.APPLICATION_JSON).post(Entity.entity(content, MediaType.APPLICATION_JSON));\r
+    private String getRequestPref() {\r
+        return CommonUtils.isHttpsEnabled() ? JerseyClient.PROTOCOL_HTTPS : JerseyClient.PROTOCOL_HTTP;\r
     }\r
 \r
-    protected Response deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine) throws IOException {\r
-        Client client = createClient();\r
-        ObjectMapper mapper = new ObjectMapper();\r
-        String content = mapper.writeValueAsString(correlationDeployRule4Engine);\r
-        WebTarget webTarget = client.target(MicroServiceConfig.getMsbServerAddr() + RuleMgtConstant.ENGINE_PATH);\r
-        return webTarget.request(MediaType.APPLICATION_JSON).put(Entity.entity(content, MediaType.APPLICATION_JSON));\r
+    private String getUrl(String ip) {\r
+        return new StringBuilder(getRequestPref())\r
+                .append(COLON)\r
+                .append(SEP)\r
+                .append(ip)\r
+                .append(COLON)\r
+                .append(PORT)\r
+                .append(ENGINE_PATH)\r
+                .toString();\r
     }\r
 }\r