2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.gui.pages;
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;
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;
54 import aaf.v2_0.Nss.Ns;
56 import aaf.v2_0.Perms;
58 import aaf.v2_0.Roles;
60 public class NsDetail extends Page {
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;
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")
77 keySlot = gui.env.slot(NAME+".ns");
78 gw_url = gui.env.getProperty(Config.GW_URL);
82 gw_url+="/aaf/"+Config.AAF_DEFAULT_API_VERSION;
87 * Implement the table content for Namespace Detail
92 private static class Model extends TableData<AAF_GUI,AuthzTrans> {
95 public void set(NsDetail nsDetail) {
100 public Cells get(final AuthzTrans trans, final AAF_GUI gui) {
101 final String nsName = trans.get(keySlot, null);
102 Validator v = new Validator();
105 trans.warn().printf("Error in NsDetail Request: %s", v.errs());
112 final ArrayList<AbsCell[]> rv = new ArrayList<>();
113 rv.add(new AbsCell[]{new TextCell("Name:"),new TextCell(nsName)});
115 final TimeTaken tt = trans.start("AAF Namespace Details",Env.REMOTE);
117 gui.clientAsUser(trans.getUserPrincipal(),new Retryable<Void>() {
119 public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
120 Future<Nss> fn = client.read("/authz/nss/"+nsName,gui.getDF(Nss.class));
122 if (fn.get(AAF_GUI.TIMEOUT)) {
125 // TimeTaken tt = trans.start("Load Data", Env.SUB);
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)});
131 addField(trans, nsName, rv, n.getAdmin(), NS_FIELD.ADMINS);
132 addField(trans, nsName, rv, n.getResponsible(), NS_FIELD.OWNERS);
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())
143 Future<Roles> fr = client.read(
144 "/authz/roles/ns/"+nsName,
145 gui.getDF(Roles.class)
147 List<String> roles = new ArrayList<>();
148 if (fr.get(AAFcli.timeout())) {
149 for (Role r : fr.value.getRole()) {
150 roles.add(r.getName());
153 addField(trans, nsName, rv, roles, NS_FIELD.ROLES);
156 Future<Perms> fp = client.read(
157 "/authz/perms/ns/"+nsName,
158 gui.getDF(Perms.class)
160 List<String> perms = new ArrayList<>();
162 if (fp.get(AAFcli.timeout())) {
163 for (Perm p : fp.value.getPerm()) {
164 perms.add(p.getType() + "|" + p.getInstance() + "|" + p.getAction());
167 addField(trans, nsName, rv, perms, NS_FIELD.PERMISSIONS);
169 String historyLink = NsHistory.HREF
171 rv.add(new AbsCell[] {new RefCell("See History",historyLink,false)});
176 rv.add(new AbsCell[] {new TextCell("*** Data Unavailable ***")});
181 } catch (Exception e) {
186 return new Cells(rv,null);
189 private void addField(AuthzTrans trans, String ns, List<AbsCell[]> rv, List<String> values, NS_FIELD field) {
190 if (!values.isEmpty()) {
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[] {
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[] {
211 new RefCell(role,RoleDetail.HREF+"?role="+role+"&ns="+ns,false)
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
222 + "?type=" + fields[0].trim()
223 + "&instance=" + fields[1].trim()
224 + "&action=" + fields[2].trim();
226 rv.add(new AbsCell[] {
228 new TextCell(perm,"style=width:60%;"),
229 new RefCell("Grant", grantLink,false,"class=button","style=width:20%;")
238 private String sentenceCase(NS_FIELD field) {
239 String sField = field.toString();
240 return sField.substring(0, 1).toUpperCase() + sField.substring(1).toLowerCase();