aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/components/distribution/engine/EnvironmentsEngine.java
blob: 7eb00ba9884a4a841b5b72f5dd9c8e8f5a55d0a1 (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
package org.openecomp.sdc.be.components.distribution.engine;

import com.att.aft.dme2.api.DME2Exception;
import com.att.aft.dme2.iterator.DME2EndpointIterator;
import com.att.aft.dme2.iterator.domain.DME2EndpointReference;
import com.att.aft.dme2.manager.registry.DME2Endpoint;
import com.att.nsa.apiClient.credentials.ApiCredential;
import com.google.common.annotations.VisibleForTesting;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import fj.data.Either;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.openecomp.sdc.be.components.distribution.engine.IDmaapNotificationData.DmaapActionEnum;
import org.openecomp.sdc.be.components.distribution.engine.IDmaapNotificationData.OperationaEnvironmentTypeEnum;
import org.openecomp.sdc.be.components.distribution.engine.report.DistributionCompleteReporter;
import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.be.config.DistributionEngineConfiguration;
import org.openecomp.sdc.be.config.DmaapConsumerConfiguration;
import org.openecomp.sdc.be.config.DmeConfiguration;
import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
import org.openecomp.sdc.be.dao.cassandra.OperationalEnvironmentDao;
import org.openecomp.sdc.be.datatypes.enums.EnvironmentStatusEnum;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.info.OperationalEnvInfo;
import org.openecomp.sdc.be.resources.data.OperationalEnvironmentEntry;
import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
import org.openecomp.sdc.common.datastructure.Wrapper;
import org.openecomp.sdc.common.http.client.api.HttpResponse;
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import static org.apache.commons.lang3.StringUtils.isEmpty;
import static org.openecomp.sdc.common.datastructure.FunctionalInterfaces.runMethodWithTimeOut;

/**
 * Allows to consume DMAAP topic and handle received notifications
 */
@Service
public class EnvironmentsEngine implements INotificationHandler {

    private static final String MESSAGE_BUS = "MessageBus";
    private static final String UNKNOWN = "Unknown";
    private static final Logger log = Logger.getLogger(EnvironmentsEngine.class.getName());
    private ConfigurationManager configurationManager = ConfigurationManager.getConfigurationManager();

    private Map<String, OperationalEnvironmentEntry> environments;
    private Map<String, AtomicBoolean> envNamePerStatus = new HashMap<>();
    private Map<String, DistributionEnginePollingTask> envNamePerPollingTask = new HashMap<>();
    private Map<String, DistributionEngineInitTask> envNamePerInitTask = new HashMap<>();

    private final DmaapConsumer dmaapConsumer;
    private final OperationalEnvironmentDao operationalEnvironmentDao;
    private final DME2EndpointIteratorCreator epIterCreator;
    private final AaiRequestHandler aaiRequestHandler;
    private final ComponentsUtils componentUtils;
    private final CambriaHandler cambriaHandler;
    private final DistributionEngineClusterHealth distributionEngineClusterHealth;
    private final DistributionCompleteReporter distributionCompleteReporter;

    public EnvironmentsEngine(DmaapConsumer dmaapConsumer, OperationalEnvironmentDao operationalEnvironmentDao, DME2EndpointIteratorCreator epIterCreator, AaiRequestHandler aaiRequestHandler, ComponentsUtils componentUtils, CambriaHandler cambriaHandler, DistributionEngineClusterHealth distributionEngineClusterHealth, DistributionCompleteReporter distributionCompleteReporter) {
        this.dmaapConsumer = dmaapConsumer;
        this.operationalEnvironmentDao = operationalEnvironmentDao;
        this.epIterCreator = epIterCreator;
        this.aaiRequestHandler = aaiRequestHandler;
        this.componentUtils = componentUtils;
        this.cambriaHandler = cambriaHandler;
        this.distributionEngineClusterHealth = distributionEngineClusterHealth;
        this.distributionCompleteReporter = distributionCompleteReporter;
    }

    @VisibleForTesting
    @PostConstruct
    void init() {
        log.trace("Environments engine has been initialized. ");
        try {
            environments = populateEnvironments();
            createUebTopicsForEnvironments();
            initDmeGlobalConfig();
            dmaapConsumer.consumeDmaapTopic(this::handleMessage,
                    (t, e) -> log.error("An error occurred upon consuming topic by Dmaap consumer client: ", e));
        } catch (Exception e) {
            log.error("An error occurred upon consuming topic by Dmaap consumer client.", e);
        }
    }

    private void initDmeGlobalConfig() {
        DmaapConsumerConfiguration dmaapConsumerParams = ConfigurationManager.getConfigurationManager().getConfiguration().getDmaapConsumerConfiguration();
        if (dmaapConsumerParams == null) {
            log.warn("cannot read dmaap configuration file,DME might not be initialized properly");
            return;
        }
        System.setProperty("AFT_ENVIRONMENT", dmaapConsumerParams.getEnvironment()); // AFTPRD for production
        System.setProperty("AFT_LATITUDE", dmaapConsumerParams.getLatitude()!=null ? dmaapConsumerParams.getLatitude().toString() : "1.0"); // Replace with actual latitude
        System.setProperty("AFT_LONGITUDE", dmaapConsumerParams.getLongitude()!=null ? dmaapConsumerParams.getLongitude().toString() : "1.0"); // Replace with actual longitude
    }

    public void connectUebTopicTenantIsolation(OperationalEnvironmentEntry opEnvEntry,
                                               AtomicBoolean status,
                                               Map<String, DistributionEngineInitTask> envNamePerInitTask, Map<String, DistributionEnginePollingTask> envNamePerPollingTask) {
        connectUebTopic(opEnvEntry, status, envNamePerInitTask, envNamePerPollingTask);

    }

    public void connectUebTopicForDistributionConfTopic(String envName,
                                                        AtomicBoolean status,
                                                        Map<String, DistributionEngineInitTask> envNamePerInitTask, Map<String, DistributionEnginePollingTask> envNamePerPollingTask) {
        connectUebTopic(environments.get(envName), status, envNamePerInitTask, envNamePerPollingTask);

    }

    /**
     * Allows to create and run UEB initializing and polling tasks
     *
     * @param status
     * @param envNamePerInitTask
     * @param envNamePerPollingTask
     * @param opEnvEntry
     */
    private void connectUebTopic(OperationalEnvironmentEntry opEnvEntry, AtomicBoolean status,
                                 Map<String, DistributionEngineInitTask> envNamePerInitTask,
                                 Map<String, DistributionEnginePollingTask> envNamePerPollingTask) {

        String envId = opEnvEntry.getEnvironmentId();

        DistributionEngineConfiguration distributionEngineConfiguration = ConfigurationManager.getConfigurationManager()
                .getDistributionEngineConfiguration();
        DistributionEnginePollingTask distributionEnginePollingTask = new DistributionEnginePollingTask(
                distributionEngineConfiguration, distributionCompleteReporter, componentUtils, distributionEngineClusterHealth,
                opEnvEntry);
        String envName = configurationManager.getDistributionEngineConfiguration().getEnvironments().get(0);
        DistributionEngineInitTask distributionEngineInitTask = new DistributionEngineInitTask(0l,
                distributionEngineConfiguration, envName, status, componentUtils, distributionEnginePollingTask,
                opEnvEntry);
        distributionEngineInitTask.startTask();
        envNamePerInitTask.put(envId, distributionEngineInitTask);
        envNamePerPollingTask.put(envId, distributionEnginePollingTask);

        log.debug("Environment envId = {} has been connected to the UEB topic", envId);
    }

    @Override
    public boolean handleMessage(String notification) {
        DmaapConsumerConfiguration dmaapConsumerParams = ConfigurationManager.getConfigurationManager()
                .getConfiguration().getDmaapConsumerConfiguration();
        Supplier<Boolean> supplier = () -> handleMessageLogic(notification);
        Either<Boolean, Boolean> eitherTimeOut = runMethodWithTimeOut(supplier,
                dmaapConsumerParams.getTimeLimitForNotificationHandleMs());

        boolean result;
        if (eitherTimeOut.isRight()) {
            result = false;
        } else {
            result = eitherTimeOut.left().value();
        }
        return result;
    }

    public boolean handleMessageLogic(String notification) {
        Wrapper<Boolean> errorWrapper = new Wrapper<>();
        Wrapper<OperationalEnvironmentEntry> opEnvEntryWrapper = new Wrapper<>();
        try {

            log.debug("handle message - for operational environment notification received: {}", notification);
            Gson gsonObj = new GsonBuilder().create();

            IDmaapNotificationData notificationData = gsonObj.fromJson(notification,
                    DmaapNotificationDataImpl.class);
            IDmaapAuditNotificationData auditNotificationData = gsonObj.fromJson(notification,
                    DmaapNotificationDataImpl.class);

            AuditingActionEnum actionEnum;
            switch (notificationData.getAction()) {
                case CREATE:
                    actionEnum = AuditingActionEnum.CREATE_ENVIRONMENT;
                    break;
                case UPDATE:
                    actionEnum = AuditingActionEnum.UPDATE_ENVIRONMENT;
                    break;
                case DELETE:
                    actionEnum = AuditingActionEnum.DELETE_ENVIRONMENT;
                    break;
                default:
                    actionEnum = AuditingActionEnum.UNKNOWN_ENVIRONMENT_NOTIFICATION;
                    break;
            }
            componentUtils.auditEnvironmentEngine(actionEnum,
                    notificationData.getOperationalEnvironmentId(), notificationData.getOperationalEnvironmentType().getEventTypenName(),
                    notificationData.getAction().getActionName(), auditNotificationData.getOperationalEnvironmentName(),
                    auditNotificationData.getTenantContext());

            if (errorWrapper.isEmpty()) {
                validateNotification(errorWrapper, notificationData, auditNotificationData);
            }
            // Perform Save In-Progress Dao
            if (errorWrapper.isEmpty()) {
                saveEntryWithInProgressStatus(errorWrapper, opEnvEntryWrapper, notificationData);
            }

            if (errorWrapper.isEmpty()) {
                buildOpEnv(errorWrapper, opEnvEntryWrapper.getInnerElement());
            }

        } catch (Exception e) {
            log.debug("handle message for operational environmet failed for notification: {} with error :{}",
                    notification, e.getMessage(), e);
            errorWrapper.setInnerElement(false);

        }
        return errorWrapper.isEmpty();
    }

    private void validateNotification(Wrapper<Boolean> errorWrapper, IDmaapNotificationData notificationData,
                                      IDmaapAuditNotificationData auditNotificationData) {
        // Check OperationaEnvironmentType
        if (errorWrapper.isEmpty()) {
            validateEnvironmentType(errorWrapper, notificationData, auditNotificationData);
        }
        // Check Action Type
        if (errorWrapper.isEmpty()) {
            validateActionType(errorWrapper, notificationData);
        }
        // Check is valid for create/update (not In-Progress state)
        if (errorWrapper.isEmpty()) {
            validateState(errorWrapper, notificationData);
        }
    }

    public void buildOpEnv(Wrapper<Boolean> errorWrapper, OperationalEnvironmentEntry opEnvEntry) {
        // Get Env Info From A&AI
        if (errorWrapper.isEmpty()) {
            retrieveOpEnvInfoFromAAI(errorWrapper, opEnvEntry);
        }

        if (errorWrapper.isEmpty()) {
            // Get List Of UEB Addresses From AFT_DME
            retrieveUebAddressesFromAftDme(errorWrapper, opEnvEntry);
        }

        // Create UEB keys and set them on EnvEntry
        if (errorWrapper.isEmpty()) {
            createUebKeys(errorWrapper, opEnvEntry);
        }

        // Create Topics
        if (errorWrapper.isEmpty()) {
            log.debug("handle message - Create Topics");
            createUebTopicsForEnvironment(opEnvEntry);
        }

        // Save Status Complete and Add to Map
        if (errorWrapper.isEmpty()) {
            saveEntryWithCompleteStatus(errorWrapper, opEnvEntry);
        }

        // Update Environments Map
        if (errorWrapper.isEmpty()) {
            environments.put(opEnvEntry.getEnvironmentId(), opEnvEntry);
        } else {
            saveEntryWithFailedStatus(errorWrapper, opEnvEntry);
        }
    }

    private void saveEntryWithFailedStatus(Wrapper<Boolean> errorWrapper, OperationalEnvironmentEntry opEnvEntry) {
        log.debug("handle message - save OperationalEnvironment Failed Status");
        opEnvEntry.setStatus(EnvironmentStatusEnum.FAILED);
        saveOpEnvEntry(errorWrapper, opEnvEntry);
    }

    void saveEntryWithCompleteStatus(Wrapper<Boolean> errorWrapper, OperationalEnvironmentEntry opEnvEntry) {
        log.debug("handle message - save OperationalEnvironment Complete Dao");
        opEnvEntry.setStatus(EnvironmentStatusEnum.COMPLETED);
        saveOpEnvEntry(errorWrapper, opEnvEntry);

    }

    void retrieveUebAddressesFromAftDme(Wrapper<Boolean> errorWrapper, OperationalEnvironmentEntry opEnvEntry) {
        log.debug("handle message - Get List Of UEB Addresses From AFT_DME");
        try {
            boolean isKeyFieldsValid = !isEmpty(opEnvEntry.getTenant()) && !isEmpty(opEnvEntry.getEcompWorkloadContext());
            if (isKeyFieldsValid) {
                String opEnvKey = map2OpEnvKey(opEnvEntry);
                String environmentId = opEnvEntry.getEnvironmentId();
                List<String> uebHosts = discoverUebHosts(opEnvKey, environmentId);
                opEnvEntry.setDmaapUebAddress(uebHosts.stream().collect(Collectors.toSet()));
            } else {
                errorWrapper.setInnerElement(false);
                log.debug("Can Not Build AFT DME Key from workLoad & Tenant Fields.");
            }

        } catch (DME2Exception e) {
            errorWrapper.setInnerElement(false);
            log.error("Failed to retrieve Ueb Addresses From DME. ", e);
        }
    }

    void createUebKeys(Wrapper<Boolean> errorWrapper, OperationalEnvironmentEntry opEnvEntry) {
        log.debug("handle message - Create UEB keys");
        List<String> discoverEndPoints = opEnvEntry.getDmaapUebAddress().stream()
                .collect(Collectors.toList());
        Either<ApiCredential, CambriaErrorResponse> eitherCreateUebKeys = cambriaHandler
                .createUebKeys(discoverEndPoints);
        if (eitherCreateUebKeys.isRight()) {
            errorWrapper.setInnerElement(false);
            log.debug("handle message - failed to create UEB Keys");
        } else {
            ApiCredential apiCredential = eitherCreateUebKeys.left().value();
            opEnvEntry.setUebApikey(apiCredential.getApiKey());
            opEnvEntry.setUebSecretKey(apiCredential.getApiSecret());
        }
    }

    void retrieveOpEnvInfoFromAAI(Wrapper<Boolean> errorWrapper, OperationalEnvironmentEntry opEnvEntry) {
        log.debug("handle message - Get Env Info From A&AI");
        Either<OperationalEnvInfo, Integer> eitherOperationalEnvInfo = getOperationalEnvById(
                opEnvEntry.getEnvironmentId());
        if (eitherOperationalEnvInfo.isRight()) {
            errorWrapper.setInnerElement(false);
            log.debug("handle message - failed to retrieve details from A&AI");
        } else {
            OperationalEnvInfo operationalEnvInfo = eitherOperationalEnvInfo.left().value();
            opEnvEntry.setEcompWorkloadContext(operationalEnvInfo.getWorkloadContext());
            opEnvEntry.setTenant(operationalEnvInfo.getTenantContext());
        }
    }

    void saveEntryWithInProgressStatus(Wrapper<Boolean> errorWrapper, Wrapper<OperationalEnvironmentEntry> opEnvEntryWrapper, IDmaapNotificationData notificationData) {
        log.debug("handle message - save OperationalEnvironment In-Progress Dao");
        OperationalEnvironmentEntry opEnvEntry = new OperationalEnvironmentEntry();
        // Entry Environment ID holds actually the environment NAME
        opEnvEntry.setEnvironmentId(notificationData.getOperationalEnvironmentId());
        opEnvEntry.setStatus(EnvironmentStatusEnum.IN_PROGRESS);
        opEnvEntry.setIsProduction(false);
        saveOpEnvEntry(errorWrapper, opEnvEntry);
        opEnvEntryWrapper.setInnerElement(opEnvEntry);

    }


    void validateState(Wrapper<Boolean> errorWrapper, IDmaapNotificationData notificationData) {
        log.debug("handle message - verify OperationalEnvironment not In-Progress");
        String opEnvId = notificationData.getOperationalEnvironmentId();

        Either<OperationalEnvironmentEntry, CassandraOperationStatus> eitherOpEnv = operationalEnvironmentDao
                .get(opEnvId);
        if (eitherOpEnv.isLeft()) {
            final OperationalEnvironmentEntry opEnvEntry = eitherOpEnv.left().value();
            if (StringUtils.equals(opEnvEntry.getStatus(), EnvironmentStatusEnum.IN_PROGRESS.getName())) {
                errorWrapper.setInnerElement(false);
                log.debug("handle message - validate State Failed Record Found With Status : {} Flow Stopped!", opEnvEntry.getStatus());
            }
        } else {
            CassandraOperationStatus operationStatus = eitherOpEnv.right().value();
            if (operationStatus != CassandraOperationStatus.NOT_FOUND) {
                errorWrapper.setInnerElement(false);
                log.debug("failed to retrieve operationa environment with id:{} cassandra error was :{}", opEnvId,
                        operationStatus);
            }
        }

    }

    void validateActionType(Wrapper<Boolean> errorWrapper, IDmaapNotificationData notificationData) {
        log.debug("handle message - verify Action Type");
        DmaapActionEnum action = notificationData.getAction();
        if (action == DmaapActionEnum.DELETE) {
            errorWrapper.setInnerElement(false);
            log.debug("handle message - validate Action Type Failed With Action Type: {} Flow Stopped!", action);
        }
    }

    void validateEnvironmentType(Wrapper<Boolean> errorWrapper, IDmaapNotificationData notificationData,
                                 IDmaapAuditNotificationData auditNotificationData) {
        log.debug("handle message - verify OperationaEnvironmentType");
        OperationaEnvironmentTypeEnum envType = notificationData.getOperationalEnvironmentType();
        if (envType != OperationaEnvironmentTypeEnum.ECOMP) {
            errorWrapper.setInnerElement(false);
            log.debug("handle message - validate Environment Type Failed With Environment Type: {} Flow Stopped!", envType);
            componentUtils.auditEnvironmentEngine(AuditingActionEnum.UNSUPPORTED_ENVIRONMENT_TYPE,
                    notificationData.getOperationalEnvironmentId(), notificationData.getOperationalEnvironmentType().getEventTypenName(),
                    notificationData.getAction().getActionName(), auditNotificationData.getOperationalEnvironmentName(),
                    auditNotificationData.getTenantContext());
        }
    }


    private void saveOpEnvEntry(Wrapper<Boolean> errorWrapper, OperationalEnvironmentEntry entry) {
        entry.setLastModified(new Date(System.currentTimeMillis()));
        CassandraOperationStatus saveStaus = operationalEnvironmentDao.save(entry);
        if (saveStaus != CassandraOperationStatus.OK) {
            errorWrapper.setInnerElement(false);
            log.debug("handle message saving  operational environmet failed for id :{} with error : {}",
                    entry.getEnvironmentId(), saveStaus);
        }
    }

    public List<String> discoverUebHosts(String opEnvKey, String env) throws DME2Exception {
        DmeConfiguration dmeConfiguration = configurationManager.getConfiguration().getDmeConfiguration();
        List<String> uebHosts = new LinkedList<>();

        String lookupURI = String.format("http://%s/service=%s/version=1.0.0/envContext=%s/partner=*", dmeConfiguration.getDme2Search(), opEnvKey,
                env);
        DME2EndpointIterator iterator = epIterCreator.create(lookupURI);

        // Beginning iteration
        while (iterator.hasNext()) {
            DME2EndpointReference ref = iterator.next();
            DME2Endpoint dmeEndpoint = ref.getEndpoint();
            log.debug("DME returns EP with UEB host {}, UEB port: {}", dmeEndpoint.getHost(), dmeEndpoint.getPort());
            uebHosts.add(dmeEndpoint.getHost());
        }

        return uebHosts;
    }

    private String map2OpEnvKey(OperationalEnvironmentEntry entry) {
        return String.format("%s.%s.%s", entry.getTenant(), entry.getEcompWorkloadContext(), MESSAGE_BUS);
    }

    private Map<String, OperationalEnvironmentEntry> populateEnvironments() {
        Map<String, OperationalEnvironmentEntry> envs = getEnvironmentsFromDb();
        OperationalEnvironmentEntry confEntry = readEnvFromConfig();
        envs.put(confEntry.getEnvironmentId(), confEntry);
        return envs;
    }

    private OperationalEnvironmentEntry readEnvFromConfig() {
        OperationalEnvironmentEntry entry = new OperationalEnvironmentEntry();
        DistributionEngineConfiguration distributionEngineConfiguration = configurationManager
                .getDistributionEngineConfiguration();
        entry.setUebApikey(distributionEngineConfiguration.getUebPublicKey());
        entry.setUebSecretKey(distributionEngineConfiguration.getUebSecretKey());

        Set<String> puebEndpoints = new HashSet<>();
        puebEndpoints.addAll(distributionEngineConfiguration.getUebServers());
        entry.setDmaapUebAddress(puebEndpoints);

        String envName = distributionEngineConfiguration.getEnvironments().size() == 1
                ? distributionEngineConfiguration.getEnvironments().get(0) : UNKNOWN;
        entry.setEnvironmentId(envName);

        if (log.isDebugEnabled()) {
            log.debug("Enviroment read from configuration: {}", entry);
        }

        return entry;
    }

    private Map<String, OperationalEnvironmentEntry> getEnvironmentsFromDb() {
        Either<List<OperationalEnvironmentEntry>, CassandraOperationStatus> opEnvResult = operationalEnvironmentDao
                .getByEnvironmentsStatus(EnvironmentStatusEnum.COMPLETED);

        if (opEnvResult.isLeft()) {
            Map<String, OperationalEnvironmentEntry> resultMap = opEnvResult.left().value().stream()
                    .collect(Collectors.toMap(OperationalEnvironmentEntry::getEnvironmentId, Function.identity()));
            resultMap.forEach((key, value) -> log.debug("Enviroment loaded from DB: {}", value));
            return resultMap;
        } else {
            CassandraOperationStatus status = opEnvResult.right().value();
            log.debug("Failed to populate Operation Envirenments Map from Cassandra, DB status: {}", status);
            return new HashMap<>();
        }
    }

    void createUebTopicsForEnvironments() {
        environments.values().forEach(this::createUebTopicsForEnvironment);
    }

    public void createUebTopicsForEnvironment(OperationalEnvironmentEntry opEnvEntry) {
        String envId = opEnvEntry.getEnvironmentId();
        log.debug("Create Environment {} on UEB Topic.", envId);
        AtomicBoolean status = new AtomicBoolean(false);
        envNamePerStatus.put(envId, status);

        connectUebTopicTenantIsolation(opEnvEntry, status, envNamePerInitTask, envNamePerPollingTask);
    }

    @VisibleForTesting
    void setConfigurationManager(ConfigurationManager configurationManager) {
        this.configurationManager = configurationManager;
    }

    public Map<String, OperationalEnvironmentEntry> getEnvironments() {
        return environments;
    }


    public Either<OperationalEnvInfo, Integer> getOperationalEnvById(String id) {
        HttpResponse<String> resp = aaiRequestHandler.getOperationalEnvById(id);
        if (resp.getStatusCode() == HttpStatus.SC_OK) {
            try {
                OperationalEnvInfo operationalEnvInfo = OperationalEnvInfo.createFromJson(resp.getResponse());

                log.debug("Get \"{}\" operational environment. {}", id, operationalEnvInfo);
                return Either.left(operationalEnvInfo);
            } catch (Exception e) {
                log.debug("Json convert to OperationalEnvInfo failed with exception ", e);
                return Either.right(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            }
        } else {
            log.debug("Get \"{}\" operational environment failed with statusCode: {}, description: {}", id,
                    resp.getStatusCode(), resp.getDescription());
            return Either.right(resp.getStatusCode());
        }
    }

    public OperationalEnvironmentEntry getEnvironmentById(String envId) {
        return environments.get(envId);
    }

    public boolean isInMap(OperationalEnvironmentEntry env) {
        return isInMap(env.getEnvironmentId());
    }

    public boolean isInMap(String envId) {
        return environments.containsKey(envId);
    }

    public void addToMap(OperationalEnvironmentEntry opEnvEntry) {
        environments.put(opEnvEntry.getEnvironmentId(), opEnvEntry);

    }
}