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