Policy:Blocker
[policy/engine.git] / BRMSGateway / src / main / java / org / onap / policy / brmsInterface / BRMSPush.java
index ac1d0ac..86cd066 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP Policy Engine
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -46,6 +46,7 @@ import java.util.UUID;
 import java.util.concurrent.TimeUnit;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
+import java.util.regex.Pattern;
 
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
@@ -374,6 +375,11 @@ public class BRMSPush {
         if (flag)
             syncGroupInfo();
     }
+    
+    public void resetDS(){
+       resetModifiedGroups();
+        controllers = new ArrayList<>();
+    }
 
     private static void resetModifiedGroups() {
         modifiedGroups = new HashMap<>();
@@ -410,6 +416,7 @@ public class BRMSPush {
                         PEDependency dependency = PolicyUtils.jsonStringToObject(value,
                                 PEDependency.class);
                         userControllerName = key.replaceFirst("$controller:", "");
+                        LOGGER.info("addRule: userControllerName - " + userControllerName + ", dependency: - " + dependency);
                         addToGroup(userControllerName, dependency);
                     } catch (Exception e) {
                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error while resolving Controller: " + e);
@@ -417,7 +424,7 @@ public class BRMSPush {
 
                 } else if ("$dependency$".equals(key) && value.startsWith("[") && value.endsWith("]")) {
                     value = value.substring(1, value.length() - 1).trim();
-                    List<String> dependencyStrings = Arrays.asList(value.split("},{"));
+                    List<String> dependencyStrings = Arrays.asList(value.split(Pattern.quote("},{")));
                     for (String dependencyString : dependencyStrings) {
                         try {
                             userDependencies
@@ -445,7 +452,8 @@ public class BRMSPush {
             // If the key is not got as parameters set by the user, setting the default value for kSessionName as
             // closedLoop
             if (kSessionName == null) {
-                if (selectedName == defaultName) {
+               LOGGER.info("kSessionName is null, selectedName is  : " + selectedName );
+                if (selectedName.equalsIgnoreCase(defaultName)) {
                     kSessionName = "closedloop";
                 } else {
                     kSessionName = "closedloop-" + selectedName;
@@ -564,19 +572,22 @@ public class BRMSPush {
         String fileName = "rule.jar";
         try {
             website = new URL(artifact.getResourceURI());
-            ReadableByteChannel rbc = Channels.newChannel(website.openStream());
-            FileOutputStream fos = new FileOutputStream(fileName);
-            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
-            fos.close();
-            extractJar(fileName, dirName);
-            new File(fileName).delete();
-        } catch (IOException e) {
-            LOGGER.error("Error while downloading the project to File System. " + e.getMessage(), e);
+            try( ReadableByteChannel rbc = Channels.newChannel(website.openStream());
+                       FileOutputStream fos = new FileOutputStream(fileName)){
+               fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
+                extractJar(fileName, dirName);
+                new File(fileName).delete();
+            }catch (IOException e) {
+         LOGGER.error("Error while downloading the project to File System. " + e.getMessage(), e);
+                       }
+          
+        } catch (IOException e1) {
+            LOGGER.error("Error while retrieve the artifact. " + e1.getMessage(), e1);
         }
     }
 
-    private void extractJar(String jarFileName, String artifactId) throws IOException {
-        JarFile jar = new JarFile(jarFileName);
+private void extractJar(String jarFileName, String artifactId){
+               try (JarFile jar = new JarFile(jarFileName)) {  
         Enumeration<?> enumEntries = jar.entries();
         while (enumEntries.hasMoreElements()) {
             JarEntry file = (JarEntry) enumEntries.nextElement();
@@ -602,18 +613,20 @@ public class BRMSPush {
                 f = new File(path + File.separator + fileName);
             }
             if (f != null) {
-                InputStream is = jar.getInputStream(file);
-                FileOutputStream fos = new FileOutputStream(f);
+                               try (InputStream is = jar.getInputStream(file); FileOutputStream fos = new FileOutputStream(f)) {
                 while (is.available() > 0) {
                     fos.write(is.read());
                 }
-                fos.close();
-                is.close();
                 LOGGER.info(fileName + " Created..");
+                               } catch (IOException e) {
+                                               LOGGER.info("exception Occured" + e);
+                                       }
             }
-        }
-        jar.close();
-    }
+         }
+               } catch (IOException e) {
+                       LOGGER.info("exception Occured" + e);
+               }
+  }
 
     private NexusArtifact getLatestArtifactFromNexus(String selectedName) {
         List<NexusArtifact> artifacts = getArtifactFromNexus(selectedName, null);
@@ -747,6 +760,7 @@ public class BRMSPush {
                 InvocationResult result = null;
                String group = entry.getKey();
                 try {
+                       LOGGER.info("PushRules: ModifiedGroups, Key: " + group + ", Value: " + entry.getValue());
                     InvocationRequest request = new DefaultInvocationRequest();
                     setVersion(group);
                     createPom(group);