aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dmaap/dbcapi/service/TopicService.java
blob: a26205c5901ee5bde667c8a5628466f30557128c (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*-
 * ============LICENSE_START=======================================================
 * org.onap.dmaap
 * ================================================================================
 * 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.onap.dmaap.dbcapi.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.ws.rs.core.Response.Status;

import org.onap.dmaap.dbcapi.aaf.AafNamespace;
import org.onap.dmaap.dbcapi.aaf.AafRole;
import org.onap.dmaap.dbcapi.aaf.AafService;
import org.onap.dmaap.dbcapi.aaf.DmaapGrant;
import org.onap.dmaap.dbcapi.aaf.AafService.ServiceType;
import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
import org.onap.dmaap.dbcapi.database.DatabaseClass;
import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
import org.onap.dmaap.dbcapi.model.ApiError;
import org.onap.dmaap.dbcapi.model.DcaeLocation;
import org.onap.dmaap.dbcapi.model.Dmaap;
import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
import org.onap.dmaap.dbcapi.model.MR_Client;
import org.onap.dmaap.dbcapi.model.MR_Cluster;
import org.onap.dmaap.dbcapi.model.MirrorMaker;
import org.onap.dmaap.dbcapi.model.ReplicationType;
import org.onap.dmaap.dbcapi.model.Topic;
import org.onap.dmaap.dbcapi.util.DmaapConfig;
import org.onap.dmaap.dbcapi.util.Fqdn;
import org.onap.dmaap.dbcapi.util.Graph;

public class TopicService extends BaseLoggingClass {

	

	// REF: https://wiki.web.att.com/pages/viewpage.action?pageId=519703122
	private static String defaultGlobalMrHost;
	
	private Map<String, Topic> mr_topics = DatabaseClass.getTopics();
	
	private static DmaapService dmaapSvc = new DmaapService();
	private static Dmaap dmaap = new DmaapService().getDmaap();
	private MR_ClientService clientService = new MR_ClientService();
	private MR_ClusterService clusters = new MR_ClusterService();
	private DcaeLocationService locations = new DcaeLocationService();
	private MirrorMakerService	bridge = new MirrorMakerService();
	
	private static String centralCname;


	public TopicService(){
		DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
		defaultGlobalMrHost = p.getProperty("MR.globalHost", "global.host.not.set");
		centralCname = p.getProperty("MR.CentralCname");

		
		logger.info( "TopicService properties: CentralCname=" + centralCname + 
				"   defaultGlobarlMrHost=" + defaultGlobalMrHost  );
	}
	
	public Map<String, Topic> getTopics() {			
		return mr_topics;
	}
		
	public List<Topic> getAllTopics() {
		return getAllTopics( true );
	}
	public List<Topic> getAllTopicsWithoutClients() {
		return getAllTopics(false);
	}
	
	private List<Topic> getAllTopics( Boolean withClients ) {
		ArrayList<Topic> topics = new ArrayList<Topic>(mr_topics.values());
		if ( withClients ) {
			for( Topic topic: topics ) {
				topic.setClients( clientService.getAllMrClients(topic.getFqtn()));
			}
		}
		return topics;
	}
	
		
	public Topic getTopic( String key, ApiError apiError ) {	
		logger.info( "getTopic: key=" + key);
		Topic t = mr_topics.get( key );
		if ( t == null ) {
			apiError.setCode(Status.NOT_FOUND.getStatusCode());
			apiError.setFields( "fqtn");
			apiError.setMessage("topic with fqtn " + key + " not found");
			return null;
		}
		t.setClients( clientService.getAllMrClients( key ));
		apiError.setCode(Status.OK.getStatusCode());
		return t;
	}
	
	private void aafTopicSetup(Topic topic, ApiError err ) {

		String t = dmaapSvc.getTopicPerm();
		if ( t == null ) {
			err.setCode(500);
			err.setMessage("Unable to establish AAF namespace root: (check /dmaap object)"  );
			err.setFields("topicNsRoot");
			return;
		}

		// establish AAF Connection using TopicMgr identity
		AafService aaf = new AafService(ServiceType.AAF_TopicMgr);
		

		
		// create AAF namespace for this topic
		AafNamespace ns = new AafNamespace( topic.getFqtn(), aaf.getIdentity());
		{
			int rc = aaf.addNamespace( ns );
			if ( rc != 201 && rc != 409 ) {
				err.setCode(500);
				err.setMessage("Unexpected response from AAF:" + rc );
				err.setFields("namespace:" + topic.getFqtn() + " identity="+ aaf.getIdentity());
				return;
			}
		}
		
		// create AAF Roles for MR clients of this topic
		String rn = "publisher";
		AafRole pubRole = new AafRole( topic.getFqtn(), rn );
		int rc = aaf.addRole( pubRole );
		if ( rc != 201 && rc != 409 ) {
			err.setCode(500);
			err.setMessage("Unexpected response from AAF:" + rc );
			err.setFields("topic:" + topic.getFqtn() + " role="+ rn);
			return;
		}
		topic.setPublisherRole( pubRole.getFullyQualifiedRole() );
		
		rn = "subscriber";
		AafRole subRole = new AafRole( topic.getFqtn(), rn );
		rc = aaf.addRole( subRole );
		if ( rc != 201 && rc != 409 ) {
			err.setCode(500);
			err.setMessage("Unexpected response from AAF:" + rc );
			err.setFields("topic:" + topic.getFqtn() + " role="+ rn);
			return;
		}
		topic.setSubscriberRole( subRole.getFullyQualifiedRole() );
	
		
		// create AAF perms checked by MR
		String instance = ":topic." + topic.getFqtn();
		String[] actions = { "pub", "sub", "view" };
		for ( String action : actions ){
			DmaapPerm perm = new DmaapPerm( t, instance, action );
			rc = aaf.addPerm( perm );
			if ( rc != 201 && rc != 409 ) {
				err.setCode(500);
				err.setMessage("Unexpected response from AAF:" + rc );
				err.setFields("t="+t + " instance="+ instance + " action="+ action);
				return;
			}
			// Grant perms to our default Roles
			if ( action.equals( "pub") || action.equals( "view") ) {
				DmaapGrant g = new DmaapGrant( perm, pubRole.getFullyQualifiedRole() );
				rc = aaf.addGrant( g );
				if ( rc != 201 && rc != 409 ) {
					err.setCode(rc);
					err.setMessage( "Grant of " + perm.toString() + " failed for " + pubRole.getFullyQualifiedRole() );
					logger.warn( err.getMessage());
					return;
				} 
			}
			if ( action.equals( "sub") || action.equals( "view") ) {
				DmaapGrant g = new DmaapGrant( perm, subRole.getFullyQualifiedRole() );
				rc = aaf.addGrant( g );
				if ( rc != 201 && rc != 409 ) {
					err.setCode(rc);
					err.setMessage( "Grant of " + perm.toString() + " failed for " + subRole.getFullyQualifiedRole() );
					logger.warn( err.getMessage());
					return;
				} 
			}

		}
	}

	public Topic addTopic( Topic topic, ApiError err, Boolean useExisting ) {
		logger.info( "Entry: addTopic");
		logger.info( "Topic name=" + topic.getTopicName() + " fqtnStyle=" + topic.getFqtnStyle() );
		String nFqtn =  topic.genFqtn();
		logger.info( "FQTN=" + nFqtn );
		Topic pTopic = getTopic( nFqtn, err );
		if ( pTopic != null ) {
			String t = "topic already exists: " + nFqtn;
			logger.info( t );
			if (  useExisting ) {
				err.setCode(Status.OK.getStatusCode());
				return pTopic;
			}
			err.setMessage( t );
			err.setFields( "fqtn");
			err.setCode(Status.CONFLICT.getStatusCode());
			return null;
		}
		err.reset();  // err filled with NOT_FOUND is expected case, but don't want to litter...

		topic.setFqtn( nFqtn );
		
		aafTopicSetup( topic, err );
		if ( err.getCode() >= 400 ) {
			return null;
		}	

		if ( topic.getReplicationCase().involvesGlobal() ) {
			if ( topic.getGlobalMrURL() == null ) {
				topic.setGlobalMrURL(defaultGlobalMrHost);
			}
			if ( ! Fqdn.isValid( topic.getGlobalMrURL())) {
				logger.error( "GlobalMR FQDN not valid: " + topic.getGlobalMrURL());
				topic.setStatus( DmaapObject_Status.INVALID);
				err.setCode(500);
				err.setMessage("Value is not a valid FQDN:" +  topic.getGlobalMrURL() );
				err.setFields("globalMrURL");
	
				return null;
			}
		}


		if ( topic.getNumClients() > 0 ) {
			ArrayList<MR_Client> clients = new ArrayList<MR_Client>(topic.getClients());
		
	
			ArrayList<MR_Client> clients2 = new ArrayList<MR_Client>();
			for ( Iterator<MR_Client> it = clients.iterator(); it.hasNext(); ) {
				MR_Client c = it.next();

				logger.info( "c fqtn=" + c.getFqtn() + " ID=" + c.getMrClientId() + " url=" + c.getTopicURL());
				MR_Client nc = new MR_Client( c.getDcaeLocationName(), topic.getFqtn(), c.getClientRole(), c.getAction());
				nc.setFqtn(topic.getFqtn());
				logger.info( "nc fqtn=" + nc.getFqtn() + " ID=" + nc.getMrClientId() + " url=" + nc.getTopicURL());
				clients2.add( clientService.addMr_Client(nc, topic, err));
				if ( ! err.is2xx()) {
					return null;
				}
			}

			topic.setClients(clients2);
		}

		Topic ntopic = checkForBridge( topic, err );
		if ( ntopic == null ) {
			topic.setStatus( DmaapObject_Status.INVALID);
			if ( ! err.is2xx()) {
				return null;
			}
		}

		
		mr_topics.put( nFqtn, ntopic );

		err.setCode(Status.OK.getStatusCode());
		return ntopic;
	}
	
		
	public Topic updateTopic( Topic topic, ApiError err ) {
		logger.info( "Entry: updateTopic");
		if ( topic.getFqtn().isEmpty()) {
			return null;
		}
		Topic ntopic = checkForBridge( topic, err );
		if ( ntopic == null ) {
			topic.setStatus( DmaapObject_Status.INVALID);
			if ( ! err.is2xx() ) {
				return null;
			}
		}
		if(ntopic != null) {
			mr_topics.put( ntopic.getFqtn(), ntopic );
		}
		err.setCode(Status.OK.getStatusCode());
		return ntopic;
	}
		
	public Topic removeTopic( String pubId, ApiError apiError ) {
		Topic topic = mr_topics.get(pubId);
		if ( topic == null ) {
			apiError.setCode(Status.NOT_FOUND.getStatusCode());
			apiError.setMessage("Topic " + pubId + " does not exist");
			apiError.setFields("fqtn");
			return null;
		}
		ArrayList<MR_Client> clients = new ArrayList<MR_Client>(clientService.getAllMrClients( pubId ));
		for ( Iterator<MR_Client> it = clients.iterator(); it.hasNext(); ) {
			MR_Client c = it.next();
			
	
			clientService.removeMr_Client(c.getMrClientId(), false, apiError);
			if ( ! apiError.is2xx()) {
				return null;
			}
		}
		apiError.setCode(Status.OK.getStatusCode());
		return mr_topics.remove(pubId);
	}	
	public static ApiError setBridgeClientPerms( MR_Cluster node ) {
		DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
		String mmProvRole = p.getProperty("MM.ProvRole");
		String mmAgentRole = p.getProperty("MM.AgentRole");
		String[] Roles = { mmProvRole, mmAgentRole };
		String[] actions = { "view", "pub", "sub" };
		Topic bridgeAdminTopic = new Topic().init();
		bridgeAdminTopic.setTopicName( dmaapSvc.getBridgeAdminFqtn() );
		bridgeAdminTopic.setTopicDescription( "RESERVED topic for MirroMaker Provisioning");
		bridgeAdminTopic.setOwner( "DBC" );
		
		ArrayList<MR_Client> clients = new ArrayList<MR_Client>();
		for( String role: Roles ) {
			MR_Client client = new MR_Client();
			client.setAction(actions);
			client.setClientRole(role);
			client.setDcaeLocationName( node.getDcaeLocationName());
			clients.add( client );
		}
		bridgeAdminTopic.setClients(clients);
		
		TopicService ts = new TopicService();
		ApiError err = new ApiError();
		ts.addTopic(bridgeAdminTopic, err, true);
		
		if ( err.is2xx() || err.getCode() == 409 ){
			err.setCode(200);
			return err;
		}
		
		errorLogger.error( DmaapbcLogMessageEnum.TOPIC_CREATE_ERROR,  bridgeAdminTopic.getFqtn(), Integer.toString(err.getCode()), err.getFields(), err.getMessage());
		return err;
	}	
	
	
	public Topic checkForBridge( Topic topic, ApiError err ) {
		
		if ( topic.getReplicationCase() == ReplicationType.REPLICATION_NONE ) {
			topic.setStatus( DmaapObject_Status.VALID);
			return topic;	
		}
		
		boolean anythingWrong = false;
		
		Set<String> groups = clusters.getGroups();
		for ( String g : groups ) {
			anythingWrong |= buildBridge( topic, err, g );
		}
		if ( anythingWrong ) {
			topic.setStatus( DmaapObject_Status.INVALID);
			if ( ! err.is2xx() ) {
				return null;
			}	
		} else {
			topic.setStatus( DmaapObject_Status.VALID);
		}
		return topic;
	}
		
	private boolean buildBridge( Topic topic, ApiError err, String group ) {

		boolean anythingWrong = false;
		Graph graph;
		if ( group == null || group.isEmpty() ) {
			graph = new Graph( topic.getClients(), true );
		} else {
			graph = new Graph( topic.getClients(), true, group );
		}
		MR_Cluster groupCentralCluster = null;
		
		if ( graph.isEmpty() ) {
			return false;
		} else if ( group == null &&  topic.getReplicationCase().involvesFQDN() ) {
			return false;
		} else if ( ! graph.hasCentral() ) {
			logger.warn( "Topic " + topic.getFqtn() + " wants to be " + topic.getReplicationCase() + " but has no central clients");
			return true;
		} else {
			groupCentralCluster = clusters.getMr_ClusterByLoc(graph.getCentralLoc());
		}
		Collection<String> clientLocations = graph.getKeys();
		for( String loc : clientLocations ) {
			logger.info( "loc=" + loc );
			DcaeLocation location = locations.getDcaeLocation(loc);
			MR_Cluster cluster = clusters.getMr_ClusterByLoc(loc);
			logger.info( "cluster=" + cluster );

			
				
			String source = null;
			String target = null;
			
			/*
			 * Provision Edge to Central bridges...
			 */
			if ( ! location.isCentral()  && ! graph.getCentralLoc().equals(cluster.getDcaeLocationName()) ) {
				switch( topic.getReplicationCase() ) {
				case REPLICATION_EDGE_TO_CENTRAL:
				case REPLICATION_EDGE_TO_CENTRAL_TO_GLOBAL:  // NOTE: this is for E2C portion only
					source = cluster.getFqdn();
					target = centralCname;
					break;
				case REPLICATION_CENTRAL_TO_EDGE:
				case REPLICATION_GLOBAL_TO_CENTRAL_TO_EDGE:  // NOTE: this is for C2E portion only
					source = centralCname;
					target = cluster.getFqdn();
					break;
				case REPLICATION_CENTRAL_TO_GLOBAL:
				case REPLICATION_GLOBAL_TO_CENTRAL:
				case REPLICATION_FQDN_TO_GLOBAL:
				case REPLICATION_GLOBAL_TO_FQDN:
					break;

				case REPLICATION_EDGE_TO_FQDN:
				case REPLICATION_EDGE_TO_FQDN_TO_GLOBAL:  // NOTE: this is for E2C portion only
					source = cluster.getFqdn();
					target = groupCentralCluster.getFqdn();
					break;
				case REPLICATION_FQDN_TO_EDGE:
				case REPLICATION_GLOBAL_TO_FQDN_TO_EDGE:  // NOTE: this is for F2E portion only
					source = groupCentralCluster.getFqdn();
					target = cluster.getFqdn();
					break;

				default:
					logger.error( "Unexpected value for ReplicationType ("+ topic.getReplicationCase() + ") for topic " + topic.getFqtn() );
					anythingWrong = true;
					err.setCode(400);
					err.setFields("topic=" + topic.genFqtn() + " replicationCase="
							+ topic.getReplicationCase() );
					err.setMessage("Unexpected value for ReplicationType");
					continue;
				}

			} else if ( location.isCentral() && graph.getCentralLoc().equals(cluster.getDcaeLocationName()) ) {
				/*
				 * Provision Central to Global bridges
				 */
				switch( topic.getReplicationCase() ) {

				case REPLICATION_CENTRAL_TO_GLOBAL:
				case REPLICATION_EDGE_TO_CENTRAL_TO_GLOBAL:
					source = centralCname;
					target = topic.getGlobalMrURL();
					break;
				case REPLICATION_GLOBAL_TO_CENTRAL:
				case REPLICATION_GLOBAL_TO_CENTRAL_TO_EDGE:  // NOTE: this is for G2C portion only
					source = topic.getGlobalMrURL();
					target = centralCname;
					break;

				case REPLICATION_EDGE_TO_FQDN_TO_GLOBAL:  // NOTE: this is for E2F portion only
					source = groupCentralCluster.getFqdn();
					target = topic.getGlobalMrURL();
					break;

				case REPLICATION_FQDN_TO_GLOBAL:
					source = groupCentralCluster.getFqdn();
					target = topic.getGlobalMrURL();
					break;
					
				case REPLICATION_GLOBAL_TO_FQDN:
				case REPLICATION_GLOBAL_TO_FQDN_TO_EDGE:  // NOTE: this is for G2F portion only
					source = topic.getGlobalMrURL();
					target = groupCentralCluster.getFqdn();
					break;

				case REPLICATION_FQDN_TO_EDGE:
				case REPLICATION_EDGE_TO_FQDN:
				case REPLICATION_EDGE_TO_CENTRAL:
				case REPLICATION_CENTRAL_TO_EDGE:
					break;
				default:
					logger.error( "Unexpected value for ReplicationType ("+ topic.getReplicationCase() + ") for topic " + topic.getFqtn() );
					anythingWrong = true;
					err.setCode(400);
					err.setFields("topic=" + topic.genFqtn() + " replicationCase="
							+ topic.getReplicationCase() );
					err.setMessage("Unexpected value for ReplicationType");
					continue;
				}				
			} else {
				logger.warn( "dcaeLocation " + loc + " is neither Edge nor Central so no mmagent provisioning was done");
				anythingWrong = true;
				continue;
			}
			if ( source != null && target != null ) {
				try { 
					logger.info( "Create a MM from " + source + " to " + target );
					MirrorMaker mm = bridge.getNextMM( source, target, topic.getFqtn());
					mm.addTopic(topic.getFqtn());
					bridge.updateMirrorMaker(mm);
				} catch ( Exception ex ) {
					err.setCode(500);
					err.setFields( "mirror_maker.topic");
					err.setMessage("Unexpected condition: " + ex );
					anythingWrong = true;
					break;
				}
			}			

			
		}
		return  anythingWrong;

	}
	
	
	/*
	 * Prior to 1707, we only supported EDGE_TO_CENTRAL replication.
	 * This was determined automatically based on presence of edge publishers and central subscribers.
	 * The following method is a modification of that original logic, to preserve some backwards compatibility, 
	 * i.e. to be used when no ReplicationType is specified.
	 */
	public ReplicationType reviewTopic( Topic topic ) {
	
		
		if ( topic.getNumClients() > 1 ) {
			Graph graph = new Graph( topic.getClients(), false );
			
			String centralFqdn = new String();
			if ( graph.hasCentral() ) {
				DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
				centralFqdn = p.getProperty("MR.CentralCname");
			}

			Collection<String> locations = graph.getKeys();
			for( String loc : locations ) {
				logger.info( "loc=" + loc );
				MR_Cluster cluster = clusters.getMr_ClusterByLoc(loc);
				if ( cluster == null ) {
					logger.info( "No MR cluster for location " + loc );
					continue;
				}
				if ( graph.hasCentral() &&  ! graph.getCentralLoc().equals(cluster.getDcaeLocationName())) {
					logger.info( "Detected case for EDGE_TO_CENTRAL from " + cluster.getFqdn() + " to " + centralFqdn );
					return ReplicationType.REPLICATION_EDGE_TO_CENTRAL;
					
				}
				
			}
		}
	
		return ReplicationType.REPLICATION_NONE;
	}

}