[DMAAP-CLIENT] First sonar issues review part2
[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  *  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
13  *
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=========================================================
20  *
21  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  *
23  *******************************************************************************/
24
25 package org.onap.dmaap.mr.tools;
26
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;
33 import java.util.Set;
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;
39
40 public class TopicCommand implements Command<MRCommandContext> {
41     final Logger logger = LoggerFactory.getLogger(TopicCommand.class);
42
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: ";
46
47     @Override
48     public String[] getMatches() {
49         return new String[] {
50             "topic (list)",
51             "topic (list) (\\S*)",
52             "topic (create) (\\S*) (\\S*) (\\S*)",
53             "topic (grant|revoke) (read|write) (\\S*) (\\S*)",
54         };
55     }
56
57     @Override
58     public void checkReady(MRCommandContext context) throws CommandNotReadyException {
59         if (!context.checkClusterReady()) {
60             throw new CommandNotReadyException("Use 'cluster' to specify a cluster to use.");
61         }
62     }
63
64     @Override
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);
68
69         try {
70             switch (parts[0]) {
71                 case "list":
72                     try {
73                         if (parts.length == 1) {
74                             for (String topic : tm.getTopics()) {
75                                 out.println(topic);
76                             }
77                         } else {
78                             final TopicInfo ti = tm.getTopicMetadata(parts[1]);
79
80                             final String owner = ti.getOwner();
81                             out.println("      owner: " + (owner == null ? "<none>" : owner));
82
83                             final String desc = ti.getDescription();
84                             out.println("description: " + (desc == null ? "<none>" : desc));
85
86                             final Set<String> prods = ti.getAllowedProducers();
87                             if (prods != null) {
88                                 out.println("  write ACL: ");
89                                 for (String key : prods) {
90                                     out.println("\t" + key);
91                                 }
92                             } else {
93                                 out.println("  write ACL: <not active>");
94                             }
95
96                             final Set<String> cons = ti.getAllowedConsumers();
97                             if (cons != null) {
98                                 out.println("   read ACL: ");
99                                 for (String key : cons) {
100                                     out.println("\t" + key);
101                                 }
102                             } else {
103                                 out.println("   read ACL: <not active>");
104                             }
105                         }
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);
112                     }
113                     break;
114                 case "create":
115                     try {
116                         final int partitions = Integer.parseInt(parts[2]);
117                         final int replicas = Integer.parseInt(parts[3]);
118
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);
129                     }
130                     break;
131                 case "grant":
132                     try {
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]);
137                         }
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);
144                     }
145                     break;
146                 case "revoke":
147                     try {
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]);
152                         }
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);
159                     }
160                     break;
161                 default:
162                     throw new CommandNotReadyException("The command " + parts[0] + " is not available");
163             }
164         } finally {
165             tm.close();
166         }
167     }
168
169     @Override
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>");
176     }
177
178 }