Search in sources :

Example 1 with ProvisioningServiceClient

use of com.microsoft.azure.sdk.iot.provisioning.service.ProvisioningServiceClient in project azure-iot-sdk-java by Azure.

the class ServiceUpdateEnrollmentSample method main.

public static void main(String[] args) throws ProvisioningServiceClientException {
    System.out.println("Starting sample...");
    // *********************************** Create a Provisioning Service Client ************************************
    ProvisioningServiceClient provisioningServiceClient = ProvisioningServiceClient.createFromConnectionString(PROVISIONING_CONNECTION_STRING);
    // ************************************ Create a new individualEnrollment **************************************
    System.out.println("\nCreate a new individualEnrollment...");
    Map<String, Object> desiredProperties = new HashMap<String, Object>() {

        {
            put("Brand", "Contoso");
            put("Model", "SSC4");
            put("Color", "White");
        }
    };
    Attestation attestation = new TpmAttestation(TPM_ENDORSEMENT_KEY);
    TwinState initialTwinState = new TwinState(null, new TwinCollection(desiredProperties));
    IndividualEnrollment individualEnrollment = new IndividualEnrollment(REGISTRATION_ID, attestation);
    individualEnrollment.setInitialTwin(initialTwinState);
    IndividualEnrollment individualEnrollmentResult = provisioningServiceClient.createOrUpdateIndividualEnrollment(individualEnrollment);
    System.out.println("\nIndividualEnrollment created with success...");
    System.out.println("Note that there is a difference between the content of the individualEnrollment that you sent and\n" + "  the individualEnrollmentResult that you received. The individualEnrollmentResult contains the eTag.");
    System.out.println("\nindividualEnrollment:\n" + individualEnrollment);
    System.out.println("\nindividualEnrollmentResult:\n" + individualEnrollmentResult);
    // ********************************* Update the info of individualEnrollment ***********************************
    /*
         * At this point, if you try to update your information in the provisioning service using the individualEnrollment
         * that you created, it will fail because of the "precondition". It will happen because the individualEnrollment
         * do not contains the eTag, and the provisioning service will not be able to check if the enrollment that you
         * are updating is the correct one.
         *
         * So, to update the information you must use the individualEnrollmentResult that the provisioning service returned
         * when you created the enrollment, another solution is get the latest enrollment from the provisioning service
         * using the provisioningServiceClient.getIndividualEnrollment(), the result of this operation is an IndividualEnrollment
         * object that contains the eTag.
         */
    System.out.println("\nUpdating the enrollment...");
    desiredProperties.put("Color", "Glace white");
    initialTwinState = new TwinState(null, new TwinCollection(desiredProperties));
    individualEnrollmentResult.setInitialTwin(initialTwinState);
    individualEnrollmentResult = provisioningServiceClient.createOrUpdateIndividualEnrollment(individualEnrollmentResult);
    System.out.println("\nIndividualEnrollment updated with success...");
    System.out.println(individualEnrollmentResult);
    // *********************************** Delete info of individualEnrollment *************************************
    System.out.println("\nDelete the individualEnrollment...");
    provisioningServiceClient.deleteIndividualEnrollment(REGISTRATION_ID);
}
Also used : HashMap(java.util.HashMap) ProvisioningServiceClient(com.microsoft.azure.sdk.iot.provisioning.service.ProvisioningServiceClient)

Example 2 with ProvisioningServiceClient

use of com.microsoft.azure.sdk.iot.provisioning.service.ProvisioningServiceClient in project azure-iot-sdk-java by Azure.

the class ServiceEnrollmentGroupSample method main.

