blob: d332ab94ee3089e97dc978ca1b06e59de7f03a8e [file] [log] [blame]
Vidyashree Rama25bf4d02016-03-29 14:37:02 +05301/*
2 * Copyright 2016 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yangutils.parser.impl.listeners;
18
19import java.util.List;
20import org.onosproject.yangutils.datamodel.YangAugment;
21import org.onosproject.yangutils.datamodel.YangNode;
22import org.onosproject.yangutils.datamodel.YangModule;
23import org.onosproject.yangutils.datamodel.YangSubModule;
24import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
25import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
26import org.onosproject.yangutils.parser.Parsable;
27import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
28import org.onosproject.yangutils.parser.exceptions.ParserException;
29import org.onosproject.yangutils.parser.impl.TreeWalkListener;
30
31import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
32import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangAugmentNode;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
34import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
35import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
36import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
37import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
38import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
39import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
40import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
41import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
42import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
43import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
44import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateMutuallyExclusiveChilds;
45import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidAbsoluteSchemaNodeId;
46import static org.onosproject.yangutils.utils.YangConstructType.AUGMENT_DATA;
47import static org.onosproject.yangutils.utils.YangConstructType.DATA_DEF_DATA;
48import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
49import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
50import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
51import static org.onosproject.yangutils.utils.YangConstructType.WHEN_DATA;
52import static org.onosproject.yangutils.utils.YangConstructType.CASE_DATA;
53
54/*
55 * Reference: RFC6020 and YANG ANTLR Grammar
56 *
57 * ABNF grammar as per RFC6020
58 * augment-stmt = augment-keyword sep augment-arg-str optsep
59 * "{" stmtsep
60 * ;; these stmts can appear in any order
61 * [when-stmt stmtsep]
62 * *(if-feature-stmt stmtsep)
63 * [status-stmt stmtsep]
64 * [description-stmt stmtsep]
65 * [reference-stmt stmtsep]
66 * 1*((data-def-stmt stmtsep) /
67 * (case-stmt stmtsep))
68 * "}"
69 *
70 * ANTLR grammar rule
71 * augmentStatement : AUGMENT_KEYWORD augment LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | statusStatement
72 * | descriptionStatement | referenceStatement | dataDefStatement | caseStatement)* RIGHT_CURLY_BRACE;
73 */
74
75/**
76 * Implements listener based call back function corresponding to the "augment"
77 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
78 */
79public final class AugmentListener {
80
81 /**
82 * Creates a new augment listener.
83 */
84 private AugmentListener() {
85 }
86
87 /**
88 * It is called when parser receives an input matching the grammar rule
89 * (augment), performs validation and updates the data model tree.
90 *
91 * @param listener listener's object
92 * @param ctx context object of the grammar rule
93 */
94 public static void processAugmentEntry(TreeWalkListener listener,
95 GeneratedYangParser.AugmentStatementContext ctx) {
96
97 // Check for stack to be non empty.
98 checkStackIsNotEmpty(listener, MISSING_HOLDER, AUGMENT_DATA, ctx.augment().getText(), ENTRY);
99
100 // Validate augment argument string
101 List<YangNodeIdentifier> targetNodes = getValidAbsoluteSchemaNodeId(ctx.augment().getText(),
102 AUGMENT_DATA, ctx);
103
104 // Validate sub statement cardinality.
105 validateSubStatementsCardinality(ctx);
106
107 // Check for identifier collision
108 int line = ctx.getStart().getLine();
109 int charPositionInLine = ctx.getStart().getCharPositionInLine();
110 detectCollidingChildUtil(listener, line, charPositionInLine, "", AUGMENT_DATA);
111
112 Parsable curData = listener.getParsedDataStack().peek();
113 if (curData instanceof YangModule || curData instanceof YangSubModule) {
114
115 YangNode curNode = (YangNode) curData;
116 YangAugment yangAugment = getYangAugmentNode(JAVA_GENERATION);
117 yangAugment.setTargetNode(targetNodes);
118 try {
119 curNode.addChild(yangAugment);
120 } catch (DataModelException e) {
121 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
122 AUGMENT_DATA, ctx.augment().getText(), ENTRY, e.getMessage()));
123 }
124 listener.getParsedDataStack().push(yangAugment);
125 } else {
126 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, AUGMENT_DATA,
127 ctx.augment().getText(), ENTRY));
128 }
129
130 }
131
132 /**
133 * It is called when parser exits from grammar rule (augment), it perform
134 * validations and updates the data model tree.
135 *
136 * @param listener listener's object
137 * @param ctx context object of the grammar rule
138 */
139 public static void processAugmentExit(TreeWalkListener listener,
140 GeneratedYangParser.AugmentStatementContext ctx) {
141
142 //Check for stack to be non empty.
143 checkStackIsNotEmpty(listener, MISSING_HOLDER, AUGMENT_DATA, ctx.augment().getText(), EXIT);
144
145 if (!(listener.getParsedDataStack().peek() instanceof YangAugment)) {
146 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, AUGMENT_DATA,
147 ctx.augment().getText(), EXIT));
148 }
149 listener.getParsedDataStack().pop();
150 }
151
152 /**
153 * Validates the cardinality of augment sub-statements as per grammar.
154 *
155 * @param ctx context object of the grammar rule
156 */
157 private static void validateSubStatementsCardinality(GeneratedYangParser.AugmentStatementContext ctx) {
158
159 validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, AUGMENT_DATA, ctx.augment().getText());
160 validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, AUGMENT_DATA, ctx.augment().getText());
161 validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, AUGMENT_DATA, ctx.augment().getText());
162 validateCardinalityMaxOne(ctx.whenStatement(), WHEN_DATA, AUGMENT_DATA, ctx.augment().getText());
163 validateMutuallyExclusiveChilds(ctx.dataDefStatement(), DATA_DEF_DATA, ctx.caseStatement(),
164 CASE_DATA, AUGMENT_DATA, ctx.augment().getText());
165 }
166}