Skip to content

Commit

Permalink
add PolicyCommandFactory
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Maute <stefan.maute@bosch-si.com>
  • Loading branch information
Stefan Maute committed Nov 28, 2018
1 parent 9f31e69 commit f4294bf
Showing 1 changed file with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2017-2018 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.concierge.cache;

import java.util.UUID;

import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.services.models.policies.commands.sudo.SudoRetrievePolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

/**
* Creates commands to access the Policies service.
*/
final class PolicyCommandFactory {

private static final Logger LOGGER = LoggerFactory.getLogger(PolicyCommandFactory.class);

private PolicyCommandFactory() {
throw new AssertionError();
}

/**
* Creates a sudo command for retrieving a policy.
*
* @param policyId the policyId.
* @return the created command.
*/
static SudoRetrievePolicy sudoRetrievePolicy(final String policyId) {
return SudoRetrievePolicy.of(policyId,
DittoHeaders.newBuilder().correlationId(getCorrelationId(policyId)).build());
}

private static String getCorrelationId(final String policyId) {
String correlationId = MDC.get("x-correlation-id");
if (null == correlationId) {
correlationId = UUID.randomUUID().toString();
LOGGER.debug("Found no correlation-id for SudoRetrievePolicy on Policy <{}>. Using new correlation-id: {}",
policyId, correlationId);
return correlationId;
} else {
LOGGER.debug("Found correlation-id [{}] in MDC for SudoRetrievePolicy on Policy <{}>. ", correlationId, policyId);
return correlationId;
}
}

}

0 comments on commit f4294bf

Please sign in to comment.