Upgrade to latest oparent
[aaf/authz.git] / authz-batch / src / main / java / com / att / authz / reports / CheckCred.java
1 /*******************************************************************************
2  * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3  *******************************************************************************/
4 package com.att.authz.reports;
5
6 import java.io.IOException;
7
8 import com.att.authz.Batch;
9 import com.att.authz.env.AuthzTrans;
10 import com.att.authz.helpers.Cred;
11 import com.att.authz.helpers.Cred.Instance;
12 import org.onap.aaf.inno.env.APIException;
13 import org.onap.aaf.inno.env.Env;
14 import org.onap.aaf.inno.env.TimeTaken;
15 import org.onap.aaf.inno.env.util.Chrono;
16
17 public class CheckCred extends Batch{
18
19         public CheckCred(AuthzTrans trans) throws APIException, IOException {
20                 super(trans.env());
21                 TimeTaken tt = trans.start("Connect to Cluster", Env.REMOTE);
22                 try {
23                         session = cluster.connect();
24                 } finally {
25                         tt.done();
26                 }
27         
28                 Cred.load(trans, session);
29         }
30
31         @Override
32         protected void run(AuthzTrans trans) {
33                 String query;
34                 for(Cred cred : Cred.data.values()) {
35                         for(Instance inst : cred.instances) {
36                                 if(inst.other==0) {
37                                         if(dryRun) {
38                                         trans.warn().log("Ensuring 'other' is numeric");
39                                 } else {
40                                 query = "UPDATE authz.cred SET other=0 WHERE "
41                                                         + "id='" + cred.id   
42                                                         + "' AND type=" + inst.type
43                                                         + " AND expires='" + Chrono.dateStamp(inst.expires)
44                                                         + "';";
45                                 session.execute(query);
46                                 trans.warn().log("resetting 'other'",query);
47                                         }
48                                 }
49                         }
50                 }
51
52         }        
53                 /*
54         /// Evaluate 
55                 for(UserRole urKey : UserRole.data) {
56                 NSSplit nss = NS.deriveParent(urKey.role);
57                 if(nss==null && NS.data.size()>0 ) { // there is no Namespace for this UserRole
58                         if(dryRun) {
59                                         trans.warn().printf("Would delete %s %s, which has no corresponding Namespace",urKey.user,urKey.role);
60                         } else {
61                                 query = "DELETE FROM authz.user_role WHERE "
62                                                         + "user='" + urKey.user 
63                                                         + "' AND role='" + urKey.role
64                                                         + "';";
65                                 session.execute(query);
66                                         trans.warn().printf("Deleting %s %s, which has no corresponding Namespace",urKey.user,urKey.role);
67                         }
68                 } else if(urKey.ns == null || urKey.rname == null || !urKey.role.equals(urKey.ns+'.'+urKey.rname)) {
69                         if(dryRun) {
70                                 trans.warn().log(urKey,"needs to be split and added to Record (", urKey.ns, urKey.rname,")");
71                         } else {
72                         query = "UPDATE authz.user_role SET ns='" + nss.ns 
73                                                 + "', rname='" + nss.other
74                                                 + "' WHERE "
75                                                 + "user='" + urKey.user 
76                                                 + "' AND role='" + urKey.role
77                                                 + "';";
78                         session.execute(query);
79                         trans.warn().log("Setting ns and rname",query);
80                                 }
81                         }
82                 }
83         }
84         */
85         @Override
86         protected void _close(AuthzTrans trans) {
87         session.close();
88         aspr.info("End " + this.getClass().getSimpleName() + " processing" );
89         }
90 }