2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dmaap.dbcapi.model;
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;
32 import org.onap.dmaap.dbcapi.util.DmaapConfig;
34 import io.swagger.annotations.ApiModelProperty;
36 import org.onap.dmaap.dbcapi.service.DmaapService;
37 import org.onap.dmaap.dbcapi.service.TopicService;
41 public class Topic extends DmaapObject {
43 @ApiModelProperty( value="Fully Qualified Topic Name constructed by dbcapi, following the rules for `fqtnStyle`")
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.")
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;
72 @ApiModelProperty( value="an array of `MR_Client` objects associated to this `Topic`")
73 private ArrayList<MR_Client> clients;
77 private static Dmaap dmaap = new DmaapService().getDmaap();
79 private static String defaultPartitionCount;
80 private static String defaultReplicationCount;
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()) {
89 return source.replaceAll("\\.", "_");
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 = ".";
98 if ( this.getTopicName().contains( signal )) {
99 // presence of a dot indicates the name is already fully qualified
100 ret = this.getTopicName();
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:
108 ret = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + p + "-" + this.getTopicName() + "-" + v;
111 case FQTN_PROJECTID_FORMAT:
113 ret = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + p + "-" + this.getTopicName();
116 case FQTN_LEGACY_FORMAT:
117 default: // for backwards compatibility
118 ret = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + this.getTopicName();
132 this.clients = new ArrayList<>();
133 this.lastMod = new Date();
134 this.replicationCase = ReplicationType.Validator("none");
136 logger.debug( "Topic constructor " + this.lastMod );
138 public Topic(String fqtn, String topicName, String topicDescription,
139 String tnxEnabled, String owner) {
142 this.topicName = topicName;
143 this.topicDescription = topicDescription;
144 this.tnxEnabled = tnxEnabled;
148 logger.debug( "Topic constructor w args " + this.getLastMod() );
151 public Topic init() {
152 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
154 defaultPartitionCount = p.getProperty( "MR.partitionCount", "2");
155 defaultReplicationCount = p.getProperty( "MR.replicationCount", "1");
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 );
166 // expects a String in JSON format, with known fields to populate Topic object
167 public Topic ( String json ) {
168 JSONParser parser = new JSONParser();
171 jsonObj = (JSONObject) parser.parse( json );
172 } catch ( ParseException pe ) {
173 logger.error( "Error parsing provisioning data: " + json );
174 this.setStatus( DmaapObject_Status.INVALID );
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"));
187 public String getFqtn() {
190 public void setFqtn(String fqtn) {
193 public String getTopicName() {
196 public void setTopicName(String topicName) {
197 this.topicName = topicName;
199 public String getTopicDescription() {
200 return topicDescription;
202 public void setTopicDescription(String topicDescription) {
203 this.topicDescription = topicDescription;
206 public String getTnxEnabled() {
209 public void setTnxEnabled(String tnxEnabled) {
210 this.tnxEnabled = tnxEnabled;
212 public String getOwner() {
215 public void setOwner(String owner) {
218 public String getPartitionCount() {
219 return partitionCount;
221 public void setPartitionCount(String partitions) {
222 this.partitionCount = partitions;
224 public String getReplicationCount() {
225 return replicationCount;
227 public void setReplicationCount(String replicationCount) {
228 this.replicationCount = replicationCount;
232 public void setClients(ArrayList<MR_Client> clients) {
233 this.clients = clients;
236 public ArrayList<MR_Client> getClients() {
240 @ApiModelProperty( hidden=true )
241 public int getNumClients() {
242 if ( this.clients == null ) {
245 return this.clients.size();
251 public String getFormatUuid() {
257 public void setFormatUuid(String formatUuid) {
258 this.formatUuid = formatUuid;
262 public ReplicationType getReplicationCase() {
263 return replicationCase;
267 public void setReplicationCase(ReplicationType t) {
268 this.replicationCase = t;
270 public FqtnType getFqtnStyle() {
275 public void setFqtnStyle(FqtnType t) {
279 public String getGlobalMrURL() {
285 public void setGlobalMrURL(String globalMrURL) {
286 this.globalMrURL = globalMrURL;
291 public String getVersion() {
297 public void setVersion(String version) {
298 this.version = version;
303 public String getPublisherRole() {
304 return publisherRole;
306 public void setPublisherRole(String publisherRole) {
307 this.publisherRole = publisherRole;
309 public String getSubscriberRole() {
310 return subscriberRole;
312 public void setSubscriberRole(String subscriberRole) {
313 this.subscriberRole = subscriberRole;
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( "\" } ");
327 logger.info( str.toString() );
328 return str.toString();
330 @ApiModelProperty( hidden=true )
331 public byte[] getBytes() {
332 return toProvJSON().getBytes(StandardCharsets.UTF_8);
337 public boolean equals(Object o) {
341 if (o == null || getClass() != o.getClass()) {
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);
352 public int hashCode() {
353 return Objects.hashCode(fqtn, topicName, tnxEnabled, owner);