blob: d8abb31f1ceb88e579a29ae6f26a587dd5812bd9 [file] [log] [blame]
Vidyashree Rama918f1622016-07-28 17:33:15 +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
Vidyashree Ramab3670472016-08-06 15:49:56 +053019import java.util.List;
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +053020
Vidyashree Ramab3670472016-08-06 15:49:56 +053021import org.onosproject.yangutils.datamodel.YangAtomicPath;
Vidyashree Rama918f1622016-07-28 17:33:15 +053022import org.onosproject.yangutils.datamodel.YangCompilerAnnotation;
23import org.onosproject.yangutils.datamodel.YangModule;
Vidyashree Ramab3670472016-08-06 15:49:56 +053024import org.onosproject.yangutils.datamodel.YangNode;
Vidyashree Rama918f1622016-07-28 17:33:15 +053025import org.onosproject.yangutils.datamodel.YangSubModule;
Vidyashree Ramab3670472016-08-06 15:49:56 +053026import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Vidyashree Rama918f1622016-07-28 17:33:15 +053027import org.onosproject.yangutils.datamodel.utils.Parsable;
Vidyashree Ramab3670472016-08-06 15:49:56 +053028import org.onosproject.yangutils.linker.impl.YangResolutionInfoImpl;
Vidyashree Rama918f1622016-07-28 17:33:15 +053029import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
30import org.onosproject.yangutils.parser.exceptions.ParserException;
31import org.onosproject.yangutils.parser.impl.TreeWalkListener;
32
Vidyashree Ramab3670472016-08-06 15:49:56 +053033import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.addResolutionInfo;
Vidyashree Rama918f1622016-07-28 17:33:15 +053034import static org.onosproject.yangutils.datamodel.utils.YangConstructType.COMPILER_ANNOTATION_DATA;
35import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
36import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
Vidyashree Ramab3670472016-08-06 15:49:56 +053037import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
Vidyashree Rama918f1622016-07-28 17:33:15 +053038import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Vidyashree Rama918f1622016-07-28 17:33:15 +053039import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Vidyashree Ramab3670472016-08-06 15:49:56 +053040import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
41import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
42import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
43import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidAbsoluteSchemaNodeId;
Vidyashree Rama918f1622016-07-28 17:33:15 +053044import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidPrefix;
45import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat;
46import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
47
48/*
49 * Reference: RFC6020 and YANG ANTLR Grammar
50 *
51 * ABNF grammar as per RFC6020
52 * compiler-annotation-stmt = prefix:compiler-annotation-keyword string
53 * "{"
54 * [app-data-structure-stmt stmtsep]
55 * [app-extended-stmt stmtsep]
56 * "}"
57 *
58 * ANTLR grammar rule
59 * compilerAnnotationStatement : COMPILER_ANNOTATION string LEFT_CURLY_BRACE
60 * compilerAnnotationBodyStatement RIGHT_CURLY_BRACE;
61 *
62 * compilerAnnotationBodyStatement : appDataStructureStatement? appExtendedStatement? ;
63 */
64
65/**
66 * Represents listener based call back function corresponding to the "compiler-annotation"
67 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
68 */
69public final class CompilerAnnotationListener {
70
71 /**
72 * Creates a new compiler-annotation listener.
73 */
74 private CompilerAnnotationListener() {
75 }
76
77 /**
78 * Performs validation and updates the data model tree. It is called when parser receives an
79 * input matching the grammar rule(compiler-annotation).
80 *
81 * @param listener listener's object
82 * @param ctx context object of the grammar rule
83 */
84 public static void processCompilerAnnotationEntry(TreeWalkListener listener,
85 GeneratedYangParser.CompilerAnnotationStatementContext ctx) {
86 // Check for stack to be non empty.
87 checkStackIsNotEmpty(listener, MISSING_HOLDER, COMPILER_ANNOTATION_DATA, ctx.string().getText(), ENTRY);
88 String prefix = getValidPrefix(ctx.COMPILER_ANNOTATION().getText(), COMPILER_ANNOTATION_DATA, ctx);
89
90 YangCompilerAnnotation compilerAnnotation = new YangCompilerAnnotation();
91 compilerAnnotation.setPrefix(prefix);
92 compilerAnnotation.setPath(removeQuotesAndHandleConcat(ctx.string().getText()));
93
Vidyashree Ramab3670472016-08-06 15:49:56 +053094 // Validate augment argument string
95 List<YangAtomicPath> targetNodes = getValidAbsoluteSchemaNodeId(ctx.string().getText(),
96 COMPILER_ANNOTATION_DATA, ctx);
97
98 compilerAnnotation.setAtomicPathList(targetNodes);
99
100 int line = ctx.getStart().getLine();
101 int charPositionInLine = ctx.getStart().getCharPositionInLine();
102
Vidyashree Rama918f1622016-07-28 17:33:15 +0530103 Parsable curData = listener.getParsedDataStack().peek();
Vidyashree Ramab3670472016-08-06 15:49:56 +0530104 if (!(curData instanceof YangModule || curData instanceof YangSubModule)) {
105 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, COMPILER_ANNOTATION_DATA,
106 ctx.string().getText(), ENTRY));
Vidyashree Rama918f1622016-07-28 17:33:15 +0530107 }
Vidyashree Ramab3670472016-08-06 15:49:56 +0530108
109 // Add resolution information to the list
110 YangResolutionInfoImpl resolutionInfo = new YangResolutionInfoImpl<YangCompilerAnnotation>(
111 compilerAnnotation, (YangNode) curData, line, charPositionInLine);
112 addToResolutionList(resolutionInfo, ctx);
113
Vidyashree Rama918f1622016-07-28 17:33:15 +0530114 listener.getParsedDataStack().push(compilerAnnotation);
115 }
116
117 /**
118 * Performs validation and updates the data model tree. It is called when parser
119 * exits from grammar rule (compiler-annotation).
120 *
121 * @param listener listener's object
122 * @param ctx context object of the grammar rule
123 */
124 public static void processCompilerAnnotationExit(TreeWalkListener listener,
125 GeneratedYangParser.CompilerAnnotationStatementContext ctx) {
126
127 checkStackIsNotEmpty(listener, MISSING_HOLDER, COMPILER_ANNOTATION_DATA, ctx.string().getText(), EXIT);
128 if (!(listener.getParsedDataStack().peek() instanceof YangCompilerAnnotation)) {
129 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, COMPILER_ANNOTATION_DATA,
130 ctx.string().getText(), EXIT));
131 }
132 listener.getParsedDataStack().pop();
133 }
Vidyashree Ramab3670472016-08-06 15:49:56 +0530134
135 /**
136 * Adds to resolution list.
137 *
138 * @param resolutionInfo resolution information.
139 * @param ctx context object of the grammar rule
140 */
141 private static void addToResolutionList(YangResolutionInfoImpl<YangCompilerAnnotation> resolutionInfo,
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +0530142 GeneratedYangParser.CompilerAnnotationStatementContext ctx) {
Vidyashree Ramab3670472016-08-06 15:49:56 +0530143
144 try {
145 addResolutionInfo(resolutionInfo);
146 } catch (DataModelException e) {
147 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
148 COMPILER_ANNOTATION_DATA, ctx.COMPILER_ANNOTATION().getText(), ENTRY, e.getMessage()));
149 }
150 }
Vidyashree Rama918f1622016-07-28 17:33:15 +0530151}