X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=auth%2Fauth-batch%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Faaf%2Fauth%2Fbatch%2FBatch.java;h=36a88b648e9820a17e80050894e09c8d641f5ce8;hb=e3163b2d5609753f874e2f52fd5ef67fa063af7a;hp=7920e48149c122bc12a9948b43019613a6a034c9;hpb=6a37b8869b1f79eaf1ccb9e6cf44accf40e99f6b;p=aaf%2Fauthz.git diff --git a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/Batch.java b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/Batch.java index 7920e481..36a88b64 100644 --- a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/Batch.java +++ b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/Batch.java @@ -34,22 +34,20 @@ import java.net.UnknownHostException; import java.nio.ByteBuffer; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.GregorianCalendar; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.TimeZone; +import org.apache.log4j.Logger; import org.onap.aaf.auth.common.Define; import org.onap.aaf.auth.dao.CassAccess; -import org.onap.aaf.auth.dao.cass.RoleDAO; -import org.onap.aaf.auth.dao.cass.UserRoleDAO; -import org.onap.aaf.auth.dao.hl.Question; import org.onap.aaf.auth.env.AuthzEnv; import org.onap.aaf.auth.env.AuthzTrans; -import org.onap.aaf.auth.layer.Result; +import org.onap.aaf.auth.log4j.Log4JAccessAppender; import org.onap.aaf.auth.org.Organization; -import org.onap.aaf.auth.org.Organization.Identity; import org.onap.aaf.auth.org.OrganizationException; import org.onap.aaf.auth.org.OrganizationFactory; import org.onap.aaf.cadi.Access.Level; @@ -59,6 +57,9 @@ import org.onap.aaf.misc.env.APIException; import org.onap.aaf.misc.env.Env; import org.onap.aaf.misc.env.StaticSlot; import org.onap.aaf.misc.env.TimeTaken; +import org.onap.aaf.misc.env.util.Chrono; +import org.onap.aaf.misc.env.util.Split; +import org.onap.aaf.misc.env.util.StringBuilderOutputStream; import com.datastax.driver.core.Cluster; import com.datastax.driver.core.ResultSet; @@ -68,13 +69,11 @@ import com.datastax.driver.core.Statement; public abstract class Batch { - private static String rootNs; - private static StaticSlot ssargs; protected static final String STARS = "*****"; - protected final Cluster cluster; + protected static Cluster cluster; protected static AuthzEnv env; protected static Session session; protected static Set specialNames; @@ -82,24 +81,21 @@ public abstract class Batch { protected static boolean dryRun; protected static String batchEnv; + private static File logdir; + public static final String CASS_ENV = "CASS_ENV"; public static final String LOG_DIR = "LOG_DIR"; - protected static final String PUNT="punt"; protected static final String MAX_EMAILS="MAX_EMAILS"; protected static final String VERSION="VERSION"; public static final String GUI_URL="GUI_URL"; protected final Organization org; - - - + protected String version; + protected static final Date now = new Date(); + protected static final Date never = new Date(0); + protected Batch(AuthzEnv env) throws APIException, IOException, OrganizationException { - // Be able to change Environments - // load extra properties, i.e. - // PERF.cassandra.clusters=.... - batchEnv = env.getProperty(CASS_ENV); if (batchEnv != null) { - batchEnv = batchEnv.trim(); env.info().log("Redirecting to ",batchEnv,"environment"); String str; for (String key : new String[]{ @@ -107,9 +103,10 @@ public abstract class Batch { CassAccess.CASSANDRA_CLUSTERS_PORT, CassAccess.CASSANDRA_CLUSTERS_USER_NAME, CassAccess.CASSANDRA_CLUSTERS_PASSWORD, - VERSION,GUI_URL,PUNT,MAX_EMAILS, + VERSION,GUI_URL,MAX_EMAILS, LOG_DIR, - "SPECIAL_NAMES" + "SPECIAL_NAMES", + "MAIL_TEST_TO" }) { if ((str = env.getProperty(batchEnv+'.'+key))!=null) { env.setProperty(key, str); @@ -118,7 +115,9 @@ public abstract class Batch { } // Setup for Dry Run - cluster = CassAccess.cluster(env,batchEnv); + if(cluster==null) { + cluster = CassAccess.cluster(env,batchEnv); + } env.info().log("cluster name - ",cluster.getClusterName()); String dryRunStr = env.getProperty( "DRY_RUN" ); if ( dryRunStr == null || "false".equals(dryRunStr.trim()) ) { @@ -129,6 +128,9 @@ public abstract class Batch { } org = OrganizationFactory.init(env); + if(org==null) { + throw new OrganizationException("Organization MUST be defined for Batch"); + } org.setTestMode(dryRun); // Special names to allow behaviors beyond normal rules @@ -147,10 +149,12 @@ public abstract class Batch { } } } + + version = env.getProperty(VERSION,Config.AAF_DEFAULT_API_VERSION); } protected abstract void run(AuthzTrans trans); - protected abstract void _close(AuthzTrans trans); + protected void _close(AuthzTrans trans) {} public String[] args() { return env.get(ssargs); @@ -333,45 +337,22 @@ public abstract class Batch { } } - // IMPORTANT! VALIDATE Organization isUser method - protected void checkOrganizationAcccess(AuthzTrans trans, Question q) throws APIException, OrganizationException { - Set testUsers = new HashSet<>(); - Result> rrd = q.roleDAO.readNS(trans, rootNs); - if (rrd.isOK()) { - for (RoleDAO.Data r : rrd.value) { - Result> rur = q.userRoleDAO.readByRole(trans, r.fullName()); - if (!rur.isOK()) { - continue; - } - for (UserRoleDAO.Data udd : rur.value) { - testUsers.add(udd.user); - } - } - if (testUsers.size() < 2) { - throw new APIException("Not enough Users in Roles for " + rootNs + " to Validate"); - } - - Identity iden; - for (String user : testUsers) { - if ((iden = org.getIdentity(trans, user)) == null) { - throw new APIException("Failed Organization Entity Validation Check: " + user); - } else { - trans.info().log("Organization Validation Check: " + iden.id()); - } - } - } - } - - protected static String logDir() { - String ld = env.getProperty(LOG_DIR); - if (ld==null) { - if (batchEnv==null) { // Deployed Batch doesn't use different ENVs, and a common logdir - ld = "logs/"; - } else { - ld = "logs/"+batchEnv; - } - } - return ld; + protected static File logDir() { + if(logdir == null) { + String ld = env.getProperty(LOG_DIR); + if (ld==null) { + if (batchEnv==null) { // Deployed Batch doesn't use different ENVs, and a common logdir + ld = "logs/"; + } else { + ld = "logs/"+batchEnv; + } + } + logdir = new File(ld); + if(!logdir.exists()) { + logdir.mkdirs(); + } + } + return logdir; } protected int count(String str, char c) { if (str==null || str.isEmpty()) { @@ -387,17 +368,31 @@ public abstract class Batch { public final void close(AuthzTrans trans) { _close(trans); - cluster.close(); + if(session!=null) { + session.close(); + session = null; + } + if(cluster!=null && !cluster.isClosed()) { + cluster.close(); + } } public static void main(String[] args) { - PropAccess access = new PropAccess(args); + // Use a StringBuilder to save off logs until a File can be setup + StringBuilderOutputStream sbos = new StringBuilderOutputStream(); + PropAccess access = new PropAccess(new PrintStream(sbos),args); + access.log(Level.INIT, "------- Starting Batch ------\n Args: "); + for(String s: args) { + sbos.getBuffer().append(s); + sbos.getBuffer().append(' '); + } + InputStream is = null; String filename; String propLoc; try { Define.set(access); - rootNs =Define.ROOT_NS(); + if(access.getProperty(Config.CADI_PROP_FILES)==null) { File f = new File("authBatch.props"); try { @@ -427,101 +422,130 @@ public abstract class Batch { access.log(Level.INFO,"Configuring from", propLoc); } - env = new AuthzEnv(access); + env = new AuthzEnv(access); + transferVMProps(env, CASS_ENV, "DRY_RUN", "NS", "Organization"); - // Flow all Env Logs to Log4j, with ENV - -// LogFileNamer lfn; -// lfn = new LogFileNamer(logDir(),"").noPID(); -// lfn.setAppender("authz-batch"); -// lfn.setAppender("aspr|ASPR"); -// lfn.setAppender("sync"); -// lfn.setAppender("jobchange"); -// lfn.setAppender("validateuser"); -// aspr = Logger.getLogger("aspr"); -// Log4JLogTarget.setLog4JEnv("authz-batch", env); -// propLoc = null; - - Batch batch = null; - // setup ATTUser and Organization Slots before starting this: - // TODO redo this - // env.slot(ATT.ATT_USERSLOT); - // - // OrganizationFactory.setDefaultOrg(env, ATT.class.getName()); - AuthzTrans trans = env.newTrans(); - - TimeTaken tt = trans.start("Total Run", Env.SUB); - try { - int len = args.length; - if (len > 0) { - String toolName = args[0]; - len -= 1; - if (len < 0) - len = 0; - String nargs[] = new String[len]; - if (len > 0) { - System.arraycopy(args, 1, nargs, 0, len); - } - - env.put(ssargs = env.staticSlot("ARGS"), nargs); - - /* - * Add New Batch Programs (inherit from Batch) here - */ - - // Might be a Report, Update or Temp Batch - Class cls; - String classifier = ""; - try { - cls = ClassLoader.getSystemClassLoader().loadClass("org.onap.aaf.auth.batch.update." + toolName); - classifier = "Update:"; - } catch (ClassNotFoundException e) { - try { - cls = ClassLoader.getSystemClassLoader().loadClass("org.onap.aaf.auth.batch.reports." + toolName); - classifier = "Report:"; - } catch (ClassNotFoundException e2) { - try { - cls = ClassLoader.getSystemClassLoader() - .loadClass("org.onap.aaf.auth.batch.temp." + toolName); - classifier = "Temp Utility:"; - } catch (ClassNotFoundException e3) { - cls = null; - } - } - } - if (cls != null) { - Constructor cnst = cls.getConstructor(AuthzTrans.class); - batch = (Batch) cnst.newInstance(trans); - env.info().log("Begin", classifier, toolName); - } - - - if (batch == null) { - trans.error().log("No Batch named", toolName, "found"); - } - /* - * End New Batch Programs (inherit from Batch) here - */ + // Be able to change Environments + // load extra properties, i.e. + // PERF.cassandra.clusters=.... + batchEnv = env.getProperty(CASS_ENV); + if(batchEnv!=null) { + batchEnv = batchEnv.trim(); + } - } - if (batch != null) { - batch.run(trans); - } + File logFile = new File(logDir() + "/batch" + Chrono.dateOnlyStamp(new Date()) + ".log" ); + PrintStream batchLog = new PrintStream(new FileOutputStream(logFile,true)); + try { + access.setStreamLogIt(batchLog); + sbos.flush(); + batchLog.print(sbos.getBuffer()); + sbos = null; + Logger.getRootLogger().addAppender(new Log4JAccessAppender(access)); + + Batch batch = null; + AuthzTrans trans = env.newTrans(); + + TimeTaken tt = trans.start("Total Run", Env.SUB); + try { + int len = args.length; + if (len > 0) { + String toolName = args[0]; + len -= 1; + if (len < 0) + len = 0; + String nargs[] = new String[len]; + if (len > 0) { + System.arraycopy(args, 1, nargs, 0, len); + } + + env.put(ssargs = env.staticSlot("ARGS"), nargs); + + /* + * Add New Batch Programs (inherit from Batch) here + */ + + // Might be a Report, Update or Temp Batch + Class cls = null; + String classifier = ""; + + String[] pkgs = new String[] { + "org.onap.aaf.auth.batch.update", + "org.onap.aaf.auth.batch.reports", + "org.onap.aaf.auth.batch.temp" + }; + + String ebp = env.getProperty("EXTRA_BATCH_PKGS"); + if(ebp!=null) { + String[] ebps = Split.splitTrim(':', ebp); + String[] temp = new String[ebps.length + pkgs.length]; + System.arraycopy(pkgs,0, temp, 0, pkgs.length); + System.arraycopy(ebps,0,temp,pkgs.length,ebps.length); + pkgs = temp; + } + + for(String p : pkgs) { + try { + cls = ClassLoader.getSystemClassLoader().loadClass(p + '.' + toolName); + int lastDot = p.lastIndexOf('.'); + if(p.length()>0 || p.length()!=lastDot) { + StringBuilder sb = new StringBuilder(); + sb.append(Character.toUpperCase(p.charAt(++lastDot))); + while(++lastDot cnst = cls.getConstructor(AuthzTrans.class); + batch = (Batch) cnst.newInstance(trans); + env.info().log("Begin", classifier, toolName); + } + + + if (batch == null) { + trans.error().log("No Batch named", toolName, "found"); + } + /* + * End New Batch Programs (inherit from Batch) here + */ + + } + if (batch != null) { + try { + batch.run(trans); + } catch (Exception e) { + if(cluster!=null && !cluster.isClosed()) { + cluster.close(); + } + trans.error().log(e); + } + } + } finally { + tt.done(); + if (batch != null) { + batch.close(trans); + } + StringBuilder sb = new StringBuilder("Task Times\n"); + trans.auditTrail(4, sb, AuthzTrans.SUB, AuthzTrans.REMOTE); + trans.info().log(sb); + } } finally { - tt.done(); - if (batch != null) { - batch.close(trans); - } - StringBuilder sb = new StringBuilder("Task Times\n"); - trans.auditTrail(4, sb, AuthzTrans.SUB, AuthzTrans.REMOTE); - trans.info().log(sb); + batchLog.close(); } + } catch (Exception e) { + if(cluster!=null && !cluster.isClosed()) { + cluster.close(); + } e.printStackTrace(System.err); - // Exceptions thrown by DB aren't stopping the whole process. - System.exit(1); } }