summaryrefslogtreecommitdiffstats
path: root/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/Approval.java
blob: ce19865a5e738cf93874723cce38ade0af46912f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/**
 * ============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.helpers;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;
import java.util.UUID;

import org.onap.aaf.auth.dao.cass.ApprovalDAO;
import org.onap.aaf.auth.env.AuthzTrans;
import org.onap.aaf.auth.layer.Result;
import org.onap.aaf.cadi.util.CSV;
import org.onap.aaf.misc.env.TimeTaken;
import org.onap.aaf.misc.env.Trans;

import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.SimpleStatement;
import com.datastax.driver.core.Statement;

public class Approval implements CacheChange.Data  {
	public static final String ADD_USER_TO_ROLE = "Add User [";
    public static final String RE_APPROVAL_IN_ROLE = "Extend access of User [";
    public static final String RE_VALIDATE_ADMIN = "Revalidate as Admin of AAF Namespace [";
    public static final String RE_VALIDATE_OWNER = "Revalidate as Owner of AAF Namespace [";

    public static TreeMap<String,List<Approval>> byApprover = new TreeMap<>();
    public static TreeMap<String,List<Approval>> byUser = new TreeMap<>();
    public static TreeMap<UUID,List<Approval>> byTicket = new TreeMap<>();
    public static List<Approval> list = new LinkedList<>();
    private final static CacheChange<Approval> cache = new CacheChange<>(); 
    
    public final ApprovalDAO.Data add;
    private String role;
    
    public Approval(UUID id, UUID ticket, String approver,// Date last_notified, 
            String user, String memo, String operation, String status, String type, long updated) {
        add = new ApprovalDAO.Data();
        add.id = id;
        add.ticket = ticket;
        add.approver = approver;
//        add.last_notified = last_notified;
        add.user = user;
        add.memo = memo;
        add.operation = operation;
        add.status = status;
        add.type = type;
        add.updated = new Date(updated);
        role = roleFromMemo(memo);
    }
    
    public static String roleFromMemo(String memo) {
        if (memo==null) {
            return null;
        }
        int first = memo.indexOf('[');
        if (first>=0) {
            int second = memo.indexOf(']', ++first);
            if (second>=0) {
                String role = memo.substring(first, second);
                if (memo.startsWith(RE_VALIDATE_ADMIN)) {
                    return role + ".admin";
                } else if (memo.startsWith(RE_VALIDATE_OWNER)) {
                    return role + ".owner";
                } else {
                	first = memo.indexOf('[',second);
                	if(first>=0) {
                		second = memo.indexOf(']', ++first);
                		if(second>=0) {
                			if(memo.startsWith(RE_APPROVAL_IN_ROLE) ||
                			   memo.startsWith(ADD_USER_TO_ROLE)) {
                				return  memo.substring(first, second);
                			}
                		}
                	}
                }
            }
        }
        return null;
    }

    public static int load(Trans trans, Session session, Creator<Approval> creator, Visitor<Approval> visitor) {
    	int count = 0;
    	try {
	    	count+=call(trans,session,creator.query(null), creator, visitor);
        } finally {
            trans.info().log("Found",count,"Approval Records");
        }
    	return count;
    }
    
	public static int load(Trans trans, Session session, Creator<Approval> creator ) {
    	int count = 0;
    	try {
	    	count+=call(trans,session,creator.query(null), creator, FullLoad);
        } finally {
            trans.info().log("Found",count,"Approval Records");
        }
    	return count;
    }
    
    public static int loadUsers(Trans trans, Session session, Set<String> users, Visitor<Approval> visitor) {
		int total = 0;
    	for(String user : users) {
			total+=call(trans,session,String.format("%s WHERE user='%s';",v2_0_17.select(), user),v2_0_17,visitor);
    	}
    	return total;
    }
    
    public static void row(CSV.RowSetter crs, Approval app) {
		crs.row("approval",app.add.id,app.add.ticket,app.add.user,app.role,app.add.memo);
	}

	private static int call(Trans trans, Session session, String query, Creator<Approval> creator, Visitor<Approval> visitor) {
    	TimeTaken tt = trans.start("DB Query", Trans.REMOTE);
        ResultSet results;
        try {
            Statement stmt = new SimpleStatement( query );
            results = session.execute(stmt);
            int count = 0;
            for (Row row : results.all()) {
            	++count;
            	visitor.visit(creator.create(row));
            }
            return count;
        } finally {
            tt.done();
        }
    }
    
    @Override
    public void expunge() {
        List<Approval> la = byApprover.get(getApprover());
        if (la!=null) {
            la.remove(this);
        }
        
        la = byUser.get(getUser());
        if (la!=null) {
            la.remove(this);
        }
        UUID ticket = this.add==null?null:this.add.ticket;
        if (ticket!=null) {
            la = byTicket.get(this.add.ticket);
            if (la!=null) {
                la.remove(this);
            }
        }
    }

    public static void clear() {
    	byApprover.clear();
    	byUser.clear();
    	byTicket.clear();
    	list.clear();
    	cache.resetLocalData();
    }

    public static Creator<Approval> v2_0_17 = new Creator<Approval>() {
        @Override
        public Approval create(Row row) {
            return new Approval(row.getUUID(0), row.getUUID(1), row.getString(2),
                    row.getString(3),row.getString(4),row.getString(5),row.getString(6),row.getString(7),
                    row.getLong(8)/1000);
        }

        @Override
        public String select() {
            return "select id,ticket,approver,user,memo,operation,status,type,WRITETIME(status) from authz.approval";
        }
    };
    
    public static Visitor<Approval> FullLoad = new Visitor<Approval>() {
		@Override
		public void visit(Approval app) {
	        List<Approval> ln;
	        list.add(app);
	        
	        String person = app.getApprover();
	        if (person!=null) {
	        ln = byApprover.get(person);
	            if (ln==null) {
	                ln = new ArrayList<>();
	                byApprover.put(app.getApprover(), ln);
	            }
	            ln.add(app);
	        }
	        
	        person = app.getUser();
	        if (person!=null) {
	            ln = byUser.get(person);
	            if (ln==null) {
	                ln = new ArrayList<>();
	                byUser.put(app.getUser(), ln);
	            }
	            ln.add(app);
	        }
	        UUID ticket = app.getTicket();
	        if (ticket!=null) {
	            ln = byTicket.get(ticket);
	            if (ln==null) {
	                ln = new ArrayList<>();
	                byTicket.put(app.getTicket(), ln);
	            }
	            ln.add(app);
	        }
		}
    };

    /**
     * @return the status
     */
    public String getStatus() {
        return add.status;
    }
    /**
     * @param status the status to set
     */
    public void setStatus(String status) {
        add.status = status;
    }
    /**
     * @return the id
     */
    public UUID getId() {
        return add.id;
    }
    /**
     * @return the ticket
     */
    public UUID getTicket() {
        return add.ticket;
    }
    /**
     * @return the approver
     */
    public String getApprover() {
        return add.approver;
    }
    /**
     * @return the user
     */
    public String getUser() {
        return add.user;
    }
    /**
     * @return the memo
     */
    public String getMemo() {
        return add.memo;
    }
    /**
     * @return the operation
     */
    public String getOperation() {
        return add.operation;
    }
    /**
     * @return the type
     */
    public String getType() {
        return add.type;
    }
    public void lapsed() {
        add.ticket=null;
        add.status="lapsed";
    }
    
    public String getRole() {
        return role;
    }
    
    public String toString() {
        return getUser() + ' ' + getMemo();
    }

    public void delayDelete(AuthzTrans trans, ApprovalDAO ad, boolean dryRun, String text) {
        if (dryRun) {
            trans.info().log(text,"- Would Delete: Approval",getId(),"on ticket",getTicket(),"for",getApprover());
        } else {
            Result<Void> rv = ad.delete(trans, add, false);
            if (rv.isOK()) {
                trans.info().log(text,"- Deleted: Approval",getId(),"on ticket",getTicket(),"for",getApprover());
                cache.delayedDelete(this);
            } else {
                trans.info().log(text,"- Failed to Delete Approval",getId());
            }
        }
    }
    

    public static void resetLocalData() {
        cache.resetLocalData();
    }
    
    public static int sizeForDeletion() {
        return cache.cacheSize();
    }

    public static void delayDelete(AuthzTrans noAvg, ApprovalDAO apprDAO, boolean dryRun, List<Approval> list, String text) {
        if (list!=null) {
            for (Approval a : list) {
                a.delayDelete(noAvg, apprDAO, dryRun,text);
            }
        }
    }

    public static boolean pendingDelete(Approval a) {
        return cache.contains(a);
    }

	public static void deleteByIDBatch(StringBuilder sb, String id) {
		sb.append("DELETE from authz.approval where id=");
		sb.append(id);
		sb.append(";\n");
	}

}