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