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.messagerouter.msgrtr.nsa.cambria.backends.memory;
24 import java.util.HashMap;
25 import java.util.LinkedList;
26 import java.util.List;
28 import java.util.TreeSet;
30 import org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.metabroker.Broker;
31 import org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.metabroker.Topic;
33 import com.att.nsa.configs.ConfigDb;
34 import com.att.nsa.drumlin.till.nv.rrNvReadable;
35 import com.att.nsa.security.NsaAcl;
36 import com.att.nsa.security.NsaApiKey;
43 public class MemoryMetaBroker implements Broker {
50 public MemoryMetaBroker(MemoryQueue mq, ConfigDb configDb) {
51 //public MemoryMetaBroker(MemoryQueue mq, ConfigDb configDb, rrNvReadable settings) {
53 fTopics = new HashMap<String, MemTopic>();
57 public List<Topic> getAllTopics() {
58 return new LinkedList<Topic>(fTopics.values());
62 public Topic getTopic(String topic) {
63 return fTopics.get(topic);
67 public Topic createTopic(String topic, String desc, String ownerApiId, int partitions, int replicas,
68 boolean transactionEnabled) throws TopicExistsException {
69 if (getTopic(topic) != null) {
70 throw new TopicExistsException(topic);
72 fQueue.createTopic(topic);
73 fTopics.put(topic, new MemTopic(topic, desc, ownerApiId, transactionEnabled));
74 return getTopic(topic);
78 public void deleteTopic(String topic) {
79 fTopics.remove(topic);
80 fQueue.removeTopic(topic);
83 private final MemoryQueue fQueue;
84 private final HashMap<String, MemTopic> fTopics;
86 private static class MemTopic implements Topic {
88 * constructor initialization
93 * @param transactionEnabled
95 public MemTopic(String name, String desc, String owner, boolean transactionEnabled) {
99 ftransactionEnabled = transactionEnabled;
105 public String getOwner() {
110 public NsaAcl getReaderAcl() {
115 public NsaAcl getWriterAcl() {
120 public void checkUserRead(NsaApiKey user) throws AccessDeniedException {
121 if (fReaders != null && (user == null || !fReaders.canUser(user.getKey()))) {
122 throw new AccessDeniedException(user == null ? "" : user.getKey());
127 public void checkUserWrite(NsaApiKey user) throws AccessDeniedException {
128 if (fWriters != null && (user == null || !fWriters.canUser(user.getKey()))) {
129 throw new AccessDeniedException(user == null ? "" : user.getKey());
134 public String getName() {
139 public String getDescription() {
144 public void permitWritesFromUser(String publisherId, NsaApiKey asUser) throws AccessDeniedException {
145 if (!fOwner.equals(asUser.getKey())) {
146 throw new AccessDeniedException("User does not own this topic " + fName);
148 if (fWriters == null) {
149 fWriters = new NsaAcl();
151 fWriters.add(publisherId);
155 public void denyWritesFromUser(String publisherId, NsaApiKey asUser) throws AccessDeniedException {
156 if (!fOwner.equals(asUser.getKey())) {
157 throw new AccessDeniedException("User does not own this topic " + fName);
159 fWriters.remove(publisherId);
163 public void permitReadsByUser(String consumerId, NsaApiKey asUser) throws AccessDeniedException {
164 if (!fOwner.equals(asUser.getKey())) {
165 throw new AccessDeniedException("User does not own this topic " + fName);
167 if (fReaders == null) {
168 fReaders = new NsaAcl();
170 fReaders.add(consumerId);
174 public void denyReadsByUser(String consumerId, NsaApiKey asUser) throws AccessDeniedException {
175 if (!fOwner.equals(asUser.getKey())) {
176 throw new AccessDeniedException("User does not own this topic " + fName);
178 fReaders.remove(consumerId);
181 private final String fName;
182 private final String fDesc;
183 private final String fOwner;
184 private NsaAcl fReaders;
185 private NsaAcl fWriters;
186 private boolean ftransactionEnabled;
189 public boolean isTransactionEnabled() {
190 return ftransactionEnabled;
194 public Set<String> getOwners() {
195 final TreeSet<String> set = new TreeSet<String> ();