Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
new version 0.20.0: Updated REST-SDK version 1.1.0, updated MQTT-SDK …
Browse files Browse the repository at this point in the history
…version 1.0.2, updated jackson dependencies 2.9.8, optimized operation handling
  • Loading branch information
sagflecke committed Jan 11, 2019
1 parent 7001b05 commit cd11431
Show file tree
Hide file tree
Showing 43 changed files with 2,793 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.telekom.cot.device.agent.common.exc;

public class DeviceServiceException extends AbstractAgentException {

/**
* serialVersionUID
*/
private static final long serialVersionUID = 1L;

public DeviceServiceException(String message, Throwable throwable, boolean enableSuppression,
boolean writableStackTrace) {
super(message, throwable, enableSuppression, writableStackTrace);
}

public DeviceServiceException(String message, Throwable throwable) {
super(message, throwable);
}

public DeviceServiceException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.telekom.cot.device.agent.common.exc;

public class MeasurementServiceException extends AbstractAgentException {

/**
* serialVersionUID
*/
private static final long serialVersionUID = 1L;

public MeasurementServiceException(String message, Throwable throwable, boolean enableSuppression,
boolean writableStackTrace) {
super(message, throwable, enableSuppression, writableStackTrace);
}

public MeasurementServiceException(String message, Throwable throwable) {
super(message, throwable);
}

public MeasurementServiceException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.telekom.cot.device.agent.common.exc;

public class OperationHandlerServiceException extends AbstractAgentException {

/**
* serialVersionUID
*/
private static final long serialVersionUID = 7752808930953738383L;

public OperationHandlerServiceException(String message) {
super(message);
}

public OperationHandlerServiceException(String message, Throwable throwable) {
super(message, throwable);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.telekom.cot.device.agent.common.exc;

import org.junit.Assert;
import org.junit.Test;

public class MeasurementServiceExceptionTest {

@Test
public void test() {
MeasurementServiceException measurementServiceException = new MeasurementServiceException("testMessage");
Assert.assertEquals("testMessage", measurementServiceException.getMessage());

measurementServiceException = new MeasurementServiceException("testMessage", new Throwable());
Assert.assertEquals("testMessage", measurementServiceException.getMessage());

measurementServiceException = new MeasurementServiceException("testMessage", new Throwable(), true, true);
Assert.assertEquals("testMessage", measurementServiceException.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.telekom.cot.device.agent.common.exc;

import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;

public class OperationHandlerServiceExceptionTest {

@Test
public void test(){
OperationHandlerServiceException exception = new OperationHandlerServiceException("message");
Assert.assertThat(exception.getMessage(), Matchers.equalTo("message"));

exception = new OperationHandlerServiceException("message", new Exception());
Assert.assertThat(exception.getMessage(), Matchers.equalTo("message"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.telekom.cot.device.agent.operation.handler;

import static com.telekom.cot.device.agent.common.util.AssertionUtil.*;

import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.telekom.cot.device.agent.common.configuration.ConfigurationManager;
import com.telekom.cot.device.agent.common.exc.AbstractAgentException;
import com.telekom.cot.device.agent.common.exc.OperationHandlerServiceException;
import com.telekom.cot.device.agent.common.injection.Inject;
import com.telekom.cot.device.agent.inventory.InventoryService;
import com.telekom.cot.device.agent.operation.operations.ConfigurationUpdateOperation;
import com.telekom.cot.device.agent.platform.objects.operation.Operation.OperationStatus;
import com.telekom.cot.device.agent.service.AbstractAgentService;
import com.telekom.cot.device.agent.system.SystemService;
import com.telekom.cot.device.agent.system.properties.ConfigurationProperties;


public class ConfigurationUpdateOperationHandler extends AbstractAgentService implements OperationHandlerService<ConfigurationUpdateOperation> {

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

@Inject
private ConfigurationManager configurationManager;

@Inject
private SystemService systemService;

@Inject
private InventoryService inventoryService;

@Override
public Class<ConfigurationUpdateOperation> getSupportedOperationType() {
return ConfigurationUpdateOperation.class;
}

@Override
public OperationStatus execute(ConfigurationUpdateOperation operation) throws AbstractAgentException {
assertNotNull(operation, OperationHandlerServiceException.class, LOGGER, "no ConfigurationUpdateOperation given to execute");
LOGGER.debug("execute ConfigurationUpdateOperation {}", operation.getProperties());

String configuration = operation.getConfiguration();
assertNotEmpty(configuration, OperationHandlerServiceException.class, LOGGER, "config value expected (is null or empty)");

LOGGER.info("update agent configuration {}", configuration);
configurationManager.updateConfiguration(configuration);

// update the managedObjects c8y_Configuration Fragment
ConfigurationProperties configurationProperties = systemService.getProperties(ConfigurationProperties.class);
if (Objects.nonNull(configurationProperties)) {
configurationProperties.setConfig(configuration);
}

inventoryService.updateDevice();
return OperationStatus.SUCCESSFUL;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.telekom.cot.device.agent.operation.handler;

import com.telekom.cot.device.agent.common.exc.AbstractAgentException;
import com.telekom.cot.device.agent.platform.objects.operation.Operation;
import com.telekom.cot.device.agent.platform.objects.operation.Operation.OperationStatus;
import com.telekom.cot.device.agent.service.AgentService;


public interface OperationHandlerService<T extends Operation> extends AgentService {

/**
* get the type of the operation this handler supports
* @return type of supported operation
*/
public Class<T> getSupportedOperationType();

/**
* executes the given operation and returns the execution status
* @param operation the operation to execute
* @return the status of operation execution
* @throws AbstractAgentException
*/
public OperationStatus execute(T operation) throws AbstractAgentException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.telekom.cot.device.agent.operation.handler;

import javax.validation.constraints.NotNull;

import com.telekom.cot.device.agent.common.ChecksumAlgorithm;
import com.telekom.cot.device.agent.common.annotations.ConfigurationPath;
import com.telekom.cot.device.agent.common.configuration.Configuration;

@ConfigurationPath("agent.operations.softwareUpdate")
public class SoftwareUpdateConfiguration implements Configuration {

@NotNull
private ChecksumAlgorithm checksumAlgorithm;

public ChecksumAlgorithm getChecksumAlgorithm() {
return checksumAlgorithm;
}

public void setChecksumAlgorithm(ChecksumAlgorithm checksumAlgorithm) {
this.checksumAlgorithm = checksumAlgorithm;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.telekom.cot.device.agent.operation.handler;

import static com.telekom.cot.device.agent.common.util.AssertionUtil.*;

import java.net.URL;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.telekom.cot.device.agent.common.exc.AbstractAgentException;
import com.telekom.cot.device.agent.common.exc.OperationHandlerServiceException;
import com.telekom.cot.device.agent.common.injection.Inject;
import com.telekom.cot.device.agent.operation.operations.SoftwareUpdateOperation;
import com.telekom.cot.device.agent.operation.softwareupdate.SoftwareInstaller;
import com.telekom.cot.device.agent.operation.softwareupdate.SoftwareInstallerFactory;
import com.telekom.cot.device.agent.platform.PlatformService;
import com.telekom.cot.device.agent.platform.objects.operation.Operation.OperationStatus;
import com.telekom.cot.device.agent.service.AbstractAgentService;
import com.telekom.cot.device.agent.system.properties.Software;


public class SoftwareUpdateOperationHandler extends AbstractAgentService implements OperationHandlerService<SoftwareUpdateOperation> {

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

private SoftwareInstaller softwareInstaller;

@Inject
private PlatformService platformService;

@Inject
private SoftwareUpdateConfiguration configuration;

@Override
public void start() throws AbstractAgentException {
softwareInstaller = SoftwareInstallerFactory.getInstance();
super.start();
}

@Override
public Class<SoftwareUpdateOperation> getSupportedOperationType() {
return SoftwareUpdateOperation.class;
}

@Override
public OperationStatus execute(SoftwareUpdateOperation operation) throws AbstractAgentException {
assertNotNull(operation, OperationHandlerServiceException.class, LOGGER, "no software update operation given");
LOGGER.info("execute software update operation {}", operation.getProperties());

URL url = getSoftwareDownloadURL(operation);
LOGGER.info("download agent software by URL {}", url);
softwareInstaller.downloadSoftware(platformService, url, configuration.getChecksumAlgorithm());

LOGGER.info("install downloaded agent software");
softwareInstaller.installSoftware();
return OperationStatus.EXECUTING;
}

private URL getSoftwareDownloadURL(SoftwareUpdateOperation operation) throws AbstractAgentException {
Software software = operation.getSoftware();
assertNotNull(software, OperationHandlerServiceException.class, LOGGER, "no software information found @ software update operation");
assertNotEmpty(software.getVersion(), OperationHandlerServiceException.class, LOGGER, "software version is missed");

try {
return new URL(software.getUrl());
} catch (Exception e) {
throw createExceptionAndLog(OperationHandlerServiceException.class, LOGGER, "can't get download url from software update operation", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.telekom.cot.device.agent.operation.handler;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;

import com.telekom.cot.device.agent.common.annotations.ConfigurationPath;
import com.telekom.cot.device.agent.common.configuration.Configuration;

@ConfigurationPath("agent.operations.testOperation")
public class TestOperationConfiguration implements Configuration {

@NotNull @Positive
private int delay;

public int getDelay() {
return delay;
}

public void setDelay(int delay) {
this.delay = delay;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.telekom.cot.device.agent.operation.handler;

import static com.telekom.cot.device.agent.common.util.AssertionUtil.*;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.telekom.cot.device.agent.common.exc.AbstractAgentException;
import com.telekom.cot.device.agent.common.exc.OperationHandlerServiceException;
import com.telekom.cot.device.agent.common.injection.Inject;
import com.telekom.cot.device.agent.operation.operations.TestOperation;
import com.telekom.cot.device.agent.operation.operations.TestOperation.GivenStatus;
import com.telekom.cot.device.agent.platform.objects.operation.Operation.OperationStatus;
import com.telekom.cot.device.agent.service.AbstractAgentService;

public class TestOperationHandler extends AbstractAgentService implements OperationHandlerService<TestOperation> {

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

@Inject
TestOperationConfiguration configuration;

@Override
public Class<TestOperation> getSupportedOperationType() {
return TestOperation.class;
}

@Override
public OperationStatus execute(TestOperation operation) throws AbstractAgentException {
assertNotNull(operation, OperationHandlerServiceException.class, LOGGER, "no TestOperation given to execute");
LOGGER.debug("execute TestOperation {}", operation.getProperties());

// delay execution
try {
TimeUnit.SECONDS.sleep(configuration.getDelay());
} catch (Exception e) {
throw createExceptionAndLog(OperationHandlerServiceException.class, LOGGER, "can't delay the execution", e);
}

// get and check given status
GivenStatus givenStatus = operation.getGivenStatus();
if(Objects.isNull(givenStatus)) {
throw createExceptionAndLog(OperationHandlerServiceException.class, LOGGER, "can't get given status from operation");
}

// perform execution with given status
switch (givenStatus) {
case GIVEN_SUCCESSFUL:
LOGGER.info("executed TestOperation with given status 'SUCCESSFUL'");
return OperationStatus.SUCCESSFUL;

case GIVEN_FAILED_BY_STATUS:
LOGGER.info("executed TestOperation with given status 'FAILED'");
return OperationStatus.FAILED;

case GIVEN_FAILED_BY_EXCEPTION:
LOGGER.info("executed TestOperation with given status 'FAILED_BY_EXCEPTION'");
throw new OperationHandlerServiceException("execution is failed by exception");

default:
LOGGER.info("can't execute TestOperation, unknown given status");
return OperationStatus.FAILED;
}
}
}
Loading

0 comments on commit cd11431

Please sign in to comment.