[SDC-BE] Remove hardcoded MR apiKey

Issue-ID: SDC-3714
Change-Id: Ie154a225bc1c47d6d3b504f915a697047e1b0440
Signed-off-by: MichaelMorris <michael.morris@est.tech>
diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/config/mocks/DistributionEngineClusterHealthMock.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/config/mocks/DistributionEngineClusterHealthMock.java
index b7273d7..4294ca6 100644
--- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/config/mocks/DistributionEngineClusterHealthMock.java
+++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/migration/config/mocks/DistributionEngineClusterHealthMock.java
@@ -26,7 +26,7 @@
 public class DistributionEngineClusterHealthMock extends DistributionEngineClusterHealth {
 
     @Override
-    protected void init() {
+    protected void init(final String publicApiKey) {
     }
 
     @Override
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/distribution/engine/CambriaHandler.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/distribution/engine/CambriaHandler.java
index 425b464..41d1073 100644
--- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/distribution/engine/CambriaHandler.java
+++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/distribution/engine/CambriaHandler.java
@@ -62,10 +62,6 @@
     private static final Logger log = Logger.getLogger(CambriaHandler.class.getName());
     private static final String PARTITION_KEY = "asdc" + "aa";
     private static final String SEND_NOTIFICATION = "send notification";
-    private static final String CONSUMER_ID = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration()
-        .getDistributionStatusTopic().getConsumerId();
-    private static final boolean USE_HTTPS_WITH_DMAAP = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration()
-        .isUseHttpsWithDmaap();
     private final Gson gson = new Gson();
 
     /**
@@ -284,7 +280,9 @@
                                                                                                String managerSecretKey) {
         AbstractAuthenticatedManagerBuilder<CambriaTopicManager> clientBuilder = createTopicManagerBuilder(hostSet)
             .authenticatedBy(managerApiKey, managerSecretKey);
-        if (USE_HTTPS_WITH_DMAAP) {
+        final boolean useHttpsWithDmaap = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration()
+                .isUseHttpsWithDmaap();
+        if (useHttpsWithDmaap) {
             clientBuilder = clientBuilder.usingHttps();
         }
         return clientBuilder;
@@ -506,7 +504,9 @@
         Either<ApiCredential, CambriaErrorResponse> result;
         try {
             CambriaIdentityManager createIdentityManager = buildCambriaClient(new IdentityManagerBuilder().usingHosts(hostSet));
-            String description = String.format("ASDC Key for %s", CONSUMER_ID);
+            final String consumerId = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration()
+                    .getDistributionStatusTopic().getConsumerId();
+            String description = String.format("ASDC Key for %s", consumerId);
             ApiCredential credential = createIdentityManager.createApiKey("", description);
             createIdentityManager.setApiCredentials(credential.getApiKey(), credential.getApiSecret());
             result = Either.left(credential);
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/distribution/engine/DistributionEngineClusterHealth.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/distribution/engine/DistributionEngineClusterHealth.java
index b1bf59f..0851f22 100644
--- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/distribution/engine/DistributionEngineClusterHealth.java
+++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/distribution/engine/DistributionEngineClusterHealth.java
@@ -31,7 +31,6 @@
 import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
-import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
 import org.openecomp.sdc.be.config.BeEcompErrorManager;
 import org.openecomp.sdc.be.config.ConfigurationManager;
@@ -66,9 +65,8 @@
     private HealthCheckInfo healthCheckInfo = HealthCheckInfoResult.UNKNOWN.getHealthCheckInfo();
     private Map<String, AtomicBoolean> envNamePerStatus = null;
     private ScheduledFuture<?> scheduledFuture = null;
-
-    @PostConstruct
-    protected void init() {
+    
+    protected void init(final String publicApiKey) {
         logger.trace("Enter init method of DistributionEngineClusterHealth");
         Long reconnectIntervalConfig = ConfigurationManager.getConfigurationManager().getConfiguration()
             .getUebHealthCheckReconnectIntervalInSeconds();
@@ -82,7 +80,7 @@
         DistributionEngineConfiguration distributionEngineConfiguration = ConfigurationManager.getConfigurationManager()
             .getDistributionEngineConfiguration();
         this.uebServers = distributionEngineConfiguration.getUebServers();
-        this.publicApiKey = distributionEngineConfiguration.getUebPublicKey();
+        this.publicApiKey = publicApiKey;
         this.healthCheckScheduledTask = new HealthCheckScheduledTask(this.uebServers);
         logger.trace("Exit init method of DistributionEngineClusterHealth");
     }
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/distribution/engine/EnvironmentsEngine.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/distribution/engine/EnvironmentsEngine.java
index 26992b9..afb311f 100644
--- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/distribution/engine/EnvironmentsEngine.java
+++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/distribution/engine/EnvironmentsEngine.java
@@ -168,6 +168,7 @@
         String envId = opEnvEntry.getEnvironmentId();
         DistributionEngineConfiguration distributionEngineConfiguration = ConfigurationManager.getConfigurationManager()
             .getDistributionEngineConfiguration();
+        distributionEngineClusterHealth.init(opEnvEntry.getUebApikey());
         DistributionEnginePollingTask distributionEnginePollingTask = new DistributionEnginePollingTask(distributionEngineConfiguration,
             distributionCompleteReporter, componentUtils, distributionEngineClusterHealth, opEnvEntry);
         String envName = configurationManager.getDistributionEngineConfiguration().getEnvironments().get(0);
@@ -430,9 +431,25 @@
     private Map<String, OperationalEnvironmentEntry> populateEnvironments() {
         Map<String, OperationalEnvironmentEntry> envs = getEnvironmentsFromDb();
         OperationalEnvironmentEntry confEntry = readEnvFromConfig();
+        readKeyFromDbOrCreate(envs, confEntry);
         envs.put(confEntry.getEnvironmentId(), confEntry);
         return envs;
     }
+    
+    private void readKeyFromDbOrCreate(Map<String, OperationalEnvironmentEntry> envs, OperationalEnvironmentEntry confEntry) {
+        if (envs.get(confEntry.getEnvironmentId()) == null){
+            log.debug("Creating UEB API key");
+            Wrapper<Boolean> errorWrapper = new Wrapper<>();
+            createUebKeys(errorWrapper, confEntry);
+            confEntry.setStatus(EnvironmentStatusEnum.COMPLETED);
+            log.debug("Saving UEB API key to DB");
+            operationalEnvironmentDao.save(confEntry);
+        } else {
+            log.debug("Reading UEB API key from DB");
+            confEntry.setUebApikey(envs.get(confEntry.getEnvironmentId()).getUebApikey());
+            confEntry.setUebSecretKey(envs.get(confEntry.getEnvironmentId()).getUebSecretKey());
+        }
+    }
 
     private OperationalEnvironmentEntry readEnvFromConfig() {
         OperationalEnvironmentEntry entry = new OperationalEnvironmentEntry();
diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/distribution/engine/DistributionEngineClusterHealthTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/distribution/engine/DistributionEngineClusterHealthTest.java
index 97cb367..5f2b412 100644
--- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/distribution/engine/DistributionEngineClusterHealthTest.java
+++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/distribution/engine/DistributionEngineClusterHealthTest.java
@@ -20,6 +20,10 @@
 
 package org.openecomp.sdc.be.components.distribution.engine;
 
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
 import mockit.Deencapsulation;
 import org.junit.Test;
 import org.openecomp.sdc.be.components.BeConfDependentTest;
@@ -30,6 +34,7 @@
 import java.util.LinkedList;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicBoolean;
+import org.openecomp.sdc.be.resources.data.OperationalEnvironmentEntry;
 
 public class DistributionEngineClusterHealthTest extends BeConfDependentTest{
 
@@ -38,15 +43,6 @@
 	}
 
 	@Test
-	public void testInit() throws Exception {
-		DistributionEngineClusterHealth testSubject;
-
-		// default test
-		testSubject = createTestSubject();
-		Deencapsulation.invoke(testSubject, "init");
-	}
-
-	@Test
 	public void testDestroy() throws Exception {
 		DistributionEngineClusterHealth testSubject;
 
@@ -67,14 +63,11 @@
 	}
 
 	@Test
-	public void testStartHealthCheckTask_1() throws Exception {
-		DistributionEngineClusterHealth testSubject;
-		Map<String, AtomicBoolean> envNamePerStatus = new HashMap<>();
-
-		// default test
-		testSubject = createTestSubject();
-		testSubject.init();
-		testSubject.startHealthCheckTask(envNamePerStatus);
+	public void testStartHealthCheckTask_1() {
+	    final DistributionEngineClusterHealth distributionEngineClusterHealth = new DistributionEngineClusterHealth();
+	    final Map<String, AtomicBoolean> envNamePerStatus = new HashMap<>();
+	    distributionEngineClusterHealth.init("myKey");
+	    distributionEngineClusterHealth.startHealthCheckTask(envNamePerStatus);
 	}
 
 	@Test
diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/distribution/engine/EnvironmentsEngineTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/distribution/engine/EnvironmentsEngineTest.java
index 9ac0488..1097434 100644
--- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/distribution/engine/EnvironmentsEngineTest.java
+++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/distribution/engine/EnvironmentsEngineTest.java
@@ -51,11 +51,12 @@
 import java.util.Map;
 import org.openecomp.sdc.common.impl.ExternalConfiguration;
 import org.openecomp.sdc.common.impl.FSConfigurationSource;
-
+import com.att.nsa.apiClient.credentials.ApiCredential;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.any;
 import static org.mockito.Mockito.when;
 
 public class EnvironmentsEngineTest {
@@ -70,6 +71,8 @@
     private DistributionEngineConfiguration distributionEngineConfiguration;
     @Mock
     private AaiRequestHandler aaiRequestHandler;
+    @Mock 
+    private CambriaHandler cambriaHandler;
 
     @Before
     public void preStart() {
@@ -94,8 +97,10 @@
         when(configurationManager.getConfiguration()).thenReturn(config);
         when(config.getDmaapConsumerConfiguration()).thenReturn(dmaapConf);
         when(dmaapConf.isActive()).thenReturn(false);
+        ApiCredential apiCredential = new ApiCredential("apiKey", "apiSecret");
+        when(cambriaHandler.createUebKeys(any())).thenReturn(Either.left(apiCredential));
         envEngine.init();
-
+        
         Map<String, OperationalEnvironmentEntry> mapEnvs = envEngine.getEnvironments();
         assertEquals("unexpected size of map",3, mapEnvs.size());
     }
diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/distribution/engine/StepsTenantIsolation.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/distribution/engine/StepsTenantIsolation.java
index 2d440aa..77c332a 100644
--- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/distribution/engine/StepsTenantIsolation.java
+++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/distribution/engine/StepsTenantIsolation.java
@@ -79,6 +79,8 @@
     private AaiRequestHandler aaiRequestHandler;
     @Mock
     private CambriaHandler cambriaHandler;
+    @Mock
+    private DistributionEngineClusterHealth distributionEngineClusterHealth;
     @InjectMocks
     @Spy
     private EnvironmentsEngine envEngine;
@@ -298,7 +300,7 @@
     @SuppressWarnings("unchecked")
     @Then("^trying to create Ueb keys (.*)$")
     public void trying_to_create_ueb_keys(boolean isActivated) throws Throwable {
-        verify(envEngine, Mockito.times(getNumberOfCallsToValidate(isActivated)))
+        verify(envEngine, Mockito.times(getNumberOfCallsToValidate(isActivated) + 1))
             .createUebKeys(Mockito.any(Wrapper.class), Mockito.any(OperationalEnvironmentEntry.class));
     }