use of org.opendaylight.yangtools.yang.common.XMLNamespace in project netconf by opendaylight.
the class RestconfDocumentedExceptionMapper method toJsonResponseBody.
private static Object toJsonResponseBody(final NormalizedNodeContext errorsNode, final DataNodeContainer errorsSchemaNode) {
final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
NormalizedNode data = errorsNode.getData();
final InstanceIdentifierContext<?> context = errorsNode.getInstanceIdentifierContext();
final DataSchemaNode schema = (DataSchemaNode) context.getSchemaNode();
final OutputStreamWriter outputWriter = new OutputStreamWriter(outStream, StandardCharsets.UTF_8);
if (data == null) {
throw new RestconfDocumentedException(Response.Status.NOT_FOUND);
}
boolean isDataRoot = false;
XMLNamespace initialNs = null;
SchemaPath path;
if (context.getSchemaNode() instanceof SchemaContext) {
isDataRoot = true;
path = SchemaPath.ROOT;
} else {
final List<QName> qNames = context.getInstanceIdentifier().getPathArguments().stream().filter(arg -> !(arg instanceof NodeIdentifierWithPredicates)).filter(arg -> !(arg instanceof AugmentationIdentifier)).map(PathArgument::getNodeType).collect(Collectors.toList());
path = SchemaPath.of(Absolute.of(qNames)).getParent();
}
if (!schema.isAugmenting() && !(schema instanceof SchemaContext)) {
initialNs = schema.getQName().getNamespace();
}
final JsonWriter jsonWriter = JsonWriterFactory.createJsonWriter(outputWriter);
final NormalizedNodeStreamWriter jsonStreamWriter = JSONNormalizedNodeStreamWriter.createExclusiveWriter(JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(context.getSchemaContext()), path, initialNs, jsonWriter);
// We create a delegating writer to special-case error-info as error-info is defined as an empty
// container in the restconf yang schema but we create a leaf node so we can output it. The delegate
// stream writer validates the node type against the schema and thus will expect a LeafSchemaNode but
// the schema has a ContainerSchemaNode so, to avoid an error, we override the leafNode behavior
// for error-info.
final NormalizedNodeStreamWriter streamWriter = new ForwardingNormalizedNodeStreamWriter() {
private boolean inOurLeaf;
@Override
protected NormalizedNodeStreamWriter delegate() {
return jsonStreamWriter;
}
@Override
public void startLeafNode(final NodeIdentifier name) throws IOException {
if (name.getNodeType().equals(RestConfModule.ERROR_INFO_QNAME)) {
inOurLeaf = true;
jsonWriter.name(RestConfModule.ERROR_INFO_QNAME.getLocalName());
} else {
super.startLeafNode(name);
}
}
@Override
public void scalarValue(final Object value) throws IOException {
if (inOurLeaf) {
jsonWriter.value(value.toString());
} else {
super.scalarValue(value);
}
}
@Override
public void endNode() throws IOException {
if (inOurLeaf) {
inOurLeaf = false;
} else {
super.endNode();
}
}
};
final NormalizedNodeWriter nnWriter = NormalizedNodeWriter.forStreamWriter(streamWriter);
try {
if (isDataRoot) {
writeDataRoot(outputWriter, nnWriter, (ContainerNode) data);
} else {
if (data instanceof MapEntryNode) {
data = ImmutableNodes.mapNodeBuilder(data.getIdentifier().getNodeType()).withChild((MapEntryNode) data).build();
}
nnWriter.write(data);
}
nnWriter.flush();
outputWriter.flush();
} catch (final IOException e) {
LOG.warn("Error writing error response body", e);
}
try {
streamWriter.close();
} catch (IOException e) {
LOG.warn("Failed to close stream writer", e);
}
return outStream.toString(StandardCharsets.UTF_8);
}
use of org.opendaylight.yangtools.yang.common.XMLNamespace in project netconf by opendaylight.
the class NormalizedNodeJsonBodyWriter method createNormalizedNodeWriter.
private static RestconfNormalizedNodeWriter createNormalizedNodeWriter(final InstanceIdentifierContext<SchemaNode> context, final SchemaPath path, final JsonWriter jsonWriter, @Nullable final Integer depth) {
final SchemaNode schema = context.getSchemaNode();
final JSONCodecFactory codecs = getCodecFactory(context);
final XMLNamespace initialNs;
if (schema instanceof DataSchemaNode && !((DataSchemaNode) schema).isAugmenting() && !(schema instanceof SchemaContext) || schema instanceof RpcDefinition) {
initialNs = schema.getQName().getNamespace();
} else {
initialNs = null;
}
final NormalizedNodeStreamWriter streamWriter = JSONNormalizedNodeStreamWriter.createNestedWriter(codecs, path, initialNs, jsonWriter);
if (depth != null) {
return DepthAwareNormalizedNodeWriter.forStreamWriter(streamWriter, depth);
}
return RestconfDelegatingNormalizedNodeWriter.forStreamWriter(streamWriter);
}
use of org.opendaylight.yangtools.yang.common.XMLNamespace in project netconf by opendaylight.
the class RuntimeRpc method handleWithNoSubsequentOperations.
@Override
protected Element handleWithNoSubsequentOperations(final Document document, final XmlElement operationElement) throws DocumentedException {
final String netconfOperationName = operationElement.getName();
final String netconfOperationNamespace;
try {
netconfOperationNamespace = operationElement.getNamespace();
} catch (final DocumentedException e) {
LOG.debug("Cannot retrieve netconf operation namespace from message due to ", e);
throw new DocumentedException("Cannot retrieve netconf operation namespace from message", e, ErrorType.PROTOCOL, ErrorTag.UNKNOWN_NAMESPACE, ErrorSeverity.ERROR);
}
final XMLNamespace namespaceURI = createNsUri(netconfOperationNamespace);
final Optional<? extends Module> moduleOptional = getModule(namespaceURI);
if (moduleOptional.isEmpty()) {
throw new DocumentedException("Unable to find module in Schema Context with namespace and name : " + namespaceURI + " " + netconfOperationName + schemaContext.getCurrentContext(), ErrorType.APPLICATION, ErrorTag.BAD_ELEMENT, ErrorSeverity.ERROR);
}
final Optional<RpcDefinition> rpcDefinitionOptional = getRpcDefinitionFromModule(moduleOptional.get(), namespaceURI, netconfOperationName);
if (rpcDefinitionOptional.isEmpty()) {
throw new DocumentedException("Unable to find RpcDefinition with namespace and name : " + namespaceURI + " " + netconfOperationName, ErrorType.APPLICATION, ErrorTag.BAD_ELEMENT, ErrorSeverity.ERROR);
}
final RpcDefinition rpcDefinition = rpcDefinitionOptional.get();
final ContainerNode inputNode = rpcToNNode(operationElement, rpcDefinition);
final DOMRpcResult result;
try {
result = rpcService.invokeRpc(rpcDefinition.getQName(), inputNode).get();
} catch (final InterruptedException | ExecutionException e) {
throw DocumentedException.wrap(e);
}
if (result.getResult() == null) {
return XmlUtil.createElement(document, XmlNetconfConstants.OK, Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
}
return transformNormalizedNode(document, result.getResult(), Absolute.of(rpcDefinition.getQName(), rpcDefinition.getOutput().getQName()));
}
use of org.opendaylight.yangtools.yang.common.XMLNamespace in project netconf by opendaylight.
the class RestDocgenUtil method resolveFullNameFromNode.
private static String resolveFullNameFromNode(final QName node, final SchemaContext schemaContext) {
final XMLNamespace namespace = node.getNamespace();
final Optional<Revision> revision = node.getRevision();
final Map<Optional<Revision>, Module> revisionToModule = NAMESPACE_AND_REVISION_TO_MODULE.computeIfAbsent(namespace, k -> new HashMap<>());
final Module module = revisionToModule.computeIfAbsent(revision, k -> schemaContext.findModule(namespace, k).orElse(null));
if (module != null) {
return module.getName() + ":" + node.getLocalName();
}
return node.getLocalName();
}
use of org.opendaylight.yangtools.yang.common.XMLNamespace in project controller by opendaylight.
the class ConfigurationImplTest method testAddModuleShardConfiguration.
@Test
public void testAddModuleShardConfiguration() throws Exception {
XMLNamespace namespace = XMLNamespace.of("urn:opendaylight:test:oven");
String moduleName = "oven";
String shardName = "oven-shard";
String shardStrategyName = ModuleShardStrategy.NAME;
Collection<MemberName> shardMemberNames = ImmutableSortedSet.of(MEMBER_1, MEMBER_4, MEMBER_5);
configuration.addModuleShardConfiguration(new ModuleShardConfiguration(namespace, moduleName, shardName, shardStrategyName, shardMemberNames));
assertEquals("getMemberShardNames", ImmutableSortedSet.of("people-1", "cars-1", "test-1", "default", shardName), ImmutableSortedSet.copyOf(configuration.getMemberShardNames(MEMBER_1)));
assertEquals("getMemberShardNames", ImmutableSortedSet.of(shardName), ImmutableSortedSet.copyOf(configuration.getMemberShardNames(MEMBER_4)));
assertEquals("getMemberShardNames", ImmutableSortedSet.of(shardName), ImmutableSortedSet.copyOf(configuration.getMemberShardNames(MEMBER_5)));
assertEquals("getMembersFromShardName", shardMemberNames, ImmutableSortedSet.copyOf(configuration.getMembersFromShardName(shardName)));
assertEquals("getShardNameForModule", shardName, configuration.getShardNameForModule(moduleName));
assertEquals("getModuleNameFromNameSpace", moduleName, configuration.getModuleNameFromNameSpace(namespace.toString()));
assertEquals("getAllShardNames", ImmutableSortedSet.of("people-1", "cars-1", "test-1", "default", shardName), ImmutableSortedSet.copyOf(configuration.getAllShardNames()));
ShardStrategy strategy = configuration.getStrategyForModule("cars");
assertNotNull("getStrategyForModule null", strategy);
assertEquals("getStrategyForModule type", ModuleShardStrategy.class, strategy.getClass());
}
Aggregations