ad26674f71005f4a8c3574ebba28873ab55493d2
[aaf/authz.git] / authz-gui / src / main / java / com / att / authz / gui / pages / PermDetail.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.util.ArrayList;
9 import java.util.List;
10
11 import com.att.aft.dme2.internal.jetty.http.HttpStatus;
12 import com.att.authz.env.AuthzEnv;
13 import com.att.authz.env.AuthzTrans;
14 import com.att.authz.gui.AuthGUI;
15 import com.att.authz.gui.BreadCrumbs;
16 import com.att.authz.gui.Page;
17 import com.att.authz.gui.Table;
18 import com.att.authz.gui.Table.Cells;
19 import com.att.authz.gui.table.AbsCell;
20 import com.att.authz.gui.table.RefCell;
21 import com.att.authz.gui.table.TextCell;
22 import org.onap.aaf.cadi.CadiException;
23 import org.onap.aaf.cadi.client.Future;
24 import org.onap.aaf.cadi.client.Rcli;
25 import org.onap.aaf.cadi.client.Retryable;
26 import org.onap.aaf.inno.env.APIException;
27 import org.onap.aaf.inno.env.Env;
28 import org.onap.aaf.inno.env.Slot;
29 import org.onap.aaf.inno.env.TimeTaken;
30
31 import aaf.v2_0.Perm;
32 import aaf.v2_0.Perms;
33
34 /**
35  * Detail Page for Permissions
36  *
37  */
38 public class PermDetail extends Page {
39         public static final String HREF = "/gui/permdetail";
40         public static final String NAME = "PermDetail";
41         private static final String BLANK = "";
42
43         public PermDetail(final AuthGUI gui, Page ... breadcrumbs) throws APIException, IOException {
44                 super(gui.env, NAME, HREF, new String[] {"type","instance","action"},
45                                 new BreadCrumbs(breadcrumbs),
46                                 new Table<AuthGUI,AuthzTrans>("Permission Details",gui.env.newTransNoAvg(),new Model(gui.env()),"class=detail")
47                                 );
48         }
49
50         /**
51          * Implement the table content for Permissions Detail
52          * 
53          *
54          */
55         private static class Model implements Table.Data<AuthGUI,AuthzTrans> {
56                 private static final String[] headers = new String[0];
57                 private Slot type, instance, action;
58                 public Model(AuthzEnv env) {
59                         type = env.slot(NAME+".type");
60                         instance = env.slot(NAME+".instance");
61                         action = env.slot(NAME+".action");
62                 }
63
64                 @Override
65                 public String[] headers() {
66                         return headers;
67                 }
68                 
69                 @Override
70                 public Cells get(final AuthGUI gui, final AuthzTrans trans) {
71                         final String pType = trans.get(type, null);
72                         final String pInstance = trans.get(instance, null);
73                         final String pAction = trans.get(action, null);
74                         if(pType==null || pInstance==null || pAction==null) {
75                                 return Cells.EMPTY;
76                         }
77                         ArrayList<AbsCell[]> rv = new ArrayList<AbsCell[]>();
78                         rv.add(new AbsCell[]{new TextCell("Type:"),new TextCell(pType)});
79                         rv.add(new AbsCell[]{new TextCell("Instance:"),new TextCell(pInstance)});
80                         rv.add(new AbsCell[]{new TextCell("Action:"),new TextCell(pAction)});
81                         try {
82                                 gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
83                                         @Override
84                                         public Void code(Rcli<?> client)throws CadiException, ConnectException, APIException {
85                                                 TimeTaken tt = trans.start("AAF Perm Details",Env.REMOTE);
86                                                 try {
87                                                         Future<Perms> fp= client.read("/authz/perms/"+pType + '/' + pInstance + '/' + pAction,gui.permsDF);
88                                         
89                                                         if(fp.get(AuthGUI.TIMEOUT)) {
90                                                                 tt.done();
91                                                                 tt = trans.start("Load Data", Env.SUB);
92                                                                 List<Perm> ps = fp.value.getPerm();
93                                                                 if(!ps.isEmpty()) {
94                                                                         Perm perm = fp.value.getPerm().get(0);
95                                                                         String desc = (perm.getDescription()!=null?perm.getDescription():BLANK);
96                                                                         rv.add(new AbsCell[]{new TextCell("Description:"),new TextCell(desc)});
97                                                                         boolean first=true;
98                                                                         for(String r : perm.getRoles()) {
99                                                                                 if(first){
100                                                                                         first=false;
101                                                                                         rv.add(new AbsCell[] {
102                                                                                                         new TextCell("Associated Roles:"),
103                                                                                                         new TextCell(r)
104                                                                                                 });
105                                                                                 } else {
106                                                                                         rv.add(new AbsCell[] {
107                                                                                                 AbsCell.Null,
108                                                                                                 new TextCell(r)
109                                                                                         });
110                                                                                 }
111                                                                         }
112                                                                 }
113                                                                 String historyLink = PermHistory.HREF 
114                                                                                 + "?type=" + pType + "&instance=" + pInstance + "&action=" + pAction;
115                                                                 
116                                                                 rv.add(new AbsCell[] {new RefCell("See History",historyLink)});
117                                                         } else {
118                                                                 rv.add(new AbsCell[] {new TextCell(
119                                                                         fp.code()==HttpStatus.NOT_FOUND_404?
120                                                                                 "*** Implicit Permission ***":
121                                                                                 "*** Data Unavailable ***"
122                                                                                 )});
123                                                         }
124                                                 } finally {
125                                                         tt.done();
126                                                 }
127
128                                                 return null;
129                                         }
130                                 });
131                         } catch (Exception e) {
132                                 e.printStackTrace();
133                         }
134                         return new Cells(rv,null);
135                 }
136         }
137 }               
138