X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=auth%2Fauth-batch%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Faaf%2Fauth%2Fbatch%2Freports%2FNotify.java;h=e6ce08498a9516f85e9f040123009200a6e7f25b;hb=d86e3224e6a5af2bd2b713f93bea5e6677d3ebc2;hp=1c1f660cdcabe7039c43c5eac373a3952de77917;hpb=a50007dcded86acc2dcd610810f8afac720a058a;p=aaf%2Fauthz.git diff --git a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/reports/Notify.java b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/reports/Notify.java index 1c1f660c..e6ce0849 100644 --- a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/reports/Notify.java +++ b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/reports/Notify.java @@ -25,22 +25,36 @@ import java.io.FileReader; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; +import java.text.ParseException; import java.util.ArrayList; +import java.util.Date; +import java.util.GregorianCalendar; import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import java.util.Set; +import java.util.TreeMap; import org.onap.aaf.auth.batch.Batch; +import org.onap.aaf.auth.batch.approvalsets.Pending; +import org.onap.aaf.auth.batch.helpers.CQLBatch; +import org.onap.aaf.auth.batch.helpers.CQLBatchLoop; +import org.onap.aaf.auth.batch.helpers.LastNotified; import org.onap.aaf.auth.batch.reports.bodies.NotifyBody; +import org.onap.aaf.auth.batch.reports.bodies.NotifyPendingApprBody; import org.onap.aaf.auth.env.AuthzTrans; import org.onap.aaf.auth.org.Mailer; import org.onap.aaf.auth.org.Organization.Identity; import org.onap.aaf.auth.org.OrganizationException; import org.onap.aaf.cadi.Access; import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.PropAccess; import org.onap.aaf.cadi.client.Holder; import org.onap.aaf.cadi.util.CSV; import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.misc.env.TimeTaken; +import org.onap.aaf.misc.env.Trans; import org.onap.aaf.misc.env.util.Chrono; public class Notify extends Batch { @@ -52,9 +66,17 @@ import org.onap.aaf.misc.env.util.Chrono; private final int indent; private final boolean urgent; public final String guiURL; + private PropAccess access; + private AuthzTrans noAvg; + private CQLBatch cqlBatch; + private LastNotified lastN; + private CQLBatchLoop cbl; public Notify(AuthzTrans trans) throws APIException, IOException, OrganizationException { super(trans.env()); + access = env.access(); + session = cluster.connect(); + String mailerCls = env.getProperty("MAILER"); String mailFrom = env.getProperty("MAIL_FROM"); String header_html = env.getProperty("HEADER_HTML"); @@ -75,19 +97,25 @@ import org.onap.aaf.misc.env.util.Chrono; String line; StringBuilder sb = new StringBuilder(); - BufferedReader br = new BufferedReader(new FileReader(header_html)); + File fhh = new File(header_html); + if(!fhh.exists()) { + throw new APIException(header_html + " does not exist"); + } + BufferedReader br = new BufferedReader(new FileReader(fhh)); try { while((line=br.readLine())!=null) { sb.append(line); sb.append('\n'); } String html_css = env.getProperty(HTML_CSS); + String temp; int hc = sb.indexOf(HTML_CSS); if(hc!=0 && html_css!=null) { - header = sb.replace(hc,hc+HTML_CSS.length(), html_css).toString(); + temp = sb.replace(hc,hc+HTML_CSS.length(), html_css).toString(); } else { - header = sb.toString(); + temp = sb.toString(); } + header = temp.replace("AAF:ENV", batchEnv); } finally { br.close(); } @@ -108,7 +136,12 @@ import org.onap.aaf.misc.env.util.Chrono; urgent = false; sb.setLength(0); - br = new BufferedReader(new FileReader(footer_html)); + fhh = new File(footer_html); + if(!fhh.exists()) { + throw new APIException(footer_html + " does not exist"); + } + + br = new BufferedReader(new FileReader(fhh)); try { while((line=br.readLine())!=null) { sb.append(line); @@ -119,6 +152,11 @@ import org.onap.aaf.misc.env.util.Chrono; br.close(); } + noAvg = trans.env().newTransNoAvg(); + cqlBatch = new CQLBatch(noAvg.debug(),session); + cbl = new CQLBatchLoop(cqlBatch,50,dryRun); + + lastN = new LastNotified(session); } /* @@ -127,10 +165,10 @@ import org.onap.aaf.misc.env.util.Chrono; */ @Override protected void run(AuthzTrans trans) { - AuthzTrans noAvg = trans.env().newTransNoAvg(); final Holder> info = new Holder<>(null); final Set errorSet = new HashSet<>(); + String fmt = "%s"+Chrono.dateOnlyStamp()+".csv"; try { // Class Load possible data @@ -145,7 +183,6 @@ import org.onap.aaf.misc.env.util.Chrono; notifyFile.add(new File(logDir, args()[i])); } } else { - String fmt = "%s"+Chrono.dateOnlyStamp()+".csv"; File file; for(NotifyBody nb : NotifyBody.getAll()) { file = new File(logDir,String.format(fmt, nb.name())); @@ -186,8 +223,105 @@ import org.onap.aaf.misc.env.util.Chrono; // now create Notification for(NotifyBody nb : NotifyBody.getAll()) { - notify(noAvg, nb); + int count = notify(noAvg, nb); + trans.info().printf("Emailed %d for %s",count,nb.name()); } + + // + // Do Pending Approval Notifies. We do this separately, because we are consolidating + // all the new entries, etc. + // + List csvList = new ArrayList<>(); + for(String s : new String[] {"Approvals","ApprovalsNew"}) { + File f = new File(logDir(),String.format(fmt, s)); + if(f.exists()) { + csvList.add(new CSV(access,f)); + } + } + + Map mpending = new TreeMap<>(); + Holder count = new Holder<>(0); + for(CSV approveCSV : csvList) { + TimeTaken tt = trans.start("Load Analyzed Reminders",Trans.SUB,approveCSV.name()); + try { + approveCSV.visit(row -> { + switch(row.get(0)) { +// case "info": +// break; + case Pending.REMIND: + try { + String user = row.get(1); + Pending p = new Pending(row); + Pending mp = mpending.get(user); + if(mp==null) { + mpending.put(user, p); + } else { + mp.inc(p); // FYI, unlikely + } + count.set(count.get()+1); + } catch (ParseException e) { + trans.error().log(e); + } + break; + } + }); + } catch (IOException | CadiException e) { + trans.error().log(e); + } finally { + tt.done(); + } + } + trans.info().printf("Read %d Reminder Rows", count.get()); + + NotifyPendingApprBody npab = new NotifyPendingApprBody(access); + + GregorianCalendar gc = new GregorianCalendar(); + gc.add(GregorianCalendar.DAY_OF_MONTH, 7); // Get from INFO? + Date oneWeek = gc.getTime(); + CSV.Saver rs = new CSV.Saver(); + + TimeTaken tt = trans.start("Obtain Last Notifications for Approvers", Trans.SUB); + try { + lastN.add(mpending.keySet()); + } finally { + tt.done(); + } + + Pending p; + + tt = trans.start("Notify for Pending", Trans.SUB); + List idList = new ArrayList(); + String id; + try { + for(Entry es : mpending.entrySet()) { + id = es.getKey(); + idList.clear(); + idList.add(id); + p = es.getValue(); + boolean nap = p.newApprovals(); + if(!nap) { + Date dateLastNotified = lastN.lastNotified(id,"pending",""); + if(dateLastNotified==null || dateLastNotified.after(oneWeek) ) { + nap=true; + } + } + if(nap) { + rs.row("appr", id,p.qty(),batchEnv); + npab.store(rs.asList()); + if(notify(noAvg, npab)>0) { + // Update + cbl.preLoop(); +// lastN.update(cbl.inc(),key,"pending",""); + npab.record(trans,cbl.inc(), id, idList, lastN); + npab.inc(); + } + } + } + } finally { + cbl.flush(); + tt.done(); + trans.info().printf("Notified %d persons of Pending Approvals", npab.count()); + } } catch (APIException | IOException e1) { trans.error().log(e1); @@ -198,82 +332,65 @@ import org.onap.aaf.misc.env.util.Chrono; } } - public int notify(AuthzTrans trans, NotifyBody nb) { + private int notify(AuthzTrans trans, NotifyBody nb) { List toList = new ArrayList<>(); List ccList = new ArrayList<>(); + List idList = new ArrayList<>(); String run = nb.type()+nb.name(); String test = dryRun?run:null; - String last = null; ONE_EMAIL: for(String id : nb.users()) { - last = id; toList.clear(); ccList.clear(); + idList.clear(); try { - Identity identity = trans.org().getIdentity(trans, id); - if(identity==null) { + List identities = trans.org().getIDs(trans, id, nb.escalation()); + if(identities.isEmpty()) { trans.warn().printf("%s is invalid for this Organization. Skipping notification.",id); } else { - if(!identity.isPerson()) { - identity = identity.responsibleTo(); - } - if(identity==null) { - trans.warn().printf("Responsible Identity %s is invalid for this Organization. Skipping notification.",id); - } else { - for(int i=1;i<=nb.escalation();++i) { - if(identity != null) { - if(i==1) { // self and Delegates - toList.add(identity.email()); - List dels = identity.delegate(); - if(dels!=null) { - for(String d : dels) { - toList.add(d); - } - } - } else { - Identity s = identity.responsibleTo(); - if(s==null) { - trans.error().printf("Identity %s has no %s", identity.fullID(), - identity.isPerson()?"supervisor":"sponsor"); - } else { - ccList.add(s.email()); - } - } - } + Identity identity = null; + for(Identity ident : identities) { + if(identity==null) { + identity = ident; + toList.add(ident.email()); + } else { + ccList.add(ident.email()); } + idList.add(ident.fullID()); + } + StringBuilder content = new StringBuilder(); + content.append(String.format(header,version,Identity.mixedCase(identity.firstName()))); - StringBuilder content = new StringBuilder(); - content.append(String.format(header,version,Identity.mixedCase(identity.firstName()))); - - nb.body(trans, content, indent, this, id); - content.append(footer); + nb.body(trans, content, indent, this, id); + content.append(footer); - if(mailer.sendEmail(trans, test, toList, ccList, nb.subject(),content.toString(), urgent)) { - nb.inc(); - } else { - trans.error().log("Mailer failed to send Mail"); - } - if(maxEmails>0 && nb.count()>=maxEmails) { - break ONE_EMAIL; - } + if(mailer.sendEmail(trans, test, toList, ccList, nb.subject(),content.toString(), urgent)) { + cbl.preLoop(); + nb.record(trans,cbl.inc(), id, idList, lastN); + nb.inc(); + } else { + trans.error().log("Mailer failed to send Mail"); + } + if(maxEmails>0 && nb.count()>=maxEmails) { + break ONE_EMAIL; } } } catch (OrganizationException e) { trans.error().log(e); } } - if(nb.count()<=1) { - trans.info().printf("Notified %s for %s",last,run); - } else { - trans.info().printf("Emailed %d for %s",nb.count(),run); - } + cbl.flush(); return nb.count(); } + /* (non-Javadoc) + * @see org.onap.aaf.auth.batch.Batch#_close(org.onap.aaf.auth.env.AuthzTrans) + */ @Override - protected void _close(AuthzTrans trans) { - } + protected void _close(AuthzTrans trans) { + cbl.flush(); + } }