blob: 1925375842beb2a9d884bab69e08c1d674b3ed47 [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.YangBase;
19import org.onosproject.yangutils.datamodel.YangIdentity;
20import org.onosproject.yangutils.datamodel.YangIdentityRef;
21import org.onosproject.yangutils.datamodel.YangNode;
22import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
23import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
24import org.onosproject.yangutils.linker.impl.YangResolutionInfoImpl;
25import org.onosproject.yangutils.datamodel.utils.Parsable;
26import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
27import org.onosproject.yangutils.parser.exceptions.ParserException;
28import org.onosproject.yangutils.parser.impl.TreeWalkListener;
29
30import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.addResolutionInfo;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
34import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
35import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.*;
36import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidNodeIdentifier;
37import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
38import static org.onosproject.yangutils.datamodel.utils.YangConstructType.BASE_DATA;
39
40/**
41 * base-stmt = base-keyword sep identifier-ref-arg-str
42 * optsep stmtend*
43 * identifier-ref-arg = [prefix ":"] identifier
44 */
45
46/**
47 * Represents listener based call back function corresponding to the "base"
48 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
49 */
50public final class BaseListener {
51
52 //Creates a new base listener.
53 private BaseListener() {
54 }
55
56 /**
57 * Performs validation and updates the data model tree when parser receives an
58 * input matching the grammar rule (base).
59 *
60 * @param listener listener's object
61 * @param ctx context object of the grammar rule
62 */
63 public static void processBaseEntry(TreeWalkListener listener,
64 GeneratedYangParser.BaseStatementContext ctx) {
65
66 // Check for stack to be non empty.
67 checkStackIsNotEmpty(listener, MISSING_HOLDER, BASE_DATA, ctx.string().getText(), ENTRY);
68
69 YangNodeIdentifier nodeIdentifier = getValidNodeIdentifier(ctx.string().getText(), BASE_DATA, ctx);
70
71 Parsable tmpData = listener.getParsedDataStack().peek();
72
73 /**
74 * For identityref base node identifier is copied in identity listener itself, so no need to process
75 * base statement for indentityref
76 */
77 if (tmpData instanceof YangIdentityRef) {
78 return;
79 }
80
81 if (!(tmpData instanceof YangIdentity)) {
82 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, BASE_DATA,
83 ctx.string().getText(), ENTRY));
84 }
85
86 YangBase yangBase = new YangBase();
87 yangBase.setBaseIdentifier(nodeIdentifier);
88 ((YangIdentity) tmpData).setBaseNode(yangBase);
89
90 int errorLine = ctx.getStart().getLine();
91 int errorPosition = ctx.getStart().getCharPositionInLine();
92
93 // Add resolution information to the list
94 YangResolutionInfoImpl resolutionInfo =
95 new YangResolutionInfoImpl<YangBase>(yangBase, (YangNode) tmpData, errorLine, errorPosition);
96 addToResolutionList(resolutionInfo, ctx);
97 }
98
99 /**
100 * Add to resolution list.
101 *
102 * @param resolutionInfo resolution information
103 * @param ctx context object of the grammar rule
104 */
105 private static void addToResolutionList(YangResolutionInfoImpl<YangBase> resolutionInfo,
106 GeneratedYangParser.BaseStatementContext ctx) {
107
108 try {
109 addResolutionInfo(resolutionInfo);
110 } catch (DataModelException e) {
111 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
112 BASE_DATA, ctx.string().getText(), EXIT, e.getMessage()));
113 }
114 }
115
116}