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 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;
31 import org.onap.dmaap.dbcapi.util.DmaapConfig;
33 import org.onap.dmaap.dbcapi.service.DmaapService;
34 import org.onap.dmaap.dbcapi.service.TopicService;
38 public class Topic extends DmaapObject {
41 private String topicName;
42 private String topicDescription;
43 private String tnxEnabled;
45 private String formatUuid;
46 private ReplicationType replicationCase;
47 private String globalMrURL; // optional: URL of global MR to replicate to/from
48 private FqtnType fqtnStyle;
49 private String version;
50 private String partitionCount;
51 private String replicationCount;
52 private String publisherRole;
53 private String subscriberRole;
56 private ArrayList<MR_Client> clients;
60 private static Dmaap dmaap = new DmaapService().getDmaap();
62 private static String defaultPartitionCount;
63 private static String defaultReplicationCount;
65 // during unit testing, discovered that presence of dots in some values
66 // creates an unplanned topic namespace as we compose the FQTN.
67 // this may create sensitivity (i.e. 403) for subsequent creation of AAF perms, so best to not allow it
68 private static String removeDots( String source, String def ) {
69 if ( source == null || source.isEmpty()) {
72 return source.replaceAll("\\.", "_");
75 // utility function to generate the FQTN of a topic
76 public String genFqtn( ) {
77 DmaapConfig dc = (DmaapConfig)DmaapConfig.getConfig();
78 String projectId = dc.getProperty("MR.projectID", "99999");
79 CharSequence signal = ".";
81 if ( this.getTopicName().contains( signal )) {
82 // presence of a dot indicates the name is already fully qualified
83 ret = this.getTopicName();
85 // these vars may not contain dots
86 String p = removeDots( projectId, "90909");
87 String v = removeDots( this.getVersion(), "v1");
88 switch( this.getFqtnStyle() ) {
89 case FQTN_PROJECTID_VERSION_FORMAT:
91 ret = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + p + "-" + this.getTopicName() + "-" + v;
94 case FQTN_PROJECTID_FORMAT:
96 ret = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + p + "-" + this.getTopicName();
99 case FQTN_LEGACY_FORMAT:
100 default: // for backwards compatibility
101 ret = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + this.getTopicName();
115 this.clients = new ArrayList<MR_Client>();
116 this.lastMod = new Date();
117 this.replicationCase = ReplicationType.Validator("none");
119 logger.debug( "Topic constructor " + this.lastMod );
121 public Topic(String fqtn, String topicName, String topicDescription,
122 String tnxEnabled, String owner) {
125 this.topicName = topicName;
126 this.topicDescription = topicDescription;
127 //this.dcaeLocationName = dcaeLocationName;
128 this.tnxEnabled = tnxEnabled;
132 logger.debug( "Topic constructor w args " + this.getLastMod() );
135 public Topic init() {
136 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
138 defaultPartitionCount = p.getProperty( "MR.partitionCount", "2");
139 defaultReplicationCount = p.getProperty( "MR.replicationCount", "1");
141 this.setStatus( DmaapObject_Status.NEW );
142 this.replicationCase = ReplicationType.Validator("none");
143 this.fqtnStyle = FqtnType.Validator("none");
144 this.setPartitionCount( defaultPartitionCount );
145 this.setReplicationCount( defaultReplicationCount );
150 // expects a String in JSON format, with known fields to populate Topic object
151 public Topic ( String json ) {
152 JSONParser parser = new JSONParser();
155 jsonObj = (JSONObject) parser.parse( json );
156 } catch ( ParseException pe ) {
157 logger.error( "Error parsing provisioning data: " + json );
158 this.setStatus( DmaapObject_Status.INVALID );
161 this.setFqtn( (String) jsonObj.get( "fqtn" ) );
162 this.setTopicName( (String) jsonObj.get( "topicName" ) );
163 this.setTopicDescription( (String) jsonObj.get( "topicDescription" ));
164 this.setOwner( (String) jsonObj.get( "owner" ) );
166 this.setStatus( (String) jsonObj.get( "status" ) );
167 this.setReplicationCase( ReplicationType.Validator( (String) jsonObj.get( "replicationCase" ) ));
168 this.setFqtnStyle( FqtnType.Validator( (String) jsonObj.get( "fqtnStyle" ) ) );
169 this.setPartitionCount( (String) jsonObj.get("partitionCount"));
172 public String getFqtn() {
175 public void setFqtn(String fqtn) {
178 public String getTopicName() {
181 public void setTopicName(String topicName) {
182 this.topicName = topicName;
184 public String getTopicDescription() {
185 return topicDescription;
187 public void setTopicDescription(String topicDescription) {
188 this.topicDescription = topicDescription;
191 public String getTnxEnabled() {
194 public void setTnxEnabled(String tnxEnabled) {
195 this.tnxEnabled = tnxEnabled;
197 public String getOwner() {
200 public void setOwner(String owner) {
203 public String getPartitionCount() {
204 return partitionCount;
206 public void setPartitionCount(String partitions) {
207 this.partitionCount = partitions;
209 public String getReplicationCount() {
210 return replicationCount;
212 public void setReplicationCount(String replicationCount) {
213 this.replicationCount = replicationCount;
217 public void setClients(ArrayList<MR_Client> clients) {
218 this.clients = clients;
221 public ArrayList<MR_Client> getClients() {
225 public int getNumClients() {
226 if ( this.clients == null ) {
229 return this.clients.size();
235 public String getFormatUuid() {
241 public void setFormatUuid(String formatUuid) {
242 this.formatUuid = formatUuid;
246 public ReplicationType getReplicationCase() {
247 return replicationCase;
251 public void setReplicationCase(ReplicationType t) {
252 this.replicationCase = t;
254 public FqtnType getFqtnStyle() {
259 public void setFqtnStyle(FqtnType t) {
263 public String getGlobalMrURL() {
269 public void setGlobalMrURL(String globalMrURL) {
270 this.globalMrURL = globalMrURL;
275 public String getVersion() {
281 public void setVersion(String version) {
282 this.version = version;
287 public String getPublisherRole() {
288 return publisherRole;
290 public void setPublisherRole(String publisherRole) {
291 this.publisherRole = publisherRole;
293 public String getSubscriberRole() {
294 return subscriberRole;
296 public void setSubscriberRole(String subscriberRole) {
297 this.subscriberRole = subscriberRole;
299 public String toProvJSON() {
300 StringBuilder str = new StringBuilder();
301 str.append("{ \"topicName\": \"");
302 str.append( this.getFqtn() );
303 str.append( "\", \"topicDescription\": \"");
304 str.append( this.getTopicDescription());
305 str.append( "\", \"partitionCount\": \"");
306 str.append( this.getPartitionCount());
307 str.append( "\", \"replicationCount\": \"");
308 str.append( this.getReplicationCount());
309 str.append( "\" } ");
311 logger.info( str.toString() );
312 return str.toString();
315 public byte[] getBytes() {
316 return toProvJSON().getBytes(StandardCharsets.UTF_8);