More unit tests to pass 50%
[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 import org.json.simple.*;
27 import org.json.simple.parser.*;
28 import javax.xml.bind.annotation.XmlRootElement;
29
30
31 import org.onap.dmaap.dbcapi.util.DmaapConfig;
32
33 import org.onap.dmaap.dbcapi.service.DmaapService;
34
35
36 @XmlRootElement
37 public class Topic extends DmaapObject  {
38
39         private String fqtn;
40         private String topicName;
41         private String  topicDescription;
42         private String  tnxEnabled;
43         private String  owner;
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;
49
50         private ArrayList<MR_Client> clients;
51
52
53         
54         private static Dmaap dmaap = new DmaapService().getDmaap();
55
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()) {
61                         return def;
62                 }
63                 return source.replaceAll("\\.", "_");
64         }
65         //
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 = ".";
71                 String ret;
72                 if ( this.getTopicName().contains( signal )) {
73                         // presence of a dot indicates the name is already fully qualified
74                         ret = this.getTopicName();
75                 } else {
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:
81
82                                 ret = dmaap.getTopicNsRoot() + "."  + dmaap.getDmaapName() + "." + p + "-" + this.getTopicName()  + "-" + v;
83                                 break;
84                                 
85                         case FQTN_PROJECTID_FORMAT:
86
87                                 ret = dmaap.getTopicNsRoot() + "."  + dmaap.getDmaapName() + "." + p + "-" + this.getTopicName();
88                                 break;
89                         
90                         case FQTN_LEGACY_FORMAT:
91                         default:  // for backwards compatibility
92                                 ret = dmaap.getTopicNsRoot() + "." + dmaap.getDmaapName() + "." + this.getTopicName();
93                                 break;
94                         
95
96                         }
97                         
98                 }
99                 return ret;
100         }
101
102
103
104         public Topic() {
105                 super();
106                 this.clients = new ArrayList<MR_Client>();
107                 this.lastMod = new Date();
108                 this.replicationCase = ReplicationType.Validator("none");
109                 this.setLastMod();
110                 logger.debug( "Topic constructor " + this.lastMod );
111         }
112         public Topic(String fqtn, String topicName, String topicDescription,
113                          String tnxEnabled, String owner) {
114                 super();
115                 this.fqtn = fqtn;
116                 this.topicName = topicName;
117                 this.topicDescription = topicDescription;
118                 //this.dcaeLocationName = dcaeLocationName;
119                 this.tnxEnabled = tnxEnabled;
120                 this.owner = owner;
121                 this.setLastMod();
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() );
126         }
127
128         // expects a String in JSON format, with known fields to populate Topic object
129         public Topic ( String json ) {
130                 JSONParser parser = new JSONParser();
131                 JSONObject jsonObj;
132                 try {
133                         jsonObj = (JSONObject) parser.parse( json );
134                 } catch ( ParseException pe ) {
135                    logger.error( "Error parsing provisioning data: " + json );
136                    this.setStatus( DmaapObject_Status.INVALID );
137                    return;
138             }
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" ) );
143                 //this.setLastMod();
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" ) ) );
147
148         }
149         public String getFqtn() {
150                 return fqtn;
151         }
152         public void setFqtn(String fqtn) {
153                 this.fqtn = fqtn;
154         }
155         public String getTopicName() {
156                 return topicName;
157         }
158         public void setTopicName(String topicName) {
159                 this.topicName = topicName;
160         }
161         public String getTopicDescription() {
162                 return topicDescription;
163         }
164         public void setTopicDescription(String topicDescription) {
165                 this.topicDescription = topicDescription;
166         }
167
168         public String getTnxEnabled() {
169                 return tnxEnabled;
170         }
171         public void setTnxEnabled(String tnxEnabled) {
172                 this.tnxEnabled = tnxEnabled;
173         }
174         public String getOwner() {
175                 return owner;
176         }
177         public void setOwner(String owner) {
178                 this.owner = owner;
179         }
180
181
182         public void setClients(ArrayList<MR_Client> clients) {
183                 this.clients = clients;
184         }
185
186         public ArrayList<MR_Client> getClients() {
187                 return clients;
188         }
189
190         public int getNumClients() {
191                 if ( this.clients == null ) {
192                         return 0;
193                 }
194                 return this.clients.size();
195         }
196
197
198
199
200         public String getFormatUuid() {
201                 return formatUuid;
202         }
203
204
205
206         public void setFormatUuid(String formatUuid) {
207                 this.formatUuid = formatUuid;
208         }
209
210
211         public ReplicationType getReplicationCase() {
212                 return replicationCase;
213         }
214
215
216
217         /*
218         public void setReplicationCase(String val) {
219                 this.replicationCase = ReplicationType.Validator(val);
220         }
221         */
222         
223         public void setReplicationCase(ReplicationType t) {
224                 this.replicationCase = t;
225         }
226         public FqtnType getFqtnStyle() {
227                 return fqtnStyle;
228         }
229
230         
231         public void setFqtnStyle(FqtnType t) {
232                 this.fqtnStyle = t;
233         }
234
235         public String getGlobalMrURL() {
236                 return globalMrURL;
237         }
238
239
240
241         public void setGlobalMrURL(String globalMrURL) {
242                 this.globalMrURL = globalMrURL;
243         }
244
245
246
247         public String getVersion() {
248                 return version;
249         }
250
251
252
253         public void setVersion(String version) {
254                 this.version = version;
255         }
256
257
258
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();
268         }
269         
270         public byte[] getBytes() {
271                 return toProvJSON().getBytes(StandardCharsets.UTF_8);
272         }
273 }