summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dmaap/dmf/mr/beans/DMaaPKafkaMetaBroker.java
blob: b27a2861739bd652efbd492bea7e25c6d729fc9a (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*******************************************************************************
 *  ============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 org.onap.dmaap.dmf.mr.beans;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ExecutionException;

import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.exception.ZkNoNodeException;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.CreateTopicsResult;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.common.KafkaFuture;
import org.json.JSONObject;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.StringUtils;
import org.onap.dmaap.dmf.mr.CambriaApiException;
import org.onap.dmaap.dmf.mr.constants.CambriaConstants;
import org.onap.dmaap.dmf.mr.metabroker.Broker1;
import org.onap.dmaap.dmf.mr.metabroker.Topic;
import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
import org.onap.dmaap.dmf.mr.utils.Utils;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;

import com.att.nsa.configs.ConfigDb;
import com.att.nsa.configs.ConfigDbException;
import com.att.nsa.configs.ConfigPath;
import com.att.nsa.drumlin.service.standards.HttpStatusCodes;
import com.att.nsa.drumlin.till.nv.rrNvReadable;
import com.att.nsa.security.NsaAcl;
import com.att.nsa.security.NsaAclUtils;
import com.att.nsa.security.NsaApiKey;


/**
 * class performing all topic operations
 * 
 * @author anowarul.islam
 *
 */
//@Component
public class DMaaPKafkaMetaBroker implements Broker1 {

	public DMaaPKafkaMetaBroker() {
		fZk = null;
		fCambriaConfig = null;
		fBaseTopicData = null;
		final Properties props = new Properties ();
		String fkafkaBrokers = com.att.ajsc.filemonitor.AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,
				"kafka.metadata.broker.list");
		if (StringUtils.isEmpty(fkafkaBrokers)) {

			fkafkaBrokers = "localhost:9092";
		}
		
	     props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, fkafkaBrokers );
	     if(Utils.isCadiEnabled()){
	     props.put("sasl.jaas.config", "org.apache.kafka.common.security.plain.PlainLoginModule required username='admin' password='"+Utils.getKafkaproperty()+"';");
	  	 props.put(AdminClientConfig.SECURITY_PROTOCOL_CONFIG, "SASL_PLAINTEXT");	     
	     props.put("sasl.mechanism", "PLAIN");
	     }
	   
	     fKafkaAdminClient=AdminClient.create ( props );
	    
	}

	private static final EELFLogger log = EELFManager.getInstance().getLogger(ConfigurationReader.class);
	private final AdminClient fKafkaAdminClient;
	
	

	/**
	 * DMaaPKafkaMetaBroker constructor initializing
	 * 
	 * @param settings
	 * @param zk
	 * @param configDb
	 */
	public DMaaPKafkaMetaBroker(@Qualifier("propertyReader") rrNvReadable settings,
			@Qualifier("dMaaPZkClient") ZkClient zk, @Qualifier("dMaaPZkConfigDb") ConfigDb configDb) {
		fZk = zk;
		fCambriaConfig = configDb;
		fBaseTopicData = configDb.parse("/topics");
		final Properties props = new Properties ();
		String fkafkaBrokers = com.att.ajsc.filemonitor.AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,
				"kafka.metadata.broker.list");
		if (null == fkafkaBrokers) {

			fkafkaBrokers = "localhost:9092";
		}
		
		 if(Utils.isCadiEnabled()){
		 props.put("sasl.jaas.config", "org.apache.kafka.common.security.plain.PlainLoginModule required username='admin' password='"+Utils.getKafkaproperty()+"';");
	  	 props.put(AdminClientConfig.SECURITY_PROTOCOL_CONFIG, "SASL_PLAINTEXT");	     
	     props.put("sasl.mechanism", "PLAIN");
		 }
	     props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, fkafkaBrokers );
	     
	     fKafkaAdminClient=AdminClient.create ( props );
	    
		
		
	}
	
	public DMaaPKafkaMetaBroker( rrNvReadable settings,
			ZkClient zk,  ConfigDb configDb,AdminClient client) {
		
		fZk = zk;
		fCambriaConfig = configDb;
		fBaseTopicData = configDb.parse("/topics");
	    fKafkaAdminClient= client;
	   
		
		
	}

	@Override
	public List<Topic> getAllTopics() throws ConfigDbException {
		log.info("Retrieving list of all the topics.");
		final LinkedList<Topic> result = new LinkedList<>();
		try {
			log.info("Retrieving all topics from root: " + zkTopicsRoot);
			final List<String> topics = fZk.getChildren(zkTopicsRoot);
			for (String topic : topics) {
				result.add(new KafkaTopic(topic, fCambriaConfig, fBaseTopicData));
			}
			JSONObject dataObj = new JSONObject();
			dataObj.put("topics", new JSONObject());

			for (String topic : topics) {
				dataObj.getJSONObject("topics").put(topic, new JSONObject());
			}
		} catch (ZkNoNodeException excp) {
			// very fresh kafka doesn't have any topics or a topics node
			log.error("ZK doesn't have a Kakfa topics node at " + zkTopicsRoot, excp);
		}
		return result;
	}

	@Override
	public Topic getTopic(String topic) throws ConfigDbException {
		if (fZk.exists(zkTopicsRoot + "/" + topic)) {
			return getKafkaTopicConfig(fCambriaConfig, fBaseTopicData, topic);
		}
		// else: no such topic in kafka
		return null;
	}

	/**
	 * static method get KafkaTopic object
	 * 
	 * @param db
	 * @param base
	 * @param topic
	 * @return
	 * @throws ConfigDbException
	 */
	public static KafkaTopic getKafkaTopicConfig(ConfigDb db, ConfigPath base, String topic) throws ConfigDbException {
		return new KafkaTopic(topic, db, base);
	}

	/**
	 * creating topic
	 */
	@Override
	public Topic createTopic(String topic, String desc, String ownerApiKey, int partitions, int replicas,
			boolean transactionEnabled) throws TopicExistsException, CambriaApiException,ConfigDbException {
		log.info("Creating topic: " + topic);
		try {
			log.info("Check if topic [" + topic + "] exist.");
			// first check for existence "our way"
			final Topic t = getTopic(topic);
			if (t != null) {
				log.info("Could not create topic [" + topic + "]. Topic Already exists.");
				throw new TopicExistsException("Could not create topic [" + topic + "]. Topic Alreay exists.");
			}
		} catch (ConfigDbException e1) {
			log.error("Topic [" + topic + "] could not be created. Couldn't check topic data in config db.", e1);
			throw new CambriaApiException(HttpStatusCodes.k503_serviceUnavailable,
					"Couldn't check topic data in config db.");
		}

		// we only allow 3 replicas. (If we don't test this, we get weird
		// results from the cluster,
		// so explicit test and fail.)
		if (replicas < 1 || replicas > 3) {
			log.info("Topic [" + topic + "] could not be created. The replica count must be between 1 and 3.");
			throw new CambriaApiException(HttpStatusCodes.k400_badRequest,
					"The replica count must be between 1 and 3.");
		}
		if (partitions < 1) {
			log.info("Topic [" + topic + "] could not be created. The partition count must be at least 1.");
			throw new CambriaApiException(HttpStatusCodes.k400_badRequest, "The partition count must be at least 1.");
		}

		// create via kafka

        try {
            final NewTopic topicRequest =
                    new NewTopic(topic, partitions, (short)replicas);
            final CreateTopicsResult ctr =
                    fKafkaAdminClient.createTopics(Arrays.asList(topicRequest));
            final KafkaFuture<Void> ctrResult = ctr.all();
            ctrResult.get();
            // underlying Kafka topic created. now setup our API info
            return createTopicEntry(topic, desc, ownerApiKey, transactionEnabled);
        } catch (InterruptedException e) {
            log.warn("Execution of describeTopics timed out.");
            throw new ConfigDbException(e);
        } catch (ExecutionException e) {
            log.warn("Execution of describeTopics failed: " + e.getCause().getMessage(), e);
            throw new ConfigDbException(e.getCause());
        }
		
	}

	@Override
	public void deleteTopic(String topic) throws CambriaApiException, TopicExistsException,ConfigDbException {
		log.info("Deleting topic: " + topic);
		try {
			log.info("Loading zookeeper client for topic deletion.");
					// topic creation. (Otherwise, the topic is only partially created
			// in ZK.)
			
			
			fKafkaAdminClient.deleteTopics(Arrays.asList(topic));
			log.info("Zookeeper client loaded successfully. Deleting topic.");
			
		} catch (Exception e) {
			log.error("Failed to delete topic [" + topic + "]. " + e.getMessage(), e);
			throw new ConfigDbException(e);
		}  finally {
			log.info("Closing zookeeper connection.");
		}
	}

	private final ZkClient fZk;
	private final ConfigDb fCambriaConfig;
	private final ConfigPath fBaseTopicData;

	private static final String zkTopicsRoot = "/brokers/topics";
	private static final JSONObject kEmptyAcl = new JSONObject();

	/**
	 * method Providing KafkaTopic Object associated with owner and
	 * transactionenabled or not
	 * 
	 * @param name
	 * @param desc
	 * @param owner
	 * @param transactionEnabled
	 * @return
	 * @throws ConfigDbException
	 */
	public KafkaTopic createTopicEntry(String name, String desc, String owner, boolean transactionEnabled)
			throws ConfigDbException {
		return createTopicEntry(fCambriaConfig, fBaseTopicData, name, desc, owner, transactionEnabled);
	}

	/**
	 * static method giving kafka topic object
	 * 
	 * @param db
	 * @param basePath
	 * @param name
	 * @param desc
	 * @param owner
	 * @param transactionEnabled
	 * @return
	 * @throws ConfigDbException
	 */
	public static KafkaTopic createTopicEntry(ConfigDb db, ConfigPath basePath, String name, String desc, String owner,
			boolean transactionEnabled) throws ConfigDbException {
		final JSONObject o = new JSONObject();
		o.put("owner", owner);
		o.put("description", desc);
		o.put("txenabled", transactionEnabled);
		db.store(basePath.getChild(name), o.toString());
		return new KafkaTopic(name, db, basePath);
	}

	/**
	 * class performing all user opearation like user is eligible to read,
	 * write. permitting a user to write and read,
	 * 
	 * @author anowarul.islam
	 *
	 */
	public static class KafkaTopic implements Topic {
		/**
		 * constructor initializes
		 * 
		 * @param name
		 * @param configdb
		 * @param baseTopic
		 * @throws ConfigDbException
		 */
		public KafkaTopic(String name, ConfigDb configdb, ConfigPath baseTopic) throws ConfigDbException {
			fName = name;
			fConfigDb = configdb;
			fBaseTopicData = baseTopic;

			String data = fConfigDb.load(fBaseTopicData.getChild(fName));
			if (data == null) {
				data = "{}";
			}

			final JSONObject o = new JSONObject(data);
			fOwner = o.optString("owner", "");
			fDesc = o.optString("description", "");
			fTransactionEnabled = o.optBoolean("txenabled", false);// default
																	// value is
																	// false
			// if this topic has an owner, it needs both read/write ACLs. If there's no
						// owner (or it's empty), null is okay -- this is for existing or implicitly
						// created topics.
						JSONObject readers = o.optJSONObject ( "readers" );
						if ( readers == null && fOwner.length () > 0 )
						{
						    readers = kEmptyAcl;
						}
						fReaders =  fromJson ( readers );

						JSONObject writers = o.optJSONObject ( "writers" );
						if ( writers == null && fOwner.length () > 0 )
						{
						    writers = kEmptyAcl;
						}
						fWriters = fromJson ( writers );
		}
		
		private NsaAcl fromJson(JSONObject o) {
			NsaAcl acl = new NsaAcl();
			if (o != null) {
				JSONArray a = o.optJSONArray("allowed");
				if (a != null) {
					for (int i = 0; i < a.length(); ++i) {
						String user = a.getString(i);
						acl.add(user);
					}
				}
			}
			return acl;
		}

		@Override
		public String getName() {
			return fName;
		}

		@Override
		public String getOwner() {
			return fOwner;
		}

		@Override
		public String getDescription() {
			return fDesc;
		}

		@Override
		public NsaAcl getReaderAcl() {
			return fReaders;
		}

		@Override
		public NsaAcl getWriterAcl() {
			return fWriters;
		}

		@Override
		public void checkUserRead(NsaApiKey user) throws AccessDeniedException  {
			NsaAclUtils.checkUserAccess ( fOwner, getReaderAcl(), user );
		}

		@Override
		public void checkUserWrite(NsaApiKey user) throws AccessDeniedException  {
			NsaAclUtils.checkUserAccess ( fOwner, getWriterAcl(), user );
		}

		@Override
		public void permitWritesFromUser(String pubId, NsaApiKey asUser)
				throws ConfigDbException, AccessDeniedException {
			updateAcl(asUser, false, true, pubId);
		}

		@Override
		public void denyWritesFromUser(String pubId, NsaApiKey asUser) throws ConfigDbException, AccessDeniedException {
			updateAcl(asUser, false, false, pubId);
		}

		@Override
		public void permitReadsByUser(String consumerId, NsaApiKey asUser)
				throws ConfigDbException, AccessDeniedException {
			updateAcl(asUser, true, true, consumerId);
		}

		@Override
		public void denyReadsByUser(String consumerId, NsaApiKey asUser)
				throws ConfigDbException, AccessDeniedException {
			updateAcl(asUser, true, false, consumerId);
		}

		private void updateAcl(NsaApiKey asUser, boolean reader, boolean add, String key)
				throws ConfigDbException, AccessDeniedException{
			try
			{
				final NsaAcl acl = NsaAclUtils.updateAcl ( this, asUser, key, reader, add );
	
				// we have to assume we have current data, or load it again. for the expected use
				// case, assuming we can overwrite the data is fine.
				final JSONObject o = new JSONObject ();
				o.put ( "owner", fOwner );
				o.put ( "readers", safeSerialize ( reader ? acl : fReaders ) );
				o.put ( "writers", safeSerialize ( reader ? fWriters : acl ) );
				fConfigDb.store ( fBaseTopicData.getChild ( fName ), o.toString () );
				
				log.info ( "ACL_UPDATE: " + asUser.getKey () + " " + ( add ? "added" : "removed" ) + ( reader?"subscriber":"publisher" ) + " " + key + " on " + fName );
	
			}
			catch ( ConfigDbException | AccessDeniedException x )
			{
				throw x;
			}
			
		}

		private JSONObject safeSerialize(NsaAcl acl) {
			return acl == null ? null : acl.serialize();
		}

		private final String fName;
		private final ConfigDb fConfigDb;
		private final ConfigPath fBaseTopicData;
		private final String fOwner;
		private final String fDesc;
		private final NsaAcl fReaders;
		private final NsaAcl fWriters;
		private boolean fTransactionEnabled;
	
		public boolean isTransactionEnabled() {
			return fTransactionEnabled;
		}

		@Override
		public Set<String> getOwners() {
			final TreeSet<String> owners = new TreeSet<>();
			owners.add ( fOwner );
			return owners;
		}
	}

}