8ff11e13abae9f30d2213ae7e180a77ad5f9f0ff
[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 javax.servlet.http.HttpServletRequest;
32
33 import org.onap.aaf.auth.common.Define;
34 import org.onap.aaf.auth.env.AuthzEnv;
35 import org.onap.aaf.auth.env.AuthzTrans;
36 import org.onap.aaf.auth.gui.AAF_GUI;
37 import org.onap.aaf.auth.gui.BreadCrumbs;
38 import org.onap.aaf.auth.gui.Page;
39 import org.onap.aaf.auth.gui.Table;
40 import org.onap.aaf.auth.gui.Table.Cells;
41 import org.onap.aaf.auth.gui.table.AbsCell;
42 import org.onap.aaf.auth.gui.table.TableData;
43 import org.onap.aaf.auth.gui.table.TextCell;
44 import org.onap.aaf.cadi.CadiException;
45 import org.onap.aaf.cadi.client.Future;
46 import org.onap.aaf.cadi.client.Rcli;
47 import org.onap.aaf.cadi.client.Retryable;
48 import org.onap.aaf.misc.env.APIException;
49 import org.onap.aaf.misc.env.Env;
50 import org.onap.aaf.misc.env.Slot;
51 import org.onap.aaf.misc.env.TimeTaken;
52
53 import aaf.v2_0.Approval;
54 import aaf.v2_0.Approvals;
55
56 public class RequestDetail extends Page {
57     public static final String HREF = "/gui/requestdetail";
58     public static final String NAME = "RequestDetail";
59     private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
60     public static final String[] FIELDS = {"ticket"};
61
62     public RequestDetail(final AAF_GUI gui, Page ... breadcrumbs) throws APIException, IOException {
63         super(gui.env, NAME, HREF, FIELDS,
64                 new BreadCrumbs(breadcrumbs),
65                 new Table<AAF_GUI,AuthzTrans>("Request Details",gui.env.newTransNoAvg(),new Model(gui.env),"class=detail")
66                 );
67     }
68
69     /**
70      * Implement the table content for Request Detail
71      *
72      * @author Jeremiah
73      *
74      */
75     private static class Model extends TableData<AAF_GUI,AuthzTrans> {
76        static 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<>();
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                                     Approval app = fa.value.getApprovals().get(0);
101                                     if(app==null) {
102                                         return Cells.EMPTY;
103                                     } else {
104                                         if (!(trans.user().equals(app.getUser()) ||
105                                               trans.user().equals(app.getApprover()))) {
106                                             HttpServletRequest req = trans.get(gui.slotHttpServletRequest,null);
107                                             if(req==null || !req.isUserInRole(Define.ROOT_NS()+"|access|*|*")) {
108                                                 return Cells.EMPTY;
109                                             }
110                                         }
111                                     }
112                                     tt.done();
113                                     tt = trans.start("Load Data", Env.SUB);
114                                     boolean first = true;
115                                     for ( Approval approval : fa.value.getApprovals()) {
116                                         AbsCell[] approverLine = new AbsCell[4];
117                                         // only print common elements once
118                                         if (first) {
119                                             DateFormat createdDF = new SimpleDateFormat(DATE_TIME_FORMAT);
120                                             UUID id = UUID.fromString(approval.getId());
121
122                                             rv.add(new AbsCell[]{new TextCell("Ticket ID:"),new TextCell(approval.getTicket(),"colspan=3")});
123                                             rv.add(new AbsCell[]{new TextCell("Memo:"),new TextCell(approval.getMemo(),"colspan=3")});
124                                             rv.add(new AbsCell[]{new TextCell("Requested On:"),
125                                                     new TextCell(createdDF.format((id.timestamp() - NUM_100NS_INTERVALS_SINCE_UUID_EPOCH)/10000),"colspan=3")
126                                             });
127                                             rv.add(new AbsCell[]{new TextCell("Operation:"),new TextCell(decodeOp(approval.getOperation()),"colspan=3")});
128                                             String user = approval.getUser();
129                                             rv.add(new AbsCell[]{new TextCell("User:"),new TextCell(user,"colspan=3")});
130
131                                             // headers for listing each approver
132                                             rv.add(new AbsCell[]{new TextCell(" ","colspan=4","class=blank_line")});
133                                             rv.add(new AbsCell[]{AbsCell.Null,
134                                                     new TextCell("Approver","class=bold"),
135                                                     new TextCell("Type","class=bold"),
136                                                     new TextCell("Status","class=bold")});
137                                             approverLine[0] = new TextCell("Approvals:");
138
139                                             first = false;
140                                         } else {
141                                             approverLine[0] = AbsCell.Null;
142                                         }
143
144                                         approverLine[1] = new TextCell(approval.getApprover());
145                                         String type = approval.getType();
146                                         if ("owner".equalsIgnoreCase(type)) {
147                                             type = "resource owner";
148                                         }
149
150                                         approverLine[2] = new TextCell(type);
151                                         approverLine[3] = new TextCell(approval.getStatus());
152                                         rv.add(approverLine);
153
154                                     }
155                                 } else {
156                                     rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***")});
157                                 }
158                             } finally {
159                                 tt.done();
160                             }
161                             return new Cells(rv,null);
162                         }
163                     });
164                 } catch (Exception e) {
165                     trans.error().log(e);
166                 }
167             }
168             return rv;
169         }
170
171         private String decodeOp(String operation) {
172             if ("C".equalsIgnoreCase(operation)) {
173                 return "Create";
174             } else if ("D".equalsIgnoreCase(operation)) {
175                 return "Delete";
176             } else if ("U".equalsIgnoreCase(operation)) {
177                 return "Update";
178             } else if ("G".equalsIgnoreCase(operation)) {
179                 return "Grant";
180             } else if ("UG".equalsIgnoreCase(operation)) {
181                 return "Un-Grant";
182             }
183             return operation;
184         }
185     }
186 }