summaryrefslogtreecommitdiffstats
path: root/auth/auth-batch/src/main/java/org/onap/aaf/auth/update/ExpiringP2.java
blob: f9797c867d7b771022d990fe312860c53e1ce00a (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
/**
 * ============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.update;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.onap.aaf.auth.Batch;
import org.onap.aaf.auth.BatchPrincipal;
import org.onap.aaf.auth.actions.Action;
import org.onap.aaf.auth.actions.ActionDAO;
import org.onap.aaf.auth.actions.CacheTouch;
import org.onap.aaf.auth.actions.URDelete;
import org.onap.aaf.auth.env.AuthzTrans;
import org.onap.aaf.auth.helpers.UserRole;
import org.onap.aaf.auth.org.OrganizationException;
import org.onap.aaf.cadi.util.Split;
import org.onap.aaf.misc.env.APIException;
import org.onap.aaf.misc.env.Env;
import org.onap.aaf.misc.env.TimeTaken;
import org.onap.aaf.misc.env.util.Chrono;

public class ExpiringP2 extends Batch {
    private final URDelete urDelete;
    private final CacheTouch cacheTouch;
    private final AuthzTrans noAvg;
    private final BufferedReader urDeleteF;

    public ExpiringP2(AuthzTrans trans) throws APIException, IOException, OrganizationException {
        super(trans.env());
        trans.info().log("Starting Connection Process");
        
        noAvg = env.newTransNoAvg();
        noAvg.setUser(new BatchPrincipal("batch:ExpiringP2"));

        TimeTaken tt0 = trans.start("Cassandra Initialization", Env.SUB);
        try {
                urDelete = new URDelete(trans, cluster,isDryRun());
                TimeTaken tt2 = trans.start("Connect to Cluster", Env.REMOTE);
                try {
                    session = urDelete.getSession(trans);
                } finally {
                    tt2.done();
                }
            cacheTouch = new CacheTouch(trans,urDelete);
            
            File data_dir = new File(env.getProperty("aaf_data_dir"));
            if(!data_dir.exists() || !data_dir.canWrite() || !data_dir.canRead()) {
                throw new IOException("Cannot read/write to Data Directory "+ data_dir.getCanonicalPath() + ": EXITING!!!");
            }
            urDeleteF = new BufferedReader(new FileReader(new File(data_dir,"UserRoleDeletes.dat")));
            
        } finally {
            tt0.done();
        }
    }

    @Override
    protected void run(AuthzTrans trans) {
        deleteURs(noAvg, urDeleteF, urDelete, cacheTouch);
    }
    
    public static void deleteURs(AuthzTrans trans, BufferedReader urDeleteF, URDelete urDelete, CacheTouch cacheTouch) {
        String line,prev="";
        try {
            UserRole ur;
            Map<String,Count> tally = new HashMap<>();
            int count=0;
            try {
                while((line=urDeleteF.readLine())!=null) {
                    if(line.startsWith("#")) {
                        Count cnt = tally.get(line);
                        if(cnt==null) {
                            tally.put(line, cnt=new Count());
                        }
                        cnt.inc();
                        prev = line;
                    } else {
                        String[] l = Split.splitTrim('|', line);
                        try {
                            // Note: following default order from "COPY TO"
                            ur = new UserRole(l[0],l[1],l[3],l[4],Chrono.iso8601Fmt.parse(l[2]));
                            urDelete.exec(trans, ur, prev);
                            ++count;
                        } catch (ParseException e) {
                            trans.error().log(e);
                        }
                    }
                }
                
                System.out.println("Tallies of UserRole Deletions");
                for(Entry<String, Count> es : tally.entrySet()) {
                    System.out.printf("  %6d\t%20s\n", es.getValue().cnt,es.getKey());
                }
            } finally {
                if(cacheTouch!=null && count>0) {
                        cacheTouch.exec(trans, "user_roles", "Removing UserRoles");
                }
            }
        } catch (IOException e) {
            trans.error().log(e);
        }
        
    }
    private static class Count {
        private int cnt=0;
        
        public /*synchonized*/ void inc() {
            ++cnt;
        }
        
        public String toString() {
            return Integer.toString(cnt);
        }
    }
    
    @Override
    protected void _close(AuthzTrans trans) {
        aspr.info("End " + this.getClass().getSimpleName() + " processing" );
        for(Action<?,?,?> action : new Action<?,?,?>[] {urDelete,cacheTouch}) {
                if(action instanceof ActionDAO) {
                    ((ActionDAO<?,?,?>)action).close(trans);
                }
        }
        session.close();
        try {
            urDeleteF.close();
        } catch (IOException e) {
            trans.error().log(e);
        }
    }

}