blob: 332ae4f3562d7bb05ea346b272503c2765a839f0 [file] [log] [blame]
Shankara-Huawei234cd092016-07-14 11:35:34 +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 */
16package org.onosproject.yangutils.parser.impl.listeners;
17
18import org.onosproject.yangutils.datamodel.YangIdentity;
19import org.onosproject.yangutils.datamodel.YangModule;
20import org.onosproject.yangutils.datamodel.YangNode;
21import org.onosproject.yangutils.datamodel.YangSubModule;
22import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
23import org.onosproject.yangutils.datamodel.utils.Parsable;
24import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
25import org.onosproject.yangutils.parser.exceptions.ParserException;
26import org.onosproject.yangutils.parser.impl.TreeWalkListener;
27
28import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
29import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangIdentityNode;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
34import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
35import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
36import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
37import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
38import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
39import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
40import static org.onosproject.yangutils.datamodel.utils.YangConstructType.IDENTITY_DATA;
41
42/**
43 * Reference: RFC6020 and YANG ANTLR Grammar.
44 *
45 * ABNF grammar as per RFC6020
46 * identity-stmt = identity-keyword sep identifier-arg-str optsep
47 * (";" /
48 * "{" stmtsep
49 * ;; these stmts can appear in any order
50 * [base-stmt stmtsep]
51 * [status-stmt stmtsep]
52 * [description-stmt stmtsep]
53 * [reference-stmt stmtsep]
54 * "}")
55 */
56
57/**
58 * Represents listener based call back function corresponding to the "identity"
59 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
60 */
61public final class IdentityListener {
62
63 //Creates a identity listener.
64 private IdentityListener() {
65 }
66
67 /**
68 * Performs validations and update the data model tree when parser receives an input
69 * matching the grammar rule (identity).
70 *
71 * @param listener Listener's object
72 * @param ctx context object of the grammar rule
73 */
74 public static void processIdentityEntry(TreeWalkListener listener,
75 GeneratedYangParser.IdentityStatementContext ctx) {
76
77 // Check for stack to be non empty.
78 checkStackIsNotEmpty(listener, MISSING_HOLDER, IDENTITY_DATA, ctx.identifier().getText(), ENTRY);
79
80 String identifier = getValidIdentifier(ctx.identifier().getText(), IDENTITY_DATA, ctx);
81
82 YangIdentity identity = getYangIdentityNode(JAVA_GENERATION);
83 identity.setName(identifier);
84
Bharat saraswale3175d32016-08-31 17:50:11 +053085 identity.setLineNumber(ctx.getStart().getLine());
86 identity.setCharPosition(ctx.getStart().getCharPositionInLine());
87 identity.setFileName(listener.getFileName());
Shankara-Huawei234cd092016-07-14 11:35:34 +053088 Parsable curData = listener.getParsedDataStack().peek();
89 if (curData instanceof YangModule || curData instanceof YangSubModule) {
90 YangNode curNode = (YangNode) curData;
91 try {
92 curNode.addChild(identity);
93 } catch (DataModelException e) {
94 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
95 IDENTITY_DATA, ctx.identifier().getText(), ENTRY, e.getMessage()));
96 }
97 // Push identity node to the stack.
98 listener.getParsedDataStack().push(identity);
99 } else {
100 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, IDENTITY_DATA,
101 ctx.identifier().getText(), ENTRY));
102 }
103
104 }
105
106 /**
107 * Performs validations and update the data model tree when parser exits from grammar
108 * rule (identity).
109 *
110 * @param listener Listener's object
111 * @param ctx context object of the grammar rule
112 */
113 public static void processIdentityExit(TreeWalkListener listener,
VinodKumarS-Huawei8f164222016-08-31 15:47:30 +0530114 GeneratedYangParser.IdentityStatementContext ctx) {
Shankara-Huawei234cd092016-07-14 11:35:34 +0530115
116 // Check for stack to be non empty.
117 checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, IDENTITY_DATA, ctx.identifier().getText(), EXIT);
118
119 Parsable parsableType = listener.getParsedDataStack().pop();
120 if (!(parsableType instanceof YangIdentity)) {
121 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, IDENTITY_DATA,
122 ctx.identifier().getText(), EXIT));
123 }
124 }
125
126}