2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7 * Modifications Copyright (C) 2019 IBM.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.dmaap.dbcapi.service;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Iterator;
28 import java.util.List;
32 import javax.ws.rs.core.Response.Status;
34 import org.onap.dmaap.dbcapi.aaf.AafNamespace;
35 import org.onap.dmaap.dbcapi.aaf.AafRole;
36 import org.onap.dmaap.dbcapi.aaf.AafService;
37 import org.onap.dmaap.dbcapi.aaf.DmaapGrant;
38 import org.onap.dmaap.dbcapi.aaf.AafService.ServiceType;
39 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
40 import org.onap.dmaap.dbcapi.database.DatabaseClass;
41 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
42 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
43 import org.onap.dmaap.dbcapi.model.ApiError;
44 import org.onap.dmaap.dbcapi.model.DcaeLocation;
45 import org.onap.dmaap.dbcapi.model.Dmaap;
46 import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status;
47 import org.onap.dmaap.dbcapi.model.MR_Client;
48 import org.onap.dmaap.dbcapi.model.MR_Cluster;
49 import org.onap.dmaap.dbcapi.model.MirrorMaker;
50 import org.onap.dmaap.dbcapi.model.ReplicationType;
51 import org.onap.dmaap.dbcapi.model.Topic;
52 import org.onap.dmaap.dbcapi.util.DmaapConfig;
53 import org.onap.dmaap.dbcapi.util.Fqdn;
54 import org.onap.dmaap.dbcapi.util.Graph;
56 public class TopicService extends BaseLoggingClass {
60 // REF: https://wiki.web.att.com/pages/viewpage.action?pageId=519703122
61 private static String defaultGlobalMrHost;
63 private Map<String, Topic> mr_topics = DatabaseClass.getTopics();
65 private static DmaapService dmaapSvc = new DmaapService();
66 private MR_ClientService clientService = new MR_ClientService();
67 private MR_ClusterService clusters = new MR_ClusterService();
68 private DcaeLocationService locations = new DcaeLocationService();
69 private MirrorMakerService bridge = new MirrorMakerService();
71 private static String centralCname;
74 public TopicService(){
75 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
76 defaultGlobalMrHost = p.getProperty("MR.globalHost", "global.host.not.set");
77 centralCname = p.getProperty("MR.CentralCname");
80 logger.info( "TopicService properties: CentralCname=" + centralCname +
81 " defaultGlobarlMrHost=" + defaultGlobalMrHost );
84 public Map<String, Topic> getTopics() {
88 public List<Topic> getAllTopics() {
89 return getAllTopics( true );
91 public List<Topic> getAllTopicsWithoutClients() {
92 return getAllTopics(false);
95 private List<Topic> getAllTopics( Boolean withClients ) {
96 ArrayList<Topic> topics = new ArrayList<>(mr_topics.values());
98 for( Topic topic: topics ) {
99 topic.setClients( clientService.getAllMrClients(topic.getFqtn()));
106 public Topic getTopic( String key, ApiError apiError ) {
107 logger.info( "getTopic: key=" + key);
108 Topic t = mr_topics.get( key );
110 apiError.setCode(Status.NOT_FOUND.getStatusCode());
111 apiError.setFields( "fqtn");
112 apiError.setMessage("topic with fqtn " + key + " not found");
115 t.setClients( clientService.getAllMrClients( key ));
116 apiError.setCode(Status.OK.getStatusCode());
120 private void aafTopicSetup(Topic topic, ApiError err ) {
122 String t = dmaapSvc.getTopicPerm();
125 err.setMessage("Unable to establish AAF namespace root: (check /dmaap object)" );
126 err.setFields("topicNsRoot");
130 // establish AAF Connection using TopicMgr identity
131 AafService aaf = new AafService(ServiceType.AAF_TopicMgr);
135 // create AAF namespace for this topic
136 AafNamespace ns = new AafNamespace( topic.getFqtn(), aaf.getIdentity());
138 int rc = aaf.addNamespace( ns );
139 if ( rc != 201 && rc != 409 ) {
141 err.setMessage("Unexpected response from AAF:" + rc );
142 err.setFields("namespace:" + topic.getFqtn() + " identity="+ aaf.getIdentity());
147 // create AAF Roles for MR clients of this topic
148 String rn = "publisher";
149 AafRole pubRole = new AafRole( topic.getFqtn(), rn );
150 int rc = aaf.addRole( pubRole );
151 if ( rc != 201 && rc != 409 ) {
153 err.setMessage("Unexpected response from AAF:" + rc );
154 err.setFields("topic:" + topic.getFqtn() + " role="+ rn);
157 topic.setPublisherRole( pubRole.getFullyQualifiedRole() );
160 AafRole subRole = new AafRole( topic.getFqtn(), rn );
161 rc = aaf.addRole( subRole );
162 if ( rc != 201 && rc != 409 ) {
164 err.setMessage("Unexpected response from AAF:" + rc );
165 err.setFields("topic:" + topic.getFqtn() + " role="+ rn);
168 topic.setSubscriberRole( subRole.getFullyQualifiedRole() );
171 // create AAF perms checked by MR
172 String instance = ":topic." + topic.getFqtn();
173 String[] actions = { "pub", "sub", "view" };
174 for ( String action : actions ){
175 DmaapPerm perm = new DmaapPerm( t, instance, action );
176 rc = aaf.addPerm( perm );
177 if ( rc != 201 && rc != 409 ) {
179 err.setMessage("Unexpected response from AAF:" + rc );
180 err.setFields("t="+t + " instance="+ instance + " action="+ action);
183 // Grant perms to our default Roles
184 if ( action.equals( "pub") || action.equals( "view") ) {
185 DmaapGrant g = new DmaapGrant( perm, pubRole.getFullyQualifiedRole() );
186 rc = aaf.addGrant( g );
187 if ( rc != 201 && rc != 409 ) {
189 err.setMessage( "Grant of " + perm.toString() + " failed for " + pubRole.getFullyQualifiedRole() );
190 logger.warn( err.getMessage());
194 if ( action.equals( "sub") || action.equals( "view") ) {
195 DmaapGrant g = new DmaapGrant( perm, subRole.getFullyQualifiedRole() );
196 rc = aaf.addGrant( g );
197 if ( rc != 201 && rc != 409 ) {
199 err.setMessage( "Grant of " + perm.toString() + " failed for " + subRole.getFullyQualifiedRole() );
200 logger.warn( err.getMessage());
208 public Topic addTopic( Topic topic, ApiError err, Boolean useExisting ) {
209 logger.info( "Entry: addTopic");
210 logger.info( "Topic name=" + topic.getTopicName() + " fqtnStyle=" + topic.getFqtnStyle() );
211 String nFqtn = topic.genFqtn();
212 logger.info( "FQTN=" + nFqtn );
213 Topic pTopic = getTopic( nFqtn, err );
214 if ( pTopic != null ) {
215 String t = "topic already exists: " + nFqtn;
218 err.setCode(Status.OK.getStatusCode());
222 err.setFields( "fqtn");
223 err.setCode(Status.CONFLICT.getStatusCode());
226 err.reset(); // err filled with NOT_FOUND is expected case, but don't want to litter...
228 topic.setFqtn( nFqtn );
230 aafTopicSetup( topic, err );
231 if ( err.getCode() >= 400 ) {
235 if ( topic.getReplicationCase().involvesGlobal() ) {
236 if ( topic.getGlobalMrURL() == null ) {
237 topic.setGlobalMrURL(defaultGlobalMrHost);
239 if ( ! Fqdn.isValid( topic.getGlobalMrURL())) {
240 logger.error( "GlobalMR FQDN not valid: " + topic.getGlobalMrURL());
241 topic.setStatus( DmaapObject_Status.INVALID);
243 err.setMessage("Value is not a valid FQDN:" + topic.getGlobalMrURL() );
244 err.setFields("globalMrURL");
251 if ( topic.getNumClients() > 0 ) {
252 ArrayList<MR_Client> clients = new ArrayList<MR_Client>(topic.getClients());
255 ArrayList<MR_Client> clients2 = new ArrayList<MR_Client>();
256 for ( Iterator<MR_Client> it = clients.iterator(); it.hasNext(); ) {
257 MR_Client c = it.next();
259 logger.info( "c fqtn=" + c.getFqtn() + " ID=" + c.getMrClientId() + " url=" + c.getTopicURL());
260 MR_Client nc = new MR_Client( c.getDcaeLocationName(), topic.getFqtn(), c.getClientRole(), c.getAction());
261 nc.setFqtn(topic.getFqtn());
262 nc.setClientIdentity( c.getClientIdentity());
263 logger.info( "nc fqtn=" + nc.getFqtn() + " ID=" + nc.getMrClientId() + " url=" + nc.getTopicURL());
264 clients2.add( clientService.addMr_Client(nc, topic, err));
265 if ( ! err.is2xx()) {
270 topic.setClients(clients2);
273 Topic ntopic = checkForBridge( topic, err );
274 if ( ntopic == null ) {
275 topic.setStatus( DmaapObject_Status.INVALID);
276 if ( ! err.is2xx()) {
282 mr_topics.put( nFqtn, ntopic );
284 err.setCode(Status.OK.getStatusCode());
289 public Topic updateTopic( Topic topic, ApiError err ) {
290 logger.info( "Entry: updateTopic");
291 if ( topic.getFqtn().isEmpty()) {
294 Topic ntopic = checkForBridge( topic, err );
295 if ( ntopic == null ) {
296 topic.setStatus( DmaapObject_Status.INVALID);
297 if ( ! err.is2xx() ) {
302 mr_topics.put( ntopic.getFqtn(), ntopic );
304 err.setCode(Status.OK.getStatusCode());
308 public Topic removeTopic( String pubId, ApiError apiError ) {
309 Topic topic = mr_topics.get(pubId);
310 if ( topic == null ) {
311 apiError.setCode(Status.NOT_FOUND.getStatusCode());
312 apiError.setMessage("Topic " + pubId + " does not exist");
313 apiError.setFields("fqtn");
316 ArrayList<MR_Client> clients = new ArrayList<MR_Client>(clientService.getAllMrClients( pubId ));
317 for ( Iterator<MR_Client> it = clients.iterator(); it.hasNext(); ) {
318 MR_Client c = it.next();
321 clientService.removeMr_Client(c.getMrClientId(), false, apiError);
322 if ( ! apiError.is2xx()) {
326 apiError.setCode(Status.OK.getStatusCode());
327 return mr_topics.remove(pubId);
329 public static ApiError setBridgeClientPerms( MR_Cluster node ) {
330 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
331 String mmProvRole = p.getProperty("MM.ProvRole");
332 String mmAgentRole = p.getProperty("MM.AgentRole");
333 String[] Roles = { mmProvRole, mmAgentRole };
334 String[] actions = { "view", "pub", "sub" };
335 Topic bridgeAdminTopic = new Topic().init();
336 bridgeAdminTopic.setTopicName( dmaapSvc.getBridgeAdminFqtn() );
337 bridgeAdminTopic.setTopicDescription( "RESERVED topic for MirroMaker Provisioning");
338 bridgeAdminTopic.setOwner( "DBC" );
340 ArrayList<MR_Client> clients = new ArrayList<MR_Client>();
341 for( String role: Roles ) {
342 MR_Client client = new MR_Client();
343 client.setAction(actions);
344 client.setClientRole(role);
345 client.setDcaeLocationName( node.getDcaeLocationName());
346 clients.add( client );
348 bridgeAdminTopic.setClients(clients);
350 TopicService ts = new TopicService();
351 ApiError err = new ApiError();
352 ts.addTopic(bridgeAdminTopic, err, true);
354 if ( err.is2xx() || err.getCode() == 409 ){
359 errorLogger.error( DmaapbcLogMessageEnum.TOPIC_CREATE_ERROR, bridgeAdminTopic.getFqtn(), Integer.toString(err.getCode()), err.getFields(), err.getMessage());
364 public Topic checkForBridge( Topic topic, ApiError err ) {
366 if ( topic.getReplicationCase() == ReplicationType.REPLICATION_NONE ) {
367 topic.setStatus( DmaapObject_Status.VALID);
371 boolean anythingWrong = false;
373 Set<String> groups = clusters.getGroups();
374 for ( String g : groups ) {
375 anythingWrong |= buildBridge( topic, err, g );
377 if ( anythingWrong ) {
378 topic.setStatus( DmaapObject_Status.INVALID);
379 if ( ! err.is2xx() ) {
383 topic.setStatus( DmaapObject_Status.VALID);
388 private boolean buildBridge( Topic topic, ApiError err, String group ) {
390 boolean anythingWrong = false;
392 if ( group == null || group.isEmpty() ) {
393 graph = new Graph( topic.getClients(), true );
395 graph = new Graph( topic.getClients(), true, group );
397 MR_Cluster groupCentralCluster = null;
399 if ( graph.isEmpty() ) {
401 } else if ( group == null && topic.getReplicationCase().involvesFQDN() ) {
403 } else if ( ! graph.hasCentral() ) {
404 logger.warn( "Topic " + topic.getFqtn() + " wants to be " + topic.getReplicationCase() + " but has no central clients");
407 groupCentralCluster = clusters.getMr_ClusterByLoc(graph.getCentralLoc());
409 Collection<String> clientLocations = graph.getKeys();
410 for( String loc : clientLocations ) {
411 logger.info( "loc=" + loc );
412 DcaeLocation location = locations.getDcaeLocation(loc);
413 MR_Cluster cluster = clusters.getMr_ClusterByLoc(loc);
414 logger.info( "cluster=" + cluster );
418 String source = null;
419 String target = null;
422 * Provision Edge to Central bridges...
424 if ( ! location.isCentral() && ! graph.getCentralLoc().equals(cluster.getDcaeLocationName()) ) {
425 switch( topic.getReplicationCase() ) {
426 case REPLICATION_EDGE_TO_CENTRAL:
427 case REPLICATION_EDGE_TO_CENTRAL_TO_GLOBAL: // NOTE: this is for E2C portion only
428 source = cluster.getFqdn();
429 target = centralCname;
431 case REPLICATION_CENTRAL_TO_EDGE:
432 case REPLICATION_GLOBAL_TO_CENTRAL_TO_EDGE: // NOTE: this is for C2E portion only
433 source = centralCname;
434 target = cluster.getFqdn();
436 case REPLICATION_CENTRAL_TO_GLOBAL:
437 case REPLICATION_GLOBAL_TO_CENTRAL:
438 case REPLICATION_FQDN_TO_GLOBAL:
439 case REPLICATION_GLOBAL_TO_FQDN:
442 case REPLICATION_EDGE_TO_FQDN:
443 case REPLICATION_EDGE_TO_FQDN_TO_GLOBAL: // NOTE: this is for E2C portion only
444 source = cluster.getFqdn();
445 target = groupCentralCluster.getFqdn();
447 case REPLICATION_FQDN_TO_EDGE:
448 case REPLICATION_GLOBAL_TO_FQDN_TO_EDGE: // NOTE: this is for F2E portion only
449 source = groupCentralCluster.getFqdn();
450 target = cluster.getFqdn();
454 logger.error( "Unexpected value for ReplicationType ("+ topic.getReplicationCase() + ") for topic " + topic.getFqtn() );
455 anythingWrong = true;
457 err.setFields("topic=" + topic.genFqtn() + " replicationCase="
458 + topic.getReplicationCase() );
459 err.setMessage("Unexpected value for ReplicationType");
463 } else if ( location.isCentral() && graph.getCentralLoc().equals(cluster.getDcaeLocationName()) ) {
465 * Provision Central to Global bridges
467 switch( topic.getReplicationCase() ) {
469 case REPLICATION_CENTRAL_TO_GLOBAL:
470 case REPLICATION_EDGE_TO_CENTRAL_TO_GLOBAL:
471 source = centralCname;
472 target = topic.getGlobalMrURL();
474 case REPLICATION_GLOBAL_TO_CENTRAL:
475 case REPLICATION_GLOBAL_TO_CENTRAL_TO_EDGE: // NOTE: this is for G2C portion only
476 source = topic.getGlobalMrURL();
477 target = centralCname;
480 case REPLICATION_EDGE_TO_FQDN_TO_GLOBAL: // NOTE: this is for E2F portion only
481 source = groupCentralCluster.getFqdn();
482 target = topic.getGlobalMrURL();
485 case REPLICATION_FQDN_TO_GLOBAL:
486 source = groupCentralCluster.getFqdn();
487 target = topic.getGlobalMrURL();
490 case REPLICATION_GLOBAL_TO_FQDN:
491 case REPLICATION_GLOBAL_TO_FQDN_TO_EDGE: // NOTE: this is for G2F portion only
492 source = topic.getGlobalMrURL();
493 target = groupCentralCluster.getFqdn();
496 case REPLICATION_FQDN_TO_EDGE:
497 case REPLICATION_EDGE_TO_FQDN:
498 case REPLICATION_EDGE_TO_CENTRAL:
499 case REPLICATION_CENTRAL_TO_EDGE:
502 logger.error( "Unexpected value for ReplicationType ("+ topic.getReplicationCase() + ") for topic " + topic.getFqtn() );
503 anythingWrong = true;
505 err.setFields("topic=" + topic.genFqtn() + " replicationCase="
506 + topic.getReplicationCase() );
507 err.setMessage("Unexpected value for ReplicationType");
511 logger.warn( "dcaeLocation " + loc + " is neither Edge nor Central so no mmagent provisioning was done");
512 anythingWrong = true;
515 if ( source != null && target != null ) {
517 logger.info( "Create a MM from " + source + " to " + target );
518 MirrorMaker mm = bridge.getNextMM( source, target, topic.getFqtn());
519 mm.addTopic(topic.getFqtn());
520 bridge.updateMirrorMaker(mm);
521 } catch ( Exception ex ) {
523 err.setFields( "mirror_maker.topic");
524 err.setMessage("Unexpected condition: " + ex );
525 anythingWrong = true;
532 return anythingWrong;
538 * Prior to 1707, we only supported EDGE_TO_CENTRAL replication.
539 * This was determined automatically based on presence of edge publishers and central subscribers.
540 * The following method is a modification of that original logic, to preserve some backwards compatibility,
541 * i.e. to be used when no ReplicationType is specified.
543 public ReplicationType reviewTopic( Topic topic ) {
546 if ( topic.getNumClients() > 1 ) {
547 Graph graph = new Graph( topic.getClients(), false );
549 String centralFqdn = new String();
550 if ( graph.hasCentral() ) {
551 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
552 centralFqdn = p.getProperty("MR.CentralCname");
555 Collection<String> locations = graph.getKeys();
556 for( String loc : locations ) {
557 logger.info( "loc=" + loc );
558 MR_Cluster cluster = clusters.getMr_ClusterByLoc(loc);
559 if ( cluster == null ) {
560 logger.info( "No MR cluster for location " + loc );
563 if ( graph.hasCentral() && ! graph.getCentralLoc().equals(cluster.getDcaeLocationName())) {
564 logger.info( "Detected case for EDGE_TO_CENTRAL from " + cluster.getFqdn() + " to " + centralFqdn );
565 return ReplicationType.REPLICATION_EDGE_TO_CENTRAL;
572 return ReplicationType.REPLICATION_NONE;