Do not use SchemaPath for error-info writeout
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / errors / StreamWriterWithDisabledValidation.java
1 /*
2  * Copyright © 2019 FRINX s.r.o. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.errors;
9
10 import java.io.IOException;
11 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
12 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.Errors;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.stream.ForwardingNormalizedNodeStreamWriter;
17 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
18 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
19
20 /**
21  * Created delegating writer to special-case error-info as error-info is defined as an empty container in the restconf
22  * yang schema but we create a leaf node so we can output it. The delegate stream writer validates the node type against
23  * the schema and thus will expect a LeafSchemaNode but the schema has a ContainerSchemaNode so, to avoid an error,
24  * we override the leafNode behavior for error-info.
25  */
26 abstract class StreamWriterWithDisabledValidation extends ForwardingNormalizedNodeStreamWriter {
27     private final QName excludedQName;
28
29     private boolean inOurLeaf;
30
31     /**
32      * Creation of the {@link NormalizedNode} stream-writer with {@link QName} that is excluded from type-check.
33      *
34      * @param excludedQName QName of the element that is excluded from type-check.
35      */
36     StreamWriterWithDisabledValidation(final QName excludedQName) {
37         this.excludedQName = excludedQName;
38     }
39
40     @Override
41     public final void startLeafNode(final NodeIdentifier name) throws IOException {
42         if (name.getNodeType().equals(excludedQName)) {
43             inOurLeaf = true;
44             startLeafNodeWithDisabledValidation(name);
45         } else {
46             super.startLeafNode(name);
47         }
48     }
49
50     /**
51      * Writing of the excluded leaf to the output stream.
52      *
53      * @param nodeIdentifier Node identifier of the leaf to be written to output stream.
54      * @throws IOException Writing of the leaf to output stream failed.
55      */
56     abstract void startLeafNodeWithDisabledValidation(NodeIdentifier nodeIdentifier) throws IOException;
57
58     @Override
59     public final void scalarValue(final Object value) throws IOException {
60         if (inOurLeaf) {
61             scalarValueWithDisabledValidation(value);
62         } else {
63             super.scalarValue(value);
64         }
65     }
66
67     /**
68      * Writing of the value of the excluded leaf to the output stream.
69      *
70      * @param value Value of the excluded leaf.
71      * @throws IOException Writing of the leaf value to the output stream failed.
72      */
73     abstract void scalarValueWithDisabledValidation(Object value) throws IOException;
74
75     @Override
76     public final void endNode() throws IOException {
77         if (inOurLeaf) {
78             inOurLeaf = false;
79             endNodeWithDisabledValidation();
80         } else {
81             super.endNode();
82         }
83     }
84
85     /**
86      * Writing of the end element with disabled validation.
87      *
88      * @throws IOException Writing of the end element to the output stream failed.
89      */
90     abstract void endNodeWithDisabledValidation() throws IOException;
91
92     static final Inference errorsContainerInference(final SchemaContextHandler schemaContextHandler) {
93         final var stack = SchemaInferenceStack.of(schemaContextHandler.get());
94         stack.enterGrouping(Errors.QNAME);
95         return stack.toInference();
96     }
97 }