public static void main(String[] args) throws ProvisioningServiceClientException {
    System.out.println("Starting sample...");
    /*
         * Create the device collection.
         */
    String enrollmentGroupId = "enrollmentgroupid-" + UUID.randomUUID();
    // *********************************** Create a Provisioning Service Client ************************************
    ProvisioningServiceClient provisioningServiceClient = ProvisioningServiceClient.createFromConnectionString(PROVISIONING_CONNECTION_STRING);
    // *************************************** Create a new enrollmentGroup ****************************************
    System.out.println("\nCreate a new enrollmentGroup...");
    Attestation attestation = X509Attestation.createFromRootCertificates(PUBLIC_KEY_CERTIFICATE_STRING);
    EnrollmentGroup enrollmentGroup = new EnrollmentGroup(enrollmentGroupId, attestation);
    // Optional parameter.
    enrollmentGroup.setIotHubHostNameFinal(IOTHUB_HOST_NAME);
    // Optional parameter.
    enrollmentGroup.setProvisioningStatusFinal(ProvisioningStatus.ENABLED);
    System.out.println("\nAdd new enrollmentGroup...");
    EnrollmentGroup enrollmentGroupResult = provisioningServiceClient.createOrUpdateEnrollmentGroup(enrollmentGroup);
    System.out.println("\nEnrollmentGroup created with success...");
    System.out.println(enrollmentGroupResult);
    // **************************************** Get info of enrollmentGroup ****************************************
    System.out.println("\nGet the enrollmentGroup information...");
    EnrollmentGroup getResult = provisioningServiceClient.getEnrollmentGroup(enrollmentGroupId);
    System.out.println(getResult);
    // *************************************** Query info of enrollmentGroup ***************************************
    System.out.println("\nCreate a query for the enrollmentGroups...");
    QuerySpecification querySpecification = new QuerySpecificationBuilder("*", QuerySpecificationBuilder.FromType.ENROLLMENT_GROUPS).createSqlQuery();
    Query query = provisioningServiceClient.createEnrollmentGroupQuery(querySpecification);
    while (query.hasNext()) {
        System.out.println("\nQuery the next enrollmentGroups...");
        QueryResult queryResult = query.next();
        System.out.println(queryResult);
    }
    // ************************************** Delete info of enrollmentGroup ***************************************
    System.out.println("\nDelete the enrollmentGroup...");
    provisioningServiceClient.deleteEnrollmentGroup(enrollmentGroupId);
}
Also used : Query(com.microsoft.azure.sdk.iot.provisioning.service.Query) ProvisioningServiceClient(com.microsoft.azure.sdk.iot.provisioning.service.ProvisioningServiceClient)

Example 3 with ProvisioningServiceClient

use of com.microsoft.azure.sdk.iot.provisioning.service.ProvisioningServiceClient in project azure-iot-sdk-java by Azure.

the class ServiceEnrollmentSample method main.

public static void main(String[] args) throws ProvisioningServiceClientException {
    System.out.println("Starting sample...");
    // *********************************** Create a Provisioning Service Client ************************************
    ProvisioningServiceClient provisioningServiceClient = ProvisioningServiceClient.createFromConnectionString(PROVISIONING_CONNECTION_STRING);
    // ******************************** Create a new individualEnrollment config **********************************
    System.out.println("\nCreate a new individualEnrollment...");
    Attestation attestation = new TpmAttestation(TPM_ENDORSEMENT_KEY);
    IndividualEnrollment individualEnrollment = new IndividualEnrollment(REGISTRATION_ID, attestation);
    // The following parameters are optional. Remove it if you don't need.
    individualEnrollment.setDeviceIdFinal(DEVICE_ID);
    individualEnrollment.setIotHubHostNameFinal(IOTHUB_HOST_NAME);
    individualEnrollment.setProvisioningStatusFinal(PROVISIONING_STATUS);
    // ************************************ Create the individualEnrollment *************************************
    System.out.println("\nAdd new individualEnrollment...");
    IndividualEnrollment individualEnrollmentResult = provisioningServiceClient.createOrUpdateIndividualEnrollment(individualEnrollment);
    System.out.println("\nIndividualEnrollment created with success...");
    System.out.println(individualEnrollmentResult);
    // ************************************* Get info of individualEnrollment *************************************
    System.out.println("\nGet the individualEnrollment information...");
    IndividualEnrollment getResult = provisioningServiceClient.getIndividualEnrollment(REGISTRATION_ID);
    System.out.println(getResult);
    // ************************************ Query info of individualEnrollment ************************************
    System.out.println("\nCreate a query for enrollments...");
    QuerySpecification querySpecification = new QuerySpecificationBuilder("*", QuerySpecificationBuilder.FromType.ENROLLMENTS).createSqlQuery();
    Query query = provisioningServiceClient.createIndividualEnrollmentQuery(querySpecification);
    while (query.hasNext()) {
        System.out.println("\nQuery the next enrollments...");
        QueryResult queryResult = query.next();
        System.out.println(queryResult);
    }
    // *********************************** Delete info of individualEnrollment ************************************
    System.out.println("\nDelete the individualEnrollment...");
    provisioningServiceClient.deleteIndividualEnrollment(REGISTRATION_ID);
}
Also used : Query(com.microsoft.azure.sdk.iot.provisioning.service.Query) ProvisioningServiceClient(com.microsoft.azure.sdk.iot.provisioning.service.ProvisioningServiceClient)

