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.dmf.mr.beans;
24 import java.util.Arrays;
25 import java.util.LinkedList;
26 import java.util.List;
27 import java.util.Properties;
29 import java.util.TreeSet;
30 import java.util.concurrent.ExecutionException;
32 import org.I0Itec.zkclient.ZkClient;
33 import org.I0Itec.zkclient.exception.ZkNoNodeException;
34 import org.apache.kafka.clients.admin.AdminClient;
35 import org.apache.kafka.clients.admin.AdminClientConfig;
36 import org.apache.kafka.clients.admin.CreateTopicsResult;
37 import org.apache.kafka.clients.admin.NewTopic;
38 import org.apache.kafka.common.KafkaFuture;
39 import org.json.JSONObject;
40 import org.json.JSONArray;
41 import org.springframework.beans.factory.annotation.Qualifier;
42 import org.springframework.stereotype.Component;
44 import com.att.dmf.mr.CambriaApiException;
45 import com.att.dmf.mr.constants.CambriaConstants;
46 import com.att.dmf.mr.metabroker.Broker;
47 import com.att.dmf.mr.metabroker.Broker1;
48 import com.att.dmf.mr.metabroker.Topic;
49 import com.att.dmf.mr.utils.ConfigurationReader;
50 import com.att.dmf.mr.utils.Utils;
51 //import org.apache.log4-j.Logger;
52 import com.att.eelf.configuration.EELFLogger;
53 import com.att.eelf.configuration.EELFManager;
55 import com.att.nsa.configs.ConfigDb;
56 import com.att.nsa.configs.ConfigDbException;
57 import com.att.nsa.configs.ConfigPath;
58 import com.att.nsa.drumlin.service.standards.HttpStatusCodes;
59 import com.att.nsa.drumlin.till.nv.rrNvReadable;
60 import com.att.nsa.security.NsaAcl;
61 import com.att.nsa.security.NsaAclUtils;
62 import com.att.nsa.security.NsaApiKey;
66 * class performing all topic operations
68 * @author anowarul.islam
72 public class DMaaPKafkaMetaBroker implements Broker1 {
74 public DMaaPKafkaMetaBroker() {
76 fCambriaConfig = null;
77 fBaseTopicData = null;
78 final Properties props = new Properties ();
79 String fkafkaBrokers = com.att.ajsc.filemonitor.AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,
80 "kafka.metadata.broker.list");
81 if (null == fkafkaBrokers) {
83 fkafkaBrokers = "localhost:9092";
86 props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, fkafkaBrokers );
87 props.put("sasl.jaas.config", "org.apache.kafka.common.security.plain.PlainLoginModule required username='admin' password='"+Utils.getKafkaproperty()+"';");
88 props.put(AdminClientConfig.SECURITY_PROTOCOL_CONFIG, "SASL_PLAINTEXT");
89 props.put("sasl.mechanism", "PLAIN");
91 fKafkaAdminClient=AdminClient.create ( props );
95 //private static final Logger log = Logger.getLogger(DMaaPKafkaMetaBroker.class);
96 private static final EELFLogger log = EELFManager.getInstance().getLogger(ConfigurationReader.class);
97 private final AdminClient fKafkaAdminClient;
102 * DMaaPKafkaMetaBroker constructor initializing
108 public DMaaPKafkaMetaBroker(@Qualifier("propertyReader") rrNvReadable settings,
109 @Qualifier("dMaaPZkClient") ZkClient zk, @Qualifier("dMaaPZkConfigDb") ConfigDb configDb) {
110 //fSettings = settings;
112 fCambriaConfig = configDb;
113 fBaseTopicData = configDb.parse("/topics");
114 final Properties props = new Properties ();
115 String fkafkaBrokers = com.att.ajsc.filemonitor.AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,
116 "kafka.metadata.broker.list");
117 if (null == fkafkaBrokers) {
119 fkafkaBrokers = "localhost:9092";
123 props.put("sasl.jaas.config", "org.apache.kafka.common.security.plain.PlainLoginModule required username='admin' password='"+Utils.getKafkaproperty()+"';");
124 props.put(AdminClientConfig.SECURITY_PROTOCOL_CONFIG, "SASL_PLAINTEXT");
125 props.put("sasl.mechanism", "PLAIN");
126 props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, fkafkaBrokers );
128 fKafkaAdminClient=AdminClient.create ( props );
134 public DMaaPKafkaMetaBroker( rrNvReadable settings,
135 ZkClient zk, ConfigDb configDb,AdminClient client) {
138 fCambriaConfig = configDb;
139 fBaseTopicData = configDb.parse("/topics");
140 fKafkaAdminClient= client;
147 public List<Topic> getAllTopics() throws ConfigDbException {
148 log.info("Retrieving list of all the topics.");
149 final LinkedList<Topic> result = new LinkedList<Topic>();
151 log.info("Retrieving all topics from root: " + zkTopicsRoot);
152 final List<String> topics = fZk.getChildren(zkTopicsRoot);
153 for (String topic : topics) {
154 result.add(new KafkaTopic(topic, fCambriaConfig, fBaseTopicData));
156 JSONObject dataObj = new JSONObject();
157 dataObj.put("topics", new JSONObject());
159 for (String topic : topics) {
160 dataObj.getJSONObject("topics").put(topic, new JSONObject());
162 } catch (ZkNoNodeException excp) {
163 // very fresh kafka doesn't have any topics or a topics node
164 log.error("ZK doesn't have a Kakfa topics node at " + zkTopicsRoot, excp);
170 public Topic getTopic(String topic) throws ConfigDbException {
171 if (fZk.exists(zkTopicsRoot + "/" + topic)) {
172 return getKafkaTopicConfig(fCambriaConfig, fBaseTopicData, topic);
174 // else: no such topic in kafka
179 * static method get KafkaTopic object
185 * @throws ConfigDbException
187 public static KafkaTopic getKafkaTopicConfig(ConfigDb db, ConfigPath base, String topic) throws ConfigDbException {
188 return new KafkaTopic(topic, db, base);
195 public Topic createTopic(String topic, String desc, String ownerApiKey, int partitions, int replicas,
196 boolean transactionEnabled) throws TopicExistsException, CambriaApiException,ConfigDbException {
197 log.info("Creating topic: " + topic);
199 log.info("Check if topic [" + topic + "] exist.");
200 // first check for existence "our way"
201 final Topic t = getTopic(topic);
203 log.info("Could not create topic [" + topic + "]. Topic Already exists.");
204 throw new TopicExistsException("Could not create topic [" + topic + "]. Topic Alreay exists.");
206 } catch (ConfigDbException e1) {
207 log.error("Topic [" + topic + "] could not be created. Couldn't check topic data in config db.", e1);
208 throw new CambriaApiException(HttpStatusCodes.k503_serviceUnavailable,
209 "Couldn't check topic data in config db.");
212 // we only allow 3 replicas. (If we don't test this, we get weird
213 // results from the cluster,
214 // so explicit test and fail.)
215 if (replicas < 1 || replicas > 3) {
216 log.info("Topic [" + topic + "] could not be created. The replica count must be between 1 and 3.");
217 throw new CambriaApiException(HttpStatusCodes.k400_badRequest,
218 "The replica count must be between 1 and 3.");
220 if (partitions < 1) {
221 log.info("Topic [" + topic + "] could not be created. The partition count must be at least 1.");
222 throw new CambriaApiException(HttpStatusCodes.k400_badRequest, "The partition count must be at least 1.");
229 final NewTopic topicRequest = new NewTopic ( topic, partitions, new Integer(replicas).shortValue () );
230 final CreateTopicsResult ctr = fKafkaAdminClient.createTopics ( Arrays.asList ( topicRequest ) );
231 final KafkaFuture<Void> ctrResult = ctr.all ();
233 // underlying Kafka topic created. now setup our API info
234 return createTopicEntry ( topic, desc, ownerApiKey, transactionEnabled );
236 catch ( InterruptedException e )
239 log.warn ( "Execution of describeTopics timed out." );
240 throw new ConfigDbException ( e );
242 catch ( ExecutionException e )
245 log.warn ( "Execution of describeTopics failed: " + e.getCause ().getMessage (), e.getCause () );
246 throw new ConfigDbException ( e.getCause () );
252 public void deleteTopic(String topic) throws CambriaApiException, TopicExistsException,ConfigDbException {
253 log.info("Deleting topic: " + topic);
254 ZkClient zkClient = null;
256 log.info("Loading zookeeper client for topic deletion.");
257 // topic creation. (Otherwise, the topic is only partially created
261 fKafkaAdminClient.deleteTopics(Arrays.asList(topic));
262 log.info("Zookeeper client loaded successfully. Deleting topic.");
264 } catch (Exception e) {
265 log.error("Failed to delete topic [" + topic + "]. " + e.getMessage(), e);
266 throw new ConfigDbException(e);
268 log.info("Closing zookeeper connection.");
269 if (zkClient != null)
273 // throw new UnsupportedOperationException ( "We can't programmatically
274 // delete Kafka topics yet." );
277 //private final rrNvReadable fSettings;
278 private final ZkClient fZk;
279 private final ConfigDb fCambriaConfig;
280 private final ConfigPath fBaseTopicData;
282 private static final String zkTopicsRoot = "/brokers/topics";
283 private static final JSONObject kEmptyAcl = new JSONObject();
286 * method Providing KafkaTopic Object associated with owner and
287 * transactionenabled or not
292 * @param transactionEnabled
294 * @throws ConfigDbException
296 public KafkaTopic createTopicEntry(String name, String desc, String owner, boolean transactionEnabled)
297 throws ConfigDbException {
298 return createTopicEntry(fCambriaConfig, fBaseTopicData, name, desc, owner, transactionEnabled);
302 * static method giving kafka topic object
309 * @param transactionEnabled
311 * @throws ConfigDbException
313 public static KafkaTopic createTopicEntry(ConfigDb db, ConfigPath basePath, String name, String desc, String owner,
314 boolean transactionEnabled) throws ConfigDbException {
315 final JSONObject o = new JSONObject();
316 o.put("owner", owner);
317 o.put("description", desc);
318 o.put("txenabled", transactionEnabled);
319 db.store(basePath.getChild(name), o.toString());
320 return new KafkaTopic(name, db, basePath);
324 * class performing all user opearation like user is eligible to read,
325 * write. permitting a user to write and read,
327 * @author anowarul.islam
330 public static class KafkaTopic implements Topic {
332 * constructor initializes
337 * @throws ConfigDbException
339 public KafkaTopic(String name, ConfigDb configdb, ConfigPath baseTopic) throws ConfigDbException {
341 fConfigDb = configdb;
342 fBaseTopicData = baseTopic;
344 String data = fConfigDb.load(fBaseTopicData.getChild(fName));
349 final JSONObject o = new JSONObject(data);
350 fOwner = o.optString("owner", "");
351 fDesc = o.optString("description", "");
352 fTransactionEnabled = o.optBoolean("txenabled", false);// default
355 // if this topic has an owner, it needs both read/write ACLs. If there's no
356 // owner (or it's empty), null is okay -- this is for existing or implicitly
358 JSONObject readers = o.optJSONObject ( "readers" );
359 if ( readers == null && fOwner.length () > 0 ) readers = kEmptyAcl;
360 fReaders = fromJson ( readers );
362 JSONObject writers = o.optJSONObject ( "writers" );
363 if ( writers == null && fOwner.length () > 0 ) writers = kEmptyAcl;
364 fWriters = fromJson ( writers );
367 private NsaAcl fromJson(JSONObject o) {
368 NsaAcl acl = new NsaAcl();
370 JSONArray a = o.optJSONArray("allowed");
372 for (int i = 0; i < a.length(); ++i) {
373 String user = a.getString(i);
382 public String getName() {
387 public String getOwner() {
392 public String getDescription() {
397 public NsaAcl getReaderAcl() {
402 public NsaAcl getWriterAcl() {
407 public void checkUserRead(NsaApiKey user) throws AccessDeniedException {
408 NsaAclUtils.checkUserAccess ( fOwner, getReaderAcl(), user );
412 public void checkUserWrite(NsaApiKey user) throws AccessDeniedException {
413 NsaAclUtils.checkUserAccess ( fOwner, getWriterAcl(), user );
417 public void permitWritesFromUser(String pubId, NsaApiKey asUser)
418 throws ConfigDbException, AccessDeniedException {
419 updateAcl(asUser, false, true, pubId);
423 public void denyWritesFromUser(String pubId, NsaApiKey asUser) throws ConfigDbException, AccessDeniedException {
424 updateAcl(asUser, false, false, pubId);
428 public void permitReadsByUser(String consumerId, NsaApiKey asUser)
429 throws ConfigDbException, AccessDeniedException {
430 updateAcl(asUser, true, true, consumerId);
434 public void denyReadsByUser(String consumerId, NsaApiKey asUser)
435 throws ConfigDbException, AccessDeniedException {
436 updateAcl(asUser, true, false, consumerId);
439 private void updateAcl(NsaApiKey asUser, boolean reader, boolean add, String key)
440 throws ConfigDbException, AccessDeniedException{
443 final NsaAcl acl = NsaAclUtils.updateAcl ( this, asUser, key, reader, add );
445 // we have to assume we have current data, or load it again. for the expected use
446 // case, assuming we can overwrite the data is fine.
447 final JSONObject o = new JSONObject ();
448 o.put ( "owner", fOwner );
449 o.put ( "readers", safeSerialize ( reader ? acl : fReaders ) );
450 o.put ( "writers", safeSerialize ( reader ? fWriters : acl ) );
451 fConfigDb.store ( fBaseTopicData.getChild ( fName ), o.toString () );
453 log.info ( "ACL_UPDATE: " + asUser.getKey () + " " + ( add ? "added" : "removed" ) + ( reader?"subscriber":"publisher" ) + " " + key + " on " + fName );
456 catch ( ConfigDbException x )
460 catch ( AccessDeniedException x )
467 private JSONObject safeSerialize(NsaAcl acl) {
468 return acl == null ? null : acl.serialize();
471 private final String fName;
472 private final ConfigDb fConfigDb;
473 private final ConfigPath fBaseTopicData;
474 private final String fOwner;
475 private final String fDesc;
476 private final NsaAcl fReaders;
477 private final NsaAcl fWriters;
478 private boolean fTransactionEnabled;
480 public boolean isTransactionEnabled() {
481 return fTransactionEnabled;
485 public Set<String> getOwners() {
486 final TreeSet<String> owners = new TreeSet<String> ();
487 owners.add ( fOwner );