AT&T 2.0.19 Code drop, stage 4
[aaf/authz.git] / authz-gui / src / main / java / com / att / authz / gui / pages / NsDetail.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.authz.env.AuthzEnv;
12 import com.att.authz.env.AuthzTrans;
13 import com.att.authz.gui.AuthGUI;
14 import com.att.authz.gui.BreadCrumbs;
15 import com.att.authz.gui.Page;
16 import com.att.authz.gui.Table;
17 import com.att.authz.gui.Table.Cells;
18 import com.att.authz.gui.table.AbsCell;
19 import com.att.authz.gui.table.RefCell;
20 import com.att.authz.gui.table.TextCell;
21 import org.onap.aaf.cadi.CadiException;
22 import org.onap.aaf.cadi.client.Future;
23 import org.onap.aaf.cadi.client.Rcli;
24 import org.onap.aaf.cadi.client.Retryable;
25 import com.att.cmd.AAFcli;
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 import org.onap.aaf.inno.env.util.Chrono;
31
32 import aaf.v2_0.Nss;
33 import aaf.v2_0.Nss.Ns;
34 import aaf.v2_0.Perm;
35 import aaf.v2_0.Perms;
36 import aaf.v2_0.Role;
37 import aaf.v2_0.Roles;
38 import aaf.v2_0.Users;
39 import aaf.v2_0.Users.User;
40
41 public class NsDetail extends Page {
42         
43         public static final String HREF = "/gui/nsdetail";
44         public static final String NAME = "NsDetail";
45         static final String WEBPHONE = "http://webphone.att.com/cgi-bin/webphones.pl?id=";
46         public static enum NS_FIELD { OWNERS, ADMINS, ROLES, PERMISSIONS, CREDS};
47         private static final String BLANK = "";
48
49         public NsDetail(final AuthGUI gui, Page ... breadcrumbs) throws APIException, IOException {
50                 super(gui.env, NAME, HREF, new String[] {"name"}, 
51                                 new BreadCrumbs(breadcrumbs),
52                                 new Table<AuthGUI,AuthzTrans>("Namespace Details",gui.env.newTransNoAvg(),new Model(gui.env()),"class=detail")
53                                 );
54         }
55
56         /**
57          * Implement the table content for Namespace Detail
58          * 
59          *
60          */
61         private static class Model implements Table.Data<AuthGUI,AuthzTrans> {
62                 private static final String[] headers = new String[0];          
63                 private static final String CSP_ATT_COM = "@csp.att.com";
64                 private Slot name;
65                 public Model(AuthzEnv env) {
66                         name = env.slot(NAME+".name");
67                 }
68
69                 @Override
70                 public String[] headers() {
71                         return headers;
72                 }
73                 
74                 @Override
75                 public Cells get(final AuthGUI gui, final AuthzTrans trans) {
76                         final String nsName = trans.get(name, null);
77                         if(nsName==null) {
78                                 return Cells.EMPTY;
79                         }
80                         ArrayList<AbsCell[]> rv = new ArrayList<AbsCell[]>();
81                         rv.add(new AbsCell[]{new TextCell("Name:"),new TextCell(nsName)});
82
83                         final TimeTaken tt = trans.start("AAF Namespace Details",Env.REMOTE);
84                         try {
85                                 gui.clientAsUser(trans.getUserPrincipal(),new Retryable<Void>() {
86                                         @Override
87                                         public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
88                                                 Future<Nss> fn = client.read("/authz/nss/"+nsName,gui.nssDF);
89
90                                                 if(fn.get(AuthGUI.TIMEOUT)) {
91                                                         tt.done();
92                                                         try {
93 //                                                              TimeTaken tt = trans.start("Load Data", Env.SUB);
94                                                                 
95                                                                 for(Ns n : fn.value.getNs()) {
96                                                                         String desc = (n.getDescription()!=null?n.getDescription():BLANK);
97                                                                         rv.add(new AbsCell[]{new TextCell("Description:"),new TextCell(desc)});
98                                                                         
99                                                                         addField(trans, rv, n.getAdmin(), NS_FIELD.ADMINS);
100                                                                         addField(trans, rv, n.getResponsible(), NS_FIELD.OWNERS);
101                         
102                                                                         Future<Users> fu = client.read(
103                                                                                                         "/authn/creds/ns/"+nsName, 
104                                                                                                         gui.usersDF
105                                                                                                         );
106                                                                         List<String> creds = new ArrayList<String>();
107                                                                         if(fu.get(AAFcli.timeout())) {
108                                                                                 for (User u : fu.value.getUser()) {
109                                                                                         StringBuilder sb = new StringBuilder(u.getId());
110                                                                                         switch(u.getType()) {
111                                                                                                 case 1: sb.append(" (U/Pass) "); break;
112                                                                                                 case 10: sb.append(" (Cert) "); break;
113                                                                                                 case 200: sb.append(" (x509) "); break;
114                                                                                                 default:
115                                                                                                         sb.append(" ");
116                                                                                         }
117                                                                                         sb.append(Chrono.niceDateStamp(u.getExpires()));
118                                                                                         creds.add(sb.toString());
119                                                                                 }
120                                                                         }
121                                                                         addField(trans, rv, creds, NS_FIELD.CREDS);
122                         
123                                                                         Future<Roles> fr = client.read(
124                                                                                                         "/authz/roles/ns/"+nsName, 
125                                                                                                         gui.rolesDF
126                                                                                                         );
127                                                                         List<String> roles = new ArrayList<String>();
128                                                                         if(fr.get(AAFcli.timeout())) {
129                                                                                 for (Role r : fr.value.getRole()) {
130                                                                                         roles.add(r.getName());
131                                                                                 }
132                                                                         }
133                                                                         addField(trans, rv, roles, NS_FIELD.ROLES);
134                                                                         
135                                                                         
136                                                                         Future<Perms> fp = client.read(
137                                                                                                         "/authz/perms/ns/"+nsName, 
138                                                                                                         gui.permsDF
139                                                                                                         );
140                                                                         List<String> perms = new ArrayList<String>();
141                         
142                                                                         if(fp.get(AAFcli.timeout())) {
143                                                                                 for (Perm p : fp.value.getPerm()) {
144                                                                                         perms.add(p.getType() + "|" + p.getInstance() + "|" + p.getAction());
145                                                                                 }
146                                                                         }
147                                                                         addField(trans, rv, perms, NS_FIELD.PERMISSIONS);
148                                                                 }
149                                                                 String historyLink = NsHistory.HREF 
150                                                                                 + "?name=" + nsName;
151                                                                 rv.add(new AbsCell[] {new RefCell("See History",historyLink)});
152                                                         } finally {
153                                                                 tt.done();
154                                                         }
155                                                 } else {
156                                                         rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***")});
157                                                 }
158                                                 return null;
159                                         }
160                                 });
161                         } catch (Exception e) {
162                                 e.printStackTrace();
163                         } finally {
164                                 tt.done();
165                         }
166                         return new Cells(rv,null);
167                 }
168
169                 private void addField(AuthzTrans trans, ArrayList<AbsCell[]> rv, List<String> values, NS_FIELD field) {
170                         if (!values.isEmpty()) {
171                                 switch(field) {
172                                 case OWNERS:
173                                 case ADMINS:
174                                 case CREDS:
175                                         for (int i=0; i< values.size(); i++) {
176                                                 AbsCell label = (i==0?new TextCell(sentenceCase(field)+":"):AbsCell.Null);
177                                                 String user = values.get(i);
178                                                 AbsCell userCell = (user.endsWith(CSP_ATT_COM)?
179                                                                 new RefCell(user,WEBPHONE + user.substring(0,user.indexOf('@'))):new TextCell(user));
180                                                 rv.add(new AbsCell[] {
181                                                                 label, 
182                                                                 userCell
183                                                 });
184                                         }
185                                         break;
186                                 case ROLES:
187                                         for (int i=0; i< values.size(); i++) {
188                                                 AbsCell label = (i==0?new TextCell(sentenceCase(field)+":"):AbsCell.Null);
189                                                 rv.add(new AbsCell[] {
190                                                                 label, 
191                                                                 new TextCell(values.get(i))
192                                                 });
193                                         }
194                                         break;
195                                 case PERMISSIONS:
196                                         for (int i=0; i< values.size(); i++) {
197                                                 AbsCell label = (i==0?new TextCell(sentenceCase(field)+":"):AbsCell.Null);
198                                                 String perm = values.get(i);
199                                                 String[] fields = perm.split("\\|");
200                                                 String grantLink = PermGrantForm.HREF 
201                                                                 + "?type=" + fields[0].trim()
202                                                                 + "&amp;instance=" + fields[1].trim()
203                                                                 + "&amp;action=" + fields[2].trim();
204                                                 
205                                                 rv.add(new AbsCell[] {
206                                                                 label, 
207                                                                 new TextCell(perm),
208                                                                 new RefCell("Grant This Perm", grantLink)
209                                                 });
210                                         }
211                                         break;
212                                 }
213
214                         }
215                 }
216
217                 private String sentenceCase(NS_FIELD field) {
218                         String sField = field.toString();
219                         return sField.substring(0, 1).toUpperCase() + sField.substring(1).toLowerCase();
220                 }
221         
222         }
223 }