Skip to content

Commit

Permalink
[pom.xml][brski][registrar] added proper telemetry printing in log; t…
Browse files Browse the repository at this point in the history
…elemetry logic fix; minor source format updates; WIP v0.3
  • Loading branch information
EskoDijk committed Aug 27, 2024
1 parent 62816a8 commit 7c06a59
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.google.openthread</groupId>
<artifactId>ot-registrar</artifactId>
<version>0.2</version>
<version>0.3</version>

<name>OT Registrar</name>
<url>https://openthread.io/</url>
Expand Down
35 changes: 22 additions & 13 deletions src/main/java/com/google/openthread/brski/StatusTelemetry.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public class StatusTelemetry {
public String parseResultStatus = "";

/**
* stores the CBOR object as sent by the Pledge, for reference. Null if couldn't be parsed as
* CBOR.
* stores the CBOR object as sent by the Pledge, for reference. Null if it couldn't be parsed as CBOR.
*/
public CBORObject cbor = null;

Expand All @@ -67,13 +66,11 @@ protected StatusTelemetry() {
}

/**
* Create a new StatusTelemetry object with given state info. This is useful to serialize it to
* CBOR or byte[].
* Create a new StatusTelemetry object with given state info.
*
* @param isSuccess true if status should indicate success, false otherwise.
* @param reason required human-readable failure reason if isSuccess==false, should be null
* otherwise (but not necessarily).
* @return
* @param reason required human-readable failure reason if isSuccess==false, should be null otherwise (but not necessarily).
* @return New StatusTelemetry object.
*/
public static StatusTelemetry create(boolean isSuccess, String reason) {
StatusTelemetry st = new StatusTelemetry();
Expand Down Expand Up @@ -108,16 +105,16 @@ public byte[] serializeToBytes() {
}

/**
* Deserialize a status telemetry report from CBOR bytes. In case of invalid 'data', i.e. invalid
* CBOR format or invalid report format, flags in the StatusTelemetry object are set to indicate
* this.
* Deserialize a status telemetry report from CBOR bytes. In case of invalid 'data', i.e. invalid CBOR
* format or invalid report format, flags in the StatusTelemetry object are set to indicate this.
*
* @param data CBOR bytes
* @return new StatusTelemetry object
* @param data CBOR bytes of an encoded status telemetry object
* @return New StatusTelemetry object
*/
public static StatusTelemetry deserialize(byte[] data) {
StatusTelemetry st = new StatusTelemetry();
st.isValidFormat = true;

try {
CBORObject stCbor = CBORObject.DecodeFromBytes(data);
if (stCbor == null
Expand Down Expand Up @@ -166,11 +163,23 @@ public static StatusTelemetry deserialize(byte[] data) {
}

// evaluate more cases of invalid format.
if (st.status == false && (st.reason == null || st.reason.length() == 0)) {
if (st.isValidFormat && !st.status && (st.reason == null || st.reason.length() == 0)) {
st.isValidFormat = false;
st.parseResultStatus = "'reason' field must be provided if status==false";
}

return st;
}

public String toString() {
String s = "{status=" + this.status;
if (reason != null) {
s += ", reason=" + reason;
}
if (!this.isValidFormat){
s += " (INVALID: " +this.parseResultStatus+ ")";
}
s += "}";
return s;
}
}
27 changes: 7 additions & 20 deletions src/main/java/com/google/openthread/registrar/Registrar.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,32 +231,21 @@ public void handlePOST(CoapExchange exchange) {

// TODO: check latest draft to see if JSON support is mandatory here.
if (contentFormat != ExtendedMediaTypeRegistry.APPLICATION_CBOR) {
logger.warn(
"unsupported content-format for voucher status report: content-format="
+ contentFormat);
exchange.respond(
ResponseCode.UNSUPPORTED_CONTENT_FORMAT,
logger.warn("unsupported content-format for voucher status report: content-format=" + contentFormat);
exchange.respond(ResponseCode.UNSUPPORTED_CONTENT_FORMAT,
"Only Content Format " + ExtendedMediaTypeRegistry.APPLICATION_CBOR + " supported.");
return;
}

voucherStatus = StatusTelemetry.deserialize(exchange.getRequestPayload());
if (voucherStatus.cbor == null) {
logger.warn(
"decoding CBOR payload failed for voucher status report: "
+ voucherStatus.parseResultStatus);
exchange.respond(
ResponseCode.BAD_REQUEST,
"decoding CBOR payload failed for voucher status report: "
+ voucherStatus.parseResultStatus);
logger.warn("decoding CBOR payload failed for voucher status report: " + voucherStatus.parseResultStatus);
exchange.respond(ResponseCode.BAD_REQUEST,
"decoding CBOR payload failed for voucher status report: " + voucherStatus.parseResultStatus);
return;
}

logger.info(
"received voucher status report; status="
+ voucherStatus.status
+ ": "
+ voucherStatus.toString());
logger.info("received voucher status report:" + voucherStatus.toString());

// log the result for this Pledge
voucherStatusLog.put(clientId, voucherStatus);
Expand All @@ -270,9 +259,7 @@ public void handlePOST(CoapExchange exchange) {
if (voucherStatus.isValidFormat) { // success response
exchange.respond(ResponseCode.CHANGED);
} else {
exchange.respond(
ResponseCode.BAD_REQUEST,
"payload error: " + voucherStatus.parseResultStatus); // client submitted wrong format.
exchange.respond(ResponseCode.BAD_REQUEST, "error: " + voucherStatus.parseResultStatus); // client submitted wrong format.
}
}
}
Expand Down

0 comments on commit 7c06a59

Please sign in to comment.