1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
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=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package org.onap.dmaap.mr.tools;
24 import java.io.IOException;
25 import java.io.PrintStream;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
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;
39 public class TopicCommand implements Command<MRCommandContext>
41 final Logger logger = LoggerFactory.getLogger(ApiKeyCommand.class);
43 public String[] getMatches ()
47 "topic (list) (\\S*)",
48 "topic (create) (\\S*) (\\S*) (\\S*)",
49 "topic (grant|revoke) (read|write) (\\S*) (\\S*)",
54 public void checkReady ( MRCommandContext context ) throws CommandNotReadyException
56 if ( !context.checkClusterReady () )
58 throw new CommandNotReadyException ( "Use 'cluster' to specify a cluster to use." );
63 public void execute ( String[] parts, MRCommandContext context, PrintStream out ) throws CommandNotReadyException
65 final MRTopicManager tm = MRClientFactory.createTopicManager ( context.getCluster(), context.getApiKey(), context.getApiPwd() );
66 context.applyTracer ( tm );
70 if ( parts[0].equals ( "list" ) )
74 if ( parts.length == 1 )
76 for ( String topic : tm.getTopics () )
78 out.println ( topic );
83 final TopicInfo ti = tm.getTopicMetadata ( parts[1] );
85 final String owner = ti.getOwner ();
86 out.println ( " owner: " + ( owner == null ? "<none>" : owner ) );
88 final String desc = ti.getDescription ();
89 out.println ( "description: " + ( desc == null ? "<none>" : desc ) );
91 final Set<String> prods = ti.getAllowedProducers ();
94 out.println ( " write ACL: " );
95 for ( String key : prods )
97 out.println ( "\t" + key );
102 out.println ( " write ACL: <not active>" );
105 final Set<String> cons = ti.getAllowedConsumers ();
108 out.println ( " read ACL: " );
109 for ( String key : cons )
111 out.println ( "\t" + key );
116 out.println ( " read ACL: <not active>" );
120 catch ( IOException x )
122 out.println ( "Problem with request: " + x.getMessage () );
123 logger.error("IOException: ", x);
125 catch ( HttpObjectNotFoundException e )
127 out.println ( "Not found: " + e.getMessage () );
128 logger.error("HttpObjectNotFoundException: ", e);
131 else if ( parts[0].equals ( "create" ) )
135 final int partitions = Integer.parseInt ( parts[2] );
136 final int replicas = Integer.parseInt ( parts[3] );
138 tm.createTopic ( parts[1], "", partitions, replicas );
140 catch ( HttpException e )
142 out.println ( "Problem with request: " + e.getMessage () );
143 logger.error("HttpException: ", e);
145 catch ( IOException e )
147 out.println ( "Problem with request: " + e.getMessage () );
148 logger.error("IOException: ", e);
150 catch ( NumberFormatException e )
152 out.println ( "Problem with request: " + e.getMessage () );
153 logger.error("NumberFormatException: ", e);
156 else if ( parts[0].equals ( "grant" ) )
160 if ( parts[1].equals ( "write" ) )
162 tm.allowProducer ( parts[2], parts[3] );
164 else if ( parts[1].equals ( "read" ) )
166 tm.allowConsumer ( parts[2], parts[3] );
169 catch ( HttpException e )
171 out.println ( "Problem with request: " + e.getMessage () );
172 logger.error("HttpException: ", e);
174 catch ( IOException e )
176 out.println ( "Problem with request: " + e.getMessage () );
177 logger.error("IOException: ", e);
180 else if ( parts[0].equals ( "revoke" ) )
184 if ( parts[1].equals ( "write" ) )
186 tm.revokeProducer ( parts[2], parts[3] );
188 else if ( parts[1].equals ( "read" ) )
190 tm.revokeConsumer ( parts[2], parts[3] );
193 catch ( HttpException e )
195 out.println ( "Problem with request: " + e.getMessage () );
196 logger.error("HttpException: ", e);
198 catch ( IOException e )
200 out.println ( "Problem with request: " + e.getMessage () );
201 logger.error("IOException: ", e);
212 public void displayHelp ( PrintStream out )
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>" );