Extenden functions of DDI client

- polling time is taken form server
- client can be stopped my call from out side

Signed-off-by: Jonathan Philip Knoblauch <JonathanPhilip.Knoblauch@bosch-si.com>
This commit is contained in:
Jonathan Philip Knoblauch
2016-04-26 14:08:42 +02:00
parent 05d9536cae
commit 99f0a4e882
6 changed files with 43 additions and 80 deletions

View File

@@ -23,9 +23,7 @@ public class ApplicationJsonRequestHeaderInterceptor implements RequestIntercept
public void apply(final RequestTemplate template) { public void apply(final RequestTemplate template) {
template.header("Accept", MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_OCTET_STREAM_VALUE, template.header("Accept", MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_OCTET_STREAM_VALUE,
MediaType.TEXT_PLAIN_VALUE); MediaType.TEXT_PLAIN_VALUE);
// template.header("Accept", MediaType.APPLICATION_OCTET_STREAM_VALUE);
template.header("Content-Type", MediaType.APPLICATION_JSON_VALUE); template.header("Content-Type", MediaType.APPLICATION_JSON_VALUE);
} }
} }

View File

@@ -33,6 +33,7 @@ import feign.jackson.JacksonDecoder;
public class DdiDecoder implements Decoder { public class DdiDecoder implements Decoder {
ObjectMapper mapper; ObjectMapper mapper;
private final String octentTypeOctetStream = "[application/octet-stream]";
public DdiDecoder() { public DdiDecoder() {
mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
@@ -44,8 +45,7 @@ public class DdiDecoder implements Decoder {
final Map<String, Collection<String>> header = response.headers(); final Map<String, Collection<String>> header = response.headers();
final String contentType = String.valueOf(header.get("Content-Type")); final String contentType = String.valueOf(header.get("Content-Type"));
// TODO parameter verwenden if (contentType.equals(octentTypeOctetStream)) {
if (contentType.equals("[application/octet-stream]")) {
return ResponseEntity.ok(response.body().asInputStream()); return ResponseEntity.ok(response.body().asInputStream());
} }
final ResponseEntityDecoder responseEntityDecoder = new ResponseEntityDecoder(new JacksonDecoder(mapper)); final ResponseEntityDecoder responseEntityDecoder = new ResponseEntityDecoder(new JacksonDecoder(mapper));

View File

@@ -8,7 +8,9 @@
*/ */
package org.eclipse.hawkbit.ddi.client; package org.eclipse.hawkbit.ddi.client;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.time.LocalTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@@ -36,9 +38,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 DdiDefaultFeignClient ddiDefaultFeignClient;
private final long pollingIntervalInMillis; private long pollingIntervalInMillis;
private final PersistenceStrategy persistenceStrategy; private final PersistenceStrategy persistenceStrategy;
private STATUS clientStatus;
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 long pollingIntervalInMillis, final PersistenceStrategy persistenceStrategy) {
this.controllerId = controllerId; this.controllerId = controllerId;
@@ -46,27 +50,28 @@ public class DdiExampleClient implements Runnable {
this.actionIdOfLastInstalltion = null; this.actionIdOfLastInstalltion = null;
this.pollingIntervalInMillis = pollingIntervalInMillis; this.pollingIntervalInMillis = pollingIntervalInMillis;
this.persistenceStrategy = persistenceStrategy; this.persistenceStrategy = persistenceStrategy;
this.clientStatus = STATUS.DOWN;
} }
@Override @Override
public void run() { public void run() {
clientStatus = STATUS.UP;
ResponseEntity<DdiControllerBase> response = ddiDefaultFeignClient.getRootControllerResourceClient()
.getControllerBase(controllerId);
final String pollingTime = response.getBody().getConfig().getPolling().getSleep();
final LocalTime localtime = LocalTime.parse(pollingTime);
pollingIntervalInMillis = localtime.toNanoOfDay();
ResponseEntity<DdiControllerBase> response; while (clientStatus == STATUS.UP) {
while (!Thread.currentThread().isInterrupted()) {
response = ddiDefaultFeignClient.getRootControllerResourceClient().getControllerBase(controllerId); response = ddiDefaultFeignClient.getRootControllerResourceClient().getControllerBase(controllerId);
final DdiControllerBase controllerBase = response.getBody(); // final DdiControllerBase controllerBase = response.getBody();
final Link controllerDeploymentBaseLink = controllerBase.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) {
startDownload(actionId, resource); startDownload(actionId, resource);
simulateSuccessfulInstallation(actionId);
actionIdOfLastInstalltion = actionId; actionIdOfLastInstalltion = actionId;
} }
} }
@@ -81,9 +86,12 @@ public class DdiExampleClient implements Runnable {
} }
public void stop() {
clientStatus = STATUS.DOWN;
}
private void startDownload(final Long actionId, final Integer resource) { private void startDownload(final Long actionId, final Integer resource) {
// resource has not been downloaded and installed
final ResponseEntity<DdiDeploymentBase> respone = ddiDefaultFeignClient.getRootControllerResourceClient() final ResponseEntity<DdiDeploymentBase> respone = ddiDefaultFeignClient.getRootControllerResourceClient()
.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();
@@ -94,7 +102,6 @@ public class DdiExampleClient implements Runnable {
.getLink("download-http"); .getLink("download-http");
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]);
// download all artifacts
for (final DdiArtifact ddiArtifact : artifactList) { for (final DdiArtifact ddiArtifact : artifactList) {
downloadArtifact(actionId, softwareModuleId, ddiArtifact.getFilename()); downloadArtifact(actionId, softwareModuleId, ddiArtifact.getFilename());
} }
@@ -111,9 +118,17 @@ public class DdiExampleClient implements Runnable {
.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); System.out.println("Finished download with stataus " + statsuCode);
persistenceStrategy.handleInputStream(responseDownloadArtifact.getBody(), artifact);
try {
persistenceStrategy.handleInputStream(responseDownloadArtifact.getBody(), artifact);
} catch (final IOException e) {
sendFeedBackMessage(actionId, ExecutionStatus.CLOSED, FinalResult.FAILURE,
"Downloaded of artifact " + artifact + " failed");
return;
}
sendFeedBackMessage(actionId, ExecutionStatus.PROCEEDING, FinalResult.NONE, "Downloaded artifact " + artifact); 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,
@@ -127,14 +142,12 @@ public class DdiExampleClient implements Runnable {
final DdiActionFeedback feedback = new DdiActionFeedback(actionId, time, ddiStatus); final DdiActionFeedback feedback = new DdiActionFeedback(actionId, time, ddiStatus);
final ResponseEntity<Void> response = ddiDefaultFeignClient.getRootControllerResourceClient() final ResponseEntity<Void> response = ddiDefaultFeignClient.getRootControllerResourceClient()
.postBasedeploymentActionFeedback(feedback, controllerId, actionId); .postBasedeploymentActionFeedback(feedback, controllerId, actionId);
final HttpStatus statsuCode = response.getStatusCode(); final HttpStatus statsuCode = response.getStatusCode();
System.out.println("Message send with stataus " + statsuCode); System.out.println("Message send with stataus " + statsuCode);
} }
private void simulateSuccessfulInstallation(final Long actionId) { private void simulateSuccessfulInstallation(final Long actionId) {
sendFeedBackMessage(actionId, ExecutionStatus.PROCEEDING, FinalResult.SUCESS, sendFeedBackMessage(actionId, ExecutionStatus.CLOSED, FinalResult.SUCESS, "Simulated installation successful");
"Simulated installation successful");
} }
private Long getActionIdOutOfLink(final Link controllerDeploymentBaseLink) { private Long getActionIdOutOfLink(final Link controllerDeploymentBaseLink) {
@@ -153,39 +166,8 @@ public class DdiExampleClient implements Runnable {
return segments[8].split(Pattern.quote("?")); return segments[8].split(Pattern.quote("?"));
} }
// private RootControllerResourceClient getDownloadFeignClient() { public enum STATUS {
//
// final Builder feignBuilder = Feign.builder().contract(new
// IgnoreMultipleConsumersProducersSpringMvcContract())
// .requestInterceptor(new
// ApplicationJsonRequestHeaderInterceptor()).logLevel(Level.FULL)
// .logger(new Logger.ErrorLogger()).encoder(new
// JacksonEncoder()).decoder(new Decoder() {
// @Override
// public Object decode(final Response response, final Type type)
// throws IOException, DecodeException, FeignException {
//
// // TODO download
// final InputStream stream = response.body().asInputStream();
//
// final FileSystem local = FileSystems.getDefault();
//
// System.out.println("Status is " + response.status());
//
// final ResponseEntity<Void> test = new ResponseEntity<Void>(
// HttpStatus.valueOf(response.status()));
//
// return test;
// }
// });
//
// final RootControllerResourceClient rootControllerResourceClient =
// feignBuilder
// .target(RootControllerResourceClient.class,
// "http://localhost:8080/DEFAULT/controller/v1");
//
// return rootControllerResourceClient;
//
// }
UP, DOWN;
}
} }

