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 org.onap.dmaap.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 org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
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;
43 public class MRMetaClient extends MRBaseClient implements MRTopicManager, MRIdentityManager
45 private static final Logger logger = LoggerFactory.getLogger(MRMetaClient.class);
46 public MRMetaClient ( Collection<String> baseUrls ) throws MalformedURLException
52 public Set<String> getTopics () throws IOException
54 final TreeSet<String> set = new TreeSet<String> ();
57 final JSONObject topicSet = get ( "/topics" );
58 final JSONArray a = topicSet.getJSONArray ( "topics" );
59 for ( int i=0; i<a.length (); i++ )
61 set.add ( a.getString ( i ) );
64 catch ( HttpObjectNotFoundException e )
66 getLog().warn ( "No /topics endpoint on service." );
67 logger.error("HttpObjectNotFoundException: ", e);
69 catch ( JSONException e )
71 getLog().warn ( "Bad /topics result from service." );
72 logger.error("JSONException: ", e);
74 catch ( HttpException e )
76 throw new IOException ( e );
82 public TopicInfo getTopicMetadata ( String topic ) throws HttpObjectNotFoundException, IOException
86 final JSONObject topicData = get ( "/topics/" + MRConstants.escape ( topic ) );
87 return new TopicInfo ()
90 public String getOwner ()
92 return topicData.optString ( "owner", null );
96 public String getDescription ()
98 return topicData.optString ( "description", null );
102 public Set<String> getAllowedProducers ()
104 final JSONObject acl = topicData.optJSONObject ( "writerAcl" );
105 if ( acl != null && acl.optBoolean ( "enabled", true ) )
107 return jsonArrayToSet ( acl.optJSONArray ( "users" ) );
113 public Set<String> getAllowedConsumers ()
115 final JSONObject acl = topicData.optJSONObject ( "readerAcl" );
116 if ( acl != null && acl.optBoolean ( "enabled", true ) )
118 return jsonArrayToSet ( acl.optJSONArray ( "users" ) );
124 catch ( JSONException e )
126 throw new IOException ( e );
128 catch ( HttpException e )
130 throw new IOException ( e );
135 public void createTopic ( String topicName, String topicDescription, int partitionCount, int replicationCount ) throws HttpException, IOException
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 );
146 public void deleteTopic ( String topic ) throws HttpException, IOException
148 delete ( "/topics/" + MRConstants.escape ( topic ) );
152 public boolean isOpenForProducing ( String topic ) throws HttpObjectNotFoundException, IOException
154 return null == getAllowedProducers ( topic );
158 public Set<String> getAllowedProducers ( String topic ) throws HttpObjectNotFoundException, IOException
160 return getTopicMetadata ( topic ).getAllowedProducers ();
164 public void allowProducer ( String topic, String apiKey ) throws HttpObjectNotFoundException, HttpException, IOException
166 put ( "/topics/" + MRConstants.escape ( topic ) + "/producers/" + MRConstants.escape ( apiKey ), new JSONObject() );
170 public void revokeProducer ( String topic, String apiKey ) throws HttpException, IOException
172 delete ( "/topics/" + MRConstants.escape ( topic ) + "/producers/" + MRConstants.escape ( apiKey ) );
176 public boolean isOpenForConsuming ( String topic ) throws HttpObjectNotFoundException, IOException
178 return null == getAllowedConsumers ( topic );
182 public Set<String> getAllowedConsumers ( String topic ) throws HttpObjectNotFoundException, IOException
184 return getTopicMetadata ( topic ).getAllowedConsumers ();
188 public void allowConsumer ( String topic, String apiKey ) throws HttpObjectNotFoundException, HttpException, IOException
190 put ( "/topics/" + MRConstants.escape ( topic ) + "/consumers/" + MRConstants.escape ( apiKey ), new JSONObject() );
194 public void revokeConsumer ( String topic, String apiKey ) throws HttpException, IOException
196 delete ( "/topics/" + MRConstants.escape ( topic ) + "/consumers/" + MRConstants.escape ( apiKey ) );
200 public ApiCredential createApiKey ( String email, String description ) throws HttpException, MRApiException, IOException
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" ) );
210 catch ( JSONException e )
212 // the response doesn't meet our expectation
213 throw new MRApiException ( "The API key response is incomplete.", e );
218 public ApiKey getApiKey ( String apiKey ) throws HttpObjectNotFoundException, HttpException, IOException
220 final JSONObject keyEntry = get ( "/apiKeys/" + MRConstants.escape ( apiKey ) );
221 if ( keyEntry == null )
229 public String getEmail ()
231 final JSONObject aux = keyEntry.optJSONObject ( "aux" );
234 return aux.optString ( "email" );
240 public String getDescription ()
242 final JSONObject aux = keyEntry.optJSONObject ( "aux" );
245 return aux.optString ( "description" );
253 public void updateCurrentApiKey ( String email, String description ) throws HttpObjectNotFoundException, HttpException, IOException
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 );
262 public void deleteCurrentApiKey () throws HttpException, IOException
264 delete ( "/apiKeys/" + MRConstants.escape ( getCurrentApiKey() ) );