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.dmf.mr.backends.memory;
24 import com.att.nsa.configs.ConfigDb;
25 import com.att.nsa.security.NsaAcl;
26 import com.att.nsa.security.NsaApiKey;
27 import org.onap.dmaap.dmf.mr.metabroker.Broker;
28 import org.onap.dmaap.dmf.mr.metabroker.Topic;
34 * @author anowarul.islam
37 public class MemoryMetaBroker implements Broker {
39 private final MemoryQueue fQueue;
40 private final HashMap<String, MemTopic> fTopics;
48 public MemoryMetaBroker(MemoryQueue mq, ConfigDb configDb) {
51 fTopics = new HashMap<>();
55 public List<Topic> getAllTopics() {
56 return new LinkedList<Topic>(fTopics.values());
60 public Topic getTopic(String topic) {
61 return fTopics.get(topic);
65 public Topic createTopic(String topic, String desc, String ownerApiId, int partitions, int replicas,
66 boolean transactionEnabled) throws TopicExistsException {
67 if (getTopic(topic) != null) {
68 throw new TopicExistsException(topic);
70 fQueue.createTopic(topic);
71 fTopics.put(topic, new MemTopic(topic, desc, ownerApiId, transactionEnabled));
72 return getTopic(topic);
76 public void deleteTopic(String topic) {
77 fTopics.remove(topic);
78 fQueue.removeTopic(topic);
81 private static class MemTopic implements Topic {
83 private final String fName;
84 private final String fDesc;
85 private final String fOwner;
86 private NsaAcl fReaders;
87 private NsaAcl fWriters;
88 private boolean ftransactionEnabled;
89 private String accessDenied = "User does not own this topic ";
92 * constructor initialization
97 * @param transactionEnabled
99 public MemTopic(String name, String desc, String owner, boolean transactionEnabled) {
103 ftransactionEnabled = transactionEnabled;
109 public String getOwner() {
114 public NsaAcl getReaderAcl() {
119 public NsaAcl getWriterAcl() {
124 public void checkUserRead(NsaApiKey user) throws AccessDeniedException {
125 if (fReaders != null && (user == null || !fReaders.canUser(user.getKey()))) {
126 throw new AccessDeniedException(user == null ? "" : user.getKey());
131 public void checkUserWrite(NsaApiKey user) throws AccessDeniedException {
132 if (fWriters != null && (user == null || !fWriters.canUser(user.getKey()))) {
133 throw new AccessDeniedException(user == null ? "" : user.getKey());
138 public String getName() {
143 public String getDescription() {
148 public void permitWritesFromUser(String publisherId, NsaApiKey asUser) throws AccessDeniedException {
149 if (!fOwner.equals(asUser.getKey())) {
150 throw new AccessDeniedException(accessDenied + fName);
152 if (fWriters == null) {
153 fWriters = new NsaAcl();
155 fWriters.add(publisherId);
159 public void denyWritesFromUser(String publisherId, NsaApiKey asUser) throws AccessDeniedException {
160 if (!fOwner.equals(asUser.getKey())) {
161 throw new AccessDeniedException(accessDenied + fName);
163 fWriters.remove(publisherId);
167 public void permitReadsByUser(String consumerId, NsaApiKey asUser) throws AccessDeniedException {
168 if (!fOwner.equals(asUser.getKey())) {
169 throw new AccessDeniedException(accessDenied + fName);
171 if (fReaders == null) {
172 fReaders = new NsaAcl();
174 fReaders.add(consumerId);
178 public void denyReadsByUser(String consumerId, NsaApiKey asUser) throws AccessDeniedException {
179 if (!fOwner.equals(asUser.getKey())) {
180 throw new AccessDeniedException(accessDenied + fName);
182 fReaders.remove(consumerId);
186 public boolean isTransactionEnabled() {
187 return ftransactionEnabled;
191 public Set<String> getOwners() {
192 final TreeSet<String> set = new TreeSet<> ();