Sonar issues fixes in UserRole
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / helpers / UserRole.java
index a289fe0..288211e 100644 (file)
@@ -7,9 +7,9 @@
  * 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.
@@ -26,6 +26,7 @@ import java.util.ArrayList;
 import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
+import java.util.SortedMap;
 import java.util.TreeMap;
 
 import org.onap.aaf.auth.actions.URDelete;
@@ -44,16 +45,36 @@ import com.datastax.driver.core.SimpleStatement;
 import com.datastax.driver.core.Statement;
 
 public class UserRole implements Cloneable, CacheChange.Data  {
-       public static final List<UserRole> data = new ArrayList<>();
-    public static final TreeMap<String,List<UserRole>> byUser = new TreeMap<>();
-    public static final TreeMap<String,List<UserRole>> byRole = new TreeMap<>();
-       private final static CacheChange<UserRole> cache = new CacheChange<>(); 
-       private static PrintStream urDelete=System.out,urRecover=System.err;
+
+       private static final String SEPARATOR = "\",\"";
+
+       // CACHE Calling
+       private static final String LOG_FMT = "%s UserRole - %s: %s-%s (%s, %s) expiring %s";
+       private static final String REPLAY_FMT = "%s|%s|%s|%s|%s\n";
+       private static final String DELETE_FMT = "# %s\n"+ REPLAY_FMT;
+
+       private static final List<UserRole> data = new ArrayList<>();
+       private static final SortedMap<String,List<UserRole>> byUser = new TreeMap<>();
+       private static final SortedMap<String,List<UserRole>> byRole = new TreeMap<>();
+       private static final CacheChange<UserRole> cache = new CacheChange<>();
+       private static PrintStream urDelete = System.out;
+       private static PrintStream urRecover = System.err;
        private static int totalLoaded;
-       private static int deleted;
-       
+       private int deleted;
        private Data urdd;
 
+       public static final Creator<UserRole> v2_0_11 = new Creator<UserRole>() {
+               @Override
+               public UserRole create(Row row) {
+                       return new UserRole(row.getString(0), row.getString(1), row.getString(2),row.getString(3),row.getTimestamp(4));
+               }
+
+               @Override
+               public String select() {
+                       return "select user,role,ns,rname,expires from authz.user_role";
+               }
+       };
+
        public UserRole(String user, String ns, String rname, Date expires) {   
                urdd = new UserRoleDAO.Data();
                urdd.user = user;
@@ -72,6 +93,18 @@ public class UserRole implements Cloneable, CacheChange.Data  {
                urdd.expires = expires;
        }
 
+       public static List<UserRole> getData() {
+               return data;
+       }
+
+       public static SortedMap<String, List<UserRole>> getByUser() {
+               return byUser;
+       }
+
+       public static SortedMap<String, List<UserRole>> getByRole() {
+               return byRole;
+       }
+
        public static void load(Trans trans, Session session, Creator<UserRole> creator ) {
                load(trans,session,creator,null);
        }
@@ -87,9 +120,9 @@ public class UserRole implements Cloneable, CacheChange.Data  {
        private static void load(Trans trans, Session session, Creator<UserRole> creator, String where) {
                String query = creator.query(where);
                trans.info().log( "query: " + query );
-        TimeTaken tt = trans.start("Read UserRoles", Env.REMOTE);
-       
-        ResultSet results;
+               TimeTaken tt = trans.start("Read UserRoles", Env.REMOTE);
+
+               ResultSet results;
                try {
                Statement stmt = new SimpleStatement( query );
                results = session.execute(stmt);
@@ -97,30 +130,9 @@ public class UserRole implements Cloneable, CacheChange.Data  {
                tt.done();
         }
         try {
-               Iterator<Row> iter = results.iterator();
-               Row row;
                tt = trans.start("Load UserRole", Env.SUB);
                try {
-                       while(iter.hasNext()) {
-                               ++totalLoaded;
-                               row = iter.next();
-                               UserRole ur = creator.create(row);
-                               data.add(ur);
-                               
-                               List<UserRole> lur = byUser.get(ur.urdd.user);
-                               if(lur==null) {
-                                       lur = new ArrayList<>();
-                                       byUser.put(ur.urdd.user, lur);
-                               }
-                               lur.add(ur);
-                               
-                               lur = byRole.get(ur.urdd.role);
-                               if(lur==null) {
-                                       lur = new ArrayList<>();
-                                       byRole.put(ur.urdd.role, lur);
-                               }
-                               lur.add(ur);
-                       }
+                                               iterateResults(creator, results.iterator());
                } finally {
                        tt.done();
                }
@@ -128,7 +140,31 @@ public class UserRole implements Cloneable, CacheChange.Data  {
                trans.info().log("Loaded",totalLoaded,"UserRoles");
         }
        }
-       
+
+       private static void iterateResults(Creator<UserRole> creator, Iterator<Row> iter ) {
+               Row row;
+               while(iter.hasNext()) {
+                       ++totalLoaded;
+                       row = iter.next();
+                       UserRole ur = creator.create(row);
+                       data.add(ur);
+
+                       List<UserRole> lur = byUser.get(ur.urdd.user);
+                       if(lur==null) {
+                               lur = new ArrayList<>();
+                               byUser.put(ur.urdd.user, lur);
+                       }
+                       lur.add(ur);
+
+                       lur = byRole.get(ur.urdd.role);
+                       if(lur==null) {
+                               lur = new ArrayList<>();
+                               byRole.put(ur.urdd.role, lur);
+                       }
+                       lur.add(ur);
+               }
+       }
+
        public int totalLoaded() {
                return totalLoaded;
        }
@@ -174,26 +210,13 @@ public class UserRole implements Cloneable, CacheChange.Data  {
         }
        }
 
-
-       public static Creator<UserRole> v2_0_11 = new Creator<UserRole>() {
-               @Override
-               public UserRole create(Row row) {
-                       return new UserRole(row.getString(0), row.getString(1), row.getString(2),row.getString(3),row.getTimestamp(4));
-               }
-
-               @Override
-               public String select() {
-                       return "select user,role,ns,rname,expires from authz.user_role";
-               }
-       };
-
        public UserRoleDAO.Data urdd() {
                return urdd;
        }
        
        public String user() {
                return urdd.user;
-       };
+       }
        
        public String role() {
                return urdd.role;
@@ -215,16 +238,16 @@ public class UserRole implements Cloneable, CacheChange.Data  {
                urdd.expires = time;
        }
 
-
-
        public String toString() {
-               return "\"" + urdd.user + "\",\"" + urdd.role + "\",\""  + urdd.ns + "\",\"" + urdd.rname + "\",\""+ Chrono.dateOnlyStamp(urdd.expires);
+               return "\"" + urdd.user + SEPARATOR + urdd.role + SEPARATOR + urdd.ns + SEPARATOR + urdd.rname + SEPARATOR
+                       + Chrono.dateOnlyStamp(urdd.expires);
        }
 
        public static UserRole get(String u, String r) {
                List<UserRole> lur = byUser.get(u);
                if(lur!=null) {
                        for(UserRole ur : lur) {
+
                                if(ur.urdd.role.equals(r)) {
                                        return ur;
                                }
@@ -232,23 +255,18 @@ public class UserRole implements Cloneable, CacheChange.Data  {
                }
                return null;
        }
-       
-       // CACHE Calling
-       private static final String logfmt = "%s UserRole - %s: %s-%s (%s, %s) expiring %s";
-       private static final String replayfmt = "%s|%s|%s|%s|%s\n";
-       private static final String deletefmt = "# %s\n"+replayfmt;
-       
+
        // SAFETY - DO NOT DELETE USER ROLES DIRECTLY FROM BATCH FILES!!!
        // We write to a file, and validate.  If the size is iffy, we email Support
        public void delayDelete(AuthzTrans trans, String text, boolean dryRun) {
                String dt = Chrono.dateTime(urdd.expires);
                if(dryRun) {
-                       trans.info().printf(logfmt,text,"Would Delete",urdd.user,urdd.role,urdd.ns,urdd.rname,dt);
+                       trans.info().printf(LOG_FMT,text,"Would Delete",urdd.user,urdd.role,urdd.ns,urdd.rname,dt);
                } else {
-                       trans.info().printf(logfmt,text,"Staged Deletion",urdd.user,urdd.role,urdd.ns,urdd.rname,dt);
+                       trans.info().printf(LOG_FMT,text,"Staged Deletion",urdd.user,urdd.role,urdd.ns,urdd.rname,dt);
                }
-               urDelete.printf(deletefmt,text,urdd.user,urdd.role,dt,urdd.ns,urdd.rname);
-               urRecover.printf(replayfmt,urdd.user,urdd.role,dt,urdd.ns,urdd.rname);
+               urDelete.printf(DELETE_FMT,text,urdd.user,urdd.role,dt,urdd.ns,urdd.rname);
+               urRecover.printf(REPLAY_FMT,urdd.user,urdd.role,dt,urdd.ns,urdd.rname);
 
                cache.delayedDelete(this);
                ++deleted;
@@ -278,5 +296,4 @@ public class UserRole implements Cloneable, CacheChange.Data  {
                cache.resetLocalData();
        }
 
-
 }
\ No newline at end of file