2306a1a53c8b5be1498c5a732aeaaa35f9efa7f2
[aaf/authz.git] / auth / auth-gui / src / main / java / org / onap / aaf / auth / gui / pages / NsInfoForm.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 static org.onap.aaf.misc.xgen.html.HTMLGen.A;
25 import static org.onap.aaf.misc.xgen.html.HTMLGen.TABLE;
26
27 import java.io.IOException;
28 import java.net.ConnectException;
29 import java.util.List;
30
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.NamedCode;
35 import org.onap.aaf.auth.gui.Page;
36 import org.onap.aaf.cadi.CadiException;
37 import org.onap.aaf.cadi.client.Future;
38 import org.onap.aaf.cadi.client.Rcli;
39 import org.onap.aaf.cadi.client.Retryable;
40 import org.onap.aaf.misc.env.APIException;
41 import org.onap.aaf.misc.env.Env;
42 import org.onap.aaf.misc.env.Slot;
43 import org.onap.aaf.misc.env.TimeTaken;
44 import org.onap.aaf.misc.xgen.Cache;
45 import org.onap.aaf.misc.xgen.DynamicCode;
46 import org.onap.aaf.misc.xgen.Mark;
47 import org.onap.aaf.misc.xgen.html.HTMLGen;
48
49 import aaf.v2_0.Nss;
50 import aaf.v2_0.Nss.Ns;
51
52 public class NsInfoForm extends Page {
53
54     // Package on purpose
55     static final String HREF = "/gui/onboard";
56     static final String NAME = "Onboarding";
57     static final String fields[] = {"ns","description","mots","owners","admins"};
58     
59     public NsInfoForm(final AAF_GUI gui, final Page ... breadcrumbs) throws APIException, IOException {
60         super(gui.env,NAME,HREF, fields,
61             new BreadCrumbs(breadcrumbs),
62             new NamedCode(true,"content") {
63
64             private final Slot sID = gui.env.slot(NsInfoForm.NAME+'.'+NsInfoForm.fields[0]);
65             @Override
66             public void code(final Cache<HTMLGen> cache, final HTMLGen hgen) throws APIException, IOException {
67                 // p tags not closing right using .p() - causes issues in IE8 password form - so using leaf for the moment
68                 hgen.leaf(HTMLGen.H2).text("Namespace Info").end()
69                      .leaf("p").text("Hover over Fields for Tool Tips, or click ")
70                          .leaf(A,"href="+gui.env.getProperty(AAF_URL_GUI_ONBOARD,"")).text("Here").end()
71                          .text(" for more information")
72                      .end()
73                     .incr("form","method=post");
74                 Mark table = new Mark(TABLE);
75                 hgen.incr(table);
76                 cache.dynamic(hgen, new DynamicCode<HTMLGen, AAF_GUI, AuthzTrans>() {
77                     @SuppressWarnings("unchecked")
78                     @Override
79                     public void code(final AAF_GUI gui, final AuthzTrans trans, final Cache<HTMLGen> cache, final HTMLGen hgen)    throws APIException, IOException {
80                         final String incomingID= trans.get(sID, "");
81                         final String[] info = new String[fields.length];
82                         final Object own_adm[] = new Object[2]; 
83                         for (int i=0;i<info.length;++i) {
84                             info[i]="";
85                         }
86                         if (incomingID.length()>0) {
87                             TimeTaken tt = trans.start("AAF Namespace Info",Env.REMOTE);
88                             try {
89                                 gui.clientAsUser(trans.getUserPrincipal(), new Retryable<Void>() {
90                                     @Override
91                                     public Void code(Rcli<?> client) throws CadiException, ConnectException, APIException {
92                                         Future<Nss> fn = client.read("/authz/nss/"+incomingID,gui.getDF(Nss.class));
93                                         if (fn.get(AAF_GUI.TIMEOUT)) {
94                                             for (Ns ns : fn.value.getNs()) {
95                                                 info[0]=ns.getName();
96                                                 info[1]=ns.getDescription();
97                                                 for (Ns.Attrib attr: ns.getAttrib()) {
98                                                     switch(attr.getKey()) {
99                                                         case "mots":
100                                                             info[2]=attr.getValue();
101                                                         default:
102                                                     }
103                                                 }
104                                                 own_adm[0]=ns.getResponsible();
105                                                 own_adm[1]=ns.getAdmin();
106                                             }
107                                         } else {
108                                             trans.error().log(fn.body());
109                                         }
110                                         return null;
111                                     }
112                                 });
113                             } catch (Exception e) {
114                                 trans.error().log("Unable to access AAF for NS Info",incomingID);
115                                 e.printStackTrace();
116                             } finally {
117                                 tt.done();
118                             }
119                         }
120                         hgen.input(fields[0],"Namespace",false,"value="+info[0],"title=AAF Namespace")
121                             .input(fields[1],"Description*",true,"value="+info[1],"title=Full Application Name, Tool Name or Group")
122                             .input(fields[2],"MOTS ID",false,"value="+info[2],"title=MOTS ID if this is an Application, and has MOTS");
123                         Mark endTD = new Mark(),endTR=new Mark();
124                         // Owners
125                         hgen.incr(endTR,HTMLGen.TR)
126                                 .incr(endTD,HTMLGen.TD)
127                                     .leaf("label","for="+fields[3]).text("Responsible Party")
128                                 .end(endTD)
129                                 .incr(endTD,HTMLGen.TD)
130                                     .tagOnly("input","id="+fields[3],"title=Owner of App, must be an Non-Bargained Employee");
131                                     if (own_adm[0]!=null) {
132                                         for (String s : (List<String>)own_adm[0]) {
133                                             hgen.incr("label",true).text(s).end();
134                                         }
135                                     }
136                             hgen.end(endTR);
137
138                             // Admins
139                             hgen.incr(endTR,HTMLGen.TR)
140                                 .incr(endTD,HTMLGen.TD)
141                                     .leaf("label","for="+fields[4]).text("Administrators")
142                                 .end(endTD)
143                                 .incr(endTD,HTMLGen.TD)
144                                     .tagOnly("input","id="+fields[4],"title=Admins may be employees, contractors or mechIDs");
145                                     if (own_adm[1]!=null) {
146                                         for (String s : (List<String>)own_adm[1]) {
147                                             hgen.incr(HTMLGen.P,true).text(s).end();
148                                         }
149                                     }
150                                 hgen.end(endTR)
151                         .end();
152                     }
153                 });
154                 hgen.end();
155                 hgen.tagOnly("input", "type=submit", "value=Submit")
156                     .end();
157
158             }
159         });
160     }
161
162 }