diff --git a/pom.xml b/pom.xml index 34565661301..b5e752db486 100644 --- a/pom.xml +++ b/pom.xml @@ -795,6 +795,11 @@ kapua-device-management-command-job ${project.version} + + org.eclipse.kapua + kapua-device-management-inventory-job + ${project.version} + org.eclipse.kapua kapua-device-management-configuration-api diff --git a/service/device/management/all/job/pom.xml b/service/device/management/all/job/pom.xml index 8d93265ac07..68dcadc77a7 100644 --- a/service/device/management/all/job/pom.xml +++ b/service/device/management/all/job/pom.xml @@ -49,6 +49,10 @@ org.eclipse.kapua kapua-device-management-packages-job + + org.eclipse.kapua + kapua-device-management-inventory-job + diff --git a/service/device/management/inventory/job/pom.xml b/service/device/management/inventory/job/pom.xml new file mode 100644 index 00000000000..cd075c33012 --- /dev/null +++ b/service/device/management/inventory/job/pom.xml @@ -0,0 +1,40 @@ + + + + 4.0.0 + + org.eclipse.kapua + kapua-device-management-inventory + 2.1.0-SNAPSHOT + + + kapua-device-management-inventory-job + + + org.eclipse.kapua + kapua-job-api + + + org.eclipse.kapua + kapua-device-management-inventory-api + + + org.eclipse.kapua + kapua-job-engine-commons + + + + \ No newline at end of file diff --git a/service/device/management/inventory/job/src/main/java/org/eclipse/kapua/service/device/management/inventory/job/DeviceContainerJobModule.java b/service/device/management/inventory/job/src/main/java/org/eclipse/kapua/service/device/management/inventory/job/DeviceContainerJobModule.java new file mode 100644 index 00000000000..3c68f1fa876 --- /dev/null +++ b/service/device/management/inventory/job/src/main/java/org/eclipse/kapua/service/device/management/inventory/job/DeviceContainerJobModule.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2024, 2022 Eurotech and/or its affiliates and others + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Eurotech - initial API and implementation + *******************************************************************************/ +package org.eclipse.kapua.service.device.management.inventory.job; + +import com.google.inject.multibindings.ProvidesIntoSet; +import org.eclipse.kapua.commons.core.AbstractKapuaModule; +import org.eclipse.kapua.service.device.management.inventory.job.definition.DeviceContainerStartJobStepDefinition; +import org.eclipse.kapua.service.device.management.inventory.job.definition.DeviceContainerStopJobStepDefinition; +import org.eclipse.kapua.service.job.step.definition.JobStepDefinition; + +public class DeviceContainerJobModule extends AbstractKapuaModule { + + @Override + protected void configureModule() { + } + + @ProvidesIntoSet + public JobStepDefinition deviceContainerStartJobStepDefinition() { + return new DeviceContainerStartJobStepDefinition(); + } + + @ProvidesIntoSet + public JobStepDefinition deviceContainerStopJobStepDefinition() { + return new DeviceContainerStopJobStepDefinition(); + } + +} \ No newline at end of file diff --git a/service/device/management/inventory/job/src/main/java/org/eclipse/kapua/service/device/management/inventory/job/DeviceContainerStartTargetProcessor.java b/service/device/management/inventory/job/src/main/java/org/eclipse/kapua/service/device/management/inventory/job/DeviceContainerStartTargetProcessor.java new file mode 100644 index 00000000000..66571938a84 --- /dev/null +++ b/service/device/management/inventory/job/src/main/java/org/eclipse/kapua/service/device/management/inventory/job/DeviceContainerStartTargetProcessor.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * Copyright (c) 2017, 2022 Eurotech and/or its affiliates and others + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Eurotech - initial API and implementation + *******************************************************************************/ +package org.eclipse.kapua.service.device.management.inventory.job; + +import org.eclipse.kapua.KapuaException; +import org.eclipse.kapua.commons.security.KapuaSecurityUtils; +import org.eclipse.kapua.job.engine.commons.operation.AbstractDeviceTargetProcessor; +import org.eclipse.kapua.job.engine.commons.wrappers.JobTargetWrapper; +import org.eclipse.kapua.service.device.management.inventory.DeviceInventoryManagementService; +import org.eclipse.kapua.service.device.management.inventory.job.definition.DeviceContainerPropertyKeys; +import org.eclipse.kapua.service.device.management.inventory.model.container.DeviceInventoryContainer; +import org.eclipse.kapua.service.device.management.inventory.model.container.DeviceInventoryContainerAction; +import org.eclipse.kapua.service.job.operation.TargetProcessor; +import org.eclipse.kapua.service.job.targets.JobTarget; + +import javax.batch.runtime.context.JobContext; +import javax.batch.runtime.context.StepContext; +import javax.inject.Inject; + +public class DeviceContainerStartTargetProcessor extends AbstractDeviceTargetProcessor implements TargetProcessor { + + @Inject + DeviceInventoryManagementService deviceInventoryManagementService; + @Inject + JobContext jobContext; + @Inject + StepContext stepContext; + + @Override + protected void initProcessing(JobTargetWrapper wrappedJobTarget) { + setContext(jobContext, stepContext); + } + + @Override + public void processTarget(JobTarget jobTarget) throws KapuaException { + + DeviceInventoryContainer containerInput = stepContextWrapper.getStepProperty(DeviceContainerPropertyKeys.CONTAINER_INPUT, DeviceInventoryContainer.class); + Long timeout = stepContextWrapper.getStepProperty(DeviceContainerPropertyKeys.TIMEOUT, Long.class); + + KapuaSecurityUtils.doPrivileged(() -> deviceInventoryManagementService.execContainer(jobTarget.getScopeId(), jobTarget.getJobTargetId(), containerInput, DeviceInventoryContainerAction.START, timeout)); + } +} + diff --git a/service/device/management/inventory/job/src/main/java/org/eclipse/kapua/service/device/management/inventory/job/DeviceContainerStopTargetProcessor.java b/service/device/management/inventory/job/src/main/java/org/eclipse/kapua/service/device/management/inventory/job/DeviceContainerStopTargetProcessor.java new file mode 100644 index 00000000000..2c282d1b84a --- /dev/null +++ b/service/device/management/inventory/job/src/main/java/org/eclipse/kapua/service/device/management/inventory/job/DeviceContainerStopTargetProcessor.java @@ -0,0 +1,54 @@ +/******************************************************************************* + * Copyright (c) 2017, 2022 Eurotech and/or its affiliates and others + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Eurotech - initial API and implementation + *******************************************************************************/ +package org.eclipse.kapua.service.device.management.inventory.job; + +import org.eclipse.kapua.KapuaException; +import org.eclipse.kapua.commons.security.KapuaSecurityUtils; +import org.eclipse.kapua.job.engine.commons.operation.AbstractDeviceTargetProcessor; +import org.eclipse.kapua.job.engine.commons.wrappers.JobTargetWrapper; +import org.eclipse.kapua.service.device.management.inventory.DeviceInventoryManagementService; +import org.eclipse.kapua.service.device.management.inventory.job.definition.DeviceContainerPropertyKeys; +import org.eclipse.kapua.service.device.management.inventory.model.container.DeviceInventoryContainer; +import org.eclipse.kapua.service.device.management.inventory.model.container.DeviceInventoryContainerAction; +import org.eclipse.kapua.service.job.operation.TargetProcessor; +import org.eclipse.kapua.service.job.targets.JobTarget; + +import javax.batch.runtime.context.JobContext; +import javax.batch.runtime.context.StepContext; +import javax.inject.Inject; + +public class DeviceContainerStopTargetProcessor extends AbstractDeviceTargetProcessor implements TargetProcessor { + + @Inject + DeviceInventoryManagementService deviceInventoryManagementService; + @Inject + JobContext jobContext; + @Inject + StepContext stepContext; + + @Override + protected void initProcessing(JobTargetWrapper wrappedJobTarget) { + setContext(jobContext, stepContext); + } + + @Override + public void processTarget(JobTarget jobTarget) throws KapuaException { + + DeviceInventoryContainer containerInput = stepContextWrapper.getStepProperty(DeviceContainerPropertyKeys.CONTAINER_INPUT, DeviceInventoryContainer.class); + Long timeout = stepContextWrapper.getStepProperty(DeviceContainerPropertyKeys.TIMEOUT, Long.class); + + KapuaSecurityUtils.doPrivileged(() -> deviceInventoryManagementService.execContainer(jobTarget.getScopeId(), jobTarget.getJobTargetId(), containerInput, DeviceInventoryContainerAction.STOP, timeout)); + } +} + + diff --git a/service/device/management/inventory/job/src/main/java/org/eclipse/kapua/service/device/management/inventory/job/definition/DeviceContainerPropertyKeys.java b/service/device/management/inventory/job/src/main/java/org/eclipse/kapua/service/device/management/inventory/job/definition/DeviceContainerPropertyKeys.java new file mode 100644 index 00000000000..ce9d43be8e9 --- /dev/null +++ b/service/device/management/inventory/job/src/main/java/org/eclipse/kapua/service/device/management/inventory/job/definition/DeviceContainerPropertyKeys.java @@ -0,0 +1,24 @@ +/******************************************************************************* + * Copyright (c) 2017, 2022 Eurotech and/or its affiliates and others + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Eurotech - initial API and implementation + *******************************************************************************/ +package org.eclipse.kapua.service.device.management.inventory.job.definition; + +import org.eclipse.kapua.service.job.step.definition.JobPropertyKey; + +public class DeviceContainerPropertyKeys implements JobPropertyKey { + + public static final String CONTAINER_INPUT = "containerInput"; + public static final String TIMEOUT = "timeout"; + + private DeviceContainerPropertyKeys() { + } +} diff --git a/service/device/management/inventory/job/src/main/java/org/eclipse/kapua/service/device/management/inventory/job/definition/DeviceContainerStartJobStepDefinition.java b/service/device/management/inventory/job/src/main/java/org/eclipse/kapua/service/device/management/inventory/job/definition/DeviceContainerStartJobStepDefinition.java new file mode 100644 index 00000000000..b92d57f620a --- /dev/null +++ b/service/device/management/inventory/job/src/main/java/org/eclipse/kapua/service/device/management/inventory/job/definition/DeviceContainerStartJobStepDefinition.java @@ -0,0 +1,56 @@ +/******************************************************************************* + * Copyright (c) 2017, 2022 Eurotech and/or its affiliates and others + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Eurotech - initial API and implementation + *******************************************************************************/ +package org.eclipse.kapua.service.device.management.inventory.job.definition; + +import com.beust.jcommander.internal.Lists; +import org.eclipse.kapua.service.device.management.inventory.job.DeviceContainerStartTargetProcessor; +import org.eclipse.kapua.service.device.management.inventory.model.container.DeviceInventoryContainer; +import org.eclipse.kapua.service.job.step.definition.JobStepDefinitionRecord; +import org.eclipse.kapua.service.job.step.definition.JobStepPropertyRecord; +import org.eclipse.kapua.service.job.step.definition.JobStepType; +import org.eclipse.kapua.service.job.step.definition.device.management.TimeoutJobStepPropertyRecord; + +public class DeviceContainerStartJobStepDefinition extends JobStepDefinitionRecord { + + public DeviceContainerStartJobStepDefinition() { + super(null, + "Container Start", + "Execute request to start a container to the target devices of the Job", + JobStepType.TARGET, + null, + DeviceContainerStartTargetProcessor.class.getName(), + null, + Lists.newArrayList( + new JobStepPropertyRecord( + DeviceContainerPropertyKeys.CONTAINER_INPUT, + "XML/JSON that defines the container to be executed", + DeviceInventoryContainer.class.getName(), + null, + "{\n" + + " \"name\": \"docker_container_1\",\n" + + " \"version\": \"nginx:latest\",\n" + + " \"containerType\": \"DOCKER\",\n" + + " \"state\": \"ACTIVE\"\n" + + "}", + Boolean.TRUE, + Boolean.FALSE, + null, + null, + null, + null, + null), + new TimeoutJobStepPropertyRecord() + ) + ); + } +} \ No newline at end of file diff --git a/service/device/management/inventory/job/src/main/java/org/eclipse/kapua/service/device/management/inventory/job/definition/DeviceContainerStopJobStepDefinition.java b/service/device/management/inventory/job/src/main/java/org/eclipse/kapua/service/device/management/inventory/job/definition/DeviceContainerStopJobStepDefinition.java new file mode 100644 index 00000000000..a61a7cfb198 --- /dev/null +++ b/service/device/management/inventory/job/src/main/java/org/eclipse/kapua/service/device/management/inventory/job/definition/DeviceContainerStopJobStepDefinition.java @@ -0,0 +1,56 @@ +/******************************************************************************* + * Copyright (c) 2024, 2022 Eurotech and/or its affiliates and others + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Eurotech - initial API and implementation + *******************************************************************************/ +package org.eclipse.kapua.service.device.management.inventory.job.definition; + +import com.beust.jcommander.internal.Lists; +import org.eclipse.kapua.service.device.management.inventory.job.DeviceContainerStopTargetProcessor; +import org.eclipse.kapua.service.device.management.inventory.model.container.DeviceInventoryContainer; +import org.eclipse.kapua.service.job.step.definition.JobStepDefinitionRecord; +import org.eclipse.kapua.service.job.step.definition.JobStepPropertyRecord; +import org.eclipse.kapua.service.job.step.definition.JobStepType; +import org.eclipse.kapua.service.job.step.definition.device.management.TimeoutJobStepPropertyRecord; + +public class DeviceContainerStopJobStepDefinition extends JobStepDefinitionRecord { + + public DeviceContainerStopJobStepDefinition() { + super(null, + "Container Stop", + "Execute request to stop a container to the target devices of the Job", + JobStepType.TARGET, + null, + DeviceContainerStopTargetProcessor.class.getName(), + null, + Lists.newArrayList( + new JobStepPropertyRecord( + DeviceContainerPropertyKeys.CONTAINER_INPUT, + "XML/JSON that defines the container to be stopped", + DeviceInventoryContainer.class.getName(), + null, + "{\n" + + " \"name\": \"docker_container_1\",\n" + + " \"version\": \"nginx:latest\",\n" + + " \"containerType\": \"DOCKER\",\n" + + " \"state\": \"ACTIVE\"\n" + + "}", + Boolean.TRUE, + Boolean.FALSE, + null, + null, + null, + null, + null), + new TimeoutJobStepPropertyRecord() + ) + ); + } +} \ No newline at end of file diff --git a/service/device/management/inventory/pom.xml b/service/device/management/inventory/pom.xml index 2a974021de2..93a63230009 100644 --- a/service/device/management/inventory/pom.xml +++ b/service/device/management/inventory/pom.xml @@ -27,6 +27,7 @@ api internal + job