Example 4 with ProvisioningServiceClient

use of com.microsoft.azure.sdk.iot.provisioning.service.ProvisioningServiceClient in project azure-iot-sdk-java by Azure.

the class ServiceBulkOperationSample method main.

public static void main(String[] args) throws ProvisioningServiceClientException {
    System.out.println("Starting sample...");
    // *********************************** Create a Provisioning Service Client ************************************
    ProvisioningServiceClient provisioningServiceClient = ProvisioningServiceClient.createFromConnectionString(PROVISIONING_CONNECTION_STRING);
    // ******************************** Create a new bulk of individual enrollment *********************************
    System.out.println("\nCreate a new set of individualEnrollments...");
    List<IndividualEnrollment> individualEnrollments = new LinkedList<>();
    for (Map.Entry<String, String> device : DEVICE_MAP.entrySet()) {
        Attestation attestation = new TpmAttestation(device.getValue());
        String registrationId = device.getKey();
        System.out.println("  Add " + registrationId);
        IndividualEnrollment individualEnrollment = new IndividualEnrollment(registrationId, attestation);
        individualEnrollments.add(individualEnrollment);
    }
    System.out.println("\nRun the bulk operation to create the individualEnrollments...");
    BulkEnrollmentOperationResult bulkOperationResult = provisioningServiceClient.runBulkEnrollmentOperation(BulkOperationMode.CREATE, individualEnrollments);
    System.out.println("Result of the Create bulk enrollment...");
    System.out.println(bulkOperationResult);
    // ************************************ Get info of individualEnrollments *************************************
    for (IndividualEnrollment individualEnrollment : individualEnrollments) {
        String registrationId = individualEnrollment.getRegistrationId();
        System.out.println("\nGet the individualEnrollment information for " + registrationId + ":");
        IndividualEnrollment getResult = provisioningServiceClient.getIndividualEnrollment(registrationId);
        System.out.println(getResult);
    }
    // ************************************ Query info of individualEnrollments ***********************************
    System.out.println("\nCreate a query for individualEnrollments...");
    QuerySpecification querySpecification = new QuerySpecificationBuilder("*", QuerySpecificationBuilder.FromType.ENROLLMENTS).createSqlQuery();
    Query query = provisioningServiceClient.createIndividualEnrollmentQuery(querySpecification, QUERY_PAGE_SIZE);
    while (query.hasNext()) {
        System.out.println("\nQuery the next individualEnrollments...");
        QueryResult queryResult = query.next();
        System.out.println(queryResult);
    }
    // ********************************** Delete bulk of individualEnrollments ************************************
    System.out.println("\nDelete the set of individualEnrollments...");
    bulkOperationResult = provisioningServiceClient.runBulkEnrollmentOperation(BulkOperationMode.DELETE, individualEnrollments);
    System.out.println(bulkOperationResult);
}
Also used : Query(com.microsoft.azure.sdk.iot.provisioning.service.Query) ProvisioningServiceClient(com.microsoft.azure.sdk.iot.provisioning.service.ProvisioningServiceClient)

Example 5 with ProvisioningServiceClient

use of com.microsoft.azure.sdk.iot.provisioning.service.ProvisioningServiceClient in project azure-iot-sdk-java by Azure.

the class ProvisioningTPMTests method provisioningTpmFlow.

