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 com.att.nsa.mr.client.impl;
24 import java.io.IOException;
25 import java.net.MalformedURLException;
26 import java.util.Collection;
28 import java.util.TreeSet;
30 import org.json.JSONArray;
31 import org.json.JSONException;
32 import org.json.JSONObject;
34 import com.att.nsa.apiClient.credentials.ApiCredential;
35 import com.att.nsa.apiClient.http.HttpException;
36 import com.att.nsa.apiClient.http.HttpObjectNotFoundException;
37 import com.att.nsa.mr.client.MRIdentityManager;
38 import com.att.nsa.mr.client.MRTopicManager;
40 public class MRMetaClient extends MRBaseClient implements MRTopicManager, MRIdentityManager
42 public MRMetaClient ( Collection<String> baseUrls ) throws MalformedURLException
48 public Set<String> getTopics () throws IOException
50 final TreeSet<String> set = new TreeSet<String> ();
53 final JSONObject topicSet = get ( "/topics" );
54 final JSONArray a = topicSet.getJSONArray ( "topics" );
55 for ( int i=0; i<a.length (); i++ )
57 set.add ( a.getString ( i ) );
60 catch ( HttpObjectNotFoundException e )
62 getLog().warn ( "No /topics endpoint on service." );
64 catch ( JSONException e )
66 getLog().warn ( "Bad /topics result from service." );
68 catch ( HttpException e )
70 throw new IOException ( e );
76 public TopicInfo getTopicMetadata ( String topic ) throws HttpObjectNotFoundException, IOException
80 final JSONObject topicData = get ( "/topics/" + MRConstants.escape ( topic ) );
81 return new TopicInfo ()
84 public String getOwner ()
86 return topicData.optString ( "owner", null );
90 public String getDescription ()
92 return topicData.optString ( "description", null );
96 public Set<String> getAllowedProducers ()
98 final JSONObject acl = topicData.optJSONObject ( "writerAcl" );
99 if ( acl != null && acl.optBoolean ( "enabled", true ) )
101 return jsonArrayToSet ( acl.optJSONArray ( "users" ) );
107 public Set<String> getAllowedConsumers ()
109 final JSONObject acl = topicData.optJSONObject ( "readerAcl" );
110 if ( acl != null && acl.optBoolean ( "enabled", true ) )
112 return jsonArrayToSet ( acl.optJSONArray ( "users" ) );
118 catch ( JSONException e )
120 throw new IOException ( e );
122 catch ( HttpException e )
124 throw new IOException ( e );
129 public void createTopic ( String topicName, String topicDescription, int partitionCount, int replicationCount ) throws HttpException, IOException
131 final JSONObject o = new JSONObject ();
132 o.put ( "topicName", topicName );
133 o.put ( "topicDescription", topicDescription );
134 o.put ( "partitionCount", partitionCount );
135 o.put ( "replicationCount", replicationCount );
136 post ( "/topics/create", o, false );
140 public void deleteTopic ( String topic ) throws HttpException, IOException
142 delete ( "/topics/" + MRConstants.escape ( topic ) );
146 public boolean isOpenForProducing ( String topic ) throws HttpObjectNotFoundException, IOException
148 return null == getAllowedProducers ( topic );
152 public Set<String> getAllowedProducers ( String topic ) throws HttpObjectNotFoundException, IOException
154 return getTopicMetadata ( topic ).getAllowedProducers ();
158 public void allowProducer ( String topic, String apiKey ) throws HttpObjectNotFoundException, HttpException, IOException
160 put ( "/topics/" + MRConstants.escape ( topic ) + "/producers/" + MRConstants.escape ( apiKey ), new JSONObject() );
164 public void revokeProducer ( String topic, String apiKey ) throws HttpException, IOException
166 delete ( "/topics/" + MRConstants.escape ( topic ) + "/producers/" + MRConstants.escape ( apiKey ) );
170 public boolean isOpenForConsuming ( String topic ) throws HttpObjectNotFoundException, IOException
172 return null == getAllowedConsumers ( topic );
176 public Set<String> getAllowedConsumers ( String topic ) throws HttpObjectNotFoundException, IOException
178 return getTopicMetadata ( topic ).getAllowedConsumers ();
182 public void allowConsumer ( String topic, String apiKey ) throws HttpObjectNotFoundException, HttpException, IOException
184 put ( "/topics/" + MRConstants.escape ( topic ) + "/consumers/" + MRConstants.escape ( apiKey ), new JSONObject() );
188 public void revokeConsumer ( String topic, String apiKey ) throws HttpException, IOException
190 delete ( "/topics/" + MRConstants.escape ( topic ) + "/consumers/" + MRConstants.escape ( apiKey ) );
194 public ApiCredential createApiKey ( String email, String description ) throws HttpException, MRApiException, IOException
198 final JSONObject o = new JSONObject ();
199 o.put ( "email", email );
200 o.put ( "description", description );
201 final JSONObject reply = post ( "/apiKeys/create", o, true );
202 return new ApiCredential ( reply.getString ( "key" ), reply.getString ( "secret" ) );
204 catch ( JSONException e )
206 // the response doesn't meet our expectation
207 throw new MRApiException ( "The API key response is incomplete.", e );
212 public ApiKey getApiKey ( String apiKey ) throws HttpObjectNotFoundException, HttpException, IOException
214 final JSONObject keyEntry = get ( "/apiKeys/" + MRConstants.escape ( apiKey ) );
215 if ( keyEntry == null )
223 public String getEmail ()
225 final JSONObject aux = keyEntry.optJSONObject ( "aux" );
228 return aux.optString ( "email" );
234 public String getDescription ()
236 final JSONObject aux = keyEntry.optJSONObject ( "aux" );
239 return aux.optString ( "description" );
247 public void updateCurrentApiKey ( String email, String description ) throws HttpObjectNotFoundException, HttpException, IOException
249 final JSONObject o = new JSONObject ();
250 if ( email != null ) o.put ( "email", email );
251 if ( description != null ) o.put ( "description", description );
252 patch ( "/apiKeys/" + MRConstants.escape ( getCurrentApiKey() ), o );
256 public void deleteCurrentApiKey () throws HttpException, IOException
258 delete ( "/apiKeys/" + MRConstants.escape ( getCurrentApiKey() ) );