[DMAAP-CLIENT] First sonar issues review part2
[dmaap/messagerouter/dmaapclient.git] / src / main / java / org / onap / dmaap / mr / tools / ApiKeyCommand.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.credentials.ApiCredential;
28 import com.att.nsa.apiClient.http.HttpException;
29 import com.att.nsa.apiClient.http.HttpObjectNotFoundException;
30 import com.att.nsa.cmdtool.Command;
31 import com.att.nsa.cmdtool.CommandNotReadyException;
32 import java.io.IOException;
33 import java.io.PrintStream;
34 import org.onap.dmaap.mr.client.MRClient.MRApiException;
35 import org.onap.dmaap.mr.client.MRClientFactory;
36 import org.onap.dmaap.mr.client.MRIdentityManager;
37 import org.onap.dmaap.mr.client.MRIdentityManager.ApiKey;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class ApiKeyCommand implements Command<MRCommandContext> {
42     final Logger logger = LoggerFactory.getLogger(ApiKeyCommand.class);
43
44     @Override
45     public String[] getMatches() {
46         return new String[] {
47             "key (create|update) (\\S*) (\\S*)",
48             "key (list) (\\S*)",
49             "key (revoke)",
50         };
51     }
52
53     @Override
54     public void checkReady(MRCommandContext context) throws CommandNotReadyException {
55         if (!context.checkClusterReady()) {
56             throw new CommandNotReadyException("Use 'cluster' to specify a cluster to use.");
57         }
58     }
59
60     @Override
61     public void execute(String[] parts, MRCommandContext context, PrintStream out) throws CommandNotReadyException {
62         final MRIdentityManager tm = MRClientFactory.createIdentityManager(context.getCluster(), context.getApiKey(), context.getApiPwd());
63         context.applyTracer(tm);
64
65         try {
66             switch (parts[0]) {
67                 case "list":
68                     final ApiKey key = tm.getApiKey(parts[1]);
69                     if (key != null) {
70                         out.println("email: " + key.getEmail());
71                         out.println("description: " + key.getDescription());
72                     } else {
73                         out.println("No key returned");
74                     }
75                     break;
76                 case "create":
77                     final ApiCredential ac = tm.createApiKey(parts[1], parts[2]);
78                     if (ac != null) {
79                         out.println("   key: " + ac.getApiKey());
80                         out.println("secret: " + ac.getApiSecret());
81                     } else {
82                         out.println("No credential returned?");
83                     }
84                     break;
85                 case "update":
86                     tm.updateCurrentApiKey(parts[1], parts[2]);
87                     out.println("Updated");
88                     break;
89                 case "revoke":
90                     tm.deleteCurrentApiKey();
91                     out.println("Updated");
92                     break;
93                 default:
94                     throw new CommandNotReadyException("The command " + parts[0] + " is not available");
95             }
96         } catch (HttpObjectNotFoundException e) {
97             out.println("Object not found: " + e.getMessage());
98             logger.error("HttpObjectNotFoundException: ", e);
99         } catch (HttpException e) {
100             out.println("HTTP exception: " + e.getMessage());
101             logger.error("HttpException: ", e);
102         } catch (MRApiException e) {
103             out.println("API exception: " + e.getMessage());
104             logger.error("MRApiException: ", e);
105         } catch (IOException e) {
106             out.println("IO exception: " + e.getMessage());
107             logger.error("IOException: ", e);
108         } finally {
109             tm.close();
110         }
111     }
112
113     @Override
114     public void displayHelp(PrintStream out) {
115         out.println("key create <email> <description>");
116         out.println("key update <email> <description>");
117         out.println("key list <key>");
118         out.println("key revoke");
119     }
120 }