-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(uat): add core device discover to SDK-based client #395
Changes from 7 commits
4edab94
4c307ad
249b0ac
5552b88
4fe8983
ebd9539
0564071
13f192d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.aws.greengrass.testing.mqtt5.client; | ||
|
||
import com.aws.greengrass.testing.mqtt.client.CoreDeviceDiscoverReply; | ||
import com.aws.greengrass.testing.mqtt.client.CoreDeviceDiscoverRequest; | ||
import com.aws.greengrass.testing.mqtt5.client.exceptions.DiscoverException; | ||
|
||
/** | ||
* Interface of discovery client. | ||
*/ | ||
public interface DiscoverClient { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discovery There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 13f192d |
||
|
||
/** | ||
* Does discover of Core device broker. | ||
* | ||
* @param request the request | ||
* @return formatted gRPC response | ||
* @throws DiscoverException on errors | ||
*/ | ||
CoreDeviceDiscoverReply discoverCoreDevice(CoreDeviceDiscoverRequest request) throws DiscoverException; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.aws.greengrass.testing.mqtt5.client.discover; | ||
|
||
import com.aws.greengrass.testing.mqtt.client.CoreDeviceConnectivityInfo; | ||
import com.aws.greengrass.testing.mqtt.client.CoreDeviceDiscoverReply; | ||
import com.aws.greengrass.testing.mqtt.client.CoreDeviceDiscoverRequest; | ||
import com.aws.greengrass.testing.mqtt.client.CoreDeviceGroup; | ||
import com.aws.greengrass.testing.mqtt5.client.DiscoverClient; | ||
import com.aws.greengrass.testing.mqtt5.client.exceptions.DiscoverException; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import software.amazon.awssdk.crt.io.SocketOptions; | ||
import software.amazon.awssdk.crt.io.TlsContextOptions; | ||
import software.amazon.awssdk.iot.discovery.DiscoveryClient; | ||
import software.amazon.awssdk.iot.discovery.DiscoveryClientConfig; | ||
import software.amazon.awssdk.iot.discovery.model.ConnectivityInfo; | ||
import software.amazon.awssdk.iot.discovery.model.DiscoverResponse; | ||
import software.amazon.awssdk.iot.discovery.model.GGCore; | ||
import software.amazon.awssdk.iot.discovery.model.GGGroup; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
/** | ||
* Implementation of discover client. | ||
*/ | ||
public class DiscoverClientImpl implements DiscoverClient { | ||
private static final Logger logger = LogManager.getLogger(DiscoverClientImpl.class); | ||
|
||
@Override | ||
public CoreDeviceDiscoverReply discoverCoreDevice(CoreDeviceDiscoverRequest request) | ||
throws DiscoverException { | ||
try (SocketOptions socketOptions = new SocketOptions(); | ||
TlsContextOptions tlsOptions = TlsContextOptions.createWithMtls(request.getCert(), request.getKey()) | ||
.withCertificateAuthority(request.getCa()) | ||
.withAlpnList(DiscoveryClient.TLS_EXT_ALPN); | ||
DiscoveryClientConfig config = new DiscoveryClientConfig(tlsOptions, socketOptions, request.getRegion(), | ||
1, null); | ||
DiscoveryClient client = new DiscoveryClient(config)) { | ||
CompletableFuture<DiscoverResponse> discoverFuture = client.discover(request.getThingName()); | ||
try { | ||
DiscoverResponse response = discoverFuture.get(request.getTimeout(), TimeUnit.SECONDS); | ||
return convertResponseToReply(response); | ||
} catch (InterruptedException | ExecutionException | TimeoutException ex) { | ||
logger.atError().withThrowable(ex).log("Failed during discover"); | ||
throw new DiscoverException("Could not do discover", ex); | ||
} | ||
} | ||
} | ||
|
||
private CoreDeviceDiscoverReply convertResponseToReply(final DiscoverResponse response) | ||
throws DiscoverException { | ||
if (response == null) { | ||
throw new DiscoverException("Discovery response is missing"); | ||
} | ||
|
||
final List<GGGroup> groups = response.getGGGroups(); | ||
if (groups == null || groups.isEmpty() || groups.get(0) == null) { | ||
throw new DiscoverException("Groups are missing in discovery response"); | ||
} | ||
|
||
CoreDeviceDiscoverReply.Builder builder = CoreDeviceDiscoverReply.newBuilder(); | ||
for (final GGGroup group : groups) { | ||
List<String> ca = group.getCAs(); | ||
logger.atInfo().log("Discovered groupId {} with {} CA", group.getGGGroupId(), ca.size()); | ||
CoreDeviceGroup.Builder groupBuiler = CoreDeviceGroup.newBuilder(); | ||
groupBuiler.addAllCaList(ca); | ||
|
||
for (final GGCore core : group.getCores()) { | ||
logger.atInfo().log("Discovered Core with thing Arn {}", core.getThingArn()); | ||
for (final ConnectivityInfo ci : core.getConnectivity()) { | ||
logger.atInfo().log("Discovered connectivity info: id {} host {} port {}", ci.getId(), | ||
ci.getHostAddress(), ci.getPortNumber()); | ||
|
||
CoreDeviceConnectivityInfo cdc = CoreDeviceConnectivityInfo.newBuilder() | ||
.setHost(ci.getHostAddress()) | ||
.setPort(ci.getPortNumber()) | ||
.build(); | ||
|
||
groupBuiler.addConnectivityInfoList(cdc); | ||
} | ||
} | ||
builder.addGroupList(groupBuiler.build()); | ||
} | ||
|
||
return builder.build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.aws.greengrass.testing.mqtt5.client.exceptions; | ||
|
||
/** | ||
* Client's exception related to discover parts. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discovery There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 13f192d |
||
*/ | ||
public class DiscoverException extends ClientException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discovery There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 13f192d |
||
private static final long serialVersionUID = -2081564070408021325L; | ||
|
||
public DiscoverException() { | ||
super(); | ||
} | ||
|
||
public DiscoverException(String message) { | ||
super(message); | ||
} | ||
|
||
public DiscoverException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public DiscoverException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | ||
super(message, cause, enableSuppression, writableStackTrace); | ||
} | ||
|
||
public DiscoverException(Throwable cause) { | ||
super(cause); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discovery
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 13f192d