View File

@@ -16,14 +16,9 @@ import java.io.InputStream;
*/ */
public class DoNotSaveArtifactsStrategy implements PersistenceStrategy { public class DoNotSaveArtifactsStrategy implements PersistenceStrategy {
@Override
public String getPersistenceStrategy() {
return "nosave";
}
@Override @Override
public void handleInputStream(final InputStream in, final String artifactName) { public void handleInputStream(final InputStream in, final String artifactName) {
// down but do not save // do nothing
} }
} }

View File

@@ -8,6 +8,7 @@
*/ */
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;
/** /**
@@ -16,8 +17,6 @@ import java.io.InputStream;
*/ */
public interface PersistenceStrategy { public interface PersistenceStrategy {
public String getPersistenceStrategy(); public void handleInputStream(InputStream in, String artifactName) throws IOException;
public void handleInputStream(InputStream in, String artifactName);
} }

View File

@@ -15,6 +15,7 @@ import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
/** /**
* @author Jonathan Knoblauch * @author Jonathan Knoblauch
@@ -23,23 +24,11 @@ import com.google.common.io.ByteStreams;
public class SaveArtifactsStrategy implements PersistenceStrategy { public class SaveArtifactsStrategy implements PersistenceStrategy {
@Override @Override
public String getPersistenceStrategy() { public void handleInputStream(final InputStream in, final String artifactName) throws IOException {
final File tempDir = Files.createTempDir();
return "save"; final File file = new File(tempDir + "\\" + artifactName);
} final OutputStream out = new FileOutputStream(file);
ByteStreams.copy(in, out);
@Override
public void handleInputStream(final InputStream in, final String artifactName) {
final File file = new File("C:\\testdownload\\" + artifactName);
try {
final OutputStream out = new FileOutputStream(file);
ByteStreams.copy(in, out);
} catch (final IOException e) {
e.printStackTrace();
// TODO throw
}
} }
} }