AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / authz-cmd / src / main / java / org / onap / aaf / cmd / ns / List.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package org.onap.aaf.cmd.ns;\r
24 \r
25 import java.util.Collections;\r
26 import java.util.Comparator;\r
27 \r
28 import org.onap.aaf.cmd.BaseCmd;\r
29 \r
30 import org.onap.aaf.cadi.client.Future;\r
31 import org.onap.aaf.inno.env.util.Chrono;\r
32 \r
33 import aaf.v2_0.Nss;\r
34 import aaf.v2_0.Nss.Ns;\r
35 import aaf.v2_0.Nss.Ns.Attrib;\r
36 import aaf.v2_0.Perms;\r
37 import aaf.v2_0.Roles;\r
38 import aaf.v2_0.Users;\r
39 import aaf.v2_0.Users.User;\r
40 \r
41 public class List extends BaseCmd<NS> {\r
42 \r
43         public List(NS parent) {\r
44                 super(parent,"list");\r
45                 cmds.add(new ListByName(this));\r
46                 \r
47 //              TODO: uncomment when on cassandra 2.1.2 if we like cli command to get all ns's \r
48 //                              a user is admin or responsible for \r
49                 cmds.add(new ListAdminResponsible(this));\r
50                 \r
51                 cmds.add(new ListActivity(this));\r
52                 cmds.add(new ListUsers(this));\r
53                 cmds.add(new ListChildren(this));\r
54                 cmds.add(new ListNsKeysByAttrib(this));\r
55         }\r
56 \r
57         private static final String sformat = "        %-72s\n";\r
58         protected static final String kformat = "  %-72s\n";\r
59 \r
60         \r
61         public void report(Future<Nss> fp, String ... str) {\r
62                 reportHead(str);\r
63                 if(fp==null) {\r
64                         pw().println("    *** Namespace Not Found ***");\r
65                 }\r
66                 \r
67                 if(fp!=null && fp.value!=null) {\r
68                     for(Ns ns : fp.value.getNs()) {\r
69                         pw().println(ns.getName());\r
70                         if (this.aafcli.isDetailed()) {\r
71                                 pw().println("    Description");\r
72                                 pw().format(sformat,ns.getDescription()==null?"":ns.getDescription());\r
73                         }\r
74                         if(ns.getAdmin().size()>0) {\r
75                                 pw().println("    Administrators");\r
76                                 for(String admin : ns.getAdmin()) {\r
77                                         pw().format(sformat,admin);\r
78                                 }\r
79                         }\r
80                         if(ns.getResponsible().size()>0) {\r
81                                 pw().println("    Responsible Parties");\r
82                                 for(String responsible : ns.getResponsible()) {\r
83                                         pw().format(sformat,responsible);\r
84                                 }\r
85                         }\r
86                         if(ns.getAttrib().size()>0) {\r
87                                 pw().println("    Namespace Attributes");\r
88                                 for(Attrib attrib : ns.getAttrib()) {\r
89                                         StringBuilder sb = new StringBuilder(attrib.getKey());\r
90                                         if(attrib.getValue()==null || attrib.getValue().length()>0) {\r
91                                                 sb.append('=');\r
92                                                 sb.append(attrib.getValue());\r
93                                         }\r
94                                         pw().format(sformat,sb.toString());\r
95                                 }\r
96                                 \r
97                         }\r
98                     }\r
99                 }\r
100         }\r
101         \r
102         public void reportName(Future<Nss> fp, String ... str) {\r
103                 reportHead(str);\r
104                 if(fp!=null && fp.value!=null) {\r
105                         java.util.List<Ns> nss = fp.value.getNs();\r
106                         Collections.sort(nss, new Comparator<Ns>() {\r
107                                 @Override\r
108                                 public int compare(Ns ns1, Ns ns2) {\r
109                                         return ns1.getName().compareTo(ns2.getName());\r
110                                 }\r
111                         });\r
112                         \r
113                         for(Ns ns : nss) {\r
114                                 pw().println(ns.getName());\r
115                                 if (this.aafcli.isDetailed() && ns.getDescription() != null) {\r
116                                     pw().println("   " + ns.getDescription());\r
117                                 }\r
118                         }\r
119                 }\r
120         }\r
121 \r
122         public void reportRole(Future<Roles> fr) {\r
123                 if(fr!=null && fr.value!=null && fr.value.getRole().size()>0) {\r
124                         pw().println("    Roles");\r
125                         for(aaf.v2_0.Role r : fr.value.getRole()) {\r
126                                 pw().format(sformat,r.getName());\r
127                         }\r
128                 }\r
129         }\r
130 \r
131         private static final String pformat = "        %-30s %-24s %-15s\n";\r
132         public void reportPerm(Future<Perms> fp) {\r
133                 if(fp!=null && fp.value!=null && fp.value.getPerm().size()>0) {\r
134                         pw().println("    Permissions");\r
135                         for(aaf.v2_0.Perm p : fp.value.getPerm()) {\r
136                                 pw().format(pformat,p.getType(),p.getInstance(),p.getAction());\r
137                         }\r
138                 }\r
139         }\r
140         \r
141         \r
142         private static final String cformat = "        %-30s %-6s %-24s\n";\r
143         public void reportCred(Future<Users> fc) {              \r
144                 if(fc!=null && fc.value!=null && fc.value.getUser().size()>0) {\r
145                         pw().println("    Credentials");\r
146                         java.util.List<User> users = fc.value.getUser();\r
147                         Collections.sort(users, new Comparator<User>() {\r
148                                 @Override\r
149                                 public int compare(User u1, User u2) {\r
150                                         return u1.getId().compareTo(u2.getId());\r
151                                 }\r
152                         });\r
153                         for(aaf.v2_0.Users.User u : users) {\r
154                                 if (this.aafcli.isTest()) {\r
155                                     pw().format(sformat,u.getId());\r
156                                 } else {\r
157                                         String type;\r
158                                         switch(u.getType()) {\r
159                                                 case 1:   type = "U/P"; break;\r
160                                                 case 10:  type="Cert"; break;\r
161                                                 case 200: type="x509"; break;\r
162                                                 default:  type = "";\r
163                                         }\r
164                                         pw().format(cformat,u.getId(),type,Chrono.niceDateStamp(u.getExpires()));\r
165                                 }\r
166                         }\r
167                 }\r
168         }\r
169 \r
170 }\r