AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / pages / NsDetail.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.io.StringWriter;
26 import java.net.ConnectException;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.onap.aaf.auth.cmd.AAFcli;
31 import org.onap.aaf.auth.env.AuthzTrans;
32 import org.onap.aaf.auth.gui.AAF_GUI;
33 import org.onap.aaf.auth.gui.BreadCrumbs;
34 import org.onap.aaf.auth.gui.Page;
35 import org.onap.aaf.auth.gui.Table;
36 import org.onap.aaf.auth.gui.Table.Cells;
37 import org.onap.aaf.auth.gui.table.AbsCell;
38 import org.onap.aaf.auth.gui.table.RefCell;
39 import org.onap.aaf.auth.gui.table.TableData;
40 import org.onap.aaf.auth.gui.table.TextCell;
41 import org.onap.aaf.auth.validation.Validator;
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.cadi.config.Config;
47 import org.onap.aaf.misc.env.APIException;
48 import org.onap.aaf.misc.env.Env;
49 import org.onap.aaf.misc.env.Slot;
50 import org.onap.aaf.misc.env.TimeTaken;
51 import org.onap.aaf.misc.xgen.html.HTMLGen;
52
53 import aaf.v2_0.Nss;
54 import aaf.v2_0.Nss.Ns;
55 import aaf.v2_0.Perm;
56 import aaf.v2_0.Perms;
57 import aaf.v2_0.Role;
58 import aaf.v2_0.Roles;
59
60 public class NsDetail extends Page {
61         
62         public static final String HREF = "/gui/nsdetail";
63         public static final String NAME = "NsDetail";
64         static final String WEBPHONE = "http://webphone.att.com/cgi-bin/webphones.pl?id=";
65         public static enum NS_FIELD { OWNERS, ADMINS, ROLES, PERMISSIONS, CREDS};
66         private static final String BLANK = "";
67         private static Slot keySlot;
68         private static Model model;
69         private static String gw_url;
70
71
72         public NsDetail(final AAF_GUI gui, Page ... breadcrumbs) throws APIException, IOException {
73                 super(gui.env, NAME, HREF, new String[] {"ns"}, 
74                                 new BreadCrumbs(breadcrumbs),
75                                 new Table<AAF_GUI,AuthzTrans>("Namespace Details",gui.env.newTransNoAvg(),model=new Model(),"class=detail")
76                                 );
77                 model.set(this);
78                 keySlot = gui.env.slot(NAME+".ns");
79                 gw_url = gui.env.getProperty(Config.GW_URL);
80                 if(gw_url==null) {
81                         gw_url="";
82                 } else {
83                         gw_url+="/aaf/2.0";
84                 }
85         }
86
87         /**
88          * Implement the table content for Namespace Detail
89          * 
90          * @author Jeremiah
91          *
92          */
93         private static class Model extends TableData<AAF_GUI,AuthzTrans> {
94                 private static final String CSP_ATT_COM = "@csp.att.com";
95                 private NsDetail nd;
96
97                 public void set(NsDetail nsDetail) {
98                         nd=nsDetail;
99                 }
100
101                 @Override
102                 public Cells get(final AuthzTrans trans, final AAF_GUI gui) {
103                         final String nsName = trans.get(keySlot, null);
104                         Validator v = new Validator();
105                         v.ns(nsName);
106                         if(v.err()) {
107                                 trans.warn().printf("Error in NsDetail Request: %s", v.errs());
108                                 return Cells.EMPTY;
109                         }
110
111                         if(nsName==null) {
112                                 return Cells.EMPTY;
113                         }
114                         final ArrayList<AbsCell[]> rv = new ArrayList<AbsCell[]>();
115                         rv.add(new AbsCell[]{new TextCell("Name:"),new TextCell(nsName)});
116
117                         final TimeTaken tt = trans.start("AAF Namespace Details",Env.REMOTE);
118                         try {
119                                 gui.clientAsUser(trans.getUserPrincipal(),new Retryable<Void>() {
120                                         @Override
121                                         public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
122                                                 Future<Nss> fn = client.read("/authz/nss/"+nsName,gui.getDF(Nss.class));
123
124                                                 if(fn.get(AAF_GUI.TIMEOUT)) {
125                                                         tt.done();
126                                                         try {
127 //                                                              TimeTaken tt = trans.start("Load Data", Env.SUB);
128                                                                 
129                                                                 for(Ns n : fn.value.getNs()) {
130                                                                         String desc = (n.getDescription()!=null?n.getDescription():BLANK);
131                                                                         rv.add(new AbsCell[]{new TextCell("Description:"),new TextCell(desc)});
132                                                                         
133                                                                         addField(trans, nsName, rv, n.getAdmin(), NS_FIELD.ADMINS);
134                                                                         addField(trans, nsName, rv, n.getResponsible(), NS_FIELD.OWNERS);
135
136                                                                         StringWriter sw = new StringWriter();
137                                                                         HTMLGen hgen = nd.clone(sw);
138                                                                         hgen.leaf(HTMLGen.A, "class=greenbutton","href="+CredDetail.HREF+"?ns="+nsName).text("Cred Details").end();
139                                                                         rv.add(new AbsCell[] {
140                                                                                         new TextCell("Credentials"),
141                                                                                         new TextCell(sw.toString())
142                                                                                 });
143                                                                         
144                         
145                                                                         Future<Roles> fr = client.read(
146                                                                                                         "/authz/roles/ns/"+nsName, 
147                                                                                                         gui.getDF(Roles.class)
148                                                                                                         );
149                                                                         List<String> roles = new ArrayList<String>();
150                                                                         if(fr.get(AAFcli.timeout())) {
151                                                                                 for (Role r : fr.value.getRole()) {
152                                                                                         roles.add(r.getName());
153                                                                                 }
154                                                                         }
155                                                                         addField(trans, nsName, rv, roles, NS_FIELD.ROLES);
156                                                                         
157                                                                         
158                                                                         Future<Perms> fp = client.read(
159                                                                                                         "/authz/perms/ns/"+nsName, 
160                                                                                                         gui.getDF(Perms.class)
161                                                                                                         );
162                                                                         List<String> perms = new ArrayList<String>();
163                         
164                                                                         if(fp.get(AAFcli.timeout())) {
165                                                                                 for (Perm p : fp.value.getPerm()) {
166                                                                                         perms.add(p.getType() + "|" + p.getInstance() + "|" + p.getAction());
167                                                                                 }
168                                                                         }
169                                                                         addField(trans, nsName, rv, perms, NS_FIELD.PERMISSIONS);
170                                                                 }
171                                                                 String historyLink = NsHistory.HREF 
172                                                                                 + "?name=" + nsName;
173                                                                 rv.add(new AbsCell[] {new RefCell("See History",historyLink,false)});
174                                                         } finally {
175                                                                 tt.done();
176                                                         }
177                                                 } else {
178                                                         rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***")});
179                                                 }
180                                                 return null;
181                                         }
182                                 });
183                         } catch (Exception e) {
184                                 e.printStackTrace();
185                         } finally {
186                                 tt.done();
187                         }
188                         return new Cells(rv,null);
189                 }
190
191                 private void addField(AuthzTrans trans, String ns, List<AbsCell[]> rv, List<String> values, NS_FIELD field) {
192                         if (!values.isEmpty()) {
193                                 switch(field) {
194                                 case OWNERS:
195                                 case ADMINS:
196                                 case CREDS:
197                                         for (int i=0; i< values.size(); i++) {
198                                                 AbsCell label = (i==0?new TextCell(sentenceCase(field)+":"):AbsCell.Null);
199                                                 String user = values.get(i);
200                                                 AbsCell userCell = (user.endsWith(CSP_ATT_COM)?
201                                                                 new RefCell(user,WEBPHONE + user.substring(0,user.indexOf('@')),true):new TextCell(user));
202                                                 rv.add(new AbsCell[] {
203                                                                 label, 
204                                                                 userCell
205                                                 });
206                                         }
207                                         break;
208                                 case ROLES:
209                                         for (int i=0; i< values.size(); i++) {
210                                                 String role = values.get(i);
211                                                 AbsCell label = (i==0?new TextCell(sentenceCase(field)+":"):AbsCell.Null);
212                                                 rv.add(new AbsCell[] {
213                                                                 label,
214                                                                 new RefCell(role,RoleDetail.HREF+"?role="+role+"&ns="+ns,false)
215                                                 });
216                                         }
217                                         break;
218                                 case PERMISSIONS:
219                                         for (int i=0; i< values.size(); i++) {
220                                                 AbsCell label = (i==0?new TextCell(sentenceCase(field)+":","style=width:20%"):AbsCell.Null);
221                                                 String perm = values.get(i);
222                                                 String[] fields = perm.split("\\|");
223                                                 String grantLink = gw_url  
224                                                                 + PermGrantForm.HREF
225                                                                 + "?type=" + fields[0].trim()
226                                                                 + "&amp;instance=" + fields[1].trim()
227                                                                 + "&amp;action=" + fields[2].trim();
228                                                 
229                                                 rv.add(new AbsCell[] {
230                                                                 label, 
231                                                                 new TextCell(perm,"style=width:60%;"),
232                                                                 new RefCell("Grant", grantLink,false,"class=button","style=width:20%;")
233                                                 });
234                                         }
235                                         break;
236                                 }
237
238                         }
239                 }
240
241                 private String sentenceCase(NS_FIELD field) {
242                         String sField = field.toString();
243                         return sField.substring(0, 1).toUpperCase() + sField.substring(1).toLowerCase();
244                 }
245         
246         }
247 }