Search in sources :

Example 1 with DescribeJobExecutionResponse

use of software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse in project aws-greengrass-nucleus by aws-greengrass.

the class IotJobsClientWrapper method SubscribeToDescribeJobExecutionAccepted.

@Override
public CompletableFuture<Integer> SubscribeToDescribeJobExecutionAccepted(DescribeJobExecutionSubscriptionRequest request, QualityOfService qos, Consumer<DescribeJobExecutionResponse> handler, Consumer<Exception> exceptionHandler) {
    if (request.jobId == null || request.thingName == null) {
        CompletableFuture result = new CompletableFuture();
        result.completeExceptionally(new MqttException("DescribeJobExecutionSubscriptionRequest must have a non-null jobId and a non-null thingName"));
        return result;
    }
    String topic = String.format(JOB_DESCRIBE_ACCEPTED_TOPIC, request.thingName, request.jobId);
    Consumer<MqttMessage> messageHandler = describeJobCbs.computeIfAbsent(new Pair<>(handler, exceptionHandler), (k) -> (message) -> {
        try {
            String payload = new String(message.getPayload(), StandardCharsets.UTF_8);
            DescribeJobExecutionResponse response = this.gson.fromJson(payload, DescribeJobExecutionResponse.class);
            handler.accept(response);
        } catch (Exception e) {
            if (exceptionHandler != null) {
                exceptionHandler.accept(e);
            }
        }
    });
    return this.connection.subscribe(topic, qos, messageHandler);
}
Also used : MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) CompletableFuture(java.util.concurrent.CompletableFuture) DescribeJobExecutionResponse(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) MqttException(software.amazon.awssdk.crt.mqtt.MqttException)

Example 2 with DescribeJobExecutionResponse

use of software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse in project aws-greengrass-nucleus by aws-greengrass.

the class IotJobsHelperTest method GIVEN_ongoing_job_deployment_with_queued_job_in_cloud_WHEN_cancel_notification_THEN_cancel_current_deployment.

