4a5a940a211ddb35dd562970a6a9788e1cfcb112
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / pages / PermDetail.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.net.ConnectException;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import org.eclipse.jetty.http.HttpStatus;
30 import org.onap.aaf.auth.env.AuthzEnv;
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.misc.env.APIException;
47 import org.onap.aaf.misc.env.Env;
48 import org.onap.aaf.misc.env.Slot;
49 import org.onap.aaf.misc.env.TimeTaken;
50
51 import aaf.v2_0.Perm;
52 import aaf.v2_0.Perms;
53
54 /**
55  * Detail Page for Permissions
56  * 
57  * @author Jonathan
58  *
59  */
60 public class PermDetail extends Page {
61     public static final String HREF = "/gui/permdetail";
62     public static final String NAME = "PermDetail";
63     private static final String BLANK = "";
64
65     public PermDetail(final AAF_GUI gui, Page ... breadcrumbs) throws APIException, IOException {
66         super(gui.env, NAME, HREF, new String[] {"type","instance","action"},
67                 new BreadCrumbs(breadcrumbs),
68                 new Table<AAF_GUI,AuthzTrans>("Permission Details",gui.env.newTransNoAvg(),new Model(gui.env),"class=detail")
69                 );
70     }
71
72     /**
73      * Implement the table content for Permissions Detail
74      * 
75      * @author Jonathan
76      *
77      */
78     private static class Model extends TableData<AAF_GUI,AuthzTrans> {
79         private Slot type, instance, action;
80         public Model(AuthzEnv env) {
81             type = env.slot(NAME+".type");
82             instance = env.slot(NAME+".instance");
83             action = env.slot(NAME+".action");
84         }
85
86         public Cells get(final AuthzTrans trans, final AAF_GUI gui) {
87             final String pType = trans.get(type, null);
88             final String pInstance = trans.get(instance, null);
89             final String pAction = trans.get(action, null);
90             Validator v = new Validator();
91             v.permTypeWithUser(trans.user(),pType)
92              .permInstance(pInstance)
93              .permAction(pAction);
94             
95             if (v.err()) {
96                 trans.warn().printf("Error in PermDetail Request: %s", v.errs());
97                 return Cells.EMPTY;
98             }
99             final ArrayList<AbsCell[]> rv = new ArrayList<>();
100             rv.add(new AbsCell[]{new TextCell("Type:"),new TextCell(pType)});
101             rv.add(new AbsCell[]{new TextCell("Instance:"),new TextCell(pInstance)});
102             rv.add(new AbsCell[]{new TextCell("Action:"),new TextCell(pAction)});
103             try {
104                 gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
105                     @Override
106                     public Void code(Rcli<?> client)throws CadiException, ConnectException, APIException {
107                         TimeTaken tt = trans.start("AAF Perm Details",Env.REMOTE);
108                         try {
109                             Future<Perms> fp= client.read("/authz/perms/"+pType + '/' + pInstance + '/' + pAction,gui.getDF(Perms.class));
110                     
111                             if (fp.get(AAF_GUI.TIMEOUT)) {
112                                 tt.done();
113                                 tt = trans.start("Load Data", Env.SUB);
114                                 List<Perm> ps = fp.value.getPerm();
115                                 if (!ps.isEmpty()) {
116                                     Perm perm = fp.value.getPerm().get(0);
117                                     String desc = (perm.getDescription()!=null?perm.getDescription():BLANK);
118                                     rv.add(new AbsCell[]{new TextCell("Description:"),new TextCell(desc)});
119                                     boolean first=true;
120                                     for (String r : perm.getRoles()) {
121                                         if (first){
122                                             first=false;
123                                             rv.add(new AbsCell[] {
124                                                     new TextCell("Associated Roles:"),
125                                                     new TextCell(r)
126                                                 });
127                                         } else {
128                                             rv.add(new AbsCell[] {
129                                                 AbsCell.Null,
130                                                 new TextCell(r)
131                                             });
132                                         }
133                                     }
134                                 }
135                                 String historyLink = PermHistory.HREF 
136                                         + "?type=" + pType + "&instance=" + pInstance + "&action=" + pAction;
137                                 
138                                 rv.add(new AbsCell[] {new RefCell("See History",historyLink,false,"class=greenbutton")});
139                             } else {
140                                 rv.add(new AbsCell[] {new TextCell(
141                                     fp.code()==HttpStatus.NOT_FOUND_404?
142                                         "*** Implicit Permission ***":
143                                         "*** Data Unavailable ***"
144                                         )});
145                             }
146                         } finally {
147                             tt.done();
148                         }
149
150                         return null;
151                     }
152                 });
153             } catch (Exception e) {
154                 e.printStackTrace();
155             }
156             return new Cells(rv,null);
157         }
158     }
159 }        
160