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