[AAF-21] Updated Copyright Headers for AAF
[aaf/authz.git] / authz-cmd / src / main / java / com / att / 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 com.att.cmd.ns;\r
24 \r
25 import java.util.Collections;\r
26 import java.util.Comparator;\r
27 \r
28 import com.att.cadi.client.Future;\r
29 import com.att.cmd.BaseCmd;\r
30 import com.att.inno.env.util.Chrono;\r
31 \r
32 import aaf.v2_0.Nss;\r
33 import aaf.v2_0.Nss.Ns;\r
34 import aaf.v2_0.Nss.Ns.Attrib;\r
35 import aaf.v2_0.Perms;\r
36 import aaf.v2_0.Roles;\r
37 import aaf.v2_0.Users;\r
38 import aaf.v2_0.Users.User;\r
39 \r
40 public class List extends BaseCmd<NS> {\r
41 \r
42         public List(NS parent) {\r
43                 super(parent,"list");\r
44                 cmds.add(new ListByName(this));\r
45                 \r
46 //              TODO: uncomment when on cassandra 2.1.2 if we like cli command to get all ns's \r
47 //                              a user is admin or responsible for \r
48                 cmds.add(new ListAdminResponsible(this));\r
49                 \r
50                 cmds.add(new ListActivity(this));\r
51                 cmds.add(new ListUsers(this));\r
52                 cmds.add(new ListChildren(this));\r
53                 cmds.add(new ListNsKeysByAttrib(this));\r
54         }\r
55 \r
56         private static final String sformat = "        %-72s\n";\r
57         protected static final String kformat = "  %-72s\n";\r
58 \r
59         \r
60         public void report(Future<Nss> fp, String ... str) {\r
61                 reportHead(str);\r
62                 if(fp==null) {\r
63                         pw().println("    *** Namespace Not Found ***");\r
64                 }\r
65                 \r
66                 if(fp!=null && fp.value!=null) {\r
67                     for(Ns ns : fp.value.getNs()) {\r
68                         pw().println(ns.getName());\r
69                         if (this.aafcli.isDetailed()) {\r
70                                 pw().println("    Description");\r
71                                 pw().format(sformat,ns.getDescription()==null?"":ns.getDescription());\r
72                         }\r
73                         if(ns.getAdmin().size()>0) {\r
74                                 pw().println("    Administrators");\r
75                                 for(String admin : ns.getAdmin()) {\r
76                                         pw().format(sformat,admin);\r
77                                 }\r
78                         }\r
79                         if(ns.getResponsible().size()>0) {\r
80                                 pw().println("    Responsible Parties");\r
81                                 for(String responsible : ns.getResponsible()) {\r
82                                         pw().format(sformat,responsible);\r
83                                 }\r
84                         }\r
85                         if(ns.getAttrib().size()>0) {\r
86                                 pw().println("    Namespace Attributes");\r
87                                 for(Attrib attrib : ns.getAttrib()) {\r
88                                         StringBuilder sb = new StringBuilder(attrib.getKey());\r
89                                         if(attrib.getValue()==null || attrib.getValue().length()>0) {\r
90                                                 sb.append('=');\r
91                                                 sb.append(attrib.getValue());\r
92                                         }\r
93                                         pw().format(sformat,sb.toString());\r
94                                 }\r
95                                 \r
96                         }\r
97                     }\r
98                 }\r
99         }\r
100         \r
101         public void reportName(Future<Nss> fp, String ... str) {\r
102                 reportHead(str);\r
103                 if(fp!=null && fp.value!=null) {\r
104                         java.util.List<Ns> nss = fp.value.getNs();\r
105                         Collections.sort(nss, new Comparator<Ns>() {\r
106                                 @Override\r
107                                 public int compare(Ns ns1, Ns ns2) {\r
108                                         return ns1.getName().compareTo(ns2.getName());\r
109                                 }\r
110                         });\r
111                         \r
112                         for(Ns ns : nss) {\r
113                                 pw().println(ns.getName());\r
114                                 if (this.aafcli.isDetailed() && ns.getDescription() != null) {\r
115                                     pw().println("   " + ns.getDescription());\r
116                                 }\r
117                         }\r
118                 }\r
119         }\r
120 \r
121         public void reportRole(Future<Roles> fr) {\r
122                 if(fr!=null && fr.value!=null && fr.value.getRole().size()>0) {\r
123                         pw().println("    Roles");\r
124                         for(aaf.v2_0.Role r : fr.value.getRole()) {\r
125                                 pw().format(sformat,r.getName());\r
126                         }\r
127                 }\r
128         }\r
129 \r
130         private static final String pformat = "        %-30s %-24s %-15s\n";\r
131         public void reportPerm(Future<Perms> fp) {\r
132                 if(fp!=null && fp.value!=null && fp.value.getPerm().size()>0) {\r
133                         pw().println("    Permissions");\r
134                         for(aaf.v2_0.Perm p : fp.value.getPerm()) {\r
135                                 pw().format(pformat,p.getType(),p.getInstance(),p.getAction());\r
136                         }\r
137                 }\r
138         }\r
139         \r
140         \r
141         private static final String cformat = "        %-30s %-6s %-24s\n";\r
142         public void reportCred(Future<Users> fc) {              \r
143                 if(fc!=null && fc.value!=null && fc.value.getUser().size()>0) {\r
144                         pw().println("    Credentials");\r
145                         java.util.List<User> users = fc.value.getUser();\r
146                         Collections.sort(users, new Comparator<User>() {\r
147                                 @Override\r
148                                 public int compare(User u1, User u2) {\r
149                                         return u1.getId().compareTo(u2.getId());\r
150                                 }\r
151                         });\r
152                         for(aaf.v2_0.Users.User u : users) {\r
153                                 if (this.aafcli.isTest()) {\r
154                                     pw().format(sformat,u.getId());\r
155                                 } else {\r
156                                         String type;\r
157                                         switch(u.getType()) {\r
158                                                 case 1:   type = "U/P"; break;\r
159                                                 case 10:  type="Cert"; break;\r
160                                                 case 200: type="x509"; break;\r
161                                                 default:  type = "";\r
162                                         }\r
163                                         pw().format(cformat,u.getId(),type,Chrono.niceDateStamp(u.getExpires()));\r
164                                 }\r
165                         }\r
166                 }\r
167         }\r
168 \r
169 }\r