blob: d8dec2c189b310abafa110d951de0f882a07061a [file] [log] [blame]
Shankara-Huaweidf7b9ca2016-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
85 Parsable curData = listener.getParsedDataStack().peek();
86 if (curData instanceof YangModule || curData instanceof YangSubModule) {
87 YangNode curNode = (YangNode) curData;
88 try {
89 curNode.addChild(identity);
90 } catch (DataModelException e) {
91 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
92 IDENTITY_DATA, ctx.identifier().getText(), ENTRY, e.getMessage()));
93 }
94 // Push identity node to the stack.
95 listener.getParsedDataStack().push(identity);
96 } else {
97 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, IDENTITY_DATA,
98 ctx.identifier().getText(), ENTRY));
99 }
100
101 }
102
103 /**
104 * Performs validations and update the data model tree when parser exits from grammar
105 * rule (identity).
106 *
107 * @param listener Listener's object
108 * @param ctx context object of the grammar rule
109 */
110 public static void processIdentityExit(TreeWalkListener listener,
111 GeneratedYangParser.IdentityStatementContext ctx) {
112
113 // Check for stack to be non empty.
114 checkStackIsNotEmpty(listener, MISSING_CURRENT_HOLDER, IDENTITY_DATA, ctx.identifier().getText(), EXIT);
115
116 Parsable parsableType = listener.getParsedDataStack().pop();
117 if (!(parsableType instanceof YangIdentity)) {
118 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, IDENTITY_DATA,
119 ctx.identifier().getText(), EXIT));
120 }
121 }
122
123}