@Test
void GIVEN_ongoing_job_deployment_with_queued_job_in_cloud_WHEN_cancel_notification_THEN_cancel_current_deployment() throws Exception {
    iotJobsHelper.postInject();
    String TEST_JOB_ID = "jobToBeCancelled";
    iotJobsHelper.setDeploymentQueue(mockDeploymentQueue);
    when(mockDeploymentQueue.isEmpty()).thenReturn(true);
    CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.completedFuture(1);
    when(mockIotJobsClientWrapper.SubscribeToJobExecutionsChangedEvents(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
    when(mockIotJobsClientWrapper.SubscribeToDescribeJobExecutionAccepted(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
    when(mockIotJobsClientWrapper.SubscribeToDescribeJobExecutionRejected(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
    DeploymentTaskMetadata mockCurrentDeploymentTaskMetadata = mock(DeploymentTaskMetadata.class);
    when(mockCurrentDeploymentTaskMetadata.getDeploymentType()).thenReturn(IOT_JOBS);
    when(mockCurrentDeploymentTaskMetadata.isCancellable()).thenReturn(true);
    when(mockDeploymentService.getCurrentDeploymentTaskMetadata()).thenReturn(mockCurrentDeploymentTaskMetadata);
    iotJobsHelper.subscribeToJobsTopics();
    verify(mockIotJobsClientWrapper, times(2)).SubscribeToDescribeJobExecutionAccepted(any(), eq(QualityOfService.AT_LEAST_ONCE), describeJobResponseCaptor.capture());
    JobExecutionData jobExecutionData = new JobExecutionData();
    jobExecutionData.jobId = TEST_JOB_ID;
    jobExecutionData.status = JobStatus.QUEUED;
    jobExecutionData.queuedAt = new Timestamp(new Date());
    HashMap<String, Object> sampleJobDocument = new HashMap<>();
    sampleJobDocument.put("DeploymentId", TEST_JOB_ID);
    jobExecutionData.jobDocument = sampleJobDocument;
    DescribeJobExecutionResponse describeJobExecutionResponse = new DescribeJobExecutionResponse();
    describeJobExecutionResponse.execution = jobExecutionData;
    describeJobResponseCaptor.getValue().accept(describeJobExecutionResponse);
    ArgumentCaptor<Deployment> deploymentArgumentCaptor = ArgumentCaptor.forClass(Deployment.class);
    verify(mockDeploymentQueue, times(2)).offer(deploymentArgumentCaptor.capture());
    // First queued deployment should be for cancellation and then next one for next queued job
    List<Deployment> actualDeployments = deploymentArgumentCaptor.getAllValues();
    assertTrue(actualDeployments.get(0).isCancelled());
    assertEquals(IOT_JOBS, actualDeployments.get(0).getDeploymentType());
    assertFalse(actualDeployments.get(1).isCancelled());
    assertEquals(IOT_JOBS, actualDeployments.get(1).getDeploymentType());
    assertEquals(TEST_JOB_ID, actualDeployments.get(1).getId());
}
Also used : JobExecutionData(software.amazon.awssdk.iot.iotjobs.model.JobExecutionData) HashMap(java.util.HashMap) DescribeJobExecutionResponse(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse) Deployment(com.aws.greengrass.deployment.model.Deployment) Timestamp(software.amazon.awssdk.iot.Timestamp) Date(java.util.Date) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DeploymentTaskMetadata(com.aws.greengrass.deployment.model.DeploymentTaskMetadata) Test(org.junit.jupiter.api.Test)

Example 3 with DescribeJobExecutionResponse

use of software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse in project aws-greengrass-nucleus by aws-greengrass.

the class IotJobsHelperTest method GIVEN_connected_to_iot_WHEN_subscribe_to_jobs_topics_THEN_get_job_description.

@Test
void GIVEN_connected_to_iot_WHEN_subscribe_to_jobs_topics_THEN_get_job_description() throws Exception {
    iotJobsHelper.postInject();
    String TEST_JOB_ID = "jobToReceive";
    iotJobsHelper.setDeploymentQueue(mockDeploymentQueue);
    CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.completedFuture(1);
    when(mockIotJobsClientWrapper.SubscribeToJobExecutionsChangedEvents(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
    when(mockIotJobsClientWrapper.SubscribeToDescribeJobExecutionAccepted(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
    when(mockIotJobsClientWrapper.SubscribeToDescribeJobExecutionRejected(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
    when(mockDeploymentService.getCurrentDeploymentTaskMetadata()).thenReturn(null);
    iotJobsHelper.subscribeToJobsTopics();
    verify(mockIotJobsClientWrapper, times(2)).SubscribeToDescribeJobExecutionAccepted(any(), eq(QualityOfService.AT_LEAST_ONCE), describeJobResponseCaptor.capture());
    verify(mockIotJobsClientWrapper, times(2)).SubscribeToDescribeJobExecutionRejected(any(), eq(QualityOfService.AT_LEAST_ONCE), rejectedErrorCaptor.capture());
    JobExecutionData jobExecutionData = new JobExecutionData();
    jobExecutionData.jobId = TEST_JOB_ID;
    jobExecutionData.status = JobStatus.QUEUED;
    jobExecutionData.queuedAt = new Timestamp(new Date());
    HashMap<String, Object> sampleJobDocument = new HashMap<>();
    sampleJobDocument.put("DeploymentId", TEST_JOB_ID);
    jobExecutionData.jobDocument = sampleJobDocument;
    DescribeJobExecutionResponse describeJobExecutionResponse = new DescribeJobExecutionResponse();
    describeJobExecutionResponse.execution = jobExecutionData;
    describeJobResponseCaptor.getValue().accept(describeJobExecutionResponse);
    ArgumentCaptor<Deployment> deploymentArgumentCaptor = ArgumentCaptor.forClass(Deployment.class);
    verify(mockDeploymentQueue).offer(deploymentArgumentCaptor.capture());
    Deployment actualDeployment = deploymentArgumentCaptor.getValue();
    assertEquals(TEST_JOB_ID, actualDeployment.getId());
    assertEquals(IOT_JOBS, actualDeployment.getDeploymentType());
    assertEquals("{\"DeploymentId\":\"jobToReceive\"}", actualDeployment.getDeploymentDocument());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JobExecutionData(software.amazon.awssdk.iot.iotjobs.model.JobExecutionData) HashMap(java.util.HashMap) DescribeJobExecutionResponse(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse) Deployment(com.aws.greengrass.deployment.model.Deployment) Timestamp(software.amazon.awssdk.iot.Timestamp) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 4 with DescribeJobExecutionResponse

use of software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse in project aws-iot-device-sdk-java-v2 by aws.

the class IotJobsClient method SubscribeToDescribeJobExecutionAccepted.

/**
 * Subscribes to the accepted topic for the DescribeJobExecution operation
 *
 * Once subscribed, `handler` is invoked each time a message matching
 * the `topic` is received. It is possible for such messages to arrive before
 * the SUBACK is received.
 *
 * AWS documentation: https://docs.aws.amazon.com/iot/latest/developerguide/jobs-api.html#mqtt-describejobexecution
 *
 * @param request Subscription request configuration
 * @param qos Maximum requested QoS that server may use when sending messages to the client.
 *            The server may grant a lower QoS in the SUBACK
 * @param handler callback function to invoke with messages received on the subscription topic
 * @param exceptionHandler callback function to invoke if an exception occurred deserializing a message
 *
 * @return a future containing the MQTT packet id used to perform the subscribe operation
 */
public CompletableFuture<Integer> SubscribeToDescribeJobExecutionAccepted(DescribeJobExecutionSubscriptionRequest request, QualityOfService qos, Consumer<DescribeJobExecutionResponse> handler, Consumer<Exception> exceptionHandler) {
    String topic = "$aws/things/{thingName}/jobs/{jobId}/get/accepted";
    if (request.thingName == null) {
        CompletableFuture<Integer> result = new CompletableFuture<Integer>();
        result.completeExceptionally(new MqttException("DescribeJobExecutionSubscriptionRequest must have a non-null thingName"));
        return result;
    }
    topic = topic.replace("{thingName}", request.thingName);
    if (request.jobId == null) {
        CompletableFuture<Integer> result = new CompletableFuture<Integer>();
        result.completeExceptionally(new MqttException("DescribeJobExecutionSubscriptionRequest must have a non-null jobId"));
        return result;
    }
    topic = topic.replace("{jobId}", request.jobId);
    Consumer<MqttMessage> messageHandler = (message) -> {
        try {
            String payload = new String(message.getPayload(), StandardCharsets.UTF_8);
            DescribeJobExecutionResponse response = gson.fromJson(payload, DescribeJobExecutionResponse.class);
            handler.accept(response);
        } catch (Exception e) {
            if (exceptionHandler != null) {
                exceptionHandler.accept(e);
            }
        }
    };
    return connection.subscribe(topic, qos, messageHandler);
}
Also used : JobExecutionData(software.amazon.awssdk.iot.iotjobs.model.JobExecutionData) UpdateJobExecutionRequest(software.amazon.awssdk.iot.iotjobs.model.UpdateJobExecutionRequest) NextJobExecutionChangedSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.NextJobExecutionChangedSubscriptionRequest) HashMap(java.util.HashMap) EnumSerializer(software.amazon.awssdk.iot.EnumSerializer) CompletableFuture(java.util.concurrent.CompletableFuture) RejectedError(software.amazon.awssdk.iot.iotjobs.model.RejectedError) JobExecutionState(software.amazon.awssdk.iot.iotjobs.model.JobExecutionState) StartNextPendingJobExecutionSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.StartNextPendingJobExecutionSubscriptionRequest) GsonBuilder(com.google.gson.GsonBuilder) ByteBuffer(java.nio.ByteBuffer) DescribeJobExecutionRequest(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionRequest) DescribeJobExecutionSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionSubscriptionRequest) UpdateJobExecutionSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.UpdateJobExecutionSubscriptionRequest) GetPendingJobExecutionsSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.GetPendingJobExecutionsSubscriptionRequest) QualityOfService(software.amazon.awssdk.crt.mqtt.QualityOfService) GetPendingJobExecutionsResponse(software.amazon.awssdk.iot.iotjobs.model.GetPendingJobExecutionsResponse) Gson(com.google.gson.Gson) JobStatus(software.amazon.awssdk.iot.iotjobs.model.JobStatus) GetPendingJobExecutionsRequest(software.amazon.awssdk.iot.iotjobs.model.GetPendingJobExecutionsRequest) StartNextPendingJobExecutionRequest(software.amazon.awssdk.iot.iotjobs.model.StartNextPendingJobExecutionRequest) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) DescribeJobExecutionResponse(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse) RejectedErrorCode(software.amazon.awssdk.iot.iotjobs.model.RejectedErrorCode) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) StandardCharsets(java.nio.charset.StandardCharsets) Consumer(java.util.function.Consumer) JobExecutionsChangedEvent(software.amazon.awssdk.iot.iotjobs.model.JobExecutionsChangedEvent) MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) NextJobExecutionChangedEvent(software.amazon.awssdk.iot.iotjobs.model.NextJobExecutionChangedEvent) StartNextJobExecutionResponse(software.amazon.awssdk.iot.iotjobs.model.StartNextJobExecutionResponse) JobExecutionsChangedSubscriptionRequest(software.amazon.awssdk.iot.iotjobs.model.JobExecutionsChangedSubscriptionRequest) Timestamp(software.amazon.awssdk.iot.Timestamp) JobExecutionSummary(software.amazon.awssdk.iot.iotjobs.model.JobExecutionSummary) UpdateJobExecutionResponse(software.amazon.awssdk.iot.iotjobs.model.UpdateJobExecutionResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) CompletableFuture(java.util.concurrent.CompletableFuture) DescribeJobExecutionResponse(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) MqttException(software.amazon.awssdk.crt.mqtt.MqttException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 5 with DescribeJobExecutionResponse

use of software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse in project aws-greengrass-nucleus by aws-greengrass.

the class IotJobsHelperTest method GIVEN_iot_job_notifications_WHEN_duplicate_or_outdated_THEN_ignore_jobs.

@Test
void GIVEN_iot_job_notifications_WHEN_duplicate_or_outdated_THEN_ignore_jobs() throws InterruptedException {
    iotJobsHelper.postInject();
    String TEST_JOB_ID = "duplicateJob";
    iotJobsHelper.setDeploymentQueue(mockDeploymentQueue);
    CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.completedFuture(1);
    when(mockIotJobsClientWrapper.SubscribeToJobExecutionsChangedEvents(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
    when(mockIotJobsClientWrapper.SubscribeToDescribeJobExecutionAccepted(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
    when(mockIotJobsClientWrapper.SubscribeToDescribeJobExecutionRejected(any(), eq(QualityOfService.AT_LEAST_ONCE), any())).thenReturn(integerCompletableFuture);
    when(mockDeploymentService.getCurrentDeploymentTaskMetadata()).thenReturn(null);
    iotJobsHelper.subscribeToJobsTopics();
    verify(mockIotJobsClientWrapper, times(2)).SubscribeToDescribeJobExecutionAccepted(any(), eq(QualityOfService.AT_LEAST_ONCE), describeJobResponseCaptor.capture());
    verify(mockIotJobsClientWrapper, times(2)).SubscribeToDescribeJobExecutionRejected(any(), eq(QualityOfService.AT_LEAST_ONCE), rejectedErrorCaptor.capture());
    // Create four mock jobs
    Timestamp current = new Timestamp(new Date());
    DescribeJobExecutionResponse describeJobExecutionResponse1 = new DescribeJobExecutionResponse();
    describeJobExecutionResponse1.execution = getMockJobExecutionData(TEST_JOB_ID, current);
    describeJobResponseCaptor.getValue().accept(describeJobExecutionResponse1);
    DescribeJobExecutionResponse describeJobExecutionResponse2 = new DescribeJobExecutionResponse();
    describeJobExecutionResponse2.execution = getMockJobExecutionData(TEST_JOB_ID, current);
    describeJobResponseCaptor.getValue().accept(describeJobExecutionResponse2);
    DescribeJobExecutionResponse describeJobExecutionResponse3 = new DescribeJobExecutionResponse();
    describeJobExecutionResponse3.execution = getMockJobExecutionData("anyId1", new Timestamp(new Date(0)));
    describeJobResponseCaptor.getValue().accept(describeJobExecutionResponse3);
    DescribeJobExecutionResponse describeJobExecutionResponse4 = new DescribeJobExecutionResponse();
    describeJobExecutionResponse4.execution = getMockJobExecutionData("anyId2", current);
    describeJobResponseCaptor.getValue().accept(describeJobExecutionResponse4);
    // Only two jobs should be queued
    ArgumentCaptor<Deployment> deploymentArgumentCaptor = ArgumentCaptor.forClass(Deployment.class);
    verify(mockDeploymentQueue, times(2)).offer(deploymentArgumentCaptor.capture());
    List<Deployment> actualDeployments = deploymentArgumentCaptor.getAllValues();
    assertEquals(2, actualDeployments.size());
    assertEquals(TEST_JOB_ID, actualDeployments.get(0).getId());
    assertEquals(IOT_JOBS, actualDeployments.get(0).getDeploymentType());
    assertEquals("{\"DeploymentId\":\"duplicateJob\"}", actualDeployments.get(0).getDeploymentDocument());
    assertEquals("anyId2", actualDeployments.get(1).getId());
    assertEquals(IOT_JOBS, actualDeployments.get(1).getDeploymentType());
    assertEquals("{\"DeploymentId\":\"anyId2\"}", actualDeployments.get(1).getDeploymentDocument());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DescribeJobExecutionResponse(software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse) Deployment(com.aws.greengrass.deployment.model.Deployment) Timestamp(software.amazon.awssdk.iot.Timestamp) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Aggregations

DescribeJobExecutionResponse (software.amazon.awssdk.iot.iotjobs.model.DescribeJobExecutionResponse)5 Timestamp (software.amazon.awssdk.iot.Timestamp)4 Deployment (com.aws.greengrass.deployment.model.Deployment)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Test (org.junit.jupiter.api.Test)3 JobExecutionData (software.amazon.awssdk.iot.iotjobs.model.JobExecutionData)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 MqttException (software.amazon.awssdk.crt.mqtt.MqttException)2 MqttMessage (software.amazon.awssdk.crt.mqtt.MqttMessage)2 DeploymentTaskMetadata (com.aws.greengrass.deployment.model.DeploymentTaskMetadata)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ByteBuffer (java.nio.ByteBuffer)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Consumer (java.util.function.Consumer)1 MqttClientConnection (software.amazon.awssdk.crt.mqtt.MqttClientConnection)1 QualityOfService (software.amazon.awssdk.crt.mqtt.QualityOfService)1