1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright © 2021 Orange.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 *******************************************************************************/
25 package org.onap.dmaap.mr.tools;
27 import com.att.nsa.apiClient.http.HttpException;
28 import com.att.nsa.apiClient.http.HttpObjectNotFoundException;
29 import com.att.nsa.cmdtool.Command;
30 import com.att.nsa.cmdtool.CommandNotReadyException;
31 import java.io.IOException;
32 import java.io.PrintStream;
34 import org.onap.dmaap.mr.client.MRClientFactory;
35 import org.onap.dmaap.mr.client.MRTopicManager;
36 import org.onap.dmaap.mr.client.MRTopicManager.TopicInfo;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class TopicCommand implements Command<MRCommandContext> {
41 final Logger logger = LoggerFactory.getLogger(TopicCommand.class);
43 private static final String REQUEST_ERROR_MESSAGE = "Problem with request: ";
44 private static final String IOEXCEPTION_MESSAGE = "IOException: ";
45 private static final String HTTP_EXCEPTION_MESSAGE = "HttpException: ";
48 public String[] getMatches() {
51 "topic (list) (\\S*)",
52 "topic (create) (\\S*) (\\S*) (\\S*)",
53 "topic (grant|revoke) (read|write) (\\S*) (\\S*)",
58 public void checkReady(MRCommandContext context) throws CommandNotReadyException {
59 if (!context.checkClusterReady()) {
60 throw new CommandNotReadyException("Use 'cluster' to specify a cluster to use.");
65 public void execute(String[] parts, MRCommandContext context, PrintStream out) throws CommandNotReadyException {
66 final MRTopicManager tm = MRClientFactory.createTopicManager(context.getCluster(), context.getApiKey(), context.getApiPwd());
67 context.applyTracer(tm);
73 if (parts.length == 1) {
74 for (String topic : tm.getTopics()) {
78 final TopicInfo ti = tm.getTopicMetadata(parts[1]);
80 final String owner = ti.getOwner();
81 out.println(" owner: " + (owner == null ? "<none>" : owner));
83 final String desc = ti.getDescription();
84 out.println("description: " + (desc == null ? "<none>" : desc));
86 final Set<String> prods = ti.getAllowedProducers();
88 out.println(" write ACL: ");
89 for (String key : prods) {
90 out.println("\t" + key);
93 out.println(" write ACL: <not active>");
96 final Set<String> cons = ti.getAllowedConsumers();
98 out.println(" read ACL: ");
99 for (String key : cons) {
100 out.println("\t" + key);
103 out.println(" read ACL: <not active>");
106 } catch (IOException x) {
107 out.println(REQUEST_ERROR_MESSAGE + x.getMessage());
108 logger.error(IOEXCEPTION_MESSAGE, x);
109 } catch (HttpObjectNotFoundException e) {
110 out.println("Not found: " + e.getMessage());
111 logger.error("HttpObjectNotFoundException: ", e);
116 final int partitions = Integer.parseInt(parts[2]);
117 final int replicas = Integer.parseInt(parts[3]);
119 tm.createTopic(parts[1], "", partitions, replicas);
120 } catch (HttpException e) {
121 out.println(REQUEST_ERROR_MESSAGE + e.getMessage());
122 logger.error(HTTP_EXCEPTION_MESSAGE, e);
123 } catch (IOException e) {
124 out.println(REQUEST_ERROR_MESSAGE + e.getMessage());
125 logger.error(IOEXCEPTION_MESSAGE, e);
126 } catch (NumberFormatException e) {
127 out.println(REQUEST_ERROR_MESSAGE + e.getMessage());
128 logger.error("NumberFormatException: ", e);
133 if (parts[1].equals("write")) {
134 tm.allowProducer(parts[2], parts[3]);
135 } else if (parts[1].equals("read")) {
136 tm.allowConsumer(parts[2], parts[3]);
138 } catch (HttpException e) {
139 out.println(REQUEST_ERROR_MESSAGE + e.getMessage());
140 logger.error(HTTP_EXCEPTION_MESSAGE, e);
141 } catch (IOException e) {
142 out.println(REQUEST_ERROR_MESSAGE + e.getMessage());
143 logger.error(IOEXCEPTION_MESSAGE, e);
148 if (parts[1].equals("write")) {
149 tm.revokeProducer(parts[2], parts[3]);
150 } else if (parts[1].equals("read")) {
151 tm.revokeConsumer(parts[2], parts[3]);
153 } catch (HttpException e) {
154 out.println(REQUEST_ERROR_MESSAGE + e.getMessage());
155 logger.error(HTTP_EXCEPTION_MESSAGE, e);
156 } catch (IOException e) {
157 out.println(REQUEST_ERROR_MESSAGE + e.getMessage());
158 logger.error(IOEXCEPTION_MESSAGE, e);
162 throw new CommandNotReadyException("The command " + parts[0] + " is not available");
170 public void displayHelp(PrintStream out) {
171 out.println("topic list");
172 out.println("topic list <topicName>");
173 out.println("topic create <topicName> <partitions> <replicas>");
174 out.println("topic grant write|read <topicName> <apiKey>");
175 out.println("topic revoke write|read <topicName> <apiKey>");