Contribute code that was missed in Amsterdam
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / model / Topic.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.dmaap.dbcapi.model;
22
23 import java.nio.charset.StandardCharsets;
24 import java.util.ArrayList;
25 import java.util.Date;
26
27 import javax.xml.bind.annotation.XmlRootElement;
28
29
30 import org.onap.dmaap.dbcapi.util.DmaapConfig;
31
32 import org.onap.dmaap.dbcapi.service.DmaapService;
33
34
35 @XmlRootElement
36 public class Topic extends DmaapObject  {
37
38         private String fqtn;
39         private String topicName;
40         private String  topicDescription;
41         private String  tnxEnabled;
42         private String  owner;
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;
48
49         private ArrayList<MR_Client> clients;
50
51
52         
53         private static Dmaap dmaap = new DmaapService().getDmaap();
54
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()) {
60                         return def;
61                 }
62                 return source.replaceAll("\\.", "_");
63         }
64         //
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 = ".";
70                 String ret;
71                 if ( this.getTopicName().contains( signal )) {
72                         // presence of a dot indicates the name is already fully qualified
73                         ret = this.getTopicName();
74                 } else {
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:
80
81                                 ret = dmaap.getTopicNsRoot() + "."  + dmaap.getDmaapName() + "." + p + "-" + this.getTopicName()  + "-" + v;
82                                 break;
83                                 
84                         case FQTN_PROJECTID_FORMAT:
85
86                                 ret = dmaap.getTopicNsRoot() + "."  + dmaap.getDmaapName() + "." + p + "-" + this.getTopicName();
87                                 break;
88                         
89                         case FQTN_LEGACY_FORMAT:
90                         default:  // for backwards compatibility
91                                 ret = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + this.getTopicName();
92                                 break;
93                         
94
95                         }
96                         
97                 }
98                 return ret;
99         }
100
101
102
103         public Topic() {
104                 super();
105                 this.clients = new ArrayList<MR_Client>();
106                 this.lastMod = new Date();
107                 this.replicationCase = ReplicationType.Validator("none");
108                 this.setLastMod();
109                 logger.debug( "Topic constructor " + this.lastMod );
110         }
111         public Topic(String fqtn, String topicName, String topicDescription,
112                          String tnxEnabled, String owner) {
113                 super();
114                 this.fqtn = fqtn;
115                 this.topicName = topicName;
116                 this.topicDescription = topicDescription;
117                 //this.dcaeLocationName = dcaeLocationName;
118                 this.tnxEnabled = tnxEnabled;
119                 this.owner = owner;
120                 this.setLastMod();
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() );
125         }
126         public String getFqtn() {
127                 return fqtn;
128         }
129         public void setFqtn(String fqtn) {
130                 this.fqtn = fqtn;
131         }
132         public String getTopicName() {
133                 return topicName;
134         }
135         public void setTopicName(String topicName) {
136                 this.topicName = topicName;
137         }
138         public String getTopicDescription() {
139                 return topicDescription;
140         }
141         public void setTopicDescription(String topicDescription) {
142                 this.topicDescription = topicDescription;
143         }
144
145         public String getTnxEnabled() {
146                 return tnxEnabled;
147         }
148         public void setTnxEnabled(String tnxEnabled) {
149                 this.tnxEnabled = tnxEnabled;
150         }
151         public String getOwner() {
152                 return owner;
153         }
154         public void setOwner(String owner) {
155                 this.owner = owner;
156         }
157
158
159         public void setClients(ArrayList<MR_Client> clients) {
160                 this.clients = clients;
161         }
162
163         public ArrayList<MR_Client> getClients() {
164                 return clients;
165         }
166
167         public int getNumClients() {
168                 if ( this.clients == null ) {
169                         return 0;
170                 }
171                 return this.clients.size();
172         }
173
174
175
176
177         public String getFormatUuid() {
178                 return formatUuid;
179         }
180
181
182
183         public void setFormatUuid(String formatUuid) {
184                 this.formatUuid = formatUuid;
185         }
186
187
188         public ReplicationType getReplicationCase() {
189                 return replicationCase;
190         }
191
192
193
194         /*
195         public void setReplicationCase(String val) {
196                 this.replicationCase = ReplicationType.Validator(val);
197         }
198         */
199         
200         public void setReplicationCase(ReplicationType t) {
201                 this.replicationCase = t;
202         }
203         public FqtnType getFqtnStyle() {
204                 return fqtnStyle;
205         }
206
207         
208         public void setFqtnStyle(FqtnType t) {
209                 this.fqtnStyle = t;
210         }
211
212         public String getGlobalMrURL() {
213                 return globalMrURL;
214         }
215
216
217
218         public void setGlobalMrURL(String globalMrURL) {
219                 this.globalMrURL = globalMrURL;
220         }
221
222
223
224         public String getVersion() {
225                 return version;
226         }
227
228
229
230         public void setVersion(String version) {
231                 this.version = version;
232         }
233
234
235
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();
245         }
246         
247         public byte[] getBytes() {
248                 return toProvJSON().getBytes(StandardCharsets.UTF_8);
249         }
250 }