aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openecomp/dmaapbc/model/Feed.java
blob: 5d89331b076740ebfa8858ea37d252aa06f78e4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*-
 * ============LICENSE_START=======================================================
 * OpenECOMP - org.openecomp.dmaapbc
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.dmaapbc.model;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

import javax.xml.bind.annotation.XmlRootElement;

import org.json.simple.*;
import org.json.simple.parser.*;
import org.openecomp.dmaapbc.service.DmaapService;

@XmlRootElement
public class Feed extends DmaapObject {
		
		private String feedId;

		private String feedName;
		private String feedVersion;
		private String feedDescription;
		private String owner;
		private String asprClassification;
		private String publishURL;
		private String subscribeURL;
		private	boolean	suspended;
		private String logURL;
		private String formatUuid;

		private	ArrayList<DR_Pub> pubs;
		private ArrayList<DR_Sub> subs;	

		

		public boolean isSuspended() {
			return suspended;
		}

		public void setSuspended(boolean suspended) {
			this.suspended = suspended;
		}

		public String getSubscribeURL() {
			return subscribeURL;
		}

		public void setSubscribeURL(String subscribeURL) {
			this.subscribeURL = subscribeURL;
		}


		
		public Feed() {
			this.pubs = new ArrayList<DR_Pub>();
			this.subs = new ArrayList<DR_Sub>();
			this.setStatus( DmaapObject_Status.EMPTY );
			
		}
		
		public	Feed( String name,
					String version,
					String description,
					String owner,
					String aspr
					 ) {
			this.feedName = name;
			this.feedVersion = version;
			this.feedDescription = description;
			this.owner = owner;
			this.asprClassification = aspr;
			this.pubs = new ArrayList<DR_Pub>();
			this.subs = new ArrayList<DR_Sub>();
			this.setStatus( DmaapObject_Status.NEW );
			
		}
		
		// expects a String in JSON format, with known fields to populate Feed object
		public Feed ( String json ) {
			JSONParser parser = new JSONParser();
			JSONObject jsonObj;
			
			try {
				jsonObj = (JSONObject) parser.parse( json );
			} catch ( ParseException pe ) {
	            logger.error( "Error parsing provisioning data: " + json );
	            this.setStatus( DmaapObject_Status.INVALID );
	            return;
	        }
			this.setFeedName( (String) jsonObj.get("name"));


			this.setFeedVersion( (String) jsonObj.get("version"));
			this.setFeedDescription( (String) jsonObj.get("description"));
			this.setOwner( (String) jsonObj.get("publisher"));

			this.setSuspended( (boolean) jsonObj.get("suspend"));
			JSONObject links = (JSONObject) jsonObj.get("links");
			String url = (String) links.get("publish");
			this.setPublishURL( url );
			this.setFeedId( url.substring( url.lastIndexOf('/')+1, url.length() ));
			logger.info( "feedid="+ this.getFeedId() );
			this.setSubscribeURL( (String) links.get("subscribe") );					
			this.setLogURL( (String) links.get("log") );
			JSONObject auth = (JSONObject) jsonObj.get("authorization");
			this.setAsprClassification( (String) auth.get("classification"));
			JSONArray pubs = (JSONArray) auth.get( "endpoint_ids");
			int i;
			ArrayList<DR_Pub> dr_pub = new ArrayList<DR_Pub>();
			this.subs = new ArrayList<DR_Sub>();

			for( i = 0; i < pubs.size(); i++ ) {
				JSONObject entry = (JSONObject) pubs.get(i);
				dr_pub.add(  new DR_Pub( "someLocation", 
									(String) entry.get("id"),
									(String) entry.get("password"),
									this.getFeedId(),
									this.getFeedId() + "." +  DR_Pub.nextKey() ));
			
			}
			this.setPubs( dr_pub );
	
			this.setStatus( DmaapObject_Status.VALID );

		}

		public String getFeedId() {
			return feedId;
		}

		public void setFeedId(String feedId) {
			this.feedId = feedId;
		}

		public String getFeedName() {
			return feedName;
		}

		public void setFeedName(String feedName) {
			this.feedName = feedName;
		}

		public String getFeedVersion() {
			return feedVersion;
		}

		public void setFeedVersion(String feedVersion) {
			this.feedVersion = feedVersion;
		}

		public String getFeedDescription() {
			return feedDescription;
		}

		public void setFeedDescription(String feedDescription) {
			this.feedDescription = feedDescription;
		}

		public String getOwner() {
			return owner;
		}

		public void setOwner(String owner) {
			this.owner = owner;
		}

		public String getAsprClassification() {
			return asprClassification;
		}

		public void setAsprClassification(String asprClassification) {
			this.asprClassification = asprClassification;
		}

		public String getPublishURL() {
			return publishURL;
		}

		public void setPublishURL(String publishURL) {
			this.publishURL = publishURL;
		}

		public String getLogURL() {
			return logURL;
		}

		public void setLogURL(String logURL) {
			this.logURL = logURL;
		}


		
		public String getFormatUuid() {
			return formatUuid;
		}

		public void setFormatUuid(String formatUuid) {
			this.formatUuid = formatUuid;
		}

		// returns the Feed object in JSON that conforms to DR Prov Server expectations
		public String toProvJSON() {

			ArrayList<DR_Pub> pubs = this.getPubs();
			String postJSON = String.format("{\"name\": \"%s\", \"version\": \"%s\", \"description\": \"%s\", \"suspend\": %s, \"authorization\": { \"classification\": \"%s\", ",  
					this.getFeedName(), 
					this.getFeedVersion(),
					this.getFeedDescription(),
					this.isSuspended() ,
					this.getAsprClassification()
					);
			int i;
			postJSON += "\"endpoint_addrs\": [],\"endpoint_ids\": [";
			String comma = "";
			for( i = 0 ; i < pubs.size(); i++) {
				postJSON +=	String.format("	%s{\"id\": \"%s\",\"password\": \"%s\"}", 
						comma,
						pubs.get(i).getUsername(),
						pubs.get(i).getUserpwd()
						) ;
				comma = ",";
			}
			postJSON += "]}}";
			
			logger.info( "postJSON=" + postJSON);		
			return postJSON;
		}
		
		public ArrayList<DR_Pub> getPubs() {
			return pubs;
		}

		public void setPubs( ArrayList<DR_Pub> pubs) {
			this.pubs = pubs;
		}

		public ArrayList<DR_Sub> getSubs() {
			return subs;
		}

		public void setSubs( ArrayList<DR_Sub> subs) {
			this.subs = subs;
		}

		public byte[] getBytes() {
			return toProvJSON().getBytes(StandardCharsets.UTF_8);
		}
		
		public static String getSubProvURL( String feedId ) {
			String ret = new String();
			ret = new DmaapService().getDmaap().getDrProvUrl() + "/subscribe/" + feedId ;
			return ret;
		}

		@Override
		public String toString() {
			String rc = String.format ( "Feed: {feedId=%s feedName=%s feedVersion=%s feedDescription=%s owner=%s asprClassification=%s publishURL=%s subscriberURL=%s suspended=%s logURL=%s formatUuid=%s}",
					feedId,
					feedName,
					feedVersion,
					feedDescription,
					owner,
					asprClassification,
					publishURL,
					subscribeURL,
					suspended,
					logURL,
					formatUuid

		
					);

			for( DR_Pub pub: pubs) {
				rc += "\n" + pub.toString();
			}

			for( DR_Sub sub: subs ) {
				rc += "\n" + sub.toString();
			}
			return rc;
		}
}