blob: 29899de986985edb248e13636ae5d8c175dbb0ac [file] [log] [blame]
Vidyashree Ramadeac28b2016-06-20 15:12:43 +05301/*
2 * Copyright 2016-present 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
19/*
20 * Reference: RFC6020 and YANG ANTLR Grammar
21 *
22 * ABNF grammar as per RFC6020
23 *
24 * if-feature-stmt = if-feature-keyword sep identifier-ref-arg-str
25 * optsep stmtend
26 *
27 * ANTLR grammar rule
28 * ifFeatureStatement : IF_FEATURE_KEYWORD string STMTEND;
29 */
30
31import org.onosproject.yangutils.datamodel.YangFeature;
32import org.onosproject.yangutils.datamodel.YangIfFeature;
33import org.onosproject.yangutils.datamodel.YangIfFeatureHolder;
34import org.onosproject.yangutils.datamodel.YangLeaf;
35import org.onosproject.yangutils.datamodel.YangLeafList;
36import org.onosproject.yangutils.datamodel.YangNode;
37import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
38import org.onosproject.yangutils.datamodel.YangResolutionInfo;
39import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
40import org.onosproject.yangutils.datamodel.utils.Parsable;
41import org.onosproject.yangutils.linker.impl.YangResolutionInfoImpl;
42import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
43import org.onosproject.yangutils.parser.exceptions.ParserException;
44import org.onosproject.yangutils.parser.impl.TreeWalkListener;
45
46import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.addResolutionInfo;
47import static org.onosproject.yangutils.datamodel.utils.ResolvableStatus.UNRESOLVED;
48import static org.onosproject.yangutils.datamodel.utils.YangConstructType.IF_FEATURE_DATA;
49import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
50import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
51import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
52import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
53import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
54import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
55import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
56import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidNodeIdentifier;
57import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
58
59/**
60 * Represents listener based call back function corresponding to the "if-feature"
61 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
62 */
63public final class IfFeatureListener {
64
65 /**
66 * Creates a new IfFeature listener.
67 */
68 private IfFeatureListener() {
69 }
70
71 /**
72 * Performs validation and updates the data model tree.It is called when parser receives
73 * an input matching the grammar rule (if-feature).
74 *
75 * @param listener listener's object
76 * @param ctx context object of the grammar rule
77 */
78 public static void processIfFeatureEntry(TreeWalkListener listener,
79 GeneratedYangParser.IfFeatureStatementContext ctx) {
80 // Check for stack to be non empty.
81 checkStackIsNotEmpty(listener, MISSING_HOLDER, IF_FEATURE_DATA, ctx.string().getText(), ENTRY);
82
83 // Validate if-feature argument string
84 YangNodeIdentifier nodeIdentifier = getValidNodeIdentifier(ctx.string().getText(),
85 IF_FEATURE_DATA, ctx);
86
87 YangIfFeature ifFeature = new YangIfFeature();
88 ifFeature.setName(nodeIdentifier);
89 ifFeature.setResolvableStatus(UNRESOLVED);
90 YangIfFeatureHolder ifFeatureHolder;
91
92 // Obtain the node of the stack.
93 Parsable tmpNode = listener.getParsedDataStack().peek();
94 if (tmpNode instanceof YangIfFeatureHolder) {
95 ifFeatureHolder = (YangIfFeatureHolder) tmpNode;
96 ifFeatureHolder.addIfFeatureList(ifFeature);
97 } else {
98 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, IF_FEATURE_DATA,
99 ctx.string().getText(), ENTRY));
100 }
101
102 // Add resolution information to the list
103 Parsable parentNode;
104 if (tmpNode instanceof YangLeafList || tmpNode instanceof YangLeaf
105 || tmpNode instanceof YangFeature) {
106 Parsable leafData = listener.getParsedDataStack().pop();
107 parentNode = listener.getParsedDataStack().peek();
108 listener.getParsedDataStack().push(leafData);
109 } else {
110 parentNode = tmpNode;
111 }
112
113 // Verify parent node of leaf
114 if (!(parentNode instanceof YangNode)) {
115 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, IF_FEATURE_DATA,
116 ctx.string().getText(), EXIT));
117 }
118
119 int errorLine = ctx.getStart().getLine();
120 int errorPosition = ctx.getStart().getCharPositionInLine();
121 YangResolutionInfoImpl resolutionInfo = new YangResolutionInfoImpl<YangIfFeature>(ifFeature,
122 (YangNode) parentNode, errorLine,
123 errorPosition);
124 addToResolutionList(resolutionInfo, ctx);
125 }
126
127 /**
128 * Add to resolution list.
129 *
130 * @param resolutionInfo resolution information.
131 * @param ctx context object of the grammar rule
132 */
133 private static void addToResolutionList(YangResolutionInfo<YangIfFeature> resolutionInfo,
134 GeneratedYangParser.IfFeatureStatementContext ctx) {
135
136 try {
137 addResolutionInfo(resolutionInfo);
138 } catch (DataModelException e) {
139 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
140 IF_FEATURE_DATA, ctx.string().getText(), EXIT, e.getMessage()));
141 }
142 }
143}