Add Initial Code Import
[dmaap/messagerouter/msgrtr.git] / src / main / java / org / onap / dmaap / messagerouter / msgrtr / nsa / cambria / backends / memory / MemoryMetaBroker.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
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
11  *  
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=========================================================
18  *
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 package org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.backends.memory;
23
24 import java.util.HashMap;
25 import java.util.LinkedList;
26 import java.util.List;
27 import java.util.Set;
28 import java.util.TreeSet;
29
30 import org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.metabroker.Broker;
31 import org.onap.dmaap.messagerouter.msgrtr.nsa.cambria.metabroker.Topic;
32
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;
37
38 /**
39  * 
40  * @author author
41  *
42  */
43 public class MemoryMetaBroker implements Broker {
44         /**
45          * 
46          * @param mq
47          * @param configDb
48          * @param settings
49          */
50         public MemoryMetaBroker(MemoryQueue mq, ConfigDb configDb) {
51         //public MemoryMetaBroker(MemoryQueue mq, ConfigDb configDb, rrNvReadable settings) {
52                 fQueue = mq;
53                 fTopics = new HashMap<String, MemTopic>();
54         }
55
56         @Override
57         public List<Topic> getAllTopics() {
58                 return new LinkedList<Topic>(fTopics.values());
59         }
60
61         @Override
62         public Topic getTopic(String topic) {
63                 return fTopics.get(topic);
64         }
65
66         @Override
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);
71                 }
72                 fQueue.createTopic(topic);
73                 fTopics.put(topic, new MemTopic(topic, desc, ownerApiId, transactionEnabled));
74                 return getTopic(topic);
75         }
76
77         @Override
78         public void deleteTopic(String topic) {
79                 fTopics.remove(topic);
80                 fQueue.removeTopic(topic);
81         }
82
83         private final MemoryQueue fQueue;
84         private final HashMap<String, MemTopic> fTopics;
85
86         private static class MemTopic implements Topic {
87                 /**
88                  * constructor initialization
89                  * 
90                  * @param name
91                  * @param desc
92                  * @param owner
93                  * @param transactionEnabled
94                  */
95                 public MemTopic(String name, String desc, String owner, boolean transactionEnabled) {
96                         fName = name;
97                         fDesc = desc;
98                         fOwner = owner;
99                         ftransactionEnabled = transactionEnabled;
100                         fReaders = null;
101                         fWriters = null;
102                 }
103
104                 @Override
105                 public String getOwner() {
106                         return fOwner;
107                 }
108
109                 @Override
110                 public NsaAcl getReaderAcl() {
111                         return fReaders;
112                 }
113
114                 @Override
115                 public NsaAcl getWriterAcl() {
116                         return fWriters;
117                 }
118
119                 @Override
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());
123                         }
124                 }
125
126                 @Override
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());
130                         }
131                 }
132
133                 @Override
134                 public String getName() {
135                         return fName;
136                 }
137
138                 @Override
139                 public String getDescription() {
140                         return fDesc;
141                 }
142
143                 @Override
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);
147                         }
148                         if (fWriters == null) {
149                                 fWriters = new NsaAcl();
150                         }
151                         fWriters.add(publisherId);
152                 }
153
154                 @Override
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);
158                         }
159                         fWriters.remove(publisherId);
160                 }
161
162                 @Override
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);
166                         }
167                         if (fReaders == null) {
168                                 fReaders = new NsaAcl();
169                         }
170                         fReaders.add(consumerId);
171                 }
172
173                 @Override
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);
177                         }
178                         fReaders.remove(consumerId);
179                 }
180
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;
187
188                 @Override
189                 public boolean isTransactionEnabled() {
190                         return ftransactionEnabled;
191                 }
192
193                 @Override
194                 public Set<String> getOwners() {
195                         final TreeSet<String> set = new TreeSet<String> ();
196                         set.add ( fOwner );
197                         return set;
198                 }
199         }
200 }