Patch set 2: changes to MR_Client
[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<MR_Client>();
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.dcaeLocationName = dcaeLocationName;
144                 this.tnxEnabled = tnxEnabled;
145                 this.owner = owner;
146                 this.init();
147                 this.setLastMod();
148                 logger.debug( "Topic constructor w args " + this.getLastMod() );
149         }
150         
151         public Topic init() {
152                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
153                 
154                 defaultPartitionCount = p.getProperty( "MR.partitionCount", "2");
155                 defaultReplicationCount = p.getProperty( "MR.replicationCount", "1");
156                 
157                 this.setStatus( DmaapObject_Status.NEW );
158                 this.replicationCase = ReplicationType.Validator("none");
159                 this.fqtnStyle = FqtnType.Validator("none");
160                 this.setPartitionCount( defaultPartitionCount );
161                 this.setReplicationCount( defaultReplicationCount );
162                 
163                 return this;
164         }
165
166         // expects a String in JSON format, with known fields to populate Topic object
167         public Topic ( String json ) {
168                 JSONParser parser = new JSONParser();
169                 JSONObject jsonObj;
170                 try {
171                         jsonObj = (JSONObject) parser.parse( json );
172                 } catch ( ParseException pe ) {
173                    logger.error( "Error parsing provisioning data: " + json );
174                    this.setStatus( DmaapObject_Status.INVALID );
175                    return;
176             }
177                 this.setFqtn( (String) jsonObj.get( "fqtn" ) );
178                 this.setTopicName( (String) jsonObj.get( "topicName" ) );
179                 this.setTopicDescription( (String) jsonObj.get( "topicDescription" ));
180                 this.setOwner( (String) jsonObj.get( "owner" ) );
181                 //this.setLastMod();
182                 this.setStatus( (String) jsonObj.get( "status" ) );
183                 this.setReplicationCase( ReplicationType.Validator( (String) jsonObj.get( "replicationCase" ) ));
184                 this.setFqtnStyle( FqtnType.Validator( (String) jsonObj.get( "fqtnStyle" ) ) );
185                 this.setPartitionCount( (String) jsonObj.get("partitionCount"));
186
187         }
188         public String getFqtn() {
189                 return fqtn;
190         }
191         public void setFqtn(String fqtn) {
192                 this.fqtn = fqtn;
193         }
194         public String getTopicName() {
195                 return topicName;
196         }
197         public void setTopicName(String topicName) {
198                 this.topicName = topicName;
199         }
200         public String getTopicDescription() {
201                 return topicDescription;
202         }
203         public void setTopicDescription(String topicDescription) {
204                 this.topicDescription = topicDescription;
205         }
206
207         public String getTnxEnabled() {
208                 return tnxEnabled;
209         }
210         public void setTnxEnabled(String tnxEnabled) {
211                 this.tnxEnabled = tnxEnabled;
212         }
213         public String getOwner() {
214                 return owner;
215         }
216         public void setOwner(String owner) {
217                 this.owner = owner;
218         }
219         public String getPartitionCount() {
220                 return partitionCount;
221         }
222         public void setPartitionCount(String partitions) {
223                 this.partitionCount = partitions;
224         }
225         public String getReplicationCount() {
226                 return replicationCount;
227         }
228         public void setReplicationCount(String replicationCount) {
229                 this.replicationCount = replicationCount;
230         }
231
232
233         public void setClients(ArrayList<MR_Client> clients) {
234                 this.clients = clients;
235         }
236
237         public ArrayList<MR_Client> getClients() {
238                 return clients;
239         }
240
241         @ApiModelProperty( hidden=true )
242         public int getNumClients() {
243                 if ( this.clients == null ) {
244                         return 0;
245                 }
246                 return this.clients.size();
247         }
248
249
250
251
252         public String getFormatUuid() {
253                 return formatUuid;
254         }
255
256
257
258         public void setFormatUuid(String formatUuid) {
259                 this.formatUuid = formatUuid;
260         }
261
262
263         public ReplicationType getReplicationCase() {
264                 return replicationCase;
265         }
266
267         
268         public void setReplicationCase(ReplicationType t) {
269                 this.replicationCase = t;
270         }
271         public FqtnType getFqtnStyle() {
272                 return fqtnStyle;
273         }
274
275         
276         public void setFqtnStyle(FqtnType t) {
277                 this.fqtnStyle = t;
278         }
279
280         public String getGlobalMrURL() {
281                 return globalMrURL;
282         }
283
284
285
286         public void setGlobalMrURL(String globalMrURL) {
287                 this.globalMrURL = globalMrURL;
288         }
289
290
291
292         public String getVersion() {
293                 return version;
294         }
295
296
297
298         public void setVersion(String version) {
299                 this.version = version;
300         }
301
302
303
304         public String getPublisherRole() {
305                 return publisherRole;
306         }
307         public void setPublisherRole(String publisherRole) {
308                 this.publisherRole = publisherRole;
309         }
310         public String getSubscriberRole() {
311                 return subscriberRole;
312         }
313         public void setSubscriberRole(String subscriberRole) {
314                 this.subscriberRole = subscriberRole;
315         }
316         public String toProvJSON() {
317                 StringBuilder str = new StringBuilder();
318                 str.append("{ \"topicName\": \"");
319                 str.append( this.getFqtn() );
320                 str.append( "\", \"topicDescription\": \"");
321                 str.append( this.getTopicDescription());
322                 str.append( "\", \"partitionCount\": \"");
323                 str.append( this.getPartitionCount());
324                 str.append( "\", \"replicationCount\": \"");
325                 str.append( this.getReplicationCount());
326                 str.append( "\" } ");
327                 
328                 logger.info( str.toString() );
329                 return str.toString();
330         }
331         @ApiModelProperty( hidden=true )
332         public byte[] getBytes() {
333                 return toProvJSON().getBytes(StandardCharsets.UTF_8);
334         }
335 }