update the package name
[dmaap/messagerouter/dmaapclient.git] / src / main / java / org / onap / dmaap / mr / client / impl / MRMetaClient.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.client.impl;
23
24 import java.io.IOException;
25 import java.net.MalformedURLException;
26 import java.util.Collection;
27 import java.util.Set;
28 import java.util.TreeSet;
29
30 import org.json.JSONArray;
31 import org.json.JSONException;
32 import org.json.JSONObject;
33
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import com.att.nsa.apiClient.credentials.ApiCredential;
38 import com.att.nsa.apiClient.http.HttpException;
39 import com.att.nsa.apiClient.http.HttpObjectNotFoundException;
40 import org.onap.dmaap.mr.client.MRIdentityManager;
41 import org.onap.dmaap.mr.client.MRTopicManager;
42
43 public class MRMetaClient extends MRBaseClient implements MRTopicManager, MRIdentityManager
44 {
45         private static final Logger logger = LoggerFactory.getLogger(MRMetaClient.class);
46         public MRMetaClient ( Collection<String> baseUrls ) throws MalformedURLException
47         {
48                 super ( baseUrls );
49         }
50
51         @Override
52         public Set<String> getTopics () throws IOException
53         {
54                 final TreeSet<String> set = new TreeSet<String> ();
55                 try
56                 {
57                         final JSONObject topicSet = get ( "/topics" );
58                         final JSONArray a = topicSet.getJSONArray ( "topics" );
59                         for ( int i=0; i<a.length (); i++ )
60                         {
61                                 set.add ( a.getString ( i ) );
62                         }
63                 }
64                 catch ( HttpObjectNotFoundException e )
65                 {
66                         getLog().warn ( "No /topics endpoint on service." );
67                         logger.error("HttpObjectNotFoundException: ", e);
68                 }
69                 catch ( JSONException e )
70                 {
71                         getLog().warn ( "Bad /topics result from service." );
72                         logger.error("JSONException: ", e);
73                 }
74                 catch ( HttpException e )
75                 {
76                         throw new IOException ( e );
77                 }
78                 return set;
79         }
80
81         @Override
82         public TopicInfo getTopicMetadata ( String topic ) throws HttpObjectNotFoundException, IOException
83         {
84                 try
85                 {
86                         final JSONObject topicData = get ( "/topics/" + MRConstants.escape ( topic ) );
87                         return new TopicInfo ()
88                         {
89                                 @Override
90                                 public String getOwner ()
91                                 {
92                                         return topicData.optString ( "owner", null );
93                                 }
94
95                                 @Override
96                                 public String getDescription ()
97                                 {
98                                         return topicData.optString ( "description", null );
99                                 }
100
101                                 @Override
102                                 public Set<String> getAllowedProducers ()
103                                 {
104                                         final JSONObject acl = topicData.optJSONObject ( "writerAcl" );
105                                         if ( acl != null && acl.optBoolean ( "enabled", true ) )
106                                         {
107                                                 return jsonArrayToSet ( acl.optJSONArray ( "users" ) );
108                                         }
109                                         return null;
110                                 }
111
112                                 @Override
113                                 public Set<String> getAllowedConsumers ()
114                                 {
115                                         final JSONObject acl = topicData.optJSONObject ( "readerAcl" );
116                                         if ( acl != null && acl.optBoolean ( "enabled", true ) )
117                                         {
118                                                 return jsonArrayToSet ( acl.optJSONArray ( "users" ) );
119                                         }
120                                         return null;
121                                 }
122                         };
123                 }
124                 catch ( JSONException e )
125                 {
126                         throw new IOException ( e );
127                 }
128                 catch ( HttpException e )
129                 {
130                         throw new IOException ( e );
131                 }
132         }
133
134         @Override
135         public void createTopic ( String topicName, String topicDescription, int partitionCount, int replicationCount ) throws HttpException, IOException
136         {
137                 final JSONObject o = new JSONObject ();
138                 o.put ( "topicName", topicName );
139                 o.put ( "topicDescription", topicDescription );
140                 o.put ( "partitionCount", partitionCount );
141                 o.put ( "replicationCount", replicationCount );
142                 post ( "/topics/create", o, false );
143         }
144
145         @Override
146         public void deleteTopic ( String topic ) throws HttpException, IOException
147         {
148                 delete ( "/topics/" + MRConstants.escape ( topic ) );
149         }
150
151         @Override
152         public boolean isOpenForProducing ( String topic ) throws HttpObjectNotFoundException, IOException
153         {
154                 return null == getAllowedProducers ( topic );
155         }
156
157         @Override
158         public Set<String> getAllowedProducers ( String topic ) throws HttpObjectNotFoundException, IOException
159         {
160                 return getTopicMetadata ( topic ).getAllowedProducers ();
161         }
162
163         @Override
164         public void allowProducer ( String topic, String apiKey ) throws HttpObjectNotFoundException, HttpException, IOException
165         {
166                 put ( "/topics/" + MRConstants.escape ( topic ) + "/producers/" + MRConstants.escape ( apiKey ), new JSONObject() );
167         }
168
169         @Override
170         public void revokeProducer ( String topic, String apiKey ) throws HttpException, IOException
171         {
172                 delete ( "/topics/" + MRConstants.escape ( topic ) + "/producers/" + MRConstants.escape ( apiKey ) );
173         }
174
175         @Override
176         public boolean isOpenForConsuming ( String topic ) throws HttpObjectNotFoundException, IOException
177         {
178                 return null == getAllowedConsumers ( topic );
179         }
180
181         @Override
182         public Set<String> getAllowedConsumers ( String topic ) throws HttpObjectNotFoundException, IOException
183         {
184                 return getTopicMetadata ( topic ).getAllowedConsumers ();
185         }
186
187         @Override
188         public void allowConsumer ( String topic, String apiKey ) throws HttpObjectNotFoundException, HttpException, IOException
189         {
190                 put ( "/topics/" + MRConstants.escape ( topic ) + "/consumers/" + MRConstants.escape ( apiKey ), new JSONObject() );
191         }
192
193         @Override
194         public void revokeConsumer ( String topic, String apiKey ) throws HttpException, IOException
195         {
196                 delete ( "/topics/" + MRConstants.escape ( topic ) + "/consumers/" + MRConstants.escape ( apiKey ) );
197         }
198
199         @Override
200         public ApiCredential createApiKey ( String email, String description ) throws HttpException, MRApiException, IOException
201         {
202                 try
203                 {
204                         final JSONObject o = new JSONObject ();
205                         o.put ( "email", email );
206                         o.put ( "description", description );
207                         final JSONObject reply = post ( "/apiKeys/create", o, true );
208                         return new ApiCredential ( reply.getString ( "key" ), reply.getString ( "secret" ) );
209                 }
210                 catch ( JSONException e )
211                 {
212                         // the response doesn't meet our expectation
213                         throw new MRApiException ( "The API key response is incomplete.", e );
214                 }
215         }
216
217         @Override
218         public ApiKey getApiKey ( String apiKey ) throws HttpObjectNotFoundException, HttpException, IOException
219         {
220                 final JSONObject keyEntry = get ( "/apiKeys/" + MRConstants.escape ( apiKey ) );
221                 if ( keyEntry == null )
222                 {
223                         return null;
224                 }
225
226                 return new ApiKey ()
227                 {
228                         @Override
229                         public String getEmail ()
230                         {
231                                 final JSONObject aux = keyEntry.optJSONObject ( "aux" );
232                                 if ( aux != null )
233                                 {
234                                         return aux.optString ( "email" );
235                                 }
236                                 return null;
237                         }
238
239                         @Override
240                         public String getDescription ()
241                         {
242                                 final JSONObject aux = keyEntry.optJSONObject ( "aux" );
243                                 if ( aux != null )
244                                 {
245                                         return aux.optString ( "description" );
246                                 }
247                                 return null;
248                         }
249                 };
250         }
251
252         @Override
253         public void updateCurrentApiKey ( String email, String description ) throws HttpObjectNotFoundException, HttpException, IOException
254         {
255                 final JSONObject o = new JSONObject ();
256                 if ( email != null ) o.put ( "email", email );
257                 if ( description != null ) o.put ( "description", description );
258                 patch ( "/apiKeys/" + MRConstants.escape ( getCurrentApiKey() ), o );
259         }
260
261         @Override
262         public void deleteCurrentApiKey () throws HttpException, IOException
263         {
264                 delete ( "/apiKeys/" + MRConstants.escape ( getCurrentApiKey() ) );
265         }
266 }