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