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;
27 import javax.xml.bind.annotation.XmlRootElement;
30 import org.onap.dmaap.dbcapi.util.DmaapConfig;
32 import org.onap.dmaap.dbcapi.service.DmaapService;
36 public class Topic extends DmaapObject {
39 private String topicName;
40 private String topicDescription;
41 private String tnxEnabled;
43 private String formatUuid;
44 private ReplicationType replicationCase;
45 private String globalMrURL; // optional: URL of global MR to replicate to/from
46 private FqtnType fqtnStyle;
47 private String version;
49 private ArrayList<MR_Client> clients;
53 private static Dmaap dmaap = new DmaapService().getDmaap();
55 // during unit testing, discovered that presence of dots in some values
56 // creates an unplanned topic namespace as we compose the FQTN.
57 // this may create sensitivity (i.e. 403) for subsequent creation of AAF perms, so best to not allow it
58 private static String removeDots( String source, String def ) {
59 if ( source == null || source.isEmpty()) {
62 return source.replaceAll("\\.", "_");
65 // utility function to generate the FQTN of a topic
66 public String genFqtn( ) {
67 DmaapConfig dc = (DmaapConfig)DmaapConfig.getConfig();
68 String projectId = dc.getProperty("MR.projectID", "99999");
69 CharSequence signal = ".";
71 if ( this.getTopicName().contains( signal )) {
72 // presence of a dot indicates the name is already fully qualified
73 ret = this.getTopicName();
75 // these vars may not contain dots
76 String p = removeDots( projectId, "90909");
77 String v = removeDots( this.getVersion(), "v1");
78 switch( this.getFqtnStyle() ) {
79 case FQTN_PROJECTID_VERSION_FORMAT:
81 ret = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + p + "-" + this.getTopicName() + "-" + v;
84 case FQTN_PROJECTID_FORMAT:
86 ret = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + p + "-" + this.getTopicName();
89 case FQTN_LEGACY_FORMAT:
90 default: // for backwards compatibility
91 ret = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + this.getTopicName();
105 this.clients = new ArrayList<MR_Client>();
106 this.lastMod = new Date();
107 this.replicationCase = ReplicationType.Validator("none");
109 logger.debug( "Topic constructor " + this.lastMod );
111 public Topic(String fqtn, String topicName, String topicDescription,
112 String tnxEnabled, String owner) {
115 this.topicName = topicName;
116 this.topicDescription = topicDescription;
117 //this.dcaeLocationName = dcaeLocationName;
118 this.tnxEnabled = tnxEnabled;
121 this.setStatus( DmaapObject_Status.NEW );
122 this.replicationCase = ReplicationType.Validator("none");
123 this.fqtnStyle = FqtnType.Validator("none");
124 logger.debug( "Topic constructor " + this.getLastMod() );
126 public String getFqtn() {
129 public void setFqtn(String fqtn) {
132 public String getTopicName() {
135 public void setTopicName(String topicName) {
136 this.topicName = topicName;
138 public String getTopicDescription() {
139 return topicDescription;
141 public void setTopicDescription(String topicDescription) {
142 this.topicDescription = topicDescription;
145 public String getTnxEnabled() {
148 public void setTnxEnabled(String tnxEnabled) {
149 this.tnxEnabled = tnxEnabled;
151 public String getOwner() {
154 public void setOwner(String owner) {
159 public void setClients(ArrayList<MR_Client> clients) {
160 this.clients = clients;
163 public ArrayList<MR_Client> getClients() {
167 public int getNumClients() {
168 if ( this.clients == null ) {
171 return this.clients.size();
177 public String getFormatUuid() {
183 public void setFormatUuid(String formatUuid) {
184 this.formatUuid = formatUuid;
188 public ReplicationType getReplicationCase() {
189 return replicationCase;
195 public void setReplicationCase(String val) {
196 this.replicationCase = ReplicationType.Validator(val);
200 public void setReplicationCase(ReplicationType t) {
201 this.replicationCase = t;
203 public FqtnType getFqtnStyle() {
208 public void setFqtnStyle(FqtnType t) {
212 public String getGlobalMrURL() {
218 public void setGlobalMrURL(String globalMrURL) {
219 this.globalMrURL = globalMrURL;
224 public String getVersion() {
230 public void setVersion(String version) {
231 this.version = version;
236 public String toProvJSON() {
237 StringBuilder str = new StringBuilder();
238 str.append("{ \"topicName\": \"");
239 str.append( this.getFqtn() );
240 str.append( "\", \"topicDescription\": \"");
241 str.append( this.getTopicDescription());
242 str.append( "\", \"partitionCount\": \"2\", \"replicationCount\": \"1\" } ");
243 logger.info( str.toString() );
244 return str.toString();
247 public byte[] getBytes() {
248 return toProvJSON().getBytes(StandardCharsets.UTF_8);