@Test
public void provisioningTpmFlow() throws SecurityProviderException, ProvisioningServiceClientException, ProvisioningDeviceClientException, InterruptedException {
    ProvisioningServiceClient provisioningServiceClient = ProvisioningServiceClient.createFromConnectionString(provisioningServiceConnectionString);
    String registrationId = UUID.randomUUID().toString();
    String provisionedDeviceId = "Some-Provisioned-Device-" + TPM + "-" + UUID.randomUUID().toString();
    SecurityProvider securityProvider = new SecurityProviderTPMEmulator(registrationId);
    Attestation attestation = new TpmAttestation(new String(encodeBase64(((SecurityProviderTpm) securityProvider).getEndorsementKey())));
    IndividualEnrollment individualEnrollment = new IndividualEnrollment(registrationId, attestation);
    individualEnrollment.setDeviceIdFinal(provisionedDeviceId);
    provisioningServiceClient.createOrUpdateIndividualEnrollment(individualEnrollment);
    ProvisioningDeviceClient provisioningDeviceClient = ProvisioningDeviceClient.create(provisioningServiceGlobalEndpoint, provisioningServiceIdScope, ProvisioningDeviceClientTransportProtocol.AMQPS, securityProvider);
    AtomicBoolean registrationCompleted = new AtomicBoolean(false);
    AtomicBoolean registrationCompletedSuccessfully = new AtomicBoolean(false);
    provisioningDeviceClient.registerDevice((provisioningDeviceClientRegistrationResult, e, context) -> {
        log.debug("Provisioning registration callback fired with result {}", provisioningDeviceClientRegistrationResult.getProvisioningDeviceClientStatus());
        if (e != null) {
            log.error("Provisioning registration callback fired with exception {}", e);
        }
        ProvisioningDeviceClientStatus status = provisioningDeviceClientRegistrationResult.getProvisioningDeviceClientStatus();
        if (status == ProvisioningDeviceClientStatus.PROVISIONING_DEVICE_STATUS_ASSIGNED) {
            registrationCompletedSuccessfully.set(true);
        }
        registrationCompleted.set(true);
    }, null);
    long startTime = System.currentTimeMillis();
    while (!registrationCompleted.get()) {
        Thread.sleep(200);
        if (System.currentTimeMillis() - startTime > REGISTRATION_TIMEOUT_MILLISECONDS) {
            fail("Timed out waiting for device registration to complete.");
        }
    }
    assertTrue("Registration completed, but not successfully", registrationCompletedSuccessfully.get());
    provisioningDeviceClient.closeNow();
}
Also used : IndividualEnrollment(com.microsoft.azure.sdk.iot.provisioning.service.configs.IndividualEnrollment) ProvisioningDeviceClient(com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClient) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SecurityProviderTPMEmulator(com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator) ProvisioningDeviceClientStatus(com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClientStatus) TpmAttestation(com.microsoft.azure.sdk.iot.provisioning.service.configs.TpmAttestation) SecurityProvider(com.microsoft.azure.sdk.iot.provisioning.security.SecurityProvider) TpmAttestation(com.microsoft.azure.sdk.iot.provisioning.service.configs.TpmAttestation) Attestation(com.microsoft.azure.sdk.iot.provisioning.service.configs.Attestation) ProvisioningServiceClient(com.microsoft.azure.sdk.iot.provisioning.service.ProvisioningServiceClient) Test(org.junit.Test)

Aggregations

ProvisioningServiceClient (com.microsoft.azure.sdk.iot.provisioning.service.ProvisioningServiceClient)5 Query (com.microsoft.azure.sdk.iot.provisioning.service.Query)3 ProvisioningDeviceClient (com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClient)1 ProvisioningDeviceClientStatus (com.microsoft.azure.sdk.iot.provisioning.device.ProvisioningDeviceClientStatus)1 SecurityProvider (com.microsoft.azure.sdk.iot.provisioning.security.SecurityProvider)1 SecurityProviderTPMEmulator (com.microsoft.azure.sdk.iot.provisioning.security.hsm.SecurityProviderTPMEmulator)1 Attestation (com.microsoft.azure.sdk.iot.provisioning.service.configs.Attestation)1 IndividualEnrollment (com.microsoft.azure.sdk.iot.provisioning.service.configs.IndividualEnrollment)1 TpmAttestation (com.microsoft.azure.sdk.iot.provisioning.service.configs.TpmAttestation)1 HashMap (java.util.HashMap)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Test (org.junit.Test)1