From deb396a3d53a84b2eb5a46d40122b4902d72c601 Mon Sep 17 00:00:00 2001 From: Instrumental Date: Wed, 11 Dec 2019 15:04:10 -0600 Subject: Add a MassMail Batch Program Had to move Holder cover for change in WS/Bind to compile Issue-ID: AAF-1059 Change-Id: I754515a6e65bd0665fa3e03dabd8f5a144241f6e Signed-off-by: Instrumental --- .../org/onap/aaf/auth/batch/helpers/UserRole.java | 2 +- .../org/onap/aaf/auth/batch/reports/Analyze.java | 10 +- .../org/onap/aaf/auth/batch/reports/MassMail.java | 301 +++++++++++++++++++++ .../org/onap/aaf/auth/batch/reports/Notify.java | 2 +- .../org/onap/aaf/auth/batch/update/Approvals.java | 2 +- .../org/onap/aaf/auth/batch/update/Extend.java | 2 +- .../org/onap/aaf/auth/batch/update/Remove.java | 2 +- 7 files changed, 315 insertions(+), 6 deletions(-) create mode 100644 auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/reports/MassMail.java (limited to 'auth/auth-batch/src/main') diff --git a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/UserRole.java b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/UserRole.java index ae46253c..536515bc 100644 --- a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/UserRole.java +++ b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/UserRole.java @@ -135,7 +135,7 @@ public class UserRole implements Cloneable, CacheChange.Data { ); } - private static void load(Trans trans, Session session, Creator creator, String where, Visitor visitor) { + public static void load(Trans trans, Session session, Creator creator, String where, Visitor visitor) { String query = creator.query(where); trans.debug().log( "query: " + query ); TimeTaken tt = trans.start("Read UserRoles", Env.REMOTE); diff --git a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/reports/Analyze.java b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/reports/Analyze.java index 78c6ae3a..5cab5297 100644 --- a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/reports/Analyze.java +++ b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/reports/Analyze.java @@ -69,7 +69,7 @@ import org.onap.aaf.misc.env.util.Chrono; public class Analyze extends Batch { - private static final int unknown=0; + private static final int unknown=0; private static final int owner=1; private static final int supervisor=2; private static final int total=0; @@ -82,6 +82,7 @@ public class Analyze extends Batch { private static final String EXPIRED_OWNERS = "ExpiredOwners"; private static final String CSV = ".csv"; private static final String INFO = "info"; + private static final String NOT_COMPLIANT = "NotCompliant"; private int minOwners; private Map writerList; private ExpireRange expireRange; @@ -89,6 +90,7 @@ public class Analyze extends Batch { private CSV.Writer deleteCW; private CSV.Writer needApproveCW; private CSV.Writer extendCW; + private CSV.Writer notCompliantCW; private Range futureRange; private final String sdate; private LastNotified ln; @@ -146,6 +148,12 @@ public class Analyze extends Batch { extendCW.row(INFO,EXTEND,sdate,1); writerList.put(EXTEND,extendCW); + // Setup NotCompliant Writer for Apps + file = new File(logDir(),NOT_COMPLIANT + sdate + CSV); + CSV ncCSV = new CSV(env.access(),file); + notCompliantCW = ncCSV.writer(); + writerList.put(NOT_COMPLIANT, notCompliantCW); + // Load full data of the following ln = new LastNotified(session); diff --git a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/reports/MassMail.java b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/reports/MassMail.java new file mode 100644 index 00000000..272a41ba --- /dev/null +++ b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/reports/MassMail.java @@ -0,0 +1,301 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 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. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package org.onap.aaf.auth.batch.reports; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.PrintStream; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +import org.onap.aaf.auth.batch.Batch; +import org.onap.aaf.auth.batch.helpers.NS; +import org.onap.aaf.auth.batch.helpers.UserRole; +import org.onap.aaf.auth.env.AuthzTrans; +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.cadi.util.CSV; +import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.misc.env.Env; +import org.onap.aaf.misc.env.TimeTaken; + +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.SimpleStatement; +import com.datastax.driver.core.Statement; + +public class MassMail extends Batch { + + private Map writerList; + private final String from; + private String subject = "AAF Information"; + private boolean bOwners = true; + private boolean bAdmins = false; + private boolean bAppIDOnly = false; + private int escalate = 0; + private final File subdir; + private final String head; + private final String content; + private final String tail; + + public MassMail(AuthzTrans trans) throws APIException, IOException, OrganizationException { + super(trans.env()); + from = trans.getProperty("MAIL_FROM"); + if (from == null) { + throw new APIException("No MAIL_FROM property set"); + } + String subdirName = null; + // Flags + for (String arg : args()) { + switch (arg) { + case "-owners": + bOwners = true; + break; + case "-admins": + bAdmins = true; + break; + case "-appid_only": + bAppIDOnly = true; + break; + default: + if (arg.length() > 10 && arg.startsWith("-escalate=")) { + escalate = Integer.parseInt(arg.substring(10)); + } else if (subdirName == null) { + subdirName = arg; + } + } + } + + subdir = new File(logDir(), subdirName == null ? "mailing" : subdirName); + if (subdir.exists()) { + if (!subdir.isDirectory()) { + throw new APIException(subdirName + " is not a directory"); + } + } else { + subdir.mkdirs(); + } + + subject = readIn(subdir, "subject"); + head = readIn(subdir, "html.head"); + content = readIn(subdir,"html.content"); + tail = readIn(subdir,"html.tail"); + + trans.info().log("Starting Connection Process"); + TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB); + try { + TimeTaken tt = trans.start("Connect to Cluster", Env.REMOTE); + try { + session = cluster.connect(); + } finally { + tt.done(); + } + + // Create Intermediate Output + writerList = new HashMap<>(); + + NS.load(trans, session, NS.v2_0_11); + UserRole.load(trans, session, UserRole.v2_0_11); +// now = new Date(); +// String sdate = Chrono.dateOnlyStamp(now); + } finally { + tt0.done(); + } + } + + private String readIn(File subdir, String name) throws IOException { + File file = new File(subdir, name); + byte[] bytes = new byte[(int) file.length()]; + FileInputStream fis = new FileInputStream(file); + try { + fis.read(bytes); + return new String(bytes); + } finally { + fis.close(); + } + } + + @Override + protected void run(AuthzTrans trans) { +// try { + trans.info().log("Create a Mass Mailing"); + + final AuthzTrans transNoAvg = trans.env().newTransNoAvg(); + final Organization org = trans.org(); + if (org != null) { + StringBuilder to = new StringBuilder(); + StringBuilder cc = new StringBuilder(); + StringBuilder greet = new StringBuilder(); + for (NS ns : NS.data.values()) { + if (bAppIDOnly) { + ResultSet results; + Statement stmt = new SimpleStatement( + String.format("SELECT count(*) FROM authz.cred WHERE ns='%s';", ns.ndd.name)); + results = session.execute(stmt); + long count = results.one().getLong(0); + if (count <= 0) { + continue; + } + } + + to.setLength(0); + cc.setLength(0); + greet.setLength(0); + if (bOwners) { + StringBuilder o = to; + List owners = UserRole.getByRole().get(ns.ndd.name + ".owner"); + if (owners.isEmpty()) { + trans.error().log(ns.ndd.name, "has no owners!"); + } else { + for (UserRole owner : owners) { + try { + Identity identity = org.getIdentity(transNoAvg, owner.user()); + if (identity.isPerson()) { + if (o.length() > 0) { + o.append(','); + greet.append(','); + } + o.append(identity.email()); + greet.append(identity.firstName()); + for (int i = 0; i < escalate; ++i) { + identity = identity.responsibleTo(); + if (identity == null) { + break; + } + if (cc.length() > 0) { + cc.append(','); + } + cc.append(identity.email()); + } + } + } catch (OrganizationException e) { + trans.error().log(e, "Error Reading Organization"); + } + } + } + } + + if (bAdmins) { + StringBuilder a; + if (bOwners) { + a = cc; + } else { + a = to; + } + List admins = UserRole.getByRole().get(ns.ndd.name + ".admin"); + if (admins.isEmpty()) { + trans.warn().log(ns.ndd.name, "has no admins!"); + } else { + for (UserRole admin : admins) { + try { + Identity identity = org.getIdentity(transNoAvg, admin.user()); + if (identity.isPerson()) { + if (a.length() > 0) { + a.append(','); + } + a.append(identity.email()); + if (!bOwners) { + if (greet.length() > 0) { + greet.append(','); + } + greet.append(identity.firstName()); + } + } + } catch (OrganizationException e) { + trans.error().log(e, "Error Reading Organization"); + } + } + } + } + + try { + PrintStream ps = new PrintStream(new FileOutputStream( + subdir.getPath() + File.separatorChar + "email_" + ns.ndd.name + ".hdr")); + try { + ps.print("TO: "); + ps.println(to); + ps.print("CC: "); + ps.println(cc); + ps.print("FROM: "); + ps.println(from); + ps.print("SUBJECT: "); + ps.println(subject); + } finally { + ps.close(); + } + + ps = new PrintStream(new FileOutputStream( + subdir.getPath() + File.separatorChar + "email_" + ns.ndd.name + ".html")); + try { + ps.println(head.replaceAll("%FIRST_NAMES%", greet.toString())); + ps.println(content.replaceAll("%NS%", ns.ndd.name)); + ps.println(tail); + } finally { + ps.close(); + } + } catch (IOException e) { + trans.error().log(e); + } + + } +// UserRole.load(transNoAvg, session, UserRole.v2_0_11, ur -> { +// if("owner".equals(ur.rname())) { +// try { +// Identity identity = org.getIdentity(transNoAvg, ur.user()); +// if(identity.isPerson()) { +// System.out.println("Emailing " + identity.email()); +// } +// } catch (OrganizationException e) { +// trans.error().log(e, "Error Reading Organization"); +// } +// } +// Organization org = trans.org(); +// Identity identity = org.getIdentity(trans, ur.row); +// +// if(!check(transNoAvg, checked, ur.user())) { +// ur.row(whichWriter(transNoAvg,ur.user()),UserRole.UR); +// } +// } catch (OrganizationException e) { +// trans.error().log(e, "Error Decrypting X509"); +// } +// }); + } + +// } catch (OrganizationException e) { +// trans.info().log(e); + + } + + @Override + protected void _close(AuthzTrans trans) { + session.close(); + for (CSV.Writer cw : writerList.values()) { + cw.close(); + } + } + +} 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 981f300d..18c8ed6a 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 @@ -50,7 +50,7 @@ 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.Holder; import org.onap.aaf.cadi.util.CSV; import org.onap.aaf.misc.env.APIException; import org.onap.aaf.misc.env.TimeTaken; diff --git a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/update/Approvals.java b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/update/Approvals.java index c63a8a9f..12f0068b 100644 --- a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/update/Approvals.java +++ b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/update/Approvals.java @@ -46,7 +46,7 @@ import org.onap.aaf.auth.env.AuthzTrans; import org.onap.aaf.auth.layer.Result; import org.onap.aaf.auth.org.OrganizationException; import org.onap.aaf.cadi.CadiException; -import org.onap.aaf.cadi.client.Holder; +import org.onap.aaf.cadi.util.Holder; import org.onap.aaf.cadi.util.CSV; import org.onap.aaf.cadi.util.CSV.Writer; import org.onap.aaf.misc.env.APIException; diff --git a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/update/Extend.java b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/update/Extend.java index 86381008..bb09c6c7 100644 --- a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/update/Extend.java +++ b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/update/Extend.java @@ -42,7 +42,7 @@ import org.onap.aaf.auth.env.AuthzTrans; import org.onap.aaf.auth.layer.Result; import org.onap.aaf.auth.org.OrganizationException; import org.onap.aaf.cadi.CadiException; -import org.onap.aaf.cadi.client.Holder; +import org.onap.aaf.cadi.util.Holder; import org.onap.aaf.cadi.util.CSV; import org.onap.aaf.misc.env.APIException; import org.onap.aaf.misc.env.Env; diff --git a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/update/Remove.java b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/update/Remove.java index 7c516b10..e41b61cc 100644 --- a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/update/Remove.java +++ b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/update/Remove.java @@ -47,7 +47,7 @@ import org.onap.aaf.auth.dao.cass.UserRoleDAO; import org.onap.aaf.auth.env.AuthzTrans; import org.onap.aaf.auth.org.OrganizationException; import org.onap.aaf.cadi.CadiException; -import org.onap.aaf.cadi.client.Holder; +import org.onap.aaf.cadi.util.Holder; import org.onap.aaf.cadi.util.CSV; import org.onap.aaf.misc.env.APIException; import org.onap.aaf.misc.env.Env; -- cgit 1.2.3-korg