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;
54 private ArrayList<MR_Client> clients;
58 private static Dmaap dmaap = new DmaapService().getDmaap();
60 private static String defaultPartitionCount;
61 private static String defaultReplicationCount;
63 // during unit testing, discovered that presence of dots in some values
64 // creates an unplanned topic namespace as we compose the FQTN.
65 // this may create sensitivity (i.e. 403) for subsequent creation of AAF perms, so best to not allow it
66 private static String removeDots( String source, String def ) {
67 if ( source == null || source.isEmpty()) {
70 return source.replaceAll("\\.", "_");
73 // utility function to generate the FQTN of a topic
74 public String genFqtn( ) {
75 DmaapConfig dc = (DmaapConfig)DmaapConfig.getConfig();
76 String projectId = dc.getProperty("MR.projectID", "99999");
77 CharSequence signal = ".";
79 if ( this.getTopicName().contains( signal )) {
80 // presence of a dot indicates the name is already fully qualified
81 ret = this.getTopicName();
83 // these vars may not contain dots
84 String p = removeDots( projectId, "90909");
85 String v = removeDots( this.getVersion(), "v1");
86 switch( this.getFqtnStyle() ) {
87 case FQTN_PROJECTID_VERSION_FORMAT:
89 ret = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + p + "-" + this.getTopicName() + "-" + v;
92 case FQTN_PROJECTID_FORMAT:
94 ret = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + p + "-" + this.getTopicName();
97 case FQTN_LEGACY_FORMAT:
98 default: // for backwards compatibility
99 ret = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + this.getTopicName();
113 this.clients = new ArrayList<MR_Client>();
114 this.lastMod = new Date();
115 this.replicationCase = ReplicationType.Validator("none");
117 logger.debug( "Topic constructor " + this.lastMod );
119 public Topic(String fqtn, String topicName, String topicDescription,
120 String tnxEnabled, String owner) {
123 this.topicName = topicName;
124 this.topicDescription = topicDescription;
125 //this.dcaeLocationName = dcaeLocationName;
126 this.tnxEnabled = tnxEnabled;
130 logger.debug( "Topic constructor w args " + this.getLastMod() );
133 public Topic init() {
134 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
136 defaultPartitionCount = p.getProperty( "MR.partitionCount", "2");
137 defaultReplicationCount = p.getProperty( "MR.replicationCount", "1");
139 this.setStatus( DmaapObject_Status.NEW );
140 this.replicationCase = ReplicationType.Validator("none");
141 this.fqtnStyle = FqtnType.Validator("none");
142 this.setPartitionCount( defaultPartitionCount );
143 this.setReplicationCount( defaultReplicationCount );
148 // expects a String in JSON format, with known fields to populate Topic object
149 public Topic ( String json ) {
150 JSONParser parser = new JSONParser();
153 jsonObj = (JSONObject) parser.parse( json );
154 } catch ( ParseException pe ) {
155 logger.error( "Error parsing provisioning data: " + json );
156 this.setStatus( DmaapObject_Status.INVALID );
159 this.setFqtn( (String) jsonObj.get( "fqtn" ) );
160 this.setTopicName( (String) jsonObj.get( "topicName" ) );
161 this.setTopicDescription( (String) jsonObj.get( "topicDescription" ));
162 this.setOwner( (String) jsonObj.get( "owner" ) );
164 this.setStatus( (String) jsonObj.get( "status" ) );
165 this.setReplicationCase( ReplicationType.Validator( (String) jsonObj.get( "replicationCase" ) ));
166 this.setFqtnStyle( FqtnType.Validator( (String) jsonObj.get( "fqtnStyle" ) ) );
167 this.setPartitionCount( (String) jsonObj.get("partitionCount"));
170 public String getFqtn() {
173 public void setFqtn(String fqtn) {
176 public String getTopicName() {
179 public void setTopicName(String topicName) {
180 this.topicName = topicName;
182 public String getTopicDescription() {
183 return topicDescription;
185 public void setTopicDescription(String topicDescription) {
186 this.topicDescription = topicDescription;
189 public String getTnxEnabled() {
192 public void setTnxEnabled(String tnxEnabled) {
193 this.tnxEnabled = tnxEnabled;
195 public String getOwner() {
198 public void setOwner(String owner) {
201 public String getPartitionCount() {
202 return partitionCount;
204 public void setPartitionCount(String partitions) {
205 this.partitionCount = partitions;
207 public String getReplicationCount() {
208 return replicationCount;
210 public void setReplicationCount(String replicationCount) {
211 this.replicationCount = replicationCount;
215 public void setClients(ArrayList<MR_Client> clients) {
216 this.clients = clients;
219 public ArrayList<MR_Client> getClients() {
223 public int getNumClients() {
224 if ( this.clients == null ) {
227 return this.clients.size();
233 public String getFormatUuid() {
239 public void setFormatUuid(String formatUuid) {
240 this.formatUuid = formatUuid;
244 public ReplicationType getReplicationCase() {
245 return replicationCase;
249 public void setReplicationCase(ReplicationType t) {
250 this.replicationCase = t;
252 public FqtnType getFqtnStyle() {
257 public void setFqtnStyle(FqtnType t) {
261 public String getGlobalMrURL() {
267 public void setGlobalMrURL(String globalMrURL) {
268 this.globalMrURL = globalMrURL;
273 public String getVersion() {
279 public void setVersion(String version) {
280 this.version = version;
285 public String toProvJSON() {
286 StringBuilder str = new StringBuilder();
287 str.append("{ \"topicName\": \"");
288 str.append( this.getFqtn() );
289 str.append( "\", \"topicDescription\": \"");
290 str.append( this.getTopicDescription());
291 str.append( "\", \"partitionCount\": \"");
292 str.append( this.getPartitionCount());
293 str.append( "\", \"replicationCount\": \"");
294 str.append( this.getReplicationCount());
295 str.append( "\" } ");
297 logger.info( str.toString() );
298 return str.toString();
301 public byte[] getBytes() {
302 return toProvJSON().getBytes(StandardCharsets.UTF_8);