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