c2f278d23d5ef25ed9cd20697b1cf43dd6ea42bf
[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
53
54         private ArrayList<MR_Client> clients;
55
56
57         
58         private static Dmaap dmaap = new DmaapService().getDmaap();
59         
60         private static String defaultPartitionCount;
61         private static String defaultReplicationCount;
62         
63         // during unit testing, discovered that presence of dots in some values
64         // creates an unplanned topic namespace as we compose the FQTN.
65         // this may create sensitivity (i.e. 403) for subsequent creation of AAF perms, so best to not allow it 
66         private static String removeDots( String source, String def ) {
67                 if ( source == null || source.isEmpty()) {
68                         return def;
69                 }
70                 return source.replaceAll("\\.", "_");
71         }
72         //
73         // utility function to generate the FQTN of a topic
74         public  String genFqtn(  ) {
75                 DmaapConfig dc = (DmaapConfig)DmaapConfig.getConfig();
76                 String projectId = dc.getProperty("MR.projectID", "99999");
77                 CharSequence signal = ".";
78                 String ret;
79                 if ( this.getTopicName().contains( signal )) {
80                         // presence of a dot indicates the name is already fully qualified
81                         ret = this.getTopicName();
82                 } else {
83                         // these vars may not contain dots
84                         String p = removeDots( projectId, "90909");
85                         String v = removeDots( this.getVersion(), "v1");
86                         switch( this.getFqtnStyle() ) {
87                         case FQTN_PROJECTID_VERSION_FORMAT:
88
89                                 ret = dmaap.getTopicNsRoot() + "."  + dmaap.getDmaapName() + "." + p + "-" + this.getTopicName()  + "-" + v;
90                                 break;
91                                 
92                         case FQTN_PROJECTID_FORMAT:
93
94                                 ret = dmaap.getTopicNsRoot() + "."  + dmaap.getDmaapName() + "." + p + "-" + this.getTopicName();
95                                 break;
96                         
97                         case FQTN_LEGACY_FORMAT:
98                         default:  // for backwards compatibility
99                                 ret = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + this.getTopicName();
100                                 break;
101                         
102
103                         }
104                         
105                 }
106                 return ret;
107         }
108
109
110
111         public Topic() {
112                 super();
113                 this.clients = new ArrayList<MR_Client>();
114                 this.lastMod = new Date();
115                 this.replicationCase = ReplicationType.Validator("none");
116                 this.setLastMod();
117                 logger.debug( "Topic constructor " + this.lastMod );
118         }
119         public Topic(String fqtn, String topicName, String topicDescription,
120                          String tnxEnabled, String owner) {
121                 super();
122                 this.fqtn = fqtn;
123                 this.topicName = topicName;
124                 this.topicDescription = topicDescription;
125                 //this.dcaeLocationName = dcaeLocationName;
126                 this.tnxEnabled = tnxEnabled;
127                 this.owner = owner;
128                 this.init();
129                 this.setLastMod();
130                 logger.debug( "Topic constructor w args " + this.getLastMod() );
131         }
132         
133         public Topic init() {
134                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
135                 
136                 defaultPartitionCount = p.getProperty( "MR.partitionCount", "2");
137                 defaultReplicationCount = p.getProperty( "MR.replicationCount", "1");
138                 
139                 this.setStatus( DmaapObject_Status.NEW );
140                 this.replicationCase = ReplicationType.Validator("none");
141                 this.fqtnStyle = FqtnType.Validator("none");
142                 this.setPartitionCount( defaultPartitionCount );
143                 this.setReplicationCount( defaultReplicationCount );
144                 
145                 return this;
146         }
147
148         // expects a String in JSON format, with known fields to populate Topic object
149         public Topic ( String json ) {
150                 JSONParser parser = new JSONParser();
151                 JSONObject jsonObj;
152                 try {
153                         jsonObj = (JSONObject) parser.parse( json );
154                 } catch ( ParseException pe ) {
155                    logger.error( "Error parsing provisioning data: " + json );
156                    this.setStatus( DmaapObject_Status.INVALID );
157                    return;
158             }
159                 this.setFqtn( (String) jsonObj.get( "fqtn" ) );
160                 this.setTopicName( (String) jsonObj.get( "topicName" ) );
161                 this.setTopicDescription( (String) jsonObj.get( "topicDescription" ));
162                 this.setOwner( (String) jsonObj.get( "owner" ) );
163                 //this.setLastMod();
164                 this.setStatus( (String) jsonObj.get( "status" ) );
165                 this.setReplicationCase( ReplicationType.Validator( (String) jsonObj.get( "replicationCase" ) ));
166                 this.setFqtnStyle( FqtnType.Validator( (String) jsonObj.get( "fqtnStyle" ) ) );
167                 this.setPartitionCount( (String) jsonObj.get("partitionCount"));
168
169         }
170         public String getFqtn() {
171                 return fqtn;
172         }
173         public void setFqtn(String fqtn) {
174                 this.fqtn = fqtn;
175         }
176         public String getTopicName() {
177                 return topicName;
178         }
179         public void setTopicName(String topicName) {
180                 this.topicName = topicName;
181         }
182         public String getTopicDescription() {
183                 return topicDescription;
184         }
185         public void setTopicDescription(String topicDescription) {
186                 this.topicDescription = topicDescription;
187         }
188
189         public String getTnxEnabled() {
190                 return tnxEnabled;
191         }
192         public void setTnxEnabled(String tnxEnabled) {
193                 this.tnxEnabled = tnxEnabled;
194         }
195         public String getOwner() {
196                 return owner;
197         }
198         public void setOwner(String owner) {
199                 this.owner = owner;
200         }
201         public String getPartitionCount() {
202                 return partitionCount;
203         }
204         public void setPartitionCount(String partitions) {
205                 this.partitionCount = partitions;
206         }
207         public String getReplicationCount() {
208                 return replicationCount;
209         }
210         public void setReplicationCount(String replicationCount) {
211                 this.replicationCount = replicationCount;
212         }
213
214
215         public void setClients(ArrayList<MR_Client> clients) {
216                 this.clients = clients;
217         }
218
219         public ArrayList<MR_Client> getClients() {
220                 return clients;
221         }
222
223         public int getNumClients() {
224                 if ( this.clients == null ) {
225                         return 0;
226                 }
227                 return this.clients.size();
228         }
229
230
231
232
233         public String getFormatUuid() {
234                 return formatUuid;
235         }
236
237
238
239         public void setFormatUuid(String formatUuid) {
240                 this.formatUuid = formatUuid;
241         }
242
243
244         public ReplicationType getReplicationCase() {
245                 return replicationCase;
246         }
247
248         
249         public void setReplicationCase(ReplicationType t) {
250                 this.replicationCase = t;
251         }
252         public FqtnType getFqtnStyle() {
253                 return fqtnStyle;
254         }
255
256         
257         public void setFqtnStyle(FqtnType t) {
258                 this.fqtnStyle = t;
259         }
260
261         public String getGlobalMrURL() {
262                 return globalMrURL;
263         }
264
265
266
267         public void setGlobalMrURL(String globalMrURL) {
268                 this.globalMrURL = globalMrURL;
269         }
270
271
272
273         public String getVersion() {
274                 return version;
275         }
276
277
278
279         public void setVersion(String version) {
280                 this.version = version;
281         }
282
283
284
285         public String toProvJSON() {
286                 StringBuilder str = new StringBuilder();
287                 str.append("{ \"topicName\": \"");
288                 str.append( this.getFqtn() );
289                 str.append( "\", \"topicDescription\": \"");
290                 str.append( this.getTopicDescription());
291                 str.append( "\", \"partitionCount\": \"");
292                 str.append( this.getPartitionCount());
293                 str.append( "\", \"replicationCount\": \"");
294                 str.append( this.getReplicationCount());
295                 str.append( "\" } ");
296                 
297                 logger.info( str.toString() );
298                 return str.toString();
299         }
300         
301         public byte[] getBytes() {
302                 return toProvJSON().getBytes(StandardCharsets.UTF_8);
303         }
304 }