AT&T 2.0.19 Code drop, stage 4
[aaf/authz.git] / authz-gui / src / main / java / com / att / authz / gui / pages / RequestDetail.java
1 /*******************************************************************************
2  * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3  *******************************************************************************/
4 package com.att.authz.gui.pages;
5
6 import java.io.IOException;
7 import java.net.ConnectException;
8 import java.text.DateFormat;
9 import java.text.SimpleDateFormat;
10 import java.util.ArrayList;
11 import java.util.UUID;
12
13 import com.att.authz.env.AuthzEnv;
14 import com.att.authz.env.AuthzTrans;
15 import com.att.authz.gui.AuthGUI;
16 import com.att.authz.gui.BreadCrumbs;
17 import com.att.authz.gui.Page;
18 import com.att.authz.gui.Table;
19 import com.att.authz.gui.Table.Cells;
20 import com.att.authz.gui.table.AbsCell;
21 import com.att.authz.gui.table.RefCell;
22 import com.att.authz.gui.table.TextCell;
23 import org.onap.aaf.cadi.CadiException;
24 import org.onap.aaf.cadi.client.Future;
25 import org.onap.aaf.cadi.client.Rcli;
26 import org.onap.aaf.cadi.client.Retryable;
27 import org.onap.aaf.inno.env.APIException;
28 import org.onap.aaf.inno.env.Env;
29 import org.onap.aaf.inno.env.Slot;
30 import org.onap.aaf.inno.env.TimeTaken;
31
32 import aaf.v2_0.Approval;
33 import aaf.v2_0.Approvals;
34
35 public class RequestDetail extends Page {
36         public static final String HREF = "/gui/requestdetail";
37         public static final String NAME = "RequestDetail";
38         private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
39         public static final String[] FIELDS = {"ticket"};
40
41         public RequestDetail(final AuthGUI gui, Page ... breadcrumbs) throws APIException, IOException {
42                 super(gui.env, NAME, HREF, FIELDS,
43                                 new BreadCrumbs(breadcrumbs),
44                                 new Table<AuthGUI,AuthzTrans>("Request Details",gui.env.newTransNoAvg(),new Model(gui.env()),"class=detail")
45                                 );
46         }
47
48         /**
49          * Implement the table content for Request Detail
50          * 
51          *
52          */
53         private static class Model implements Table.Data<AuthGUI,AuthzTrans> {
54                 static final String WEBPHONE = "http://webphone.att.com/cgi-bin/webphones.pl?id=";
55                 private static final String CSP_ATT_COM = "@csp.att.com";
56                 final long NUM_100NS_INTERVALS_SINCE_UUID_EPOCH = 0x01b21dd213814000L;
57                 private static final String[] headers = new String[0];
58                 private Slot sTicket;
59                 public Model(AuthzEnv env) {
60                         sTicket = env.slot(NAME+".ticket");
61                 }
62
63                 @Override
64                 public String[] headers() {
65                         return headers;
66                 }
67                 
68                 @Override
69                 public Cells get(final AuthGUI gui, final AuthzTrans trans) {
70                         Cells rv=Cells.EMPTY;
71                         final String ticket = trans.get(sTicket, null);
72                         if(ticket!=null) {
73                                 try {
74                                         rv = gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Cells>() {
75                                                 @Override
76                                                 public Cells code(Rcli<?> client) throws CadiException, ConnectException, APIException {
77                                                         TimeTaken tt = trans.start("AAF Approval Details",Env.REMOTE);
78                                                         ArrayList<AbsCell[]> rv = new ArrayList<AbsCell[]>();
79                                                         try {
80                                                                 Future<Approvals> fa = client.read(
81                                                                         "/authz/approval/ticket/"+ticket, 
82                                                                         gui.approvalsDF
83                                                                         );
84                                                                 
85                                                                 if(fa.get(AuthGUI.TIMEOUT)) {
86                                                                         if (!trans.user().equals(fa.value.getApprovals().get(0).getUser())) {
87                                                                                 return Cells.EMPTY;
88                                                                         }
89                                                                         tt.done();
90                                                                         tt = trans.start("Load Data", Env.SUB);
91                                                                         boolean first = true;
92                                                                         for ( Approval approval : fa.value.getApprovals()) {
93                                                                                 AbsCell[] approverLine = new AbsCell[4];
94                                                                                 // only print common elements once
95                                                                                 if (first) {
96                                                                                         DateFormat createdDF = new SimpleDateFormat(DATE_TIME_FORMAT);
97                                                                                         UUID id = UUID.fromString(approval.getId());
98                                                                                         
99                                                                                         rv.add(new AbsCell[]{new TextCell("Ticket ID:"),new TextCell(approval.getTicket(),"colspan=3")});
100                                                                                         rv.add(new AbsCell[]{new TextCell("Memo:"),new TextCell(approval.getMemo(),"colspan=3")});
101                                                                                         rv.add(new AbsCell[]{new TextCell("Requested On:"), 
102                                                                                                         new TextCell(createdDF.format((id.timestamp() - NUM_100NS_INTERVALS_SINCE_UUID_EPOCH)/10000),"colspan=3")
103                                                                                         });
104                                                                                         rv.add(new AbsCell[]{new TextCell("Operation:"),new TextCell(decodeOp(approval.getOperation()),"colspan=3")});
105                                                                                         String user = approval.getUser();
106                                                                                         if (user.endsWith(CSP_ATT_COM)) {
107                                                                                                 rv.add(new AbsCell[]{new TextCell("User:"),
108                                                                                                                 new RefCell(user,WEBPHONE + user.substring(0, user.indexOf("@")),"colspan=3")});
109                                                                                         } else {
110                                                                                                 rv.add(new AbsCell[]{new TextCell("User:"),new TextCell(user,"colspan=3")});
111                                                                                         }
112                                                                                         
113                                                                                         // headers for listing each approver
114                                                                                         rv.add(new AbsCell[]{new TextCell(" ","colspan=4","class=blank_line")});
115                                                                                         rv.add(new AbsCell[]{AbsCell.Null,
116                                                                                                         new TextCell("Approver","class=bold"), 
117                                                                                                         new TextCell("Type","class=bold"), 
118                                                                                                         new TextCell("Status","class=bold")});
119                                                                                         approverLine[0] = new TextCell("Approvals:");
120                                                                                         
121                                                                                         first = false;
122                                                                                 } else {
123                                                                                     approverLine[0] = AbsCell.Null;
124                                                                                 }
125                                                                                 
126                                                                                 String approver = approval.getApprover();
127                                                                                 String approverShort = approver.substring(0,approver.indexOf('@'));
128                                                                                 
129                                                                                 if (approver.endsWith(CSP_ATT_COM)) {
130                                                                                         approverLine[1] = new RefCell(approver, WEBPHONE + approverShort);
131                                                                                 } else {
132                                                                                         approverLine[1] = new TextCell(approval.getApprover());
133                                                                                 }
134                                                                                 
135                                                                                 String type = approval.getType();
136                                                                                 if ("owner".equalsIgnoreCase(type)) {
137                                                                                         type = "resource owner";
138                                                                                 }
139                                                                                 
140                                                                                 approverLine[2] = new TextCell(type);
141                                                                                 approverLine[3] = new TextCell(approval.getStatus());
142                                                                                 rv.add(approverLine);
143                                                                         
144                                                                         }
145                                                                 } else {
146                                                                         rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***")});
147                                                                 }
148                                                         } finally {
149                                                                 tt.done();
150                                                         }
151                                                         return new Cells(rv,null);
152                                                 }
153                                         });
154                                 } catch (Exception e) {
155                                         trans.error().log(e);
156                                 }
157                         }
158                         return rv;
159                 }
160
161                 private String decodeOp(String operation) {
162                         if ("C".equalsIgnoreCase(operation)) {
163                                 return "Create";
164                         } else if ("D".equalsIgnoreCase(operation)) {
165                                 return "Delete";
166                         } else if ("U".equalsIgnoreCase(operation)) {
167                                 return "Update";
168                         } else if ("G".equalsIgnoreCase(operation)) {
169                                 return "Grant";
170                         } else if ("UG".equalsIgnoreCase(operation)) {
171                                 return "Un-Grant";
172                         }
173                         return operation;
174                 }
175         }
176 }