aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/com/att/nsa/mr/client/impl/MRMetaClient.java
blob: 609540b0909bdb0cf622cd653aa4b5cab1a55f1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*******************************************************************************
 *  ============LICENSE_START=======================================================
 *  org.onap.dmaap
 *  ================================================================================
 *  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  ============LICENSE_END=========================================================
 *
 *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
 *  
 *******************************************************************************/
package com.att.nsa.mr.client.impl;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Collection;
import java.util.Set;
import java.util.TreeSet;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.att.nsa.apiClient.credentials.ApiCredential;
import com.att.nsa.apiClient.http.HttpException;
import com.att.nsa.apiClient.http.HttpObjectNotFoundException;
import com.att.nsa.mr.client.MRIdentityManager;
import com.att.nsa.mr.client.MRTopicManager;

public class MRMetaClient extends MRBaseClient implements MRTopicManager, MRIdentityManager
{
	private static final Logger logger = LoggerFactory.getLogger(MRMetaClient.class);
	public MRMetaClient ( Collection<String> baseUrls ) throws MalformedURLException
	{
		super ( baseUrls );
	}

	@Override
	public Set<String> getTopics () throws IOException
	{
		final TreeSet<String> set = new TreeSet<String> ();
		try
		{
			final JSONObject topicSet = get ( "/topics" );
			final JSONArray a = topicSet.getJSONArray ( "topics" );
			for ( int i=0; i<a.length (); i++ )
			{
				set.add ( a.getString ( i ) );
			}
		}
		catch ( HttpObjectNotFoundException e )
		{
			getLog().warn ( "No /topics endpoint on service." );
                        logger.error("HttpObjectNotFoundException: ", e);
		}
		catch ( JSONException e )
		{
			getLog().warn ( "Bad /topics result from service." );
                        logger.error("JSONException: ", e);
		}
		catch ( HttpException e )
		{
			throw new IOException ( e );
		}
		return set;
	}

	@Override
	public TopicInfo getTopicMetadata ( String topic ) throws HttpObjectNotFoundException, IOException
	{
		try
		{
			final JSONObject topicData = get ( "/topics/" + MRConstants.escape ( topic ) );
			return new TopicInfo ()
			{
				@Override
				public String getOwner ()
				{
					return topicData.optString ( "owner", null );
				}

				@Override
				public String getDescription ()
				{
					return topicData.optString ( "description", null );
				}

				@Override
				public Set<String> getAllowedProducers ()
				{
					final JSONObject acl = topicData.optJSONObject ( "writerAcl" );
					if ( acl != null && acl.optBoolean ( "enabled", true ) )
					{
						return jsonArrayToSet ( acl.optJSONArray ( "users" ) );
					}
					return null;
				}

				@Override
				public Set<String> getAllowedConsumers ()
				{
					final JSONObject acl = topicData.optJSONObject ( "readerAcl" );
					if ( acl != null && acl.optBoolean ( "enabled", true ) )
					{
						return jsonArrayToSet ( acl.optJSONArray ( "users" ) );
					}
					return null;
				}
			};
		}
		catch ( JSONException e )
		{
			throw new IOException ( e );
		}
		catch ( HttpException e )
		{
			throw new IOException ( e );
		}
	}

	@Override
	public void createTopic ( String topicName, String topicDescription, int partitionCount, int replicationCount ) throws HttpException, IOException
	{
		final JSONObject o = new JSONObject ();
		o.put ( "topicName", topicName );
		o.put ( "topicDescription", topicDescription );
		o.put ( "partitionCount", partitionCount );
		o.put ( "replicationCount", replicationCount );
		post ( "/topics/create", o, false );
	}

	@Override
	public void deleteTopic ( String topic ) throws HttpException, IOException
	{
		delete ( "/topics/" + MRConstants.escape ( topic ) );
	}

	@Override
	public boolean isOpenForProducing ( String topic ) throws HttpObjectNotFoundException, IOException
	{
		return null == getAllowedProducers ( topic );
	}

	@Override
	public Set<String> getAllowedProducers ( String topic ) throws HttpObjectNotFoundException, IOException
	{
		return getTopicMetadata ( topic ).getAllowedProducers ();
	}

	@Override
	public void allowProducer ( String topic, String apiKey ) throws HttpObjectNotFoundException, HttpException, IOException
	{
		put ( "/topics/" + MRConstants.escape ( topic ) + "/producers/" + MRConstants.escape ( apiKey ), new JSONObject() );
	}

	@Override
	public void revokeProducer ( String topic, String apiKey ) throws HttpException, IOException
	{
		delete ( "/topics/" + MRConstants.escape ( topic ) + "/producers/" + MRConstants.escape ( apiKey ) );
	}

	@Override
	public boolean isOpenForConsuming ( String topic ) throws HttpObjectNotFoundException, IOException
	{
		return null == getAllowedConsumers ( topic );
	}

	@Override
	public Set<String> getAllowedConsumers ( String topic ) throws HttpObjectNotFoundException, IOException
	{
		return getTopicMetadata ( topic ).getAllowedConsumers ();
	}

	@Override
	public void allowConsumer ( String topic, String apiKey ) throws HttpObjectNotFoundException, HttpException, IOException
	{
		put ( "/topics/" + MRConstants.escape ( topic ) + "/consumers/" + MRConstants.escape ( apiKey ), new JSONObject() );
	}

	@Override
	public void revokeConsumer ( String topic, String apiKey ) throws HttpException, IOException
	{
		delete ( "/topics/" + MRConstants.escape ( topic ) + "/consumers/" + MRConstants.escape ( apiKey ) );
	}

	@Override
	public ApiCredential createApiKey ( String email, String description ) throws HttpException, MRApiException, IOException
	{
		try
		{
			final JSONObject o = new JSONObject ();
			o.put ( "email", email );
			o.put ( "description", description );
			final JSONObject reply = post ( "/apiKeys/create", o, true );
			return new ApiCredential ( reply.getString ( "key" ), reply.getString ( "secret" ) );
		}
		catch ( JSONException e )
		{
			// the response doesn't meet our expectation
			throw new MRApiException ( "The API key response is incomplete.", e );
		}
	}

	@Override
	public ApiKey getApiKey ( String apiKey ) throws HttpObjectNotFoundException, HttpException, IOException
	{
		final JSONObject keyEntry = get ( "/apiKeys/" + MRConstants.escape ( apiKey ) );
		if ( keyEntry == null )
		{
			return null;
		}

		return new ApiKey ()
		{
			@Override
			public String getEmail ()
			{
				final JSONObject aux = keyEntry.optJSONObject ( "aux" );
				if ( aux != null )
				{
					return aux.optString ( "email" );
				}
				return null;
			}

			@Override
			public String getDescription ()
			{
				final JSONObject aux = keyEntry.optJSONObject ( "aux" );
				if ( aux != null )
				{
					return aux.optString ( "description" );
				}
				return null;
			}
		};
	}

	@Override
	public void updateCurrentApiKey ( String email, String description ) throws HttpObjectNotFoundException, HttpException, IOException
	{
		final JSONObject o = new JSONObject ();
		if ( email != null ) o.put ( "email", email );
		if ( description != null ) o.put ( "description", description );
		patch ( "/apiKeys/" + MRConstants.escape ( getCurrentApiKey() ), o );
	}

	@Override
	public void deleteCurrentApiKey () throws HttpException, IOException
	{
		delete ( "/apiKeys/" + MRConstants.escape ( getCurrentApiKey() ) );
	}
}