Added javadoc and logger to ddi client

Signed-off-by: Jonathan Philip Knoblauch <JonathanPhilip.Knoblauch@bosch-si.com>
This commit is contained in:
Jonathan Philip Knoblauch
2016-04-26 14:57:39 +02:00
parent 99f0a4e882
commit 8addb05f09
5 changed files with 61 additions and 29 deletions

View File

@@ -18,8 +18,7 @@ import feign.Logger.Level;
import feign.jackson.JacksonEncoder; import feign.jackson.JacksonEncoder;
/** /**
* TODO * Default implementation of DDI client.
*
*/ */
public class DdiDefaultFeignClient { public class DdiDefaultFeignClient {
@@ -39,10 +38,20 @@ public class DdiDefaultFeignClient {
this.tenant = tenant; this.tenant = tenant;
} }
/**
* Get the feign builder.
*
* @return the feign builder
*/
public Builder getFeignBuilder() { public Builder getFeignBuilder() {
return feignBuilder; return feignBuilder;
} }
/**
* Get the rootController resource client.
*
* @return the rootController resource client
*/
public RootControllerResourceClient getRootControllerResourceClient() { public RootControllerResourceClient getRootControllerResourceClient() {
if (rootControllerResourceClient == null) { if (rootControllerResourceClient == null) {

View File

@@ -10,6 +10,7 @@ package org.eclipse.hawkbit.ddi.client;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.LocalTime; import java.time.LocalTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -31,6 +32,9 @@ import org.springframework.hateoas.Link;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
/**
* DDI example client based on defualt DDI feign client.
*/
public class DdiExampleClient implements Runnable { public class DdiExampleClient implements Runnable {
private static final Logger LOGGER = LoggerFactory.getLogger(DdiExampleClient.class); private static final Logger LOGGER = LoggerFactory.getLogger(DdiExampleClient.class);
@@ -38,17 +42,26 @@ 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 DdiDefaultFeignClient ddiDefaultFeignClient;
private long pollingIntervalInMillis;
private final PersistenceStrategy persistenceStrategy; private final PersistenceStrategy persistenceStrategy;
private STATUS clientStatus; private STATUS clientStatus;
/**
* Constructor for the DDI example client.
*
* @param baseUrl
* the base url of the hawkBit server
* @param controllerId
* the controller id that will be simulated
* @param tenant
* the tenant
* @param persistenceStrategy
* the persistence strategy for downloading artifacts
*/
public DdiExampleClient(final String baseUrl, final String controllerId, final String tenant, public DdiExampleClient(final String baseUrl, final String controllerId, final String tenant,
final long pollingIntervalInMillis, final PersistenceStrategy persistenceStrategy) { final PersistenceStrategy persistenceStrategy) {
this.controllerId = controllerId; this.controllerId = controllerId;
this.ddiDefaultFeignClient = new DdiDefaultFeignClient(baseUrl, tenant); this.ddiDefaultFeignClient = new DdiDefaultFeignClient(baseUrl, tenant);
this.actionIdOfLastInstalltion = null; this.actionIdOfLastInstalltion = null;
this.pollingIntervalInMillis = pollingIntervalInMillis;
this.persistenceStrategy = persistenceStrategy; this.persistenceStrategy = persistenceStrategy;
this.clientStatus = STATUS.DOWN; this.clientStatus = STATUS.DOWN;
} }
@@ -56,15 +69,13 @@ public class DdiExampleClient implements Runnable {
@Override @Override
public void run() { public void run() {
clientStatus = STATUS.UP; clientStatus = STATUS.UP;
ResponseEntity<DdiControllerBase> response = ddiDefaultFeignClient.getRootControllerResourceClient() ResponseEntity<DdiControllerBase> response;
.getControllerBase(controllerId);
final String pollingTime = response.getBody().getConfig().getPolling().getSleep();
final LocalTime localtime = LocalTime.parse(pollingTime);
pollingIntervalInMillis = localtime.toNanoOfDay();
while (clientStatus == STATUS.UP) { while (clientStatus == STATUS.UP) {
LOGGER.info(" Controller {} polling from hawkBit server", controllerId);
response = ddiDefaultFeignClient.getRootControllerResourceClient().getControllerBase(controllerId); response = ddiDefaultFeignClient.getRootControllerResourceClient().getControllerBase(controllerId);
// final DdiControllerBase controllerBase = response.getBody(); final String pollingTime = response.getBody().getConfig().getPolling().getSleep();
final LocalTime localtime = LocalTime.parse(pollingTime);
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) {
@@ -78,14 +89,15 @@ public class DdiExampleClient implements Runnable {
try { try {
Thread.sleep(pollingIntervalInMillis); Thread.sleep(pollingIntervalInMillis);
System.out.println("polling ...");
} catch (final InterruptedException e) { } catch (final InterruptedException e) {
LOGGER.error("Error during sleep"); LOGGER.error("Error during sleep");
} }
} }
} }
/**
* Stop the DDI example client
*/
public void stop() { public void stop() {
clientStatus = STATUS.DOWN; clientStatus = STATUS.DOWN;
} }
@@ -112,12 +124,12 @@ public class DdiExampleClient implements Runnable {
sendFeedBackMessage(actionId, ExecutionStatus.PROCEEDING, FinalResult.NONE, sendFeedBackMessage(actionId, ExecutionStatus.PROCEEDING, FinalResult.NONE,
"Starting download of artifact " + artifact); "Starting download of artifact " + artifact);
System.out.println("Starting download for artifact " + artifact); LOGGER.info("Starting download of artifact " + artifact);
final ResponseEntity<InputStream> responseDownloadArtifact = ddiDefaultFeignClient final ResponseEntity<InputStream> responseDownloadArtifact = ddiDefaultFeignClient
.getRootControllerResourceClient().downloadArtifact(controllerId, softwareModuleId, artifact); .getRootControllerResourceClient().downloadArtifact(controllerId, softwareModuleId, artifact);
final HttpStatus statsuCode = responseDownloadArtifact.getStatusCode(); final HttpStatus statsuCode = responseDownloadArtifact.getStatusCode();
System.out.println("Finished download with stataus " + statsuCode); LOGGER.info("Finished download with stataus {}", statsuCode);
try { try {
persistenceStrategy.handleInputStream(responseDownloadArtifact.getBody(), artifact); persistenceStrategy.handleInputStream(responseDownloadArtifact.getBody(), artifact);
@@ -138,12 +150,11 @@ public class DdiExampleClient implements Runnable {
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 = null; final String time = String.valueOf(LocalDateTime.now());
final DdiActionFeedback feedback = new DdiActionFeedback(actionId, time, ddiStatus); final DdiActionFeedback feedback = new DdiActionFeedback(actionId, time, ddiStatus);
final ResponseEntity<Void> response = ddiDefaultFeignClient.getRootControllerResourceClient() ddiDefaultFeignClient.getRootControllerResourceClient().postBasedeploymentActionFeedback(feedback, controllerId,
.postBasedeploymentActionFeedback(feedback, controllerId, actionId); actionId);
final HttpStatus statsuCode = response.getStatusCode(); LOGGER.info("Sent feedback message to HaktBit");
System.out.println("Message send with stataus " + statsuCode);
} }
private void simulateSuccessfulInstallation(final Long actionId) { private void simulateSuccessfulInstallation(final Long actionId) {
@@ -166,6 +177,9 @@ public class DdiExampleClient implements Runnable {
return segments[8].split(Pattern.quote("?")); return segments[8].split(Pattern.quote("?"));
} }
/**
* Enum for DDI running status.
*/
public enum STATUS { public enum STATUS {
UP, DOWN; UP, DOWN;

View File

@@ -8,16 +8,17 @@
*/ */
package org.eclipse.hawkbit.ddi.client.strategy; package org.eclipse.hawkbit.ddi.client.strategy;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
/** /**
* @author Jonathan Knoblauch * Implementation of {@link PersistenceStrategy} for not downloading any
* * artifacts.
*/ */
public class DoNotSaveArtifactsStrategy implements PersistenceStrategy { public class DoNotSaveArtifactsStrategy implements PersistenceStrategy {
@Override @Override
public void handleInputStream(final InputStream in, final String artifactName) { public void handleInputStream(final InputStream in, final String artifactName) throws IOException {
// do nothing // do nothing
} }

View File

@@ -12,11 +12,19 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
/** /**
* @author Jonathan Knoblauch * Interface of persistence strategy.
*
*/ */
public interface PersistenceStrategy { public interface PersistenceStrategy {
/**
* Method handling the artifact download.
*
* @param in
* the input stream
* @param artifactName
* the name of the artifact
* @throws IOException
*/
public void handleInputStream(InputStream in, String artifactName) throws IOException; public void handleInputStream(InputStream in, String artifactName) throws IOException;
} }

View File

@@ -18,8 +18,8 @@ import com.google.common.io.ByteStreams;
import com.google.common.io.Files; import com.google.common.io.Files;
/** /**
* @author Jonathan Knoblauch * Implementation of {@link PersistenceStrategy} for downloading artifacts to
* * the temp directory.
*/ */
public class SaveArtifactsStrategy implements PersistenceStrategy { public class SaveArtifactsStrategy implements PersistenceStrategy {