Changed feign client calls to keep client up when download failed
Signed-off-by: Jonathan Philip Knoblauch <JonathanPhilip.Knoblauch@bosch-si.com>
This commit is contained in:
@@ -16,6 +16,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import org.eclipse.hawkbit.ddi.client.resource.RootControllerResourceClient;
|
||||||
import org.eclipse.hawkbit.ddi.client.strategy.PersistenceStrategy;
|
import org.eclipse.hawkbit.ddi.client.strategy.PersistenceStrategy;
|
||||||
import org.eclipse.hawkbit.ddi.json.model.DdiActionFeedback;
|
import org.eclipse.hawkbit.ddi.json.model.DdiActionFeedback;
|
||||||
import org.eclipse.hawkbit.ddi.json.model.DdiArtifact;
|
import org.eclipse.hawkbit.ddi.json.model.DdiArtifact;
|
||||||
@@ -41,9 +42,11 @@ public class DdiExampleClient implements Runnable {
|
|||||||
|
|
||||||
private final String controllerId;
|
private final String controllerId;
|
||||||
private Long actionIdOfLastInstalltion;
|
private Long actionIdOfLastInstalltion;
|
||||||
private final DdiDefaultFeignClient ddiDefaultFeignClient;
|
private final RootControllerResourceClient rootControllerResourceClient;
|
||||||
private final PersistenceStrategy persistenceStrategy;
|
private final PersistenceStrategy persistenceStrategy;
|
||||||
private STATUS clientStatus;
|
private DdiClientStatus clientStatus;
|
||||||
|
|
||||||
|
private FinalResult finalReusltOfCurrentUpdate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for the DDI example client.
|
* Constructor for the DDI example client.
|
||||||
@@ -60,33 +63,34 @@ public class DdiExampleClient implements Runnable {
|
|||||||
public DdiExampleClient(final String baseUrl, final String controllerId, final String tenant,
|
public DdiExampleClient(final String baseUrl, final String controllerId, final String tenant,
|
||||||
final PersistenceStrategy persistenceStrategy) {
|
final PersistenceStrategy persistenceStrategy) {
|
||||||
this.controllerId = controllerId;
|
this.controllerId = controllerId;
|
||||||
this.ddiDefaultFeignClient = new DdiDefaultFeignClient(baseUrl, tenant);
|
this.rootControllerResourceClient = new DdiDefaultFeignClient(baseUrl, tenant)
|
||||||
|
.getRootControllerResourceClient();
|
||||||
this.actionIdOfLastInstalltion = null;
|
this.actionIdOfLastInstalltion = null;
|
||||||
this.persistenceStrategy = persistenceStrategy;
|
this.persistenceStrategy = persistenceStrategy;
|
||||||
this.clientStatus = STATUS.DOWN;
|
this.clientStatus = DdiClientStatus.DOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
clientStatus = STATUS.UP;
|
clientStatus = DdiClientStatus.UP;
|
||||||
ResponseEntity<DdiControllerBase> response;
|
ResponseEntity<DdiControllerBase> response;
|
||||||
while (clientStatus == STATUS.UP) {
|
while (clientStatus == DdiClientStatus.UP) {
|
||||||
LOGGER.info(" Controller {} polling from hawkBit server", controllerId);
|
LOGGER.info(" Controller {} polling from hawkBit server", controllerId);
|
||||||
response = ddiDefaultFeignClient.getRootControllerResourceClient().getControllerBase(controllerId);
|
response = rootControllerResourceClient.getControllerBase(controllerId);
|
||||||
final String pollingTime = response.getBody().getConfig().getPolling().getSleep();
|
final String pollingTimeFormReponse = response.getBody().getConfig().getPolling().getSleep();
|
||||||
final LocalTime localtime = LocalTime.parse(pollingTime);
|
final LocalTime localtime = LocalTime.parse(pollingTimeFormReponse);
|
||||||
final long pollingIntervalInMillis = localtime.toNanoOfDay();
|
final long pollingIntervalInMillis = localtime.toNanoOfDay();
|
||||||
final Link controllerDeploymentBaseLink = response.getBody().getLink("deploymentBase");
|
final Link controllerDeploymentBaseLink = response.getBody().getLink("deploymentBase");
|
||||||
|
|
||||||
if (controllerDeploymentBaseLink != null) {
|
if (controllerDeploymentBaseLink != null) {
|
||||||
final Long actionId = getActionIdOutOfLink(controllerDeploymentBaseLink);
|
final Long actionId = getActionIdOutOfLink(controllerDeploymentBaseLink);
|
||||||
final Integer resource = getResourceOutOfLink(controllerDeploymentBaseLink);
|
final Integer resource = getResourceOutOfLink(controllerDeploymentBaseLink);
|
||||||
if (actionId != actionIdOfLastInstalltion) {
|
if (actionId != actionIdOfLastInstalltion) {
|
||||||
|
finalReusltOfCurrentUpdate = FinalResult.NONE;
|
||||||
startDownload(actionId, resource);
|
startDownload(actionId, resource);
|
||||||
|
finishUpdateProcess(actionId);
|
||||||
actionIdOfLastInstalltion = actionId;
|
actionIdOfLastInstalltion = actionId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(pollingIntervalInMillis);
|
Thread.sleep(pollingIntervalInMillis);
|
||||||
} catch (final InterruptedException e) {
|
} catch (final InterruptedException e) {
|
||||||
@@ -99,12 +103,12 @@ public class DdiExampleClient implements Runnable {
|
|||||||
* Stop the DDI example client
|
* Stop the DDI example client
|
||||||
*/
|
*/
|
||||||
public void stop() {
|
public void stop() {
|
||||||
clientStatus = STATUS.DOWN;
|
clientStatus = DdiClientStatus.DOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startDownload(final Long actionId, final Integer resource) {
|
private void startDownload(final Long actionId, final Integer resource) {
|
||||||
|
|
||||||
final ResponseEntity<DdiDeploymentBase> respone = ddiDefaultFeignClient.getRootControllerResourceClient()
|
final ResponseEntity<DdiDeploymentBase> respone = rootControllerResourceClient
|
||||||
.getControllerBasedeploymentAction(controllerId, Long.valueOf(actionId), Integer.valueOf(resource));
|
.getControllerBasedeploymentAction(controllerId, Long.valueOf(actionId), Integer.valueOf(resource));
|
||||||
final DdiDeploymentBase ddiDeploymentBase = respone.getBody();
|
final DdiDeploymentBase ddiDeploymentBase = respone.getBody();
|
||||||
final List<DdiChunk> chunks = ddiDeploymentBase.getDeployment().getChunks();
|
final List<DdiChunk> chunks = ddiDeploymentBase.getDeployment().getChunks();
|
||||||
@@ -115,7 +119,9 @@ public class DdiExampleClient implements Runnable {
|
|||||||
final String[] downloadLinkSep = downloadLink.getHref().split(Pattern.quote("/"));
|
final String[] downloadLinkSep = downloadLink.getHref().split(Pattern.quote("/"));
|
||||||
final Long softwareModuleId = Long.valueOf(downloadLinkSep[8]);
|
final Long softwareModuleId = Long.valueOf(downloadLinkSep[8]);
|
||||||
for (final DdiArtifact ddiArtifact : artifactList) {
|
for (final DdiArtifact ddiArtifact : artifactList) {
|
||||||
downloadArtifact(actionId, softwareModuleId, ddiArtifact.getFilename());
|
if (finalReusltOfCurrentUpdate != FinalResult.FAILURE) {
|
||||||
|
downloadArtifact(actionId, softwareModuleId, ddiArtifact.getFilename());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -126,39 +132,46 @@ public class DdiExampleClient implements Runnable {
|
|||||||
"Starting download of artifact " + artifact);
|
"Starting download of artifact " + artifact);
|
||||||
LOGGER.info("Starting download of artifact " + artifact);
|
LOGGER.info("Starting download of artifact " + artifact);
|
||||||
|
|
||||||
final ResponseEntity<InputStream> responseDownloadArtifact = ddiDefaultFeignClient
|
final ResponseEntity<InputStream> responseDownloadArtifact = rootControllerResourceClient
|
||||||
.getRootControllerResourceClient().downloadArtifact(controllerId, softwareModuleId, artifact);
|
.downloadArtifact(controllerId, softwareModuleId, artifact);
|
||||||
final HttpStatus statsuCode = responseDownloadArtifact.getStatusCode();
|
final HttpStatus statsuCode = responseDownloadArtifact.getStatusCode();
|
||||||
LOGGER.info("Finished download with stataus {}", statsuCode);
|
LOGGER.info("Finished download with stataus {}", statsuCode);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
persistenceStrategy.handleInputStream(responseDownloadArtifact.getBody(), artifact);
|
persistenceStrategy.handleInputStream(responseDownloadArtifact.getBody(), artifact);
|
||||||
|
sendFeedBackMessage(actionId, ExecutionStatus.PROCEEDING, FinalResult.NONE,
|
||||||
|
"Downloaded artifact " + artifact);
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
sendFeedBackMessage(actionId, ExecutionStatus.CLOSED, FinalResult.FAILURE,
|
sendFeedBackMessage(actionId, ExecutionStatus.PROCEEDING, FinalResult.NONE,
|
||||||
"Downloaded of artifact " + artifact + " failed");
|
"Downloaded of artifact " + artifact + "failed");
|
||||||
return;
|
finalReusltOfCurrentUpdate = FinalResult.FAILURE;
|
||||||
}
|
}
|
||||||
sendFeedBackMessage(actionId, ExecutionStatus.PROCEEDING, FinalResult.NONE, "Downloaded artifact " + artifact);
|
|
||||||
|
|
||||||
simulateSuccessfulInstallation(actionId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendFeedBackMessage(final Long actionId, final ExecutionStatus executionStatus,
|
private void sendFeedBackMessage(final Long actionId, final ExecutionStatus executionStatus,
|
||||||
final FinalResult finalResult, final String message) {
|
final FinalResult finalResult, final String message) {
|
||||||
|
|
||||||
final DdiResult result = new DdiResult(finalResult, null);
|
final DdiResult result = new DdiResult(finalResult, null);
|
||||||
final List<String> details = new ArrayList<>();
|
final List<String> details = new ArrayList<>();
|
||||||
details.add(message);
|
details.add(message);
|
||||||
final DdiStatus ddiStatus = new DdiStatus(executionStatus, result, details);
|
final DdiStatus ddiStatus = new DdiStatus(executionStatus, result, details);
|
||||||
final String time = String.valueOf(LocalDateTime.now());
|
final String time = String.valueOf(LocalDateTime.now());
|
||||||
final DdiActionFeedback feedback = new DdiActionFeedback(actionId, time, ddiStatus);
|
final DdiActionFeedback feedback = new DdiActionFeedback(actionId, time, ddiStatus);
|
||||||
ddiDefaultFeignClient.getRootControllerResourceClient().postBasedeploymentActionFeedback(feedback, controllerId,
|
rootControllerResourceClient.postBasedeploymentActionFeedback(feedback, controllerId, actionId);
|
||||||
actionId);
|
|
||||||
LOGGER.info("Sent feedback message to HaktBit");
|
LOGGER.info("Sent feedback message to HaktBit");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void simulateSuccessfulInstallation(final Long actionId) {
|
private void finishUpdateProcess(final Long actionId) {
|
||||||
sendFeedBackMessage(actionId, ExecutionStatus.CLOSED, FinalResult.SUCESS, "Simulated installation successful");
|
|
||||||
|
if (finalReusltOfCurrentUpdate == FinalResult.FAILURE) {
|
||||||
|
sendFeedBackMessage(actionId, ExecutionStatus.CLOSED, FinalResult.FAILURE, "Error during update process");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (finalReusltOfCurrentUpdate == FinalResult.NONE) {
|
||||||
|
sendFeedBackMessage(actionId, ExecutionStatus.CLOSED, FinalResult.SUCESS,
|
||||||
|
"Simulated installation successful");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Long getActionIdOutOfLink(final Link controllerDeploymentBaseLink) {
|
private Long getActionIdOutOfLink(final Link controllerDeploymentBaseLink) {
|
||||||
@@ -180,8 +193,8 @@ public class DdiExampleClient implements Runnable {
|
|||||||
/**
|
/**
|
||||||
* Enum for DDI running status.
|
* Enum for DDI running status.
|
||||||
*/
|
*/
|
||||||
public enum STATUS {
|
public enum DdiClientStatus {
|
||||||
|
|
||||||
UP, DOWN;
|
UP, DOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user