AT&T 2.0.19 Code drop, stage 4
[aaf/authz.git] / authz-gui / src / main / java / com / att / authz / gui / pages / PermGrantAction.java
1 /*******************************************************************************
2  * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
3  *******************************************************************************/
4 package com.att.authz.gui.pages;
5
6 import java.io.IOException;
7 import java.net.ConnectException;
8
9 import com.att.authz.env.AuthzTrans;
10 import com.att.authz.gui.AuthGUI;
11 import com.att.authz.gui.BreadCrumbs;
12 import com.att.authz.gui.NamedCode;
13 import com.att.authz.gui.Page;
14 import org.onap.aaf.cadi.CadiException;
15 import org.onap.aaf.cadi.client.Future;
16 import org.onap.aaf.cadi.client.Rcli;
17 import org.onap.aaf.cadi.client.Retryable;
18 import org.onap.aaf.inno.env.APIException;
19 import org.onap.aaf.inno.env.Env;
20 import org.onap.aaf.inno.env.Slot;
21 import org.onap.aaf.inno.env.TimeTaken;
22 import com.att.xgen.Cache;
23 import com.att.xgen.DynamicCode;
24 import com.att.xgen.html.HTMLGen;
25
26 import aaf.v2_0.Pkey;
27 import aaf.v2_0.RolePermRequest;
28
29 public class PermGrantAction extends Page {
30         
31         
32         public PermGrantAction(final AuthGUI gui, final Page ... breadcrumbs) throws APIException, IOException {
33                 super(gui.env,PermGrantForm.NAME, PermGrantForm.HREF, PermGrantForm.fields,
34                         new BreadCrumbs(breadcrumbs),
35                         new NamedCode(true,"content") {
36                                 final Slot sType = gui.env.slot(PermGrantForm.NAME+'.'+PermGrantForm.fields[0]);
37                                 final Slot sInstance = gui.env.slot(PermGrantForm.NAME+'.'+PermGrantForm.fields[1]);
38                                 final Slot sAction = gui.env.slot(PermGrantForm.NAME+'.'+PermGrantForm.fields[2]);
39                                 final Slot sRole = gui.env.slot(PermGrantForm.NAME+'.'+PermGrantForm.fields[3]);
40                                 
41                                 @Override
42                                 public void code(Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
43                                         cache.dynamic(hgen, new DynamicCode<HTMLGen,AuthGUI, AuthzTrans>() {
44                                                 @Override
45                                                 public void code(final AuthGUI gui, final AuthzTrans trans,Cache<HTMLGen> cache, HTMLGen hgen) throws APIException, IOException {
46
47                                                         String type = trans.get(sType,null);
48                                                         String instance = trans.get(sInstance,null);
49                                                         String action = trans.get(sAction,null);
50                                                         String role = trans.get(sRole,null);
51                                                         
52                                                         String lastPage = PermGrantForm.HREF 
53                                                                         + "?type=" + type + "&instance=" + instance + "&action=" + action;
54                                                         
55                                                         // Run Validations
56                                                         boolean fail = true;
57                                                 
58                                                         TimeTaken tt = trans.start("AAF Grant Permission to Role",Env.REMOTE);
59                                                         try {
60                                                                 
61                                                                 final RolePermRequest grantReq = new RolePermRequest();
62                                                                 Pkey pkey = new Pkey();
63                                                                 pkey.setType(type);
64                                                                 pkey.setInstance(instance);
65                                                                 pkey.setAction(action);
66                                                                 grantReq.setPerm(pkey);
67                                                                 grantReq.setRole(role);
68                                                                 
69                                                                 fail = gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Boolean>() {
70                                                                         @Override
71                                                                         public Boolean code(Rcli<?> client) throws CadiException, ConnectException, APIException {
72                                                                                 boolean fail = true;
73                                                                                 Future<RolePermRequest> fgrant = client.create(
74                                                                                                 "/authz/role/perm",
75                                                                                                 gui.rolePermReqDF,
76                                                                                                 grantReq
77                                                                                                 );
78
79                                                                                 if(fgrant.get(5000)) {
80                                                                                         hgen.p("Permission has been granted to role.");
81                                                                                         fail = false;
82                                                                                 } else {
83                                                                                         if (202==fgrant.code()) {
84                                                                                                 hgen.p("Permission Grant Request sent, but must be Approved before actualizing");
85                                                                                                 fail = false;
86                                                                                         } else {
87                                                                                                 gui.writeError(trans, fgrant, hgen);
88                                                                                         }
89                                                                                 }
90                                                                                 return fail;
91                                                                         }
92                                                                 });
93                                                         } catch (Exception e) {
94                                                                 hgen.p("Unknown Error");
95                                                                 e.printStackTrace();
96                                                         } finally {
97                                                                 tt.done();
98                                                         }
99                                                                 
100                                                         hgen.br();
101                                                         hgen.incr("a",true,"href="+lastPage);
102                                                         if (fail) {
103                                                                 hgen.text("Try again");
104                                                         } else {
105                                                                 hgen.text("Grant this Permission to Another Role");
106                                                         }
107                                                         hgen.end();
108                                                         hgen.js()
109                                                                 .text("alterLink('permgrant', '"+lastPage + "');")                                                      
110                                                                 .done();
111
112                                                 }
113                                         });
114                                 }
115                         });
116         }
117 }