Change HTTP Requests into HTTPS Ones 59/34159/4
authorShiwei Tian <tian.shiwei@zte.com.cn>
Tue, 6 Mar 2018 00:56:31 +0000 (08:56 +0800)
committerShiwei Tian <tian.shiwei@zte.com.cn>
Tue, 6 Mar 2018 01:47:56 +0000 (09:47 +0800)
Issue-ID: HOLMES-104

Change-Id: I73d23418fbfaa23121ec825b11bbb46e55b2058c
Signed-off-by: Shiwei Tian <tian.shiwei@zte.com.cn>
23 files changed:
pom.xml
rulemgt-standalone/src/main/assembly/bin/run.sh
rulemgt-standalone/src/main/assembly/conf/rulemgt.yml
rulemgt-standalone/target/assembly/linux64/conf/holmes.keystore [new file with mode: 0644]
rulemgt/pom.xml
rulemgt/src/main/java/org/onap/holmes/rulemgt/RuleActiveApp.java
rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationCheckRule4Engine.java
rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/CorrelationDeployRule4Engine.java
rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleCreateRequest.java
rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleDeleteRequest.java
rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleQueryCondition.java
rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/request/RuleUpdateRequest.java
rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleAddAndUpdateResponse.java
rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleQueryListResponse.java
rulemgt/src/main/java/org/onap/holmes/rulemgt/bean/response/RuleResult4API.java
rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineService.java
rulemgt/src/main/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapper.java
rulemgt/src/main/java/org/onap/holmes/rulemgt/constant/RuleMgtConstant.java
rulemgt/src/main/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPolling.java
rulemgt/src/main/java/org/onap/holmes/rulemgt/resources/RuleMgtResources.java
rulemgt/src/test/java/org/onap/holmes/rulemgt/bolt/enginebolt/EngineWrapperTest.java
rulemgt/src/test/java/org/onap/holmes/rulemgt/dcae/DcaeConfigurationPollingTest.java
rulemgt/src/test/java/org/onap/holmes/rulemgt/resources/RuleMgtResourcesTest.java

diff --git a/pom.xml b/pom.xml
index 93da107..99651cc 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -71,7 +71,7 @@
             <dependency>\r
                 <groupId>org.onap.msb.java-sdk</groupId>\r
                 <artifactId>msb-java-sdk</artifactId>\r
-                <version>1.0.0</version>\r
+                <version>1.1.0-SNAPSHOT</version>\r
             </dependency>\r
             <dependency>\r
                 <groupId>org.reflections</groupId>\r
index fc1d63f..f0beb56 100644 (file)
@@ -70,6 +70,14 @@ if [ ! -z ${URL_JDBC} ] && [ `expr index $URL_JDBC :` != 0 ]; then
 fi
 echo DB_PORT=$DB_PORT
 
+KEY_PATH="$main_path/conf/holmes.keystore"
+KEY_PASSWORD="holmes"
+
+#HTTPS Configurations
+sed -i "s|keyStorePath:.*|keyStorePath: $KEY_PATH|" "$main_path/conf/rulemgt.yml"
+sed -i "s|keyStorePassword:.*|keyStorePassword: $KEY_PASSWORD|" "$main_path/conf/rulemgt.yml"
+
+
 ./bin/initDB.sh $JDBC_USERNAME $JDBC_PASSWORD $DB_NAME $DB_PORT "${URL_JDBC%:*}"
 
 "$JAVA" $JAVA_OPTS -classpath "$class_path" org.onap.holmes.rulemgt.RuleActiveApp server "$main_path/conf/rulemgt.yml"
index 3c94fa7..b0a934b 100644 (file)
@@ -9,12 +9,16 @@ apidescription: ZTE Holmes rule Management rest API
 
 server:
   type: simple
-  rootPath:  '/api/holmes-rule-mgmt/v1/*'
+  rootPath: '/api/holmes-rule-mgmt/v1/*'
   applicationContextPath: /
   adminContextPath: /admin
   connector:
-    type: http
+    type: https
     port: 9101
+    keyStorePath: /home/holmes/conf/holmes.keystore
+    keyStorePassword: holmes
+    validateCerts: false
+    validatePeers: false
 
 # Logging settings.
 logging:
diff --git a/rulemgt-standalone/target/assembly/linux64/conf/holmes.keystore b/rulemgt-standalone/target/assembly/linux64/conf/holmes.keystore
new file mode 100644 (file)
index 0000000..a6266bb
Binary files /dev/null and b/rulemgt-standalone/target/assembly/linux64/conf/holmes.keystore differ
index feb40fd..262ad48 100644 (file)
         <dependency>
             <groupId>org.apache.httpcomponents</groupId>
             <artifactId>httpclient</artifactId>
-            <version>4.4</version>
+            <version>4.5.3</version>
         </dependency>
         <dependency>
             <groupId>com.googlecode.json-simple</groupId>
             <artifactId>guava</artifactId>
             <version>19.0</version>
         </dependency>
-        <!-- https://mvnrepository.com/artifact/org.codehaus.groovy.maven/gmaven-plugin -->
-        <dependency>
-            <groupId>org.codehaus.groovy.maven</groupId>
-            <artifactId>gmaven-plugin</artifactId>
-            <version>1.0</version>
-        </dependency>
-
     </dependencies>
     <build>
         <plugins>
