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