Bug 3874: Support of yang modeled AnyXML - XML deserialization
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / YangStatementParserListenerImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  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.yangtools.yang.parser.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Verify;
12 import java.util.ArrayList;
13 import java.util.List;
14 import javax.annotation.concurrent.Immutable;
15 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.ArgumentContext;
16 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.KeywordContext;
17 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParser.StatementContext;
18 import org.opendaylight.yangtools.antlrv4.code.gen.YangStatementParserBaseListener;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.YangConstants;
21 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
23 import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
24 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
25 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
28 import org.opendaylight.yangtools.yang.parser.spi.source.StatementWriter;
29 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
30 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 @Immutable
35 public class YangStatementParserListenerImpl extends YangStatementParserBaseListener {
36     private static final Logger LOG = LoggerFactory.getLogger(YangStatementParserListenerImpl.class);
37
38     private final List<String> toBeSkipped = new ArrayList<>();
39     private final String sourceName;
40     private QNameToStatementDefinition stmtDef;
41     private PrefixToModule prefixes;
42     private StatementWriter writer;
43
44     public YangStatementParserListenerImpl(final String sourceName) {
45         this.sourceName = sourceName;
46     }
47
48     public void setAttributes(final StatementWriter writer, final QNameToStatementDefinition stmtDef) {
49         this.writer = writer;
50         this.stmtDef = stmtDef;
51     }
52
53     public void setAttributes(final StatementWriter writer, final QNameToStatementDefinition stmtDef, final PrefixToModule prefixes) {
54         this.writer = writer;
55         this.stmtDef = stmtDef;
56         this.prefixes = prefixes;
57     }
58
59     @Override
60     public void enterStatement(final StatementContext ctx) {
61         final StatementSourceReference ref = DeclarationInTextSource.atPosition(sourceName, ctx
62                 .getStart().getLine(), ctx.getStart().getCharPositionInLine());
63         final KeywordContext keywordCtx = Verify.verifyNotNull(ctx.getChild(KeywordContext.class, 0));
64         final ArgumentContext argumentCtx = ctx.getChild(ArgumentContext.class, 0);
65         final String keywordTxt = keywordCtx.getText();
66
67         final QName identifier = QName.create(YangConstants.RFC6020_YIN_MODULE, keywordTxt);
68         final QName validStatementDefinition = Utils.getValidStatementDefinition(prefixes, stmtDef, identifier);
69         if (stmtDef != null && validStatementDefinition != null && toBeSkipped.isEmpty()) {
70             final String argument = argumentCtx != null ? Utils.stringFromStringContext(argumentCtx) : null;
71             // FIXME: Refactor/clean up this special case
72             if (identifier.equals(Rfc6020Mapping.TYPE.getStatementName())) {
73                 Preconditions.checkArgument(argument != null);
74                 if (TypeUtils.isYangTypeBodyStmtString(argument)) {
75                     writer.startStatement(QName.create(YangConstants.RFC6020_YIN_MODULE, argument), ref);
76                 } else {
77                     writer.startStatement(QName.create(YangConstants.RFC6020_YIN_MODULE, Rfc6020Mapping
78                         .TYPE.getStatementName().getLocalName()), ref);
79                 }
80                 writer.argumentValue(argument, ref);
81             } else {
82                 writer.startStatement(validStatementDefinition, ref);
83                 if (argument != null) {
84                     writer.argumentValue(argument, ref);
85                 }
86             }
87         } else {
88             Preconditions.checkArgument(writer.getPhase() != ModelProcessingPhase.FULL_DECLARATION,
89                     "%s is not a YANG statement or use of extension. Source: %s", identifier.getLocalName(), ref);
90             toBeSkipped.add(keywordTxt);
91         }
92     }
93
94     @Override
95     public void exitStatement(final StatementContext ctx) {
96         final StatementSourceReference ref = DeclarationInTextSource.atPosition(
97             sourceName, ctx.getStart().getLine(), ctx.getStart().getCharPositionInLine());
98
99         try {
100             KeywordContext keyword = ctx.getChild(KeywordContext.class, 0);
101             String statementName = keyword.getText();
102             QName identifier = QName.create(YangConstants.RFC6020_YIN_MODULE, statementName);
103             if (stmtDef != null && Utils.getValidStatementDefinition(prefixes, stmtDef, identifier) != null
104                     && toBeSkipped.isEmpty()) {
105                 writer.endStatement(ref);
106             }
107
108             // No-op if the statement is not on the list
109             toBeSkipped.remove(statementName);
110         } catch (SourceException e) {
111             LOG.warn(e.getMessage(), e);
112         }
113     }
114 }