index 6409794..e7f30a7 100644 (file)
@@ -69,6 +69,7 @@ public class RuleActiveApp extends IOCApplication<RuleAppConfig> {
         msinfo.setUrl("/api/holmes-rule-mgmt/v1");
         msinfo.setProtocol("REST");
         msinfo.setVisualRange("0|1");
+        msinfo.setEnable_ssl(true);
         Set<Node> nodes = new HashSet<>();
         Node node = new Node();
         node.setIp(serviceAddrInfo[0]);
index b38ea47..4c578e4 100644 (file)
@@ -16,6 +16,7 @@
 package org.onap.holmes.rulemgt.bean.request;\r
 \r
 import com.fasterxml.jackson.annotation.JsonProperty;\r
+import com.google.gson.annotations.SerializedName;\r
 import lombok.Getter;\r
 import lombok.Setter;\r
 \r
@@ -23,12 +24,9 @@ import lombok.Setter;
 @Setter\r
 public class CorrelationDeployRule4Engine {\r
 \r
-    @JsonProperty(value = "content")\r
     private String content;\r
 \r
-    @JsonProperty(value = "engineid")\r
     private String engineId;\r
 \r
-    @JsonProperty(value = "loopcontrolname")\r
     private String loopControlName;\r
 }\r
index c7e8a6f..8dc98d8 100644 (file)
  */\r
 package org.onap.holmes.rulemgt.bean.request;\r
 \r
-import com.fasterxml.jackson.annotation.JsonProperty;\r
 import lombok.Getter;\r
 import lombok.Setter;\r
 \r
 @Setter\r
 @Getter\r
 public class RuleCreateRequest {\r
-    @JsonProperty(value = "rulename")\r
+\r
     private String ruleName;\r
-    @JsonProperty(value = "loopcontrolname")\r
+\r
     private String loopControlName;\r
-    @JsonProperty\r
+\r
     private String description;\r
-    @JsonProperty\r
+\r
     private String content;\r
-    @JsonProperty\r
+\r
     private int enabled;\r
 }\r
index 57aeafc..49b646a 100644 (file)
@@ -15,7 +15,6 @@
  */\r
 package org.onap.holmes.rulemgt.bean.request;\r
 \r
-import com.fasterxml.jackson.annotation.JsonProperty;\r
 import lombok.AllArgsConstructor;\r
 import lombok.Getter;\r
 import lombok.NoArgsConstructor;\r
@@ -27,6 +26,5 @@ import lombok.Setter;
 @AllArgsConstructor\r
 @NoArgsConstructor\r
 public class RuleDeleteRequest {\r
-    @JsonProperty(value = "ruleid")\r
     private String ruleId;\r
 }\r
index f3b9b91..c6ba4b7 100644 (file)
@@ -15,7 +15,7 @@
  */\r
 package org.onap.holmes.rulemgt.bean.request;\r
 \r
-import com.fasterxml.jackson.annotation.JsonProperty;\r
+import com.google.gson.annotations.SerializedName;\r
 import lombok.Getter;\r
 import lombok.Setter;\r
 \r
@@ -23,9 +23,9 @@ import lombok.Setter;
 @Setter\r
 public class RuleQueryCondition {\r
 \r
-    @JsonProperty(value = "ruleid")\r
+    @SerializedName(value = "ruleid")\r
     private String rid;\r
-    @JsonProperty(value = "rulename")\r
+    @SerializedName(value = "rulename")\r
     private String name;\r
     private int enabled;\r
     private String creator;\r
index 99af585..1bcd494 100644 (file)
  */\r
 package org.onap.holmes.rulemgt.bean.request;\r
 \r
-import com.fasterxml.jackson.annotation.JsonProperty;\r
 import lombok.Getter;\r
 import lombok.Setter;\r
 \r
 @Getter\r
 @Setter\r
 public class RuleUpdateRequest {\r
-    @JsonProperty\r
+\r
     private String description;\r
-    @JsonProperty\r
+\r
     private String content;\r
-    @JsonProperty\r
+\r
     private int enabled;\r
-    @JsonProperty(value="ruleid")\r
+\r
     private String ruleId;\r
-    @JsonProperty(value = "loopcontrolname")\r
+\r
     private String loopControlName;\r
 }\r
index 7fe432e..760ad4f 100644 (file)
  */\r
 package org.onap.holmes.rulemgt.bean.response;\r
 \r
-import com.fasterxml.jackson.annotation.JsonInclude;\r
-import com.fasterxml.jackson.annotation.JsonProperty;\r
 import lombok.Getter;\r
 import lombok.Setter;\r
 \r
-@JsonInclude(JsonInclude.Include.ALWAYS)\r
 @Getter\r
 @Setter\r
 public class RuleAddAndUpdateResponse{\r
-    @JsonProperty(value="ruleid")\r
     private String ruleId;\r
 }\r
index aab6dc5..9305496 100644 (file)
  */\r
 package org.onap.holmes.rulemgt.bean.response;\r
 \r
-import com.google.gson.annotations.SerializedName;\r
 import lombok.Getter;\r
 import lombok.Setter;\r
 \r
 import java.util.ArrayList;\r
 import java.util.List;\r
 \r
+\r
 @Getter\r
 @Setter\r
 public class RuleQueryListResponse {\r
-    @SerializedName(value = "rules")\r
     private List<RuleResult4API> correlationRules = new ArrayList<RuleResult4API>();\r
-    @SerializedName(value = "totalcount")\r
     private int totalCount;\r
 }\r
index e97b94d..39e1bb6 100644 (file)
  */\r
 package org.onap.holmes.rulemgt.bean.response;\r
 \r
-import com.fasterxml.jackson.annotation.JsonInclude;\r
 import com.fasterxml.jackson.annotation.JsonProperty;\r
 import lombok.Getter;\r
 import lombok.Setter;\r
 \r
 import java.util.Date;\r
 \r
-@JsonInclude(JsonInclude.Include.ALWAYS)\r
 @Setter\r
 @Getter\r
 public class RuleResult4API {\r
-    @JsonProperty(value = "ruleid")\r
     private String ruleId;\r
-    @JsonProperty(value = "rulename")\r
     private String ruleName;\r
     private String description;\r
     private String content;\r
-    @JsonProperty(value = "createtime")\r
     private Date createTime;\r
     private String creator;\r
-    @JsonProperty(value = "updatetime")\r
     private Date updateTime;\r
     private String modifier;\r
     private int enabled;\r
-    @JsonProperty(value = "loopcontrolname")\r
     private String closedControlLoopName;\r
 }\r
index 5dc97b4..319347e 100644 (file)
  */\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 java.util.HashMap;\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.apache.http.HttpResponse;\r
+import org.apache.http.entity.StringEntity;\r
 import org.jvnet.hk2.annotations.Service;\r
+import org.onap.holmes.common.utils.GsonUtil;\r
+import org.onap.holmes.common.utils.HttpsUtils;\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
@@ -35,32 +32,31 @@ import org.onap.holmes.common.config.MicroServiceConfig;
 @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.getMsbServerAddrWithHttpPrefix() + RuleMgtConstant.ENGINE_PATH + "/" + packageName);\r
-        return webTarget.request(MediaType.APPLICATION_JSON).delete();\r
+    protected HttpResponse delete(String packageName) throws Exception {\r
+        HashMap headers = createHeaders();\r
+        String url = MicroServiceConfig.getMsbServerAddrWithHttpPrefix() + RuleMgtConstant.ENGINE_PATH + "/" + packageName;\r
+        return HttpsUtils.delete(url, headers);\r
     }\r
 \r
-    private Client createClient() {\r
-        ClientConfig clientConfig = new ClientConfig();\r
-        return ClientBuilder.newClient(clientConfig);\r
+    protected HttpResponse check(CorrelationCheckRule4Engine correlationCheckRule4Engine)\r
+            throws Exception {\r
+        String content = GsonUtil.beanToJson(correlationCheckRule4Engine);\r
+        HashMap headers = createHeaders();\r
+        String url = MicroServiceConfig.getMsbServerAddrWithHttpPrefix() + RuleMgtConstant.ENGINE_PATH;\r
+        return HttpsUtils.post(url, headers, new HashMap<>(), new StringEntity(content));\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.getMsbServerAddrWithHttpPrefix() + RuleMgtConstant.ENGINE_PATH);\r
-        return webTarget.request(MediaType.APPLICATION_JSON).post(Entity.entity(content, MediaType.APPLICATION_JSON));\r
+    protected HttpResponse deploy(CorrelationDeployRule4Engine correlationDeployRule4Engine) throws Exception {\r
+        String content = GsonUtil.beanToJson(correlationDeployRule4Engine);\r
+        HashMap headers = createHeaders();\r
+        String url = MicroServiceConfig.getMsbServerAddrWithHttpPrefix() + RuleMgtConstant.ENGINE_PATH;\r
+        return HttpsUtils.put(url, headers, new HashMap<>(), new StringEntity(content));\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.getMsbServerAddrWithHttpPrefix() + RuleMgtConstant.ENGINE_PATH);\r
-        return webTarget.request(MediaType.APPLICATION_JSON).put(Entity.entity(content, MediaType.APPLICATION_JSON));\r
+    private HashMap<String, String> createHeaders() {\r
+        HashMap<String, String> headers = new HashMap<>();\r
+        headers.put("Content-Type", MediaType.APPLICATION_JSON);\r
+        headers.put("Accept", MediaType.APPLICATION_JSON);\r
+        return headers;\r
     }\r
 }\r
index 733cae0..183e52f 100644 (file)
 package org.onap.holmes.rulemgt.bolt.enginebolt;\r
 \r
 import javax.inject.Inject;\r
-import javax.ws.rs.core.Response;\r
 import lombok.extern.slf4j.Slf4j;\r
 import net.sf.json.JSONObject;\r
+import org.apache.http.HttpResponse;\r
 import org.jvnet.hk2.annotations.Service;\r
+import org.onap.holmes.common.utils.HttpsUtils;\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
@@ -33,16 +34,16 @@ public class EngineWrapper {
     private EngineService engineService;\r
 \r
     public String deployEngine(CorrelationDeployRule4Engine correlationRule) throws CorrelationException {\r
-        Response response;\r
+        HttpResponse response;\r
         try {\r
             response = engineService.deploy(correlationRule);\r
         } catch (Exception e) {\r
             throw new CorrelationException("Failed to call the rule deployment RESTful API.", e);\r
         }\r
-        if (response.getStatus() == RuleMgtConstant.RESPONSE_STATUS_OK) {\r
+        if (response.getStatusLine().getStatusCode() == RuleMgtConstant.RESPONSE_STATUS_OK) {\r
             log.info("Succeeded in calling the rule deployment RESTful API from the engine management service.");\r
             try {\r
-                JSONObject json = JSONObject.fromObject(response.readEntity(String.class));\r
+                JSONObject json = JSONObject.fromObject(HttpsUtils.extractResponseEntity(response));\r
                 return json.get(RuleMgtConstant.PACKAGE).toString();\r
             } catch (Exception e) {\r
                 throw new CorrelationException("Failed to parse the value returned by the engine management service.", e);\r
@@ -53,13 +54,13 @@ public class EngineWrapper {
     }\r
 \r
     public boolean deleteRuleFromEngine(String packageName) throws CorrelationException {\r
-        Response response;\r
+        HttpResponse response;\r
         try {\r
             response = engineService.delete(packageName);\r
         } catch (Exception e) {\r
             throw new CorrelationException("Failed to call the rule deleting RESTful API.", e);\r
         }\r
-        if (response.getStatus() == RuleMgtConstant.RESPONSE_STATUS_OK) {\r
+        if (response.getStatusLine().getStatusCode() == RuleMgtConstant.RESPONSE_STATUS_OK) {\r
             log.info("Succeeded in calling the rule deleting RESTful API from the engine management service.");\r
             return true;\r
         } else {\r
@@ -70,17 +71,17 @@ public class EngineWrapper {
     public boolean checkRuleFromEngine(CorrelationCheckRule4Engine correlationCheckRule4Engine)\r
             throws CorrelationException {\r
         log.info("Rule Contents: " + correlationCheckRule4Engine.getContent());\r
-        Response response;\r
+        HttpResponse response;\r
         try {\r
             response = engineService.check(correlationCheckRule4Engine);\r
         } catch (Exception e) {\r
             throw new CorrelationException("Failed to call the rule verification RESTful API.", e);\r
         }\r
-        if (response.getStatus() == RuleMgtConstant.RESPONSE_STATUS_OK) {\r
+        if (response.getStatusLine().getStatusCode() == RuleMgtConstant.RESPONSE_STATUS_OK) {\r
             log.info("Succeeded in calling the rule verification RESTful API from the engine management service.");\r
             return true;\r
         } else {\r
-            log.info(response.getStatus() + " " + response.getStatusInfo() + " " + response.getEntity());\r
+            log.info(response.getStatusLine().getStatusCode() + " " + response.getEntity());\r
             throw new CorrelationException("Failed to verify the rule. The contents of the rule are invalid.");\r
         }\r
     }\r
index a5ec87c..5ac4fe6 100644 (file)
@@ -23,7 +23,7 @@ public class RuleMgtConstant {
     public static final int STATUS_RULE_OPEN = 1;\r
     public static final int STATUS_RULE_CLOSE = 0;\r
     public static final int STATUS_RULE_ALL = 2;\r
-    public static final String PACKAGE = "package";\r
+    public static final String PACKAGE = "packageName";\r
     public static final String ENGINE_PATH = "/api/holmes-engine-mgmt/v1/rule";\r
     public static final int RESPONSE_STATUS_OK = 200;\r
 }\r
index d0513ca..0e4f790 100644 (file)
  */
 package org.onap.holmes.rulemgt.dcae;
 
+import com.alibaba.fastjson.JSON;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonDeserializationContext;
+import com.google.gson.JsonDeserializer;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonParseException;
+import com.google.gson.reflect.TypeToken;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.lang.reflect.Type;
+import java.util.HashMap;
 import java.util.List;
-import javax.ws.rs.client.Client;
-import javax.ws.rs.client.ClientBuilder;
-import javax.ws.rs.client.Entity;
-import javax.ws.rs.client.WebTarget;
 import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
 import lombok.extern.slf4j.Slf4j;
-import org.glassfish.jersey.client.ClientConfig;
+import org.apache.http.HttpResponse;
+import org.apache.http.entity.StringEntity;
 import org.onap.holmes.common.dcae.DcaeConfigurationQuery;
 import org.onap.holmes.common.dcae.entity.DcaeConfigurations;
 import org.onap.holmes.common.dcae.entity.Rule;
 import org.onap.holmes.common.exception.CorrelationException;
 import org.onap.holmes.common.utils.GsonUtil;
+import org.onap.holmes.common.utils.HttpsUtils;
 import org.onap.holmes.common.utils.Md5Util;
 import org.onap.holmes.rulemgt.bean.request.RuleCreateRequest;
 import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;
@@ -39,7 +48,7 @@ public class DcaeConfigurationPolling implements Runnable {
 
     private String hostname;
 
-    private String url = "http://127.0.0.1:9101/api/holmes-rule-mgmt/v1/rule";
+    private String url = "https://127.0.0.1:9101/api/holmes-rule-mgmt/v1/rule";
 
     public DcaeConfigurationPolling(String hostname) {
         this.hostname = hostname;
@@ -55,7 +64,7 @@ public class DcaeConfigurationPolling implements Runnable {
         try {
             dcaeConfigurations = DcaeConfigurationQuery.getDcaeConfigurations(hostname);
             String md5 = Md5Util.md5(dcaeConfigurations);
-            if (prevResult && prevConfigMd5.equals(md5)) {
+            if (prevResult && prevConfigMd5.equals(md5)){
                 log.info("Operation aborted due to identical Configurations.");
                 return;
             }
@@ -63,9 +72,20 @@ public class DcaeConfigurationPolling implements Runnable {
             prevResult = false;
         } catch (CorrelationException e) {
             log.error("Failed to fetch DCAE configurations. " + e.getMessage(), e);
+        } catch (Exception e) {
+            log.info("Failed to generate the MD5 information for new configurations.", e);
         }
+        RuleQueryListResponse ruleQueryListResponse = null;
         if (dcaeConfigurations != null) {
-            RuleQueryListResponse ruleQueryListResponse = getAllCorrelationRules();
+            try {
+                ruleQueryListResponse = getAllCorrelationRules();
+            } catch (CorrelationException e) {
+                log.error("Failed to get right response!" + e.getMessage(), e);
+            } catch (IOException e) {
+                log.error("Failed to extract response entity. " + e.getMessage(), e);
+            }
+        }
+        if (ruleQueryListResponse != null) {
             List<RuleResult4API> ruleResult4APIs = ruleQueryListResponse.getCorrelationRules();
             deleteAllCorrelationRules(ruleResult4APIs);
             try {
@@ -77,24 +97,39 @@ public class DcaeConfigurationPolling implements Runnable {
         }
     }
 
-    private RuleQueryListResponse getAllCorrelationRules() {
-        Client client = ClientBuilder.newClient(new ClientConfig());
-        WebTarget webTarget = client.target(url);
-        return webTarget.request("application/json").get()
-                .readEntity(RuleQueryListResponse.class);
+    public RuleQueryListResponse getAllCorrelationRules() throws CorrelationException, IOException {
+              HashMap<String, String> headers = new HashMap<>();
+        headers.put("Content-Type", MediaType.APPLICATION_JSON);
+        HttpResponse httpResponse = HttpsUtils.get(url, headers);
+        String response = HttpsUtils.extractResponseEntity(httpResponse);
+        return JSON.parseObject(response,RuleQueryListResponse.class);
     }
 
-    private boolean addAllCorrelationRules(DcaeConfigurations dcaeConfigurations)
-            throws CorrelationException {
+    private boolean addAllCorrelationRules(DcaeConfigurations dcaeConfigurations) throws CorrelationException {
         boolean suc = false;
         for (Rule rule : dcaeConfigurations.getDefaultRules()) {
             RuleCreateRequest ruleCreateRequest = getRuleCreateRequest(rule);
-            Client client = ClientBuilder.newClient(new ClientConfig());
-            String content = GsonUtil.beanToJson(ruleCreateRequest);
-            WebTarget webTarget = client.target(url);
-            Response response = webTarget.request(MediaType.APPLICATION_JSON)
-                    .put(Entity.entity(content, MediaType.APPLICATION_JSON));
-            suc = response.getStatus() == 200;
+            String content = "";
+            try {
+                content = GsonUtil.beanToJson(ruleCreateRequest);
+            } catch (Exception e) {
+                throw new CorrelationException("Failed to convert the message object to a json string.", e);
+            }
+            HashMap<String, String> headers = new HashMap<>();
+            headers.put("Content-Type", MediaType.APPLICATION_JSON);
+            headers.put("Accept", MediaType.APPLICATION_JSON);
+            HttpResponse httpResponse;
+            try {
+                httpResponse = HttpsUtils
+                        .put(url, headers, new HashMap<>(), new StringEntity(content));
+            } catch (UnsupportedEncodingException e) {
+                throw new CorrelationException("Failed to create https entity.", e);
+            } catch (Exception e) {
+                throw new CorrelationException(e.getMessage());
+            }
+            if (httpResponse != null) {
+                suc = httpResponse.getStatusLine().getStatusCode() == 200;
+            }
             if (!suc) {
                 break;
             }
@@ -102,11 +137,16 @@ public class DcaeConfigurationPolling implements Runnable {
         return suc;
     }
 
-    private void deleteAllCorrelationRules(List<RuleResult4API> ruleResult4APIs) {
-        ruleResult4APIs.forEach(correlationRule -> {
-            Client client = ClientBuilder.newClient(new ClientConfig());
-            WebTarget webTarget = client.target(url + "/" + correlationRule.getRuleId());
-            webTarget.request(MediaType.APPLICATION_JSON).delete();
+    private void deleteAllCorrelationRules(List<RuleResult4API> ruleResult4APIs){
+        ruleResult4APIs.forEach(correlationRule ->{
+            HashMap<String, String> headers = new HashMap<>();
+            headers.put("Content-Type", MediaType.APPLICATION_JSON);
+            try {
+                HttpsUtils.delete(url + "/" + correlationRule.getRuleId(), headers);
+            } catch (Exception e) {
+                log.warn("Failed to delete rule, the rule id is : " + correlationRule.getRuleId()
+                        + " exception messge is : " + e.getMessage(), e);
+            }
         });
     }
 
index 041377f..d3c01ae 100644 (file)
@@ -5,7 +5,7 @@
  * 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
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
  *\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
@@ -20,6 +20,9 @@ import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;\r
 import io.swagger.annotations.ApiParam;\r
 import io.swagger.annotations.SwaggerDefinition;\r
+import java.util.ArrayList;\r
+import java.util.Date;\r
+import java.util.List;\r
 import java.util.Locale;\r
 import javax.inject.Inject;\r
 import javax.servlet.http.HttpServletRequest;\r
@@ -47,6 +50,7 @@ import org.onap.holmes.rulemgt.bean.request.RuleQueryCondition;
 import org.onap.holmes.rulemgt.bean.request.RuleUpdateRequest;\r
 import org.onap.holmes.rulemgt.bean.response.RuleAddAndUpdateResponse;\r
 import org.onap.holmes.rulemgt.bean.response.RuleQueryListResponse;\r
+import org.onap.holmes.rulemgt.bean.response.RuleResult4API;\r
 import org.onap.holmes.rulemgt.constant.RuleMgtConstant;\r
 import org.onap.holmes.rulemgt.wrapper.RuleMgtWrapper;\r
 \r
@@ -67,10 +71,9 @@ public class RuleMgtResources {
             response = RuleAddAndUpdateResponse.class)\r
     @Timed\r
     public RuleAddAndUpdateResponse addCorrelationRule(@Context HttpServletRequest request,\r
-            @ApiParam(value =\r
-                    "The request entity of the HTTP call, which comprises \"rulename\"(required), "\r
-                            + "\"loopcontrolname\"(required), \"content\"(required), \"enabled\"(required) "\r
-                            + "and \"description\"(optional)", required = true)\r
+            @ApiParam(value = "The request entity of the HTTP call, which comprises \"ruleName\"(required), "\r
+                    + "\"loopControlName\"(required), \"content\"(required), \"enabled\"(required) "\r
+                    + "and \"description\"(optional)", required = true)\r
                     RuleCreateRequest ruleCreateRequest) {\r
         Locale locale = LanguageUtil.getLocale(request);\r
         RuleAddAndUpdateResponse ruleChangeResponse;\r
@@ -90,15 +93,13 @@ public class RuleMgtResources {
     @ApiOperation(value = "Update an existing rule; deploy it to the Drools engine if it is enabled.", response = RuleAddAndUpdateResponse.class)\r
     @Timed\r
     public RuleAddAndUpdateResponse updateCorrelationRule(@Context HttpServletRequest request,\r
-            @ApiParam(value =\r
-                    "The request entity of the HTTP call, which comprises \"ruleid\"(required), "\r
-                            + "\"content\"(required), \"enabled\"(required) and \"description\"(optional)", required = true)\r
+            @ApiParam(value = "The request entity of the HTTP call, which comprises \"ruleId\"(required), "\r
+                    + "\"content\"(required), \"enabled\"(required) and \"description\"(optional)", required = true)\r
                     RuleUpdateRequest ruleUpdateRequest) {\r
         Locale locale = LanguageUtil.getLocale(request);\r
         RuleAddAndUpdateResponse ruleChangeResponse;\r
         try {\r
-            ruleChangeResponse = ruleMgtWrapper\r
-                    .updateCorrelationRule(UserUtil.getUserName(request), ruleUpdateRequest);\r
+            ruleChangeResponse = ruleMgtWrapper.updateCorrelationRule(UserUtil.getUserName(request), ruleUpdateRequest);\r
             log.info("update rule:" + ruleUpdateRequest.getRuleId() + " successful");\r
             return ruleChangeResponse;\r
         } catch (CorrelationException e) {\r
@@ -130,19 +131,18 @@ public class RuleMgtResources {
     @ApiOperation(value = "Query rules using certain criteria.", response = RuleQueryListResponse.class)\r
     @Timed\r
     public RuleQueryListResponse getCorrelationRules(@Context HttpServletRequest request,\r
-            @ApiParam(value =\r
-                    "A JSON string used as a query parameter, which comprises \"ruleid\"(optional), "\r
-                            + "\"rulename\"(optional), \"creator\"(optional), "\r
-                            + "\"modifier\"(optional) and \"enabled\"(optional). E.g. {\"ruleid\":\"rule_1484727187317\"}",\r
+            @ApiParam(value = "A JSON string used as a query parameter, which comprises \"ruleid\"(optional), "\r
+                    + "\"rulename\"(optional), \"creator\"(optional), "\r
+                    + "\"modifier\"(optional) and \"enabled\"(optional). E.g. {\"ruleid\":\"rule_1484727187317\"}",\r
                     required = false) @QueryParam("queryrequest") String ruleQueryRequest) {\r
         Locale locale = LanguageUtil.getLocale(request);\r
         RuleQueryListResponse ruleQueryListResponse;\r
+\r
         RuleQueryCondition ruleQueryCondition = getRuleQueryCondition(ruleQueryRequest, request);\r
         try {\r
             ruleQueryListResponse = ruleMgtWrapper\r
                     .getCorrelationRuleByCondition(ruleQueryCondition);\r
-            log.info("query rule successful by condition:" + JSONObject\r
-                    .fromObject(ruleQueryCondition));\r
+            log.info("query rule successful by condition:" + JSONObject.fromObject(ruleQueryCondition));\r
             return ruleQueryListResponse;\r
         } catch (CorrelationException e) {\r
             log.error("query rule failed,cause query condition conversion failure", e);\r
@@ -153,7 +153,6 @@ public class RuleMgtResources {
     private RuleQueryCondition getRuleQueryCondition(String queryRequest,\r
             HttpServletRequest request) {\r
         Locale locale = LanguageUtil.getLocale(request);\r
-\r
         RuleQueryCondition ruleQueryCondition = GsonUtil.jsonToBean(queryRequest, RuleQueryCondition.class);\r
         if (queryRequest == null) {\r
             if(ruleQueryCondition==null){\r
index b63b106..cc8c047 100644 (file)
@@ -20,32 +20,40 @@ package org.onap.holmes.rulemgt.bolt.enginebolt;
 import static org.hamcrest.MatcherAssert.assertThat;\r
 import static org.hamcrest.Matchers.equalTo;\r
 \r
-import javax.ws.rs.core.Response;\r
+import org.apache.http.HttpResponse;\r
 import org.apache.http.StatusLine;\r
 import org.easymock.EasyMock;\r
 import org.junit.Before;\r
 import org.junit.Rule;\r
 import org.junit.Test;\r
 import org.junit.rules.ExpectedException;\r
+import org.junit.runner.RunWith;\r
+import org.onap.holmes.common.utils.HttpsUtils;\r
 import org.onap.holmes.rulemgt.bean.request.CorrelationCheckRule4Engine;\r
 import org.onap.holmes.rulemgt.bean.request.CorrelationDeployRule4Engine;\r
 import org.onap.holmes.common.exception.CorrelationException;\r
 import org.powermock.api.easymock.PowerMock;\r
+import org.powermock.api.mockito.PowerMockito;\r
+import org.powermock.core.classloader.annotations.PrepareForTest;\r
+import org.powermock.modules.junit4.PowerMockRunner;\r
 import org.powermock.reflect.Whitebox;\r
 \r
+@PrepareForTest({EngineWrapper.class, EngineService.class, HttpsUtils.class, HttpResponse.class,\r
+        StatusLine.class})\r
+@RunWith(PowerMockRunner.class)\r
 public class EngineWrapperTest {\r
 \r
     @Rule\r
     public ExpectedException thrown = ExpectedException.none();\r
     private EngineWrapper engineWrapper = new EngineWrapper();\r
     private EngineService engineServiceMock;\r
-    private Response response;\r
+    private HttpResponse httpResponse;\r
     private StatusLine statusLineMock;\r
 \r
     @Before\r
     public void setUp() throws Exception {\r
         engineServiceMock = PowerMock.createMock(EngineService.class);\r
-        response = PowerMock.createMock(Response.class);\r
+        httpResponse = PowerMock.createMock(HttpResponse.class);\r
         statusLineMock = PowerMock.createMock(StatusLine.class);\r
         Whitebox.setInternalState(engineWrapper, "engineService", engineServiceMock);\r
     }\r
@@ -55,8 +63,10 @@ public class EngineWrapperTest {
         thrown.expect(CorrelationException.class);\r
         thrown.expectMessage("Failed to call the rule deployment RESTful API.");\r
 \r
-        EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class))).andThrow(\r
-                new RuntimeException(""));\r
+        EasyMock.expect(\r
+                engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))\r
+                .andThrow(\r
+                        new RuntimeException(""));\r
         PowerMock.replayAll();\r
 \r
         engineWrapper.deployEngine(new CorrelationDeployRule4Engine());\r
@@ -69,9 +79,11 @@ public class EngineWrapperTest {
         thrown.expect(CorrelationException.class);\r
         thrown.expectMessage("Failed to deploy the rule!");\r
 \r
-        EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))\r
-                .andReturn(response);\r
-        EasyMock.expect(response.getStatus()).andReturn(400);\r
+        EasyMock.expect(\r
+                engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))\r
+                .andReturn(httpResponse);\r
+        EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);\r
+        EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400);\r
         PowerMock.replayAll();\r
 \r
         engineWrapper.deployEngine(new CorrelationDeployRule4Engine());\r
@@ -81,14 +93,18 @@ public class EngineWrapperTest {
 \r
     @Test\r
     public void deployEngine_parse_content_exception() throws Exception {\r
+        PowerMock.resetAll();\r
         String content = "";\r
-\r
+        PowerMockito.mockStatic(HttpsUtils.class);\r
         thrown.expect(CorrelationException.class);\r
-        thrown.expectMessage("Failed to parse the value returned by the engine management service.");\r
-        EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))\r
-                .andReturn(response);\r
-        EasyMock.expect(response.getStatus()).andReturn(200);\r
-        EasyMock.expect(response.readEntity(String.class)).andReturn(content);\r
+        thrown.expectMessage(\r
+                "Failed to parse the value returned by the engine management service.");\r
+        EasyMock.expect(\r
+                engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))\r
+                .andReturn(httpResponse);\r
+        EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);\r
+        EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);\r
+        PowerMockito.when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn(content);\r
         PowerMock.replayAll();\r
 \r
         engineWrapper.deployEngine(new CorrelationDeployRule4Engine());\r
@@ -98,11 +114,15 @@ public class EngineWrapperTest {
 \r
     @Test\r
     public void deployEngine_success() throws Exception {\r
-        String content = "{\"package\":\"test\"}";\r
-        EasyMock.expect(engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))\r
-                .andReturn(response);\r
-        EasyMock.expect(response.getStatus()).andReturn(200);\r
-        EasyMock.expect(response.readEntity(String.class)).andReturn(content);\r
+        PowerMock.resetAll();\r
+        String content = "{\"packageName\":\"test\"}";\r
+        PowerMockito.mockStatic(HttpsUtils.class);\r
+        EasyMock.expect(\r
+                engineServiceMock.deploy(EasyMock.anyObject(CorrelationDeployRule4Engine.class)))\r
+                .andReturn(httpResponse);\r
+        EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);\r
+        EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);\r
+        PowerMockito.when(HttpsUtils.extractResponseEntity(httpResponse)).thenReturn(content);\r
         PowerMock.replayAll();\r
 \r
         String result = engineWrapper.deployEngine(new CorrelationDeployRule4Engine());\r
@@ -131,8 +151,9 @@ public class EngineWrapperTest {
         thrown.expectMessage("Failed to delete the rule!");\r
 \r
         EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class)))\r
-                .andReturn(response);\r
-        EasyMock.expect(response.getStatus()).andReturn(400);\r
+                .andReturn(httpResponse);\r
+        EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);\r
+        EasyMock.expect(statusLineMock.getStatusCode()).andReturn(400);\r
 \r
         PowerMock.replayAll();\r
 \r
@@ -144,8 +165,9 @@ public class EngineWrapperTest {
     @Test\r
     public void deleteRuleFromEngine_success() throws Exception {\r
         EasyMock.expect(engineServiceMock.delete(EasyMock.anyObject(String.class)))\r
-                .andReturn(response);\r
-        EasyMock.expect(response.getStatus()).andReturn(200);\r
+                .andReturn(httpResponse);\r
+        EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);\r
+        EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);\r
 \r
         PowerMock.replayAll();\r
 \r
@@ -159,8 +181,10 @@ public class EngineWrapperTest {
         thrown.expect(CorrelationException.class);\r
         thrown.expectMessage("Failed to call the rule verification RESTful API.");\r
 \r
-        EasyMock.expect(engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class))).andThrow(\r
-                new RuntimeException(""));\r
+        EasyMock.expect(\r
+                engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))\r
+                .andThrow(\r
+                        new RuntimeException(""));\r
         PowerMock.replayAll();\r
 \r
         engineWrapper.checkRuleFromEngine(new CorrelationCheckRule4Engine());\r
@@ -170,9 +194,11 @@ public class EngineWrapperTest {
 \r
     @Test\r
     public void checkRuleFromEngine_success() throws Exception {\r
-        EasyMock.expect(engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))\r
-                .andReturn(response);\r
-        EasyMock.expect(response.getStatus()).andReturn(200);\r
+        EasyMock.expect(\r
+                engineServiceMock.check(EasyMock.anyObject(CorrelationCheckRule4Engine.class)))\r
+                .andReturn(httpResponse);\r
+        EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLineMock);\r
+        EasyMock.expect(statusLineMock.getStatusCode()).andReturn(200);\r
 \r
         PowerMock.replayAll();\r
 \r
index 8d16198..1037495 100644 (file)
  */
 package org.onap.holmes.rulemgt.dcae;
 
-import static org.easymock.EasyMock.anyObject;
-import static org.hamcrest.CoreMatchers.anyOf;
 import static org.hamcrest.CoreMatchers.equalTo;
 import static org.junit.Assert.assertThat;
 import static org.powermock.api.mockito.PowerMockito.when;
-
-import com.alibaba.fastjson.JSONException;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import javax.ws.rs.ProcessingException;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
@@ -40,7 +34,7 @@ import org.powermock.core.classloader.annotations.PrepareForTest;
 import org.powermock.modules.junit4.PowerMockRunner;
 import org.powermock.reflect.Whitebox;
 
-@PrepareForTest({DcaeConfigurationPolling.class, MicroServiceConfig.class, ObjectMapper.class})
+@PrepareForTest({DcaeConfigurationPolling.class, MicroServiceConfig.class})
 @RunWith(PowerMockRunner.class)
 public class DcaeConfigurationPollingTest {
 
@@ -64,7 +58,7 @@ public class DcaeConfigurationPollingTest {
                 .thenReturn("host");
         PowerMock.createMock(DcaeConfigurationParser.class);
         PowerMock.expectPrivate(DcaeConfigurationParser.class, "parse", "host")
-                .andThrow(new CorrelationException("")).anyTimes();
+                .andThrow(new CorrelationException("tests")).anyTimes();
 
         PowerMock.replayAll();
         Whitebox.invokeMethod(daceConfigurationPolling, "getDcaeConfigurations");
@@ -94,7 +88,7 @@ public class DcaeConfigurationPollingTest {
     public void testDaceConfigurationPolling_addAllCorrelationRules_connection_exception()
             throws Exception {
         PowerMock.resetAll();
-        thrown.expect(ProcessingException.class);
+        thrown.expect(CorrelationException.class);
         DcaeConfigurations dcaeConfigurations = new DcaeConfigurations();
         Rule rule = new Rule("test", "test", "tset",1);
         dcaeConfigurations.getDefaultRules().add(rule);
index 0c3bd89..a35f6b8 100644 (file)
@@ -57,6 +57,7 @@ public class RuleMgtResourcesTest {
 \r
     @Test\r
     public void addCorrelationRule_correlation_exception() throws Exception {\r
+        PowerMock.resetAll();\r
         thrown.expect(WebApplicationException.class);\r
 \r
         final RuleCreateRequest ruleCreateRequest = new RuleCreateRequest();\r