DMAAP-MR - Merge MR repos
[dmaap/messagerouter/messageservice.git] / src / main / java / org / onap / dmaap / dmf / mr / 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.dmf.mr.backends.memory;
23
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;
29
30 import java.util.*;
31
32 /**
33  * 
34  * @author anowarul.islam
35  *
36  */
37 public class MemoryMetaBroker implements Broker {
38
39         private final MemoryQueue fQueue;
40         private final HashMap<String, MemTopic> fTopics;
41         
42         /**
43          * 
44          * @param mq
45          * @param configDb
46          * @param settings
47          */
48         public MemoryMetaBroker(MemoryQueue mq, ConfigDb configDb) {
49         
50                 fQueue = mq;
51                 fTopics = new HashMap<>();
52         }
53
54         @Override
55         public List<Topic> getAllTopics() {
56                 return new LinkedList<Topic>(fTopics.values());
57         }
58
59         @Override
60         public Topic getTopic(String topic) {
61                 return fTopics.get(topic);
62         }
63
64         @Override
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);
69                 }
70                 fQueue.createTopic(topic);
71                 fTopics.put(topic, new MemTopic(topic, desc, ownerApiId, transactionEnabled));
72                 return getTopic(topic);
73         }
74
75         @Override
76         public void deleteTopic(String topic) {
77                 fTopics.remove(topic);
78                 fQueue.removeTopic(topic);
79         }
80
81         private static class MemTopic implements Topic {
82
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 ";
90                 
91                 /**
92                  * constructor initialization
93                  * 
94                  * @param name
95                  * @param desc
96                  * @param owner
97                  * @param transactionEnabled
98                  */
99                 public MemTopic(String name, String desc, String owner, boolean transactionEnabled) {
100                         fName = name;
101                         fDesc = desc;
102                         fOwner = owner;
103                         ftransactionEnabled = transactionEnabled;
104                         fReaders = null;
105                         fWriters = null;
106                 }
107
108                 @Override
109                 public String getOwner() {
110                         return fOwner;
111                 }
112
113                 @Override
114                 public NsaAcl getReaderAcl() {
115                         return fReaders;
116                 }
117
118                 @Override
119                 public NsaAcl getWriterAcl() {
120                         return fWriters;
121                 }
122
123                 @Override
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());
127                         }
128                 }
129
130                 @Override
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());
134                         }
135                 }
136
137                 @Override
138                 public String getName() {
139                         return fName;
140                 }
141
142                 @Override
143                 public String getDescription() {
144                         return fDesc;
145                 }
146
147                 @Override
148                 public void permitWritesFromUser(String publisherId, NsaApiKey asUser) throws AccessDeniedException {
149                         if (!fOwner.equals(asUser.getKey())) {
150                                 throw new AccessDeniedException(accessDenied + fName);
151                         }
152                         if (fWriters == null) {
153                                 fWriters = new NsaAcl();
154                         }
155                         fWriters.add(publisherId);
156                 }
157
158                 @Override
159                 public void denyWritesFromUser(String publisherId, NsaApiKey asUser) throws AccessDeniedException {
160                         if (!fOwner.equals(asUser.getKey())) {
161                                 throw new AccessDeniedException(accessDenied + fName);
162                         }
163                         fWriters.remove(publisherId);
164                 }
165
166                 @Override
167                 public void permitReadsByUser(String consumerId, NsaApiKey asUser) throws AccessDeniedException {
168                         if (!fOwner.equals(asUser.getKey())) {
169                                 throw new AccessDeniedException(accessDenied + fName);
170                         }
171                         if (fReaders == null) {
172                                 fReaders = new NsaAcl();
173                         }
174                         fReaders.add(consumerId);
175                 }
176
177                 @Override
178                 public void denyReadsByUser(String consumerId, NsaApiKey asUser) throws AccessDeniedException {
179                         if (!fOwner.equals(asUser.getKey())) {
180                                 throw new AccessDeniedException(accessDenied + fName);
181                         }
182                         fReaders.remove(consumerId);
183                 }
184
185                 @Override
186                 public boolean isTransactionEnabled() {
187                         return ftransactionEnabled;
188                 }
189
190                 @Override
191                 public Set<String> getOwners() {
192                         final TreeSet<String> set = new TreeSet<> ();
193                         set.add ( fOwner );
194                         return set;
195                 }
196         }
197 }