update the package name
[dmaap/messagerouter/dmaapclient.git] / src / main / java / org / onap / dmaap / mr / tools / TopicCommand.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 2017 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  *        http://www.apache.org/licenses/LICENSE-2.0
11  *  
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  *
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 package org.onap.dmaap.mr.tools;
23
24 import java.io.IOException;
25 import java.io.PrintStream;
26 import java.util.Set;
27
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.att.nsa.apiClient.http.HttpException;
32 import com.att.nsa.apiClient.http.HttpObjectNotFoundException;
33 import com.att.nsa.cmdtool.Command;
34 import com.att.nsa.cmdtool.CommandNotReadyException;
35 import org.onap.dmaap.mr.client.MRClientFactory;
36 import org.onap.dmaap.mr.client.MRTopicManager;
37 import org.onap.dmaap.mr.client.MRTopicManager.TopicInfo;
38
39 public class TopicCommand implements Command<MRCommandContext>
40 {
41         final Logger logger = LoggerFactory.getLogger(ApiKeyCommand.class);
42         @Override
43         public String[] getMatches ()
44         {
45                 return new String[]{
46                         "topic (list)",
47                         "topic (list) (\\S*)",
48                         "topic (create) (\\S*) (\\S*) (\\S*)",
49                         "topic (grant|revoke) (read|write) (\\S*) (\\S*)",
50                 };
51         }
52
53         @Override
54         public void checkReady ( MRCommandContext context ) throws CommandNotReadyException
55         {
56                 if ( !context.checkClusterReady () )
57                 {
58                         throw new CommandNotReadyException ( "Use 'cluster' to specify a cluster to use." );
59                 }
60         }
61
62         @Override
63         public void execute ( String[] parts, MRCommandContext context, PrintStream out ) throws CommandNotReadyException
64         {
65                 final MRTopicManager tm = MRClientFactory.createTopicManager ( context.getCluster(), context.getApiKey(), context.getApiPwd() );
66                 context.applyTracer ( tm );
67
68                 try
69                 {
70                         if ( parts[0].equals ( "list" ) )
71                         {
72                                 try
73                                 {
74                                         if ( parts.length == 1 )
75                                         {
76                                                 for ( String topic : tm.getTopics () )
77                                                 {
78                                                         out.println ( topic );
79                                                 }
80                                         }
81                                         else
82                                         {
83                                                 final TopicInfo ti = tm.getTopicMetadata ( parts[1] );
84
85                                                 final String owner = ti.getOwner ();
86                                                 out.println ( "      owner: " + ( owner == null ? "<none>" : owner ) );
87
88                                                 final String desc = ti.getDescription ();
89                                                 out.println ( "description: " + ( desc == null ? "<none>" : desc ) );
90
91                                                 final Set<String> prods = ti.getAllowedProducers ();
92                                                 if ( prods != null )
93                                                 {
94                                                         out.println ( "  write ACL: " );
95                                                         for ( String key : prods )
96                                                         {
97                                                                 out.println ( "\t" + key );
98                                                         }
99                                                 }
100                                                 else
101                                                 {
102                                                         out.println ( "  write ACL: <not active>" );
103                                                 }
104
105                                                 final Set<String> cons = ti.getAllowedConsumers ();
106                                                 if ( cons != null )
107                                                 {
108                                                         out.println ( "   read ACL: " );
109                                                         for ( String key : cons )
110                                                         {
111                                                                 out.println ( "\t" + key );
112                                                         }
113                                                 }
114                                                 else
115                                                 {
116                                                         out.println ( "   read ACL: <not active>" );
117                                                 }
118                                         }
119                                 }
120                                 catch ( IOException x )
121                                 {
122                                         out.println ( "Problem with request: " + x.getMessage () );
123                                     logger.error("IOException: ", x);
124                                 }
125                                 catch ( HttpObjectNotFoundException e )
126                                 {
127                                         out.println ( "Not found: " + e.getMessage () );
128                                     logger.error("HttpObjectNotFoundException: ", e);
129                                 }
130                         }
131                         else if ( parts[0].equals ( "create" ) )
132                         {
133                                 try
134                                 {
135                                         final int partitions = Integer.parseInt ( parts[2] );
136                                         final int replicas = Integer.parseInt ( parts[3] );
137                                         
138                                         tm.createTopic ( parts[1], "", partitions, replicas );
139                                 }
140                                 catch ( HttpException e )
141                                 {
142                                         out.println ( "Problem with request: " + e.getMessage () );
143                                     logger.error("HttpException: ", e);
144                                 }
145                                 catch ( IOException e )
146                                 {
147                                         out.println ( "Problem with request: " + e.getMessage () );
148                                     logger.error("IOException: ", e);
149                                 }
150                                 catch ( NumberFormatException e )
151                                 {
152                                         out.println ( "Problem with request: " + e.getMessage () );
153                                     logger.error("NumberFormatException: ", e);
154                                 }
155                         }
156                         else if ( parts[0].equals ( "grant" ) )
157                         {
158                                 try
159                                 {
160                                         if ( parts[1].equals ( "write" ) ) 
161                                         {
162                                                 tm.allowProducer ( parts[2], parts[3] );
163                                         }
164                                         else if ( parts[1].equals ( "read" ) )
165                                         {
166                                                 tm.allowConsumer ( parts[2], parts[3] );
167                                         }
168                                 }
169                                 catch ( HttpException e )
170                                 {
171                                         out.println ( "Problem with request: " + e.getMessage () );
172                                     logger.error("HttpException: ", e);
173                                 }
174                                 catch ( IOException e )
175                                 {
176                                         out.println ( "Problem with request: " + e.getMessage () );
177                                     logger.error("IOException: ", e);
178                                 }
179                         }
180                         else if ( parts[0].equals ( "revoke" ) )
181                         {
182                                 try
183                                 {
184                                         if ( parts[1].equals ( "write" ) ) 
185                                         {
186                                                 tm.revokeProducer ( parts[2], parts[3] );
187                                         }
188                                         else if ( parts[1].equals ( "read" ) )
189                                         {
190                                                 tm.revokeConsumer ( parts[2], parts[3] );
191                                         }
192                                 }
193                                 catch ( HttpException e )
194                                 {
195                                         out.println ( "Problem with request: " + e.getMessage () );
196                                     logger.error("HttpException: ", e);
197                                 }
198                                 catch ( IOException e )
199                                 {
200                                         out.println ( "Problem with request: " + e.getMessage () );
201                                     logger.error("IOException: ", e);
202                                 }
203                         }
204                 }
205                 finally
206                 {
207                         tm.close ();
208                 }
209         }
210
211         @Override
212         public void displayHelp ( PrintStream out )
213         {
214                 out.println ( "topic list" );
215                 out.println ( "topic list <topicName>" );
216                 out.println ( "topic create <topicName> <partitions> <replicas>" );
217                 out.println ( "topic grant write|read <topicName> <apiKey>" );
218                 out.println ( "topic revoke write|read <topicName> <apiKey>" );
219         }
220
221 }