c5c6568890702c59fe365801e872f6189f2913d4
[netconf.git] / restconf / restconf-nb-rfc8040 / 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.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
13 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
14 import org.opendaylight.yangtools.yang.data.api.schema.stream.ForwardingNormalizedNodeStreamWriter;
15
16 /**
17  * Created delegating writer to special-case error-info as error-info is defined as an empty container in the restconf
18  * yang schema but we create a leaf node so we can output it. The delegate stream writer validates the node type against
19  * the schema and thus will expect a LeafSchemaNode but the schema has a ContainerSchemaNode so, to avoid an error,
20  * we override the leafNode behavior for error-info.
21  */
22 abstract class StreamWriterWithDisabledValidation extends ForwardingNormalizedNodeStreamWriter {
23
24     private final QName excludedQName;
25     private boolean inOurLeaf;
26
27     /**
28      * Creation of the {@link NormalizedNode} stream-writer with {@link QName} that is excluded from type-check.
29      *
30      * @param excludedQName QName of the element that is excluded from type-check.
31      */
32     StreamWriterWithDisabledValidation(final QName excludedQName) {
33         this.excludedQName = excludedQName;
34     }
35
36     @Override
37     public void startLeafNode(final NodeIdentifier name) throws IOException {
38         if (name.getNodeType().equals(excludedQName)) {
39             inOurLeaf = true;
40             startLeafNodeWithDisabledValidation(name);
41         } else {
42             super.startLeafNode(name);
43         }
44     }
45
46     /**
47      * Writing of the excluded leaf to the output stream.
48      *
49      * @param nodeIdentifier Node identifier of the leaf to be written to output stream.
50      * @throws IOException Writing of the leaf to output stream failed.
51      */
52     abstract void startLeafNodeWithDisabledValidation(NodeIdentifier nodeIdentifier) throws IOException;
53
54     @Override
55     public void scalarValue(final Object value) throws IOException {
56         if (inOurLeaf) {
57             scalarValueWithDisabledValidation(value);
58         } else {
59             super.scalarValue(value);
60         }
61     }
62
63     /**
64      * Writing of the value of the excluded leaf to the output stream.
65      *
66      * @param value Value of the excluded leaf.
67      * @throws IOException Writing of the leaf value to the output stream failed.
68      */
69     abstract void scalarValueWithDisabledValidation(Object value) throws IOException;
70
71     @Override
72     public void endNode() throws IOException {
73         if (inOurLeaf) {
74             inOurLeaf = false;
75             endNodeWithDisabledValidation();
76         } else {
77             super.endNode();
78         }
79     }
80
81     /**
82      * Writing of the end element with disabled validation.
83      *
84      * @throws IOException Writing of the end element to the output stream failed.
85      */
86     abstract void endNodeWithDisabledValidation() throws IOException;
87 }