MR_ClientService-replace type with diamonds
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / service / MR_ClientService.java
index 40f86b6..6010784 100644 (file)
@@ -3,6 +3,8 @@
  * org.onap.dmaap
  * ================================================================================
  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ *
+ * Modifications Copyright (C) 2019 IBM.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -38,6 +40,7 @@ import org.onap.dmaap.dbcapi.aaf.AafService;
 import org.onap.dmaap.dbcapi.aaf.DmaapGrant;
 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
 import org.onap.dmaap.dbcapi.aaf.AafService.ServiceType;
+import org.onap.dmaap.dbcapi.aaf.AafUserRole;
 import org.onap.dmaap.dbcapi.client.MrProvConnection;
 import org.onap.dmaap.dbcapi.database.DatabaseClass;
 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
@@ -57,10 +60,12 @@ public class MR_ClientService extends BaseLoggingClass{
        private Map<String, Topic> topics = DatabaseClass.getTopics();
        private Map<String, DcaeLocation> locations = DatabaseClass.getDcaeLocations();
        private DmaapService dmaap = new DmaapService();
+       private String centralCname;
        
        public MR_ClientService() {
                DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
-               
+       
+               centralCname = p.getProperty("MR.CentralCname", "MRcname.not.set");
                deleteLevel = Integer.valueOf(p.getProperty("MR.ClientDeleteLevel", "0" ));
        }
        
@@ -73,7 +78,7 @@ public class MR_ClientService extends BaseLoggingClass{
        }
        
        public ArrayList<MR_Client> getAllMrClients(String fqtn) {
-               ArrayList<MR_Client> results = new ArrayList<MR_Client>();
+               ArrayList<MR_Client> results = new ArrayList<>();
                for (Map.Entry<String, MR_Client> entry : mr_clients.entrySet())
                {
                        MR_Client client = entry.getValue();
@@ -126,18 +131,33 @@ public class MR_ClientService extends BaseLoggingClass{
                        logger.info( "Client  dcaeLocation that doesn't exist or not specified" );
                        return null;
                }
-               grantClientPerms(  client,  err);
+               // original style: clients specified Role.  This has precedence for backwards
+               //                 compatibility.
+               // ONAP style: clients specify Identity to be assigned to generated Role
+               String role = client.getClientRole();
+               if ( role != null ) {
+                       grantClientRolePerms(  client,  err);
+               } else if ( client.hasClientIdentity() ){
+                       if ( client.isSubscriber() ) {
+                               role = topic.getSubscriberRole();
+                               assignIdentityToRole( client, role, err );
+                       } 
+                       if (client.isPublisher() ) {
+                               role = topic.getPublisherRole();
+                               assignIdentityToRole( client, role, err );
+                       }       
+               } 
                if ( ! client.isStatusValid()) {
                        return null;
                }
                String centralFqdn = null;
                DcaeLocation candidate = locations.get(client.getDcaeLocationName());
-               if ( candidate != null && candidate.isCentral() ) {
-                       DmaapConfig p = ( DmaapConfig)DmaapConfig.getConfig();
-                       centralFqdn = p.getProperty("MR.CentralCname");
-               }
+
                MR_Cluster cluster = clusters.get( client.getDcaeLocationName());
-               if (  cluster != null ) {
+               if (  cluster != null && candidate != null ) {
+                       if ( candidate.isCentral() && ! topic.getReplicationCase().involvesFQDN() ) {
+                               centralFqdn = centralCname;
+                       }
                        client.setTopicURL(cluster.genTopicURL(centralFqdn, client.getFqtn()));
                        if ( centralFqdn == null ) {
                                client.setStatus( addTopicToCluster( cluster, topic, err));
@@ -148,8 +168,8 @@ public class MR_ClientService extends BaseLoggingClass{
                        
                        } else {
                                MR_ClusterService clusters = new MR_ClusterService();   
-                               // in 1610, MM should only exist for edge-to-central
-                               //  we use a cname for the central target
+                               //  MM should only exist for edge-to-central
+                               //  we use a cname for the central target (default resiliency with no replicationGroup set)
                                // but still need to provision topics on all central MRs
                                for( MR_Cluster central: clusters.getCentralClusters() ) {
                                        client.setStatus( addTopicToCluster( central, topic, err));
@@ -161,7 +181,7 @@ public class MR_ClientService extends BaseLoggingClass{
                        }
                        
                } else {
-                       logger.info( "Client references a dcaeLocation that doesn't exist:" + client.getDcaeLocationName());
+                       logger.warn( "Client references a dcaeLocation that doesn't exist:" + client.getDcaeLocationName());
                        client.setStatus( DmaapObject_Status.STAGED);
                        //return null;
                }
@@ -187,26 +207,46 @@ public class MR_ClientService extends BaseLoggingClass{
                return DmaapObject_Status.INVALID;
        }
        
-       private void grantClientPerms( MR_Client client, ApiError err) {
+       private void grantClientRolePerms( MR_Client client, ApiError err) {
                AafService aaf = new AafService(ServiceType.AAF_TopicMgr);
                
                String instance = ":topic." + client.getFqtn();
                client.setStatus( DmaapObject_Status.VALID);
+               String role = client.getClientRole();
                for( String want : client.getAction() ) {
                        int rc;
                        DmaapPerm perm = new DmaapPerm( dmaap.getTopicPerm(), instance, want );
-                       DmaapGrant g = new DmaapGrant( perm, client.getClientRole() );
-                       rc = aaf.addGrant( g );
-                       if ( rc != 201 && rc != 409 ) {
-                               client.setStatus( DmaapObject_Status.INVALID);
-                               err.setCode(rc);
-                               err.setMessage( "Grant of " + dmaap.getTopicPerm() + "|" + instance + "|" + want + " failed for " + client.getClientRole() );
-                               logger.warn( err.getMessage());
-                               return;
-                       } 
+                       if ( role != null ) {
+                               DmaapGrant g = new DmaapGrant( perm, role );
+                               rc = aaf.addGrant( g );
+                               if ( rc != 201 && rc != 409 ) {
+                                       client.setStatus( DmaapObject_Status.INVALID);
+                                       err.setCode(rc);
+                                       err.setMessage( "Grant of " + dmaap.getTopicPerm() + "|" + instance + "|" + want + " failed for " + role );
+                                       logger.warn( err.getMessage());
+                                       return;
+                               } 
+                       } else {
+                               logger.warn( "No Grant of " + dmaap.getTopicPerm() + "|" + instance + "|" + want + " because role is null " );
+                       }
                }
        }
        
+       private void assignIdentityToRole( MR_Client client, String role, ApiError err ) {
+               AafService aaf = new AafService(ServiceType.AAF_TopicMgr);
+
+               AafUserRole ur = new AafUserRole( client.getClientIdentity(), role );
+               int rc = aaf.addUserRole( ur );
+               if ( rc != 201 && rc != 409 ) {
+                       client.setStatus( DmaapObject_Status.INVALID);
+                       err.setCode(rc);
+                       err.setMessage( "Failed to add user " + client.getClientIdentity()+  "  to " + role );
+                       logger.warn( err.getMessage());
+                       return;
+               }
+               client.setStatus( DmaapObject_Status.VALID);
+
+       }
        private void revokeClientPerms( MR_Client client, ApiError err) {
                AafService aaf = new AafService(ServiceType.AAF_TopicMgr);