Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import gnmi.Gnmi;
import gnmi.Gnmi.Update;
Expand Down Expand Up @@ -84,7 +85,12 @@ private NormalizedNode updateToNormalizedNode(final Update update, final YangIns
Check if response is rooted deeper than requested, if yes, wrap it so it is rooted at
the same level as identifier last path arg points to.
*/
if (!identifier.isEmpty() && isResponseJsonDeeperThanRequested(identifier, responseJson)) {
if (!identifier.isEmpty()) {

final String lastPathArgName = identifier.getLastPathArgument().getNodeType().getLocalName();
JsonElement jsonObject = JsonParser.parseString(responseJson);

if (isResponseJsonDeeperThanRequested(lastPathArgName, jsonObject)) {
final QName lastName = identifier.getLastPathArgument().getNodeType();
final Module moduleByQName =
DataConverter.findModuleByQName(lastName, schemaContextProvider.getSchemaContext())
Expand All @@ -101,8 +107,13 @@ private NormalizedNode updateToNormalizedNode(final Update update, final YangIns
} else {
responseJson = JsonUtils.wrapJsonWithObject(responseJson, wrapWith, gson);
}
} else if (isResponseJsonWithoutNamespace(jsonObject)) {
// Add missing namespace to the response from the request.
JsonElement responseJsonWithNamespace = addNamespaceToResponseJson(jsonObject, identifier, schemaContextProvider);
responseJson = responseJsonWithNamespace.toString();
}
return resolveJsonResponse(identifier, responseJson);
}
return resolveJsonResponse(identifier, responseJson);
/*
In the case of primitive values, only the value is present in response.
Since json parser works only with object, always wrap the value (wrapPrimitive()).
Expand Down Expand Up @@ -133,10 +144,8 @@ Since json parser works only with object, always wrap the value (wrapPrimitive()
}
}

private static boolean isResponseJsonDeeperThanRequested(final YangInstanceIdentifier identifier,
final String responseJson) {
final String lastPathArgName = identifier.getLastPathArgument().getNodeType().getLocalName();
final JsonElement jsonObject = JsonParser.parseString(responseJson);
private static boolean isResponseJsonDeeperThanRequested(final String lastPathArgName,
final JsonElement jsonObject) {
if (!jsonObject.isJsonObject()) {
return true;
}
Expand All @@ -154,6 +163,34 @@ private static boolean isResponseJsonDeeperThanRequested(final YangInstanceIdent
return true;
}

private static boolean isResponseJsonWithoutNamespace(JsonElement jsonElement) {
Comment thread
tokarenko marked this conversation as resolved.
Outdated
Map.Entry<String, JsonElement> firstElement = jsonElement.getAsJsonObject().entrySet().iterator().next();
Comment thread
tokarenko marked this conversation as resolved.
Outdated
if (firstElement.getKey().contains(":")) {
return false;
} else {
return true;
}
}
Comment thread
tokarenko marked this conversation as resolved.
Outdated

private static JsonElement addNamespaceToResponseJson(JsonElement jsonElement,
Comment thread
tokarenko marked this conversation as resolved.
Outdated
final YangInstanceIdentifier identifier,
final SchemaContextProvider schemaContextProvider)
throws GnmiCodecException {
final QName lastName = identifier.getLastPathArgument().getNodeType();
final Module moduleByQName =
DataConverter.findModuleByQName(lastName, schemaContextProvider.getSchemaContext())
.orElseThrow(() -> new GnmiCodecException(
String.format("Unable to find module of node %s", lastName)));

final String moduleNameWithNamespace = String.format("%s:%s", moduleByQName.getName(),
lastName.getLocalName());

Map.Entry<String, JsonElement> entry = jsonElement.getAsJsonObject().entrySet().iterator().next();
Comment thread
tokarenko marked this conversation as resolved.
Outdated
JsonObject jsonObject = new JsonObject();
jsonObject.add(moduleNameWithNamespace, entry.getValue());
return jsonObject;
}
Comment thread
tokarenko marked this conversation as resolved.
Outdated

@SuppressWarnings("IllegalCatch")
private NormalizedNode resolveJsonResponse(YangInstanceIdentifier identifier, String inputJson)
throws GnmiCodecException {
Expand Down