2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.auth.batch.reports;
24 import java.io.BufferedReader;
26 import java.io.FileInputStream;
27 import java.io.FileOutputStream;
28 import java.io.FileReader;
29 import java.io.IOException;
30 import java.io.PrintStream;
31 import java.util.HashMap;
32 import java.util.List;
34 import java.util.Properties;
36 import org.onap.aaf.auth.batch.Batch;
37 import org.onap.aaf.auth.batch.helpers.NS;
38 import org.onap.aaf.auth.batch.helpers.UserRole;
39 import org.onap.aaf.auth.env.AuthzTrans;
40 import org.onap.aaf.auth.org.Organization;
41 import org.onap.aaf.auth.org.Organization.Identity;
42 import org.onap.aaf.auth.org.OrganizationException;
43 import org.onap.aaf.cadi.util.CSV;
44 import org.onap.aaf.misc.env.APIException;
45 import org.onap.aaf.misc.env.Env;
46 import org.onap.aaf.misc.env.TimeTaken;
48 import com.datastax.driver.core.ResultSet;
49 import com.datastax.driver.core.SimpleStatement;
50 import com.datastax.driver.core.Statement;
52 public class MassMail extends Batch {
54 private Map<String, CSV.Writer> writerList;
55 private final String from;
56 private String subject = "AAF Information";
57 private boolean bOwners = true;
58 private boolean bAdmins = false;
59 private boolean bAppIDOnly = false;
60 private int escalate = 0;
61 private final File subdir;
62 private final String head;
63 private final String content;
64 private final String tail;
66 public MassMail(AuthzTrans trans) throws APIException, IOException, OrganizationException {
68 from = trans.getProperty("MAIL_FROM");
70 throw new APIException("No MAIL_FROM property set");
72 String subdirName = null;
74 for (String arg : args()) {
86 if (arg.length() > 10 && arg.startsWith("-escalate=")) {
87 escalate = Integer.parseInt(arg.substring(10));
88 } else if (subdirName == null) {
94 subdir = new File(logDir(), subdirName == null ? "mailing" : subdirName);
95 if (subdir.exists()) {
96 if (!subdir.isDirectory()) {
97 throw new APIException(subdirName + " is not a directory");
103 subject = readIn(subdir, "subject");
104 head = readIn(subdir, "html.head");
105 content = readIn(subdir,"html.content");
106 tail = readIn(subdir,"html.tail");
108 trans.info().log("Starting Connection Process");
109 TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
111 TimeTaken tt = trans.start("Connect to Cluster", Env.REMOTE);
113 session = cluster.connect();
118 // Create Intermediate Output
119 writerList = new HashMap<>();
121 NS.load(trans, session, NS.v2_0_11);
122 UserRole.load(trans, session, UserRole.v2_0_11);
124 // String sdate = Chrono.dateOnlyStamp(now);
130 private String readIn(File subdir, String name) throws IOException {
131 File file = new File(subdir, name);
132 byte[] bytes = new byte[(int) file.length()];
133 FileInputStream fis = new FileInputStream(file);
136 return new String(bytes);
143 protected void run(AuthzTrans trans) {
145 trans.info().log("Create a Mass Mailing");
147 final AuthzTrans transNoAvg = trans.env().newTransNoAvg();
148 final Organization org = trans.org();
150 StringBuilder to = new StringBuilder();
151 StringBuilder cc = new StringBuilder();
152 StringBuilder greet = new StringBuilder();
153 for (NS ns : NS.data.values()) {
156 Statement stmt = new SimpleStatement(
157 String.format("SELECT count(*) FROM authz.cred WHERE ns='%s';", ns.ndd.name));
158 results = session.execute(stmt);
159 long count = results.one().getLong(0);
169 StringBuilder o = to;
170 List<UserRole> owners = UserRole.getByRole().get(ns.ndd.name + ".owner");
171 if (owners.isEmpty()) {
172 trans.error().log(ns.ndd.name, "has no owners!");
174 for (UserRole owner : owners) {
176 Identity identity = org.getIdentity(transNoAvg, owner.user());
177 if (identity.isPerson()) {
178 if (o.length() > 0) {
182 o.append(identity.email());
183 greet.append(identity.firstName());
184 for (int i = 0; i < escalate; ++i) {
185 identity = identity.responsibleTo();
186 if (identity == null) {
189 if (cc.length() > 0) {
192 cc.append(identity.email());
195 } catch (OrganizationException e) {
196 trans.error().log(e, "Error Reading Organization");
209 List<UserRole> admins = UserRole.getByRole().get(ns.ndd.name + ".admin");
210 if (admins.isEmpty()) {
211 trans.warn().log(ns.ndd.name, "has no admins!");
213 for (UserRole admin : admins) {
215 Identity identity = org.getIdentity(transNoAvg, admin.user());
216 if (identity.isPerson()) {
217 if (a.length() > 0) {
220 a.append(identity.email());
222 if (greet.length() > 0) {
225 greet.append(identity.firstName());
228 } catch (OrganizationException e) {
229 trans.error().log(e, "Error Reading Organization");
236 PrintStream ps = new PrintStream(new FileOutputStream(
237 subdir.getPath() + File.separatorChar + "email_" + ns.ndd.name + ".hdr"));
245 ps.print("SUBJECT: ");
251 ps = new PrintStream(new FileOutputStream(
252 subdir.getPath() + File.separatorChar + "email_" + ns.ndd.name + ".html"));
254 ps.println(head.replaceAll("%FIRST_NAMES%", greet.toString()));
255 ps.println(content.replaceAll("%NS%", ns.ndd.name));
260 } catch (IOException e) {
261 trans.error().log(e);
265 // UserRole.load(transNoAvg, session, UserRole.v2_0_11, ur -> {
266 // if("owner".equals(ur.rname())) {
268 // Identity identity = org.getIdentity(transNoAvg, ur.user());
269 // if(identity.isPerson()) {
270 // System.out.println("Emailing " + identity.email());
272 // } catch (OrganizationException e) {
273 // trans.error().log(e, "Error Reading Organization");
276 // Organization org = trans.org();
277 // Identity identity = org.getIdentity(trans, ur.row);
279 // if(!check(transNoAvg, checked, ur.user())) {
280 // ur.row(whichWriter(transNoAvg,ur.user()),UserRole.UR);
282 // } catch (OrganizationException e) {
283 // trans.error().log(e, "Error Decrypting X509");
288 // } catch (OrganizationException e) {
289 // trans.info().log(e);
294 protected void _close(AuthzTrans trans) {
296 for (CSV.Writer cw : writerList.values()) {