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=1c1f660cdcabe7039c43c5eac373a3952de77917;hb=a174f8ddbc5eb78a648fb68b33ef18cb64d81fda;hp=cb57497e80ec341f1920b1e12a68fcfcc14b49f2;hpb=218728980874ca82b07a10fceb26debd6045d2ea;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 cb57497e..1c1f660c 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 @@ -19,16 +19,15 @@ * */package org.onap.aaf.auth.batch.reports; + import java.io.BufferedReader; import java.io.File; -import java.io.FileInputStream; +import java.io.FileReader; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; -import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Set; import org.onap.aaf.auth.batch.Batch; @@ -42,144 +41,239 @@ import org.onap.aaf.cadi.CadiException; 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.util.Chrono; -public class Notify extends Batch { - private final Mailer mailer; - private final String mailFrom; - private final String header; - private final String footer; - private List notifyFile; - - public Notify(AuthzTrans trans) throws APIException, IOException, OrganizationException { - super(trans.env()); - String mailerCls = env.getProperty("MAILER"); - mailFrom = env.getProperty("MAIL_FROM"); - String header_html = env.getProperty("HEADER_HTML"); - String footer_html = env.getProperty("FOOTER_HTML"); - if(mailerCls==null || mailFrom==null || header_html==null || footer_html==null) { - throw new APIException("Notify requires MAILER, MAILER_FROM, HEADER_HTML and FOOTER_HTML properties"); - } - try { - Class mailc = Class.forName(mailerCls); - Constructor mailcst = mailc.getConstructor(Access.class); - mailer = (Mailer)mailcst.newInstance(env.access()); - } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { - throw new APIException("Unable to construct " + mailerCls,e); - } - - FileInputStream fis = new FileInputStream(header_html); - try { - byte[] content = new byte[(int)fis.getChannel().size()]; - fis.read(content); - header = new String(content); - } finally { - fis.close(); - } - - fis = new FileInputStream(footer_html); - try { - byte[] content = new byte[(int)fis.getChannel().size()]; - fis.read(content); - footer = new String(content); - } finally { - fis.close(); - } - - // Class Load possible data - NotifyBody.load(env.access()); - - // Create Intermediate Output - File logDir = new File(logDir()); - notifyFile = new ArrayList<>(); - if(args().length>0) { - for(int i=0;i toList = new ArrayList<>(); - List ccList = new ArrayList<>(); - AuthzTrans noAvg = trans.env().newTransNoAvg(); - String subject = "Test Notify"; - boolean urgent = false; - - - - final Notify notify = this; - final Holder> info = new Holder<>(null); - final Set errorSet = new HashSet<>(); - - try { - for(File f : notifyFile) { - CSV csv = new CSV(f); - try { - csv.visit(new CSV.Visitor() { - @Override - public void visit(List row) throws IOException, CadiException { - if("info".equals(row.get(0))) { - info.set(row); - } - if(info.get()==null) { - throw new CadiException("First line of Feed MUST contain 'info' record"); - } - String key = row.get(0)+'|'+info.get().get(1); - NotifyBody body = NotifyBody.get(key); - if(body==null) { - errorSet.add("No NotifyBody defined for " + key); - } else { - body.store(row); - } - } - }); - } catch (IOException | CadiException e) { - e.printStackTrace(); - } - - // now create Notification - for(NotifyBody nb : NotifyBody.getAll()) { - for(String id : nb.users()) { - toList.clear(); - ccList.clear(); - try { - String bodyS = nb.body(noAvg, notify, id); - Identity identity = trans.org().getIdentity(noAvg, id); - if(!identity.isPerson()) { - identity = identity.responsibleTo(); - } - for(int i=1;i mailc = Class.forName(mailerCls); + Constructor mailcst = mailc.getConstructor(Access.class); + mailer = (Mailer)mailcst.newInstance(env.access()); + } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + throw new APIException("Unable to construct " + mailerCls,e); + } + + String line; + StringBuilder sb = new StringBuilder(); + BufferedReader br = new BufferedReader(new FileReader(header_html)); + try { + while((line=br.readLine())!=null) { + sb.append(line); + sb.append('\n'); + } + String html_css = env.getProperty(HTML_CSS); + int hc = sb.indexOf(HTML_CSS); + if(hc!=0 && html_css!=null) { + header = sb.replace(hc,hc+HTML_CSS.length(), html_css).toString(); + } else { + header = sb.toString(); + } + } finally { + br.close(); + } + + // Establish index from header + int lastTag = header.lastIndexOf('<'); + if(lastTag>0) { + int prevCR = header.lastIndexOf('\n',lastTag); + if(prevCR>0) { + indent = lastTag-prevCR; + } else { + indent = 6; //arbitrary + } + } else { + indent = 6; + } + + urgent = false; + + sb.setLength(0); + br = new BufferedReader(new FileReader(footer_html)); + try { + while((line=br.readLine())!=null) { + sb.append(line); + sb.append('\n'); + } + footer = sb.toString(); + } finally { + br.close(); + } + + } + + /* + * Note: We try to put things related to Notify as Main Class in Run, where we might have put in + * Constructor, so that we can have other Classes call just the "notify" method. + */ + @Override + protected void run(AuthzTrans trans) { + AuthzTrans noAvg = trans.env().newTransNoAvg(); + + final Holder> info = new Holder<>(null); + final Set errorSet = new HashSet<>(); + + try { + // Class Load possible data + NotifyBody.load(env.access()); + + + // Create Intermediate Output + File logDir = logDir(); + Set notifyFile = new HashSet<>(); + if(args().length>0) { + for(int i=0;i row) throws IOException, CadiException { + if("info".equals(row.get(0))) { + info.set(row); + } + if(info.get()==null) { + throw new CadiException("First line of Feed MUST contain 'info' record"); + } String key = row.get(0)+'|'+info.get().get(1); + NotifyBody body = NotifyBody.get(key); + if(body==null) { + errorSet.add("No NotifyBody defined for " + key); + } else { + body.store(row); + } + } + }); + } catch (IOException | CadiException e) { + e.printStackTrace(); + } + + } + + // now create Notification + for(NotifyBody nb : NotifyBody.getAll()) { + notify(noAvg, nb); + } + + } catch (APIException | IOException e1) { + trans.error().log(e1); } finally { - for(String s : errorSet) { - trans.audit().log(s); - } - } - } - + for(String s : errorSet) { + trans.audit().log(s); + } + } + } + + public int notify(AuthzTrans trans, NotifyBody nb) { + List toList = new ArrayList<>(); + List ccList = 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(); + try { + Identity identity = trans.org().getIdentity(trans, id); + if(identity==null) { + 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()); + } + } + } + } + + StringBuilder content = new StringBuilder(); + content.append(String.format(header,version,Identity.mixedCase(identity.firstName()))); + + 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; + } + } + } + } 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); + } + return nb.count(); + } + @Override - protected void _close(AuthzTrans trans) { - } + protected void _close(AuthzTrans trans) { + } -} + }