cffe448fb1682dfcb1dec0f574efb543952df8f7
[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 io.swagger.annotations.ApiModelProperty;
34
35 import org.onap.dmaap.dbcapi.service.DmaapService;
36 import org.onap.dmaap.dbcapi.service.TopicService;
37
38
39 @XmlRootElement
40 public class Topic extends DmaapObject  {
41
42         @ApiModelProperty( value="Fully Qualified Topic Name constructed by dbcapi, following the rules for `fqtnStyle`")
43         private String fqtn;
44         @ApiModelProperty( value="the short name used by humans, and utilized to construct the `FQTN`")
45         private String topicName;
46         @ApiModelProperty( value="a description of what this Topic is used for")
47         private String  topicDescription;
48         private String  tnxEnabled;
49         @ApiModelProperty( value="a label used to identify who requested this `Topic` to be provisioned.  In the future this "
50                         + "may be an AAF Identity.")
51         private String  owner;
52         @ApiModelProperty( value="a reference to an identifier that describes a data format used for this `Topic`")
53         private String  formatUuid;
54         @ApiModelProperty( value="An indicator for how this `Topic` should be replicated when there are more than one `MR_Cluster` instances")
55         private ReplicationType replicationCase;  
56         @ApiModelProperty( value="the URL of an outside MR instance")
57         private String  globalMrURL;            // optional: URL of global MR to replicate to/from
58         @ApiModelProperty( value="the construction rule for the `fqtn` field")
59         private FqtnType  fqtnStyle;
60         @ApiModelProperty( value="a hook for any versioning needed for managing a `Topic` over time")
61         private String  version;
62         @ApiModelProperty( value="the kafka attribute for specifying the number of partitions")
63         private String  partitionCount;
64         @ApiModelProperty( value="the kafka attribute for specifying replication within an `MR_Cluster` instance")
65         private String  replicationCount;
66         @ApiModelProperty( value="a value generated by dbcapi, this AAF Role has permission to publish to this `Topic`")
67         private String  publisherRole;
68         @ApiModelProperty( value="a value generated by dbcapi, this AAF Role has permission to subscribe to this `Topic`")
69         private String  subscriberRole;
70
71         @ApiModelProperty( value="an array of `MR_Client` objects associated to this `Topic`")
72         private ArrayList<MR_Client> clients;
73
74
75         
76         private static Dmaap dmaap = new DmaapService().getDmaap();
77         
78         private static String defaultPartitionCount;
79         private static String defaultReplicationCount;
80         
81         // during unit testing, discovered that presence of dots in some values
82         // creates an unplanned topic namespace as we compose the FQTN.
83         // this may create sensitivity (i.e. 403) for subsequent creation of AAF perms, so best to not allow it 
84         private static String removeDots( String source, String def ) {
85                 if ( source == null || source.isEmpty()) {
86                         return def;
87                 }
88                 return source.replaceAll("\\.", "_");
89         }
90         //
91         // utility function to generate the FQTN of a topic
92         public  String genFqtn(  ) {
93                 DmaapConfig dc = (DmaapConfig)DmaapConfig.getConfig();
94                 String projectId = dc.getProperty("MR.projectID", "99999");
95                 CharSequence signal = ".";
96                 String ret;
97                 if ( this.getTopicName().contains( signal )) {
98                         // presence of a dot indicates the name is already fully qualified
99                         ret = this.getTopicName();
100                 } else {
101                         // these vars may not contain dots
102                         String p = removeDots( projectId, "90909");
103                         String v = removeDots( this.getVersion(), "v1");
104                         switch( this.getFqtnStyle() ) {
105                         case FQTN_PROJECTID_VERSION_FORMAT:
106
107                                 ret = dmaap.getTopicNsRoot() + "."  + dmaap.getDmaapName() + "." + p + "-" + this.getTopicName()  + "-" + v;
108                                 break;
109                                 
110                         case FQTN_PROJECTID_FORMAT:
111
112                                 ret = dmaap.getTopicNsRoot() + "."  + dmaap.getDmaapName() + "." + p + "-" + this.getTopicName();
113                                 break;
114                         
115                         case FQTN_LEGACY_FORMAT:
116                         default:  // for backwards compatibility
117                                 ret = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + this.getTopicName();
118                                 break;
119                         
120
121                         }
122                         
123                 }
124                 return ret;
125         }
126
127
128
129         public Topic() {
130                 super();
131                 this.clients = new ArrayList<>();
132                 this.lastMod = new Date();
133                 this.replicationCase = ReplicationType.Validator("none");
134                 this.setLastMod();
135                 logger.debug( "Topic constructor " + this.lastMod );
136         }
137         public Topic(String fqtn, String topicName, String topicDescription,
138                          String tnxEnabled, String owner) {
139                 super();
140                 this.fqtn = fqtn;
141                 this.topicName = topicName;
142                 this.topicDescription = topicDescription;
143                 this.tnxEnabled = tnxEnabled;
144                 this.owner = owner;
145                 this.init();
146                 this.setLastMod();
147                 logger.debug( "Topic constructor w args " + this.getLastMod() );
148         }
149         
150         public Topic init() {
151                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
152                 
153                 defaultPartitionCount = p.getProperty( "MR.partitionCount", "2");
154                 defaultReplicationCount = p.getProperty( "MR.replicationCount", "1");
155                 
156                 this.setStatus( DmaapObject_Status.NEW );
157                 this.replicationCase = ReplicationType.Validator("none");
158                 this.fqtnStyle = FqtnType.Validator("none");
159                 this.setPartitionCount( defaultPartitionCount );
160                 this.setReplicationCount( defaultReplicationCount );
161                 
162                 return this;
163         }
164
165         // expects a String in JSON format, with known fields to populate Topic object
166         public Topic ( String json ) {
167                 JSONParser parser = new JSONParser();
168                 JSONObject jsonObj;
169                 try {
170                         jsonObj = (JSONObject) parser.parse( json );
171                 } catch ( ParseException pe ) {
172                    logger.error( "Error parsing provisioning data: " + json );
173                    this.setStatus( DmaapObject_Status.INVALID );
174                    return;
175             }
176                 this.setFqtn( (String) jsonObj.get( "fqtn" ) );
177                 this.setTopicName( (String) jsonObj.get( "topicName" ) );
178                 this.setTopicDescription( (String) jsonObj.get( "topicDescription" ));
179                 this.setOwner( (String) jsonObj.get( "owner" ) );
180                 this.setStatus( (String) jsonObj.get( "status" ) );
181                 this.setReplicationCase( ReplicationType.Validator( (String) jsonObj.get( "replicationCase" ) ));
182                 this.setFqtnStyle( FqtnType.Validator( (String) jsonObj.get( "fqtnStyle" ) ) );
183                 this.setPartitionCount( (String) jsonObj.get("partitionCount"));
184
185         }
186         public String getFqtn() {
187                 return fqtn;
188         }
189         public void setFqtn(String fqtn) {
190                 this.fqtn = fqtn;
191         }
192         public String getTopicName() {
193                 return topicName;
194         }
195         public void setTopicName(String topicName) {
196                 this.topicName = topicName;
197         }
198         public String getTopicDescription() {
199                 return topicDescription;
200         }
201         public void setTopicDescription(String topicDescription) {
202                 this.topicDescription = topicDescription;
203         }
204
205         public String getTnxEnabled() {
206                 return tnxEnabled;
207         }
208         public void setTnxEnabled(String tnxEnabled) {
209                 this.tnxEnabled = tnxEnabled;
210         }
211         public String getOwner() {
212                 return owner;
213         }
214         public void setOwner(String owner) {
215                 this.owner = owner;
216         }
217         public String getPartitionCount() {
218                 return partitionCount;
219         }
220         public void setPartitionCount(String partitions) {
221                 this.partitionCount = partitions;
222         }
223         public String getReplicationCount() {
224                 return replicationCount;
225         }
226         public void setReplicationCount(String replicationCount) {
227                 this.replicationCount = replicationCount;
228         }
229
230
231         public void setClients(ArrayList<MR_Client> clients) {
232                 this.clients = clients;
233         }
234
235         public ArrayList<MR_Client> getClients() {
236                 return clients;
237         }
238
239         @ApiModelProperty( hidden=true )
240         public int getNumClients() {
241                 if ( this.clients == null ) {
242                         return 0;
243                 }
244                 return this.clients.size();
245         }
246
247
248
249
250         public String getFormatUuid() {
251                 return formatUuid;
252         }
253
254
255
256         public void setFormatUuid(String formatUuid) {
257                 this.formatUuid = formatUuid;
258         }
259
260
261         public ReplicationType getReplicationCase() {
262                 return replicationCase;
263         }
264
265         
266         public void setReplicationCase(ReplicationType t) {
267                 this.replicationCase = t;
268         }
269         public FqtnType getFqtnStyle() {
270                 return fqtnStyle;
271         }
272
273         
274         public void setFqtnStyle(FqtnType t) {
275                 this.fqtnStyle = t;
276         }
277
278         public String getGlobalMrURL() {
279                 return globalMrURL;
280         }
281
282
283
284         public void setGlobalMrURL(String globalMrURL) {
285                 this.globalMrURL = globalMrURL;
286         }
287
288
289
290         public String getVersion() {
291                 return version;
292         }
293
294
295
296         public void setVersion(String version) {
297                 this.version = version;
298         }
299
300
301
302         public String getPublisherRole() {
303                 return publisherRole;
304         }
305         public void setPublisherRole(String publisherRole) {
306                 this.publisherRole = publisherRole;
307         }
308         public String getSubscriberRole() {
309                 return subscriberRole;
310         }
311         public void setSubscriberRole(String subscriberRole) {
312                 this.subscriberRole = subscriberRole;
313         }
314         public String toProvJSON() {
315                 StringBuilder str = new StringBuilder();
316                 str.append("{ \"topicName\": \"");
317                 str.append( this.getFqtn() );
318                 str.append( "\", \"topicDescription\": \"");
319                 str.append( this.getTopicDescription());
320                 str.append( "\", \"partitionCount\": \"");
321                 str.append( this.getPartitionCount());
322                 str.append( "\", \"replicationCount\": \"");
323                 str.append( this.getReplicationCount());
324                 str.append( "\" } ");
325                 
326                 logger.info( str.toString() );
327                 return str.toString();
328         }
329         @ApiModelProperty( hidden=true )
330         public byte[] getBytes() {
331                 return toProvJSON().getBytes(StandardCharsets.UTF_8);
332         }
333 }