Introduce default pub/sub Roles for Topic
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / model / Topic.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 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  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dmaap.dbcapi.model;
22
23 import java.nio.charset.StandardCharsets;
24 import java.util.ArrayList;
25 import java.util.Date;
26 import org.json.simple.*;
27 import org.json.simple.parser.*;
28 import javax.xml.bind.annotation.XmlRootElement;
29
30
31 import org.onap.dmaap.dbcapi.util.DmaapConfig;
32
33 import org.onap.dmaap.dbcapi.service.DmaapService;
34 import org.onap.dmaap.dbcapi.service.TopicService;
35
36
37 @XmlRootElement
38 public class Topic extends DmaapObject  {
39
40         private String fqtn;
41         private String topicName;
42         private String  topicDescription;
43         private String  tnxEnabled;
44         private String  owner;
45         private String  formatUuid;
46         private ReplicationType replicationCase;  
47         private String  globalMrURL;            // optional: URL of global MR to replicate to/from
48         private FqtnType  fqtnStyle;
49         private String  version;        
50         private String  partitionCount;
51         private String  replicationCount;
52         private String  publisherRole;
53         private String  subscriberRole;
54
55
56         private ArrayList<MR_Client> clients;
57
58
59         
60         private static Dmaap dmaap = new DmaapService().getDmaap();
61         
62         private static String defaultPartitionCount;
63         private static String defaultReplicationCount;
64         
65         // during unit testing, discovered that presence of dots in some values
66         // creates an unplanned topic namespace as we compose the FQTN.
67         // this may create sensitivity (i.e. 403) for subsequent creation of AAF perms, so best to not allow it 
68         private static String removeDots( String source, String def ) {
69                 if ( source == null || source.isEmpty()) {
70                         return def;
71                 }
72                 return source.replaceAll("\\.", "_");
73         }
74         //
75         // utility function to generate the FQTN of a topic
76         public  String genFqtn(  ) {
77                 DmaapConfig dc = (DmaapConfig)DmaapConfig.getConfig();
78                 String projectId = dc.getProperty("MR.projectID", "99999");
79                 CharSequence signal = ".";
80                 String ret;
81                 if ( this.getTopicName().contains( signal )) {
82                         // presence of a dot indicates the name is already fully qualified
83                         ret = this.getTopicName();
84                 } else {
85                         // these vars may not contain dots
86                         String p = removeDots( projectId, "90909");
87                         String v = removeDots( this.getVersion(), "v1");
88                         switch( this.getFqtnStyle() ) {
89                         case FQTN_PROJECTID_VERSION_FORMAT:
90
91                                 ret = dmaap.getTopicNsRoot() + "."  + dmaap.getDmaapName() + "." + p + "-" + this.getTopicName()  + "-" + v;
92                                 break;
93                                 
94                         case FQTN_PROJECTID_FORMAT:
95
96                                 ret = dmaap.getTopicNsRoot() + "."  + dmaap.getDmaapName() + "." + p + "-" + this.getTopicName();
97                                 break;
98                         
99                         case FQTN_LEGACY_FORMAT:
100                         default:  // for backwards compatibility
101                                 ret = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + this.getTopicName();
102                                 break;
103                         
104
105                         }
106                         
107                 }
108                 return ret;
109         }
110
111
112
113         public Topic() {
114                 super();
115                 this.clients = new ArrayList<MR_Client>();
116                 this.lastMod = new Date();
117                 this.replicationCase = ReplicationType.Validator("none");
118                 this.setLastMod();
119                 logger.debug( "Topic constructor " + this.lastMod );
120         }
121         public Topic(String fqtn, String topicName, String topicDescription,
122                          String tnxEnabled, String owner) {
123                 super();
124                 this.fqtn = fqtn;
125                 this.topicName = topicName;
126                 this.topicDescription = topicDescription;
127                 //this.dcaeLocationName = dcaeLocationName;
128                 this.tnxEnabled = tnxEnabled;
129                 this.owner = owner;
130                 this.init();
131                 this.setLastMod();
132                 logger.debug( "Topic constructor w args " + this.getLastMod() );
133         }
134         
135         public Topic init() {
136                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
137                 
138                 defaultPartitionCount = p.getProperty( "MR.partitionCount", "2");
139                 defaultReplicationCount = p.getProperty( "MR.replicationCount", "1");
140                 
141                 this.setStatus( DmaapObject_Status.NEW );
142                 this.replicationCase = ReplicationType.Validator("none");
143                 this.fqtnStyle = FqtnType.Validator("none");
144                 this.setPartitionCount( defaultPartitionCount );
145                 this.setReplicationCount( defaultReplicationCount );
146                 
147                 return this;
148         }
149
150         // expects a String in JSON format, with known fields to populate Topic object
151         public Topic ( String json ) {
152                 JSONParser parser = new JSONParser();
153                 JSONObject jsonObj;
154                 try {
155                         jsonObj = (JSONObject) parser.parse( json );
156                 } catch ( ParseException pe ) {
157                    logger.error( "Error parsing provisioning data: " + json );
158                    this.setStatus( DmaapObject_Status.INVALID );
159                    return;
160             }
161                 this.setFqtn( (String) jsonObj.get( "fqtn" ) );
162                 this.setTopicName( (String) jsonObj.get( "topicName" ) );
163                 this.setTopicDescription( (String) jsonObj.get( "topicDescription" ));
164                 this.setOwner( (String) jsonObj.get( "owner" ) );
165                 //this.setLastMod();
166                 this.setStatus( (String) jsonObj.get( "status" ) );
167                 this.setReplicationCase( ReplicationType.Validator( (String) jsonObj.get( "replicationCase" ) ));
168                 this.setFqtnStyle( FqtnType.Validator( (String) jsonObj.get( "fqtnStyle" ) ) );
169                 this.setPartitionCount( (String) jsonObj.get("partitionCount"));
170
171         }
172         public String getFqtn() {
173                 return fqtn;
174         }
175         public void setFqtn(String fqtn) {
176                 this.fqtn = fqtn;
177         }
178         public String getTopicName() {
179                 return topicName;
180         }
181         public void setTopicName(String topicName) {
182                 this.topicName = topicName;
183         }
184         public String getTopicDescription() {
185                 return topicDescription;
186         }
187         public void setTopicDescription(String topicDescription) {
188                 this.topicDescription = topicDescription;
189         }
190
191         public String getTnxEnabled() {
192                 return tnxEnabled;
193         }
194         public void setTnxEnabled(String tnxEnabled) {
195                 this.tnxEnabled = tnxEnabled;
196         }
197         public String getOwner() {
198                 return owner;
199         }
200         public void setOwner(String owner) {
201                 this.owner = owner;
202         }
203         public String getPartitionCount() {
204                 return partitionCount;
205         }
206         public void setPartitionCount(String partitions) {
207                 this.partitionCount = partitions;
208         }
209         public String getReplicationCount() {
210                 return replicationCount;
211         }
212         public void setReplicationCount(String replicationCount) {
213                 this.replicationCount = replicationCount;
214         }
215
216
217         public void setClients(ArrayList<MR_Client> clients) {
218                 this.clients = clients;
219         }
220
221         public ArrayList<MR_Client> getClients() {
222                 return clients;
223         }
224
225         public int getNumClients() {
226                 if ( this.clients == null ) {
227                         return 0;
228                 }
229                 return this.clients.size();
230         }
231
232
233
234
235         public String getFormatUuid() {
236                 return formatUuid;
237         }
238
239
240
241         public void setFormatUuid(String formatUuid) {
242                 this.formatUuid = formatUuid;
243         }
244
245
246         public ReplicationType getReplicationCase() {
247                 return replicationCase;
248         }
249
250         
251         public void setReplicationCase(ReplicationType t) {
252                 this.replicationCase = t;
253         }
254         public FqtnType getFqtnStyle() {
255                 return fqtnStyle;
256         }
257
258         
259         public void setFqtnStyle(FqtnType t) {
260                 this.fqtnStyle = t;
261         }
262
263         public String getGlobalMrURL() {
264                 return globalMrURL;
265         }
266
267
268
269         public void setGlobalMrURL(String globalMrURL) {
270                 this.globalMrURL = globalMrURL;
271         }
272
273
274
275         public String getVersion() {
276                 return version;
277         }
278
279
280
281         public void setVersion(String version) {
282                 this.version = version;
283         }
284
285
286
287         public String getPublisherRole() {
288                 return publisherRole;
289         }
290         public void setPublisherRole(String publisherRole) {
291                 this.publisherRole = publisherRole;
292         }
293         public String getSubscriberRole() {
294                 return subscriberRole;
295         }
296         public void setSubscriberRole(String subscriberRole) {
297                 this.subscriberRole = subscriberRole;
298         }
299         public String toProvJSON() {
300                 StringBuilder str = new StringBuilder();
301                 str.append("{ \"topicName\": \"");
302                 str.append( this.getFqtn() );
303                 str.append( "\", \"topicDescription\": \"");
304                 str.append( this.getTopicDescription());
305                 str.append( "\", \"partitionCount\": \"");
306                 str.append( this.getPartitionCount());
307                 str.append( "\", \"replicationCount\": \"");
308                 str.append( this.getReplicationCount());
309                 str.append( "\" } ");
310                 
311                 logger.info( str.toString() );
312                 return str.toString();
313         }
314         
315         public byte[] getBytes() {
316                 return toProvJSON().getBytes(StandardCharsets.UTF_8);
317         }
318 }