AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / pages / RoleDetailAction.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 import java.util.Map;
29 import java.util.Map.Entry;
30
31 import javax.servlet.http.HttpServletRequest;
32
33 import org.onap.aaf.auth.env.AuthzTrans;
34 import org.onap.aaf.auth.gui.AAF_GUI;
35 import org.onap.aaf.auth.gui.BreadCrumbs;
36 import org.onap.aaf.auth.gui.NamedCode;
37 import org.onap.aaf.auth.gui.Page;
38 import org.onap.aaf.auth.gui.table.TableData;
39 import org.onap.aaf.cadi.CadiException;
40 import org.onap.aaf.cadi.client.Future;
41 import org.onap.aaf.cadi.client.Rcli;
42 import org.onap.aaf.cadi.client.Retryable;
43 import org.onap.aaf.cadi.util.Split;
44 import org.onap.aaf.misc.env.APIException;
45 import org.onap.aaf.misc.env.Env;
46 import org.onap.aaf.misc.env.Slot;
47 import org.onap.aaf.misc.env.TimeTaken;
48 import org.onap.aaf.misc.xgen.Cache;
49 import org.onap.aaf.misc.xgen.DynamicCode;
50 import org.onap.aaf.misc.xgen.html.HTMLGen;
51
52 import aaf.v2_0.Pkey;
53 import aaf.v2_0.RolePermRequest;
54 import aaf.v2_0.RoleRequest;
55
56 public class RoleDetailAction extends Page {
57         public RoleDetailAction(final AAF_GUI gui, final Page ... breadcrumbs) throws APIException, IOException {
58                 super(gui.env,RoleDetail.NAME, RoleDetail.HREF, TableData.headers,
59                         new BreadCrumbs(breadcrumbs),
60                         new NamedCode(true,"content") {
61                                 final Slot sReq = gui.env.slot(AAF_GUI.HTTP_SERVLET_REQUEST);
62                                 
63                                 @Override
64                                 public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
65                                         cache.dynamic(hgen, new DynamicCode<HTMLGen,AAF_GUI, AuthzTrans>() {
66                                                 @Override
67                                                 public void code(final AAF_GUI gui, final AuthzTrans trans,final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
68                                                         final HttpServletRequest req = trans.get(sReq, null);
69                                                         final String role = getSingleParam(req,"role");
70                                                         if(role==null) {
71                                                                 hgen.text("Parameter 'role' is required").end(); 
72                                                         } else {
73                                                                 // Run Validations
74 //                                                              boolean fail;
75                                                                 try {
76                                                                         /*fail =*/ gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Boolean>() {
77                                                                                 @Override
78                                                                                 public Boolean code(Rcli<?> client) throws CadiException, ConnectException, APIException {
79                                                                                         List<TypedFuture> ltf = new ArrayList<TypedFuture>();
80                                                                                         String text;
81                                                                                         Map<String, String[]> pm = (Map<String, String[]>)req.getParameterMap();
82                                                                                         for(final Entry<String, String[]> es : pm.entrySet()) {
83                                                                                                 for(final String v : es.getValue()) {
84                                                                                                         TimeTaken tt = null; 
85                                                                                                         try {
86                                                                                                                 switch(es.getKey()) {
87                                                                                                                         case "desc": // Check box set
88                                                                                                                                 String desc = getSingleParam(req, "description");
89                                                                                                                                 if(desc!=null) {
90                                                                                                                                         text = "Setting Description on " + role + " to " + desc;
91                                                                                                                                         tt = trans.start(text, Env.REMOTE);
92                                                                                                                                         RoleRequest rr = new RoleRequest();
93                                                                                                                                         rr.setName(role);
94                                                                                                                                         rr.setDescription(desc);
95                                                                                                                                         ltf.add(new TypedFuture(ActionType.desc, text, 
96                                                                                                                                                         client.update("/authz/role",
97                                                                                                                                                                         gui.getDF(RoleRequest.class),rr
98                                                                                                                                                 )));
99                                                                                                                                 }
100                                                                                                                                 break;
101                                                                                                                         case "perm.ungrant":
102                                                                                                                                 text = "Ungranting Permission '" + v + "' from '" + role + '\'';
103                                                                                                                                 tt = trans.start(text, Env.REMOTE);
104                                                                                                                                 String[] pf = Split.splitTrim('|', v);
105                                                                                                                                 if(pf.length==3) {
106                                                                                                                                         Pkey perm = new Pkey();
107                                                                                                                                         perm.setType(pf[0]);
108                                                                                                                                         perm.setInstance(pf[1]);
109                                                                                                                                         perm.setAction(pf[2]);
110                                                                                                                                         RolePermRequest rpr = new RolePermRequest();
111                                                                                                                                         rpr.setPerm(perm);
112                                                                                                                                         rpr.setRole(role);
113                                                                                                                                         ltf.add(new TypedFuture(ActionType.ungrant,text,
114                                                                                                                                                         client.delete("/authz/role/" + role + "/perm", 
115                                                                                                                                                                 gui.getDF(RolePermRequest.class),rpr
116                                                                                                                                                         )));
117                                                                                                                                 } else {
118                                                                                                                                         hgen.p(v + " is not a valid Perm for ungranting");
119                                                                                                                                 }
120                                                                                                                                 break;
121                                                                                                                         case "userrole.extend":
122                                                                                                                                 text = "Extending " + v + " in " + role;
123                                                                                                                                 tt = trans.start(text, Env.REMOTE);
124                                                                                                                                 ltf.add(new TypedFuture(ActionType.extendUR,text,
125                                                                                                                                                 client.update("/authz/userRole/extend/" + v + '/' + role)));
126                                                                                                                                 break;
127                                                                                                                         case "userrole.delete":
128                                                                                                                                 text = "Deleting " + v + " from " + role;
129                                                                                                                                 tt = trans.start(text, Env.REMOTE);
130                                                                                                                                 ltf.add(new TypedFuture(ActionType.deleteUR,text,
131                                                                                                                                                 client.delete("/authz/userRole/" + v + '/' + role, Void.class)));
132                                                                                                                                 break;
133
134                                                                                                                         default:
135 //                                                                                                                              System.out.println(es.getKey() + "=" + v);
136                                                                                                                 }
137                                                                                                         } finally {
138                                                                                                                 if(tt!=null) {
139                                                                                                                         tt.done();
140                                                                                                                         tt=null;
141                                                                                                                 }
142                                                                                                         }
143                                                                                                 }
144                                                                                         }
145                                                                                         
146                                                                                         if(ltf.isEmpty()) {
147                                                                                                 hgen.p("No Changes");
148                                                                                         } else {
149                                                                                                 for(TypedFuture tf : ltf) {
150                                                                                                         if(tf.future.get(5000)) {
151                                                                                                                 hgen.p("<font color=\"green\"><i>Success</i>:</font> " + tf.text);
152                                                                                                         } else {
153                                                                                                                 // Note: if handling of special Error codes is required, use 
154                                                                                                                 // switch(tf.type) {
155                                                                                                                 // }
156                                                                                                                 hgen.p(tf.text);
157                                                                                                                 gui.writeError(trans, tf.future, hgen,4);
158                                                                                                         }
159                                                                                                 }
160                                                                                         }
161                                                                                         return true;
162                                                                                 }
163                                                                         });
164                                                                 } catch (Exception e) {
165                                                                         hgen.p("Unknown Error");
166                                                                         e.printStackTrace();
167                                                                 }
168                                                         }
169                                                 }
170
171                                         });
172                                 }
173                         });
174         }
175         
176         enum ActionType {desc, ungrant, deleteUR, extendUR};
177         private static class TypedFuture {
178 //              public final ActionType type;
179                 public final Future<?> future;
180                 public final String text;
181                 
182                 public TypedFuture(ActionType type, String text, Future<?> future) {
183 //                      this.type = type;
184                         this.future = future;
185                         this.text = text;
186                 }
187         }
188 }