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;
37 public class Topic extends DmaapObject {
40 private String topicName;
41 private String topicDescription;
42 private String tnxEnabled;
44 private String formatUuid;
45 private ReplicationType replicationCase;
46 private String globalMrURL; // optional: URL of global MR to replicate to/from
47 private FqtnType fqtnStyle;
48 private String version;
50 private ArrayList<MR_Client> clients;
54 private static Dmaap dmaap = new DmaapService().getDmaap();
56 // during unit testing, discovered that presence of dots in some values
57 // creates an unplanned topic namespace as we compose the FQTN.
58 // this may create sensitivity (i.e. 403) for subsequent creation of AAF perms, so best to not allow it
59 private static String removeDots( String source, String def ) {
60 if ( source == null || source.isEmpty()) {
63 return source.replaceAll("\\.", "_");
66 // utility function to generate the FQTN of a topic
67 public String genFqtn( ) {
68 DmaapConfig dc = (DmaapConfig)DmaapConfig.getConfig();
69 String projectId = dc.getProperty("MR.projectID", "99999");
70 CharSequence signal = ".";
72 if ( this.getTopicName().contains( signal )) {
73 // presence of a dot indicates the name is already fully qualified
74 ret = this.getTopicName();
76 // these vars may not contain dots
77 String p = removeDots( projectId, "90909");
78 String v = removeDots( this.getVersion(), "v1");
79 switch( this.getFqtnStyle() ) {
80 case FQTN_PROJECTID_VERSION_FORMAT:
82 ret = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + p + "-" + this.getTopicName() + "-" + v;
85 case FQTN_PROJECTID_FORMAT:
87 ret = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + p + "-" + this.getTopicName();
90 case FQTN_LEGACY_FORMAT:
91 default: // for backwards compatibility
92 ret = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + this.getTopicName();
106 this.clients = new ArrayList<MR_Client>();
107 this.lastMod = new Date();
108 this.replicationCase = ReplicationType.Validator("none");
110 logger.debug( "Topic constructor " + this.lastMod );
112 public Topic(String fqtn, String topicName, String topicDescription,
113 String tnxEnabled, String owner) {
116 this.topicName = topicName;
117 this.topicDescription = topicDescription;
118 //this.dcaeLocationName = dcaeLocationName;
119 this.tnxEnabled = tnxEnabled;
122 this.setStatus( DmaapObject_Status.NEW );
123 this.replicationCase = ReplicationType.Validator("none");
124 this.fqtnStyle = FqtnType.Validator("none");
125 logger.debug( "Topic constructor " + this.getLastMod() );
128 // expects a String in JSON format, with known fields to populate Topic object
129 public Topic ( String json ) {
130 JSONParser parser = new JSONParser();
133 jsonObj = (JSONObject) parser.parse( json );
134 } catch ( ParseException pe ) {
135 logger.error( "Error parsing provisioning data: " + json );
136 this.setStatus( DmaapObject_Status.INVALID );
139 this.setFqtn( (String) jsonObj.get( "fqtn" ) );
140 this.setTopicName( (String) jsonObj.get( "topicName" ) );
141 this.setTopicDescription( (String) jsonObj.get( "topicDescription" ));
142 this.setOwner( (String) jsonObj.get( "owner" ) );
144 this.setStatus( (String) jsonObj.get( "status" ) );
145 this.setReplicationCase( ReplicationType.Validator( (String) jsonObj.get( "replicationCase" ) ));
146 this.setFqtnStyle( FqtnType.Validator( (String) jsonObj.get( "fqtnStyle" ) ) );
149 public String getFqtn() {
152 public void setFqtn(String fqtn) {
155 public String getTopicName() {
158 public void setTopicName(String topicName) {
159 this.topicName = topicName;
161 public String getTopicDescription() {
162 return topicDescription;
164 public void setTopicDescription(String topicDescription) {
165 this.topicDescription = topicDescription;
168 public String getTnxEnabled() {
171 public void setTnxEnabled(String tnxEnabled) {
172 this.tnxEnabled = tnxEnabled;
174 public String getOwner() {
177 public void setOwner(String owner) {
182 public void setClients(ArrayList<MR_Client> clients) {
183 this.clients = clients;
186 public ArrayList<MR_Client> getClients() {
190 public int getNumClients() {
191 if ( this.clients == null ) {
194 return this.clients.size();
200 public String getFormatUuid() {
206 public void setFormatUuid(String formatUuid) {
207 this.formatUuid = formatUuid;
211 public ReplicationType getReplicationCase() {
212 return replicationCase;
218 public void setReplicationCase(String val) {
219 this.replicationCase = ReplicationType.Validator(val);
223 public void setReplicationCase(ReplicationType t) {
224 this.replicationCase = t;
226 public FqtnType getFqtnStyle() {
231 public void setFqtnStyle(FqtnType t) {
235 public String getGlobalMrURL() {
241 public void setGlobalMrURL(String globalMrURL) {
242 this.globalMrURL = globalMrURL;
247 public String getVersion() {
253 public void setVersion(String version) {
254 this.version = version;
259 public String toProvJSON() {
260 StringBuilder str = new StringBuilder();
261 str.append("{ \"topicName\": \"");
262 str.append( this.getFqtn() );
263 str.append( "\", \"topicDescription\": \"");
264 str.append( this.getTopicDescription());
265 str.append( "\", \"partitionCount\": \"2\", \"replicationCount\": \"1\" } ");
266 logger.info( str.toString() );
267 return str.toString();
270 public byte[] getBytes() {
271 return toProvJSON().getBytes(StandardCharsets.UTF_8);