2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/eplv10.html
8 package org.opendaylight.controller.yang.model.parser.util;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.Date;
14 import java.util.List;
15 import java.util.Stack;
17 import org.antlr.v4.runtime.tree.ParseTree;
18 import org.opendaylight.controller.antlrv4.code.gen.YangParser;
19 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Argument_stmtContext;
20 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Bit_stmtContext;
21 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Bits_specificationContext;
22 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Config_argContext;
23 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Config_stmtContext;
24 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Decimal64_specificationContext;
25 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Description_stmtContext;
26 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Enum_specificationContext;
27 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Enum_stmtContext;
28 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Fraction_digits_stmtContext;
29 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Leafref_specificationContext;
30 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Length_stmtContext;
31 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Mandatory_argContext;
32 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Mandatory_stmtContext;
33 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Max_elements_stmtContext;
34 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Min_elements_stmtContext;
35 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Must_stmtContext;
36 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Numerical_restrictionsContext;
37 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Ordered_by_argContext;
38 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Ordered_by_stmtContext;
39 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Path_stmtContext;
40 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Pattern_stmtContext;
41 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Position_stmtContext;
42 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Range_stmtContext;
43 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Reference_stmtContext;
44 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Require_instance_argContext;
45 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Require_instance_stmtContext;
46 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Status_argContext;
47 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Status_stmtContext;
48 import org.opendaylight.controller.antlrv4.code.gen.YangParser.StringContext;
49 import org.opendaylight.controller.antlrv4.code.gen.YangParser.String_restrictionsContext;
50 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Type_body_stmtsContext;
51 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Units_stmtContext;
52 import org.opendaylight.controller.antlrv4.code.gen.YangParser.When_stmtContext;
53 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Yin_element_argContext;
54 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Yin_element_stmtContext;
55 import org.opendaylight.controller.yang.common.QName;
56 import org.opendaylight.controller.yang.model.api.RevisionAwareXPath;
57 import org.opendaylight.controller.yang.model.api.SchemaPath;
58 import org.opendaylight.controller.yang.model.api.Status;
59 import org.opendaylight.controller.yang.model.api.TypeDefinition;
60 import org.opendaylight.controller.yang.model.api.UnknownSchemaNode;
61 import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition;
62 import org.opendaylight.controller.yang.model.api.type.BitsTypeDefinition.Bit;
63 import org.opendaylight.controller.yang.model.api.type.EnumTypeDefinition;
64 import org.opendaylight.controller.yang.model.api.type.LengthConstraint;
65 import org.opendaylight.controller.yang.model.api.type.PatternConstraint;
66 import org.opendaylight.controller.yang.model.api.type.RangeConstraint;
67 import org.opendaylight.controller.yang.model.parser.builder.api.SchemaNodeBuilder;
68 import org.opendaylight.controller.yang.model.parser.builder.impl.ConstraintsBuilder;
69 import org.opendaylight.controller.yang.model.util.BaseConstraints;
70 import org.opendaylight.controller.yang.model.util.BinaryType;
71 import org.opendaylight.controller.yang.model.util.BitsType;
72 import org.opendaylight.controller.yang.model.util.EnumerationType;
73 import org.opendaylight.controller.yang.model.util.InstanceIdentifier;
74 import org.opendaylight.controller.yang.model.util.Leafref;
75 import org.opendaylight.controller.yang.model.util.RevisionAwareXPathImpl;
76 import org.opendaylight.controller.yang.model.util.StringType;
77 import org.opendaylight.controller.yang.model.util.UnknownType;
78 import org.opendaylight.controller.yang.model.util.YangTypesConverter;
79 import org.slf4j.Logger;
80 import org.slf4j.LoggerFactory;
82 public final class YangModelBuilderUtil {
84 private static final Logger logger = LoggerFactory
85 .getLogger(YangModelBuilderUtil.class);
88 * Parse given tree and get first string value.
92 * @return first string value from given tree
94 public static String stringFromNode(final ParseTree treeNode) {
95 final String result = "";
96 for (int i = 0; i < treeNode.getChildCount(); ++i) {
97 if (treeNode.getChild(i) instanceof StringContext) {
98 final StringContext context = (StringContext) treeNode
100 if (context != null) {
101 return context.getChild(0).getText().replace("\"", "");
109 * Parse 'description', 'reference' and 'status' statements and fill in
115 * builder to fill in with parsed statements
117 public static void parseSchemaNodeArgs(ParseTree ctx,
118 SchemaNodeBuilder builder) {
119 for (int i = 0; i < ctx.getChildCount(); i++) {
120 ParseTree child = ctx.getChild(i);
121 if (child instanceof Description_stmtContext) {
122 String desc = stringFromNode(child);
123 builder.setDescription(desc);
124 } else if (child instanceof Reference_stmtContext) {
125 String ref = stringFromNode(child);
126 builder.setReference(ref);
127 } else if (child instanceof Status_stmtContext) {
128 Status status = parseStatus((Status_stmtContext) child);
129 builder.setStatus(status);
135 * Parse given context and return its value;
139 * @return value parsed from context
141 public static Status parseStatus(Status_stmtContext ctx) {
142 for (int i = 0; i < ctx.getChildCount(); i++) {
143 ParseTree statusArg = ctx.getChild(i);
144 if (statusArg instanceof Status_argContext) {
145 String statusArgStr = stringFromNode(statusArg);
146 if (statusArgStr.equals("current")) {
147 return Status.CURRENT;
148 } else if (statusArgStr.equals("deprecated")) {
149 return Status.DEPRECATED;
150 } else if (statusArgStr.equals("obsolete")) {
151 return Status.OBSOLETE;
153 logger.warn("Invalid 'status' statement: " + statusArgStr);
161 * Parse given tree and returns units statement as string.
165 * @return value of units statement as string or null if there is no units
168 public static String parseUnits(ParseTree ctx) {
170 for (int i = 0; i < ctx.getChildCount(); i++) {
171 ParseTree child = ctx.getChild(i);
172 if (child instanceof Units_stmtContext) {
173 units = stringFromNode(child);
181 * Create SchemaPath object from given path list with namespace, revision
182 * and prefix based on given values.
188 * @return SchemaPath object.
190 public static SchemaPath createActualSchemaPath(List<String> actualPath,
191 URI namespace, Date revision, String prefix) {
192 final List<QName> path = new ArrayList<QName>();
194 for (String pathElement : actualPath) {
195 qname = new QName(namespace, revision, prefix, pathElement);
198 return new SchemaPath(path, true);
202 * Create SchemaPath from given string.
205 * string representation of path
206 * @return SchemaPath object
208 public static SchemaPath parseAugmentPath(String augmentPath) {
209 boolean absolute = augmentPath.startsWith("/");
210 String[] splittedPath = augmentPath.split("/");
211 List<QName> path = new ArrayList<QName>();
213 for (String pathElement : splittedPath) {
214 if (pathElement.length() > 0) {
215 String[] splittedElement = pathElement.split(":");
216 if (splittedElement.length == 1) {
217 name = new QName(null, null, null, splittedElement[0]);
219 name = new QName(null, null, splittedElement[0],
225 return new SchemaPath(path, absolute);
229 * Create java.util.List of QName objects from given key definition as
232 * @param keyDefinition
233 * key definition as string
240 * @return YANG list key as java.util.List of QName objects
242 public static List<QName> createListKey(String keyDefinition,
243 URI namespace, Date revision, String prefix) {
244 List<QName> key = new ArrayList<QName>();
245 String[] splittedKey = keyDefinition.split(" ");
248 for (String keyElement : splittedKey) {
249 if (keyElement.length() != 0) {
250 qname = new QName(namespace, revision, prefix, keyElement);
257 private static List<EnumTypeDefinition.EnumPair> getEnumConstants(
258 Type_body_stmtsContext ctx, List<String> path, URI namespace,
259 Date revision, String prefix) {
260 List<EnumTypeDefinition.EnumPair> enumConstants = new ArrayList<EnumTypeDefinition.EnumPair>();
262 out: for (int j = 0; j < ctx.getChildCount(); j++) {
263 ParseTree enumSpecChild = ctx.getChild(j);
264 if (enumSpecChild instanceof Enum_specificationContext) {
265 for (int k = 0; k < enumSpecChild.getChildCount(); k++) {
266 ParseTree enumChild = enumSpecChild.getChild(k);
267 if (enumChild instanceof Enum_stmtContext) {
268 enumConstants.add(createEnumPair(
269 (Enum_stmtContext) enumChild, k, path,
270 namespace, revision, prefix));
271 if (k == enumSpecChild.getChildCount() - 1) {
278 return enumConstants;
281 private static EnumTypeDefinition.EnumPair createEnumPair(
282 Enum_stmtContext ctx, final int value, List<String> path,
283 final URI namespace, final Date revision, final String prefix) {
284 final String name = stringFromNode(ctx);
285 final QName qname = new QName(namespace, revision, prefix, name);
286 String description = null;
287 String reference = null;
288 Status status = null;
289 List<String> enumPairPath = new ArrayList<String>(path);
290 enumPairPath.add(name);
292 for (int i = 0; i < ctx.getChildCount(); i++) {
293 ParseTree child = ctx.getChild(i);
294 if (child instanceof Description_stmtContext) {
295 description = stringFromNode(child);
296 } else if (child instanceof Reference_stmtContext) {
297 reference = stringFromNode(child);
298 } else if (child instanceof Status_stmtContext) {
299 status = parseStatus((Status_stmtContext) child);
303 EnumPairImpl result = new EnumPairImpl();
304 result.qname = qname;
305 result.path = createActualSchemaPath(enumPairPath, namespace, revision,
307 result.description = description;
308 result.reference = reference;
309 result.status = status;
311 result.value = value;
315 private static class EnumPairImpl implements EnumTypeDefinition.EnumPair {
318 private SchemaPath path;
319 private String description;
320 private String reference;
321 private Status status;
322 private List<UnknownSchemaNode> extensionSchemaNodes = Collections
325 private Integer value;
328 public QName getQName() {
333 public SchemaPath getPath() {
338 public String getDescription() {
343 public String getReference() {
348 public Status getStatus() {
353 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
354 return extensionSchemaNodes;
358 public String getName() {
363 public Integer getValue() {
368 public int hashCode() {
369 final int prime = 31;
371 result = prime * result + ((qname == null) ? 0 : qname.hashCode());
372 result = prime * result + ((path == null) ? 0 : path.hashCode());
373 result = prime * result
374 + ((description == null) ? 0 : description.hashCode());
375 result = prime * result
376 + ((reference == null) ? 0 : reference.hashCode());
377 result = prime * result
378 + ((status == null) ? 0 : status.hashCode());
381 + ((extensionSchemaNodes == null) ? 0
382 : extensionSchemaNodes.hashCode());
383 result = prime * result + ((name == null) ? 0 : name.hashCode());
384 result = prime * result + ((value == null) ? 0 : value.hashCode());
389 public boolean equals(Object obj) {
396 if (getClass() != obj.getClass()) {
399 EnumPairImpl other = (EnumPairImpl) obj;
401 if (other.qname != null) {
404 } else if (!qname.equals(other.qname)) {
408 if (other.path != null) {
411 } else if (!path.equals(other.path)) {
414 if (description == null) {
415 if (other.description != null) {
418 } else if (!description.equals(other.description)) {
421 if (reference == null) {
422 if (other.reference != null) {
425 } else if (!reference.equals(other.reference)) {
428 if (status == null) {
429 if (other.status != null) {
432 } else if (!status.equals(other.status)) {
435 if (extensionSchemaNodes == null) {
436 if (other.extensionSchemaNodes != null) {
439 } else if (!extensionSchemaNodes.equals(other.extensionSchemaNodes)) {
443 if (other.name != null) {
446 } else if (!name.equals(other.name)) {
450 if (other.value != null) {
453 } else if (!value.equals(other.value)) {
460 public String toString() {
461 return EnumTypeDefinition.EnumPair.class.getSimpleName() + "[name="
462 + name + ", value=" + value + "]";
466 private static List<RangeConstraint> getRangeConstraints(
467 Type_body_stmtsContext ctx) {
468 final List<RangeConstraint> rangeConstraints = new ArrayList<RangeConstraint>();
469 for (int j = 0; j < ctx.getChildCount(); j++) {
470 ParseTree numRestrChild = ctx.getChild(j);
471 if (numRestrChild instanceof Numerical_restrictionsContext) {
472 for (int k = 0; k < numRestrChild.getChildCount(); k++) {
473 ParseTree rangeChild = numRestrChild.getChild(k);
474 if (rangeChild instanceof Range_stmtContext) {
476 .addAll(parseRangeConstraints((Range_stmtContext) rangeChild));
482 return rangeConstraints;
485 private static List<RangeConstraint> parseRangeConstraints(
486 Range_stmtContext ctx) {
487 List<RangeConstraint> rangeConstraints = new ArrayList<RangeConstraint>();
488 String description = null;
489 String reference = null;
491 for (int i = 0; i < ctx.getChildCount(); i++) {
492 ParseTree child = ctx.getChild(i);
493 if (child instanceof Description_stmtContext) {
494 description = stringFromNode(child);
495 } else if (child instanceof Reference_stmtContext) {
496 reference = stringFromNode(child);
500 String rangeStr = stringFromNode(ctx);
501 String trimmed = rangeStr.replace(" ", "");
502 String[] splittedRange = trimmed.split("\\|");
503 for (String rangeDef : splittedRange) {
504 String[] splittedRangeDef = rangeDef.split("\\.\\.");
507 if (splittedRangeDef.length == 1) {
508 min = max = parseRangeValue(splittedRangeDef[0]);
510 min = parseRangeValue(splittedRangeDef[0]);
511 max = parseRangeValue(splittedRangeDef[1]);
513 RangeConstraint range = BaseConstraints.rangeConstraint(min, max,
514 description, reference);
515 rangeConstraints.add(range);
518 return rangeConstraints;
521 private static List<LengthConstraint> getLengthConstraints(
522 Type_body_stmtsContext ctx) {
523 List<LengthConstraint> lengthConstraints = new ArrayList<LengthConstraint>();
524 for (int j = 0; j < ctx.getChildCount(); j++) {
525 ParseTree stringRestrChild = ctx.getChild(j);
526 if (stringRestrChild instanceof String_restrictionsContext) {
527 for (int k = 0; k < stringRestrChild.getChildCount(); k++) {
528 ParseTree lengthChild = stringRestrChild.getChild(k);
529 if (lengthChild instanceof Length_stmtContext) {
531 .addAll(parseLengthConstraints((Length_stmtContext) lengthChild));
536 return lengthConstraints;
539 private static List<LengthConstraint> parseLengthConstraints(
540 Length_stmtContext ctx) {
541 List<LengthConstraint> lengthConstraints = new ArrayList<LengthConstraint>();
542 String description = null;
543 String reference = null;
545 for (int i = 0; i < ctx.getChildCount(); i++) {
546 ParseTree child = ctx.getChild(i);
547 if (child instanceof Description_stmtContext) {
548 description = stringFromNode(child);
549 } else if (child instanceof Reference_stmtContext) {
550 reference = stringFromNode(child);
554 String lengthStr = stringFromNode(ctx);
555 String trimmed = lengthStr.replace(" ", "");
556 String[] splittedRange = trimmed.split("\\|");
557 for (String rangeDef : splittedRange) {
558 String[] splittedRangeDef = rangeDef.split("\\.\\.");
561 if (splittedRangeDef.length == 1) {
562 min = max = parseRangeValue(splittedRangeDef[0]);
564 min = parseRangeValue(splittedRangeDef[0]);
565 max = parseRangeValue(splittedRangeDef[1]);
567 LengthConstraint range = BaseConstraints.lengthConstraint(min, max,
568 description, reference);
569 lengthConstraints.add(range);
572 return lengthConstraints;
575 private static Number parseRangeValue(String value) {
576 Number result = null;
577 if(value.equals("min") || value.equals("max")) {
578 result = new UnknownBoundaryNumber(value);
580 result = Long.valueOf(value);
585 private static List<PatternConstraint> getPatternConstraint(
586 Type_body_stmtsContext ctx) {
587 List<PatternConstraint> patterns = new ArrayList<PatternConstraint>();
589 out: for (int j = 0; j < ctx.getChildCount(); j++) {
590 ParseTree stringRestrChild = ctx.getChild(j);
591 if (stringRestrChild instanceof String_restrictionsContext) {
592 for (int k = 0; k < stringRestrChild.getChildCount(); k++) {
593 ParseTree lengthChild = stringRestrChild.getChild(k);
594 if (lengthChild instanceof Pattern_stmtContext) {
595 patterns.add(parsePatternConstraint((Pattern_stmtContext) lengthChild));
596 if (k == lengthChild.getChildCount() - 1) {
607 * Internal helper method.
611 * @return PatternConstraint object
613 private static PatternConstraint parsePatternConstraint(
614 Pattern_stmtContext ctx) {
615 String description = null;
616 String reference = null;
617 for (int i = 0; i < ctx.getChildCount(); i++) {
618 ParseTree child = ctx.getChild(i);
619 if (child instanceof Description_stmtContext) {
620 description = stringFromNode(child);
621 } else if (child instanceof Reference_stmtContext) {
622 reference = stringFromNode(child);
625 String pattern = patternStringFromNode(ctx);
626 return BaseConstraints.patternConstraint(pattern, description,
631 * Parse given context and return pattern value.
632 * @param ctx context to parse
633 * @return pattern value as String
635 public static String patternStringFromNode(final Pattern_stmtContext ctx) {
637 for (int i = 0; i < ctx.getChildCount(); ++i) {
638 ParseTree child = ctx.getChild(i);
639 if (child instanceof StringContext) {
640 for(int j = 0; j < child.getChildCount(); j++) {
642 String patternToken = child.getChild(j).getText();
643 result += patternToken.substring(1, patternToken.length()-1);
651 private static Integer getFractionDigits(Type_body_stmtsContext ctx) {
652 for (int j = 0; j < ctx.getChildCount(); j++) {
653 ParseTree dec64specChild = ctx.getChild(j);
654 if (dec64specChild instanceof Decimal64_specificationContext) {
655 return parseFractionDigits((Decimal64_specificationContext) dec64specChild);
661 private static Integer parseFractionDigits(
662 Decimal64_specificationContext ctx) {
663 for (int k = 0; k < ctx.getChildCount(); k++) {
664 ParseTree fdChild = ctx.getChild(k);
665 if (fdChild instanceof Fraction_digits_stmtContext) {
666 return Integer.valueOf(stringFromNode(fdChild));
672 private static List<BitsTypeDefinition.Bit> getBits(
673 Type_body_stmtsContext ctx, List<String> actualPath, URI namespace,
674 Date revision, String prefix) {
675 List<BitsTypeDefinition.Bit> bits = new ArrayList<BitsTypeDefinition.Bit>();
676 for (int j = 0; j < ctx.getChildCount(); j++) {
677 ParseTree bitsSpecChild = ctx.getChild(j);
678 if (bitsSpecChild instanceof Bits_specificationContext) {
679 for (int k = 0; k < bitsSpecChild.getChildCount(); k++) {
680 ParseTree bitChild = bitsSpecChild.getChild(k);
681 if (bitChild instanceof Bit_stmtContext) {
682 bits.add(parseBit((Bit_stmtContext) bitChild,
683 actualPath, namespace, revision, prefix));
691 private static boolean isRequireInstance(Type_body_stmtsContext ctx) {
692 for (int i = 0; i < ctx.getChildCount(); i++) {
693 ParseTree child = ctx.getChild(i);
694 if (child instanceof Require_instance_stmtContext) {
695 for (int j = 0; j < child.getChildCount(); j++) {
696 ParseTree reqArg = child.getChild(j);
697 if (reqArg instanceof Require_instance_argContext) {
698 return Boolean.valueOf(stringFromNode(reqArg));
706 private static BitsTypeDefinition.Bit parseBit(final Bit_stmtContext ctx,
707 List<String> actualPath, final URI namespace, final Date revision,
708 final String prefix) {
709 String name = stringFromNode(ctx);
710 final QName qname = new QName(namespace, revision, prefix, name);
711 Long position = null;
713 String description = null;
714 String reference = null;
715 Status status = Status.CURRENT;
717 Stack<String> bitPath = new Stack<String>();
718 bitPath.addAll(actualPath);
721 SchemaPath schemaPath = createActualSchemaPath(bitPath, namespace,
724 for (int i = 0; i < ctx.getChildCount(); i++) {
725 ParseTree child = ctx.getChild(i);
726 if (child instanceof Position_stmtContext) {
727 String positionStr = stringFromNode(child);
728 position = Long.valueOf(positionStr);
729 if (position < 0 || position > 4294967295L) {
730 throw new YangParseException(
731 "position value MUST be in the range 0 to 4294967295, but was: "
734 } else if (child instanceof Description_stmtContext) {
735 description = stringFromNode(child);
736 } else if (child instanceof Reference_stmtContext) {
737 reference = stringFromNode(child);
738 } else if (child instanceof Status_stmtContext) {
739 status = parseStatus((Status_stmtContext) child);
743 final List<UnknownSchemaNode> extensionSchemaNodes = Collections
745 return createBit(qname, schemaPath, description, reference, status,
746 extensionSchemaNodes, position);
749 private static BitsTypeDefinition.Bit createBit(final QName qname,
750 final SchemaPath schemaPath, final String description,
751 final String reference, final Status status,
752 final List<UnknownSchemaNode> extensionDefinitions,
753 final Long position) {
754 return new BitsTypeDefinition.Bit() {
757 public QName getQName() {
762 public SchemaPath getPath() {
767 public String getDescription() {
772 public String getReference() {
777 public Status getStatus() {
782 public List<UnknownSchemaNode> getUnknownSchemaNodes() {
783 return extensionDefinitions;
787 public Long getPosition() {
792 public String getName() {
793 return qname.getLocalName();
797 public int hashCode() {
798 final int prime = 31;
800 result = prime * result
801 + ((qname == null) ? 0 : qname.hashCode());
802 result = prime * result
803 + ((schemaPath == null) ? 0 : schemaPath.hashCode());
804 result = prime * result
805 + ((description == null) ? 0 : description.hashCode());
806 result = prime * result
807 + ((reference == null) ? 0 : reference.hashCode());
808 result = prime * result
809 + ((status == null) ? 0 : status.hashCode());
810 result = prime * result
811 + ((position == null) ? 0 : position.hashCode());
814 + ((extensionDefinitions == null) ? 0
815 : extensionDefinitions.hashCode());
820 public boolean equals(Object obj) {
827 if (getClass() != obj.getClass()) {
830 Bit other = (Bit) obj;
832 if (other.getQName() != null) {
835 } else if (!qname.equals(other.getQName())) {
838 if (schemaPath == null) {
839 if (other.getPath() != null) {
842 } else if (!schemaPath.equals(other.getPath())) {
845 if (description == null) {
846 if (other.getDescription() != null) {
849 } else if (!description.equals(other.getDescription())) {
852 if (reference == null) {
853 if (other.getReference() != null) {
856 } else if (!reference.equals(other.getReference())) {
859 if (status == null) {
860 if (other.getStatus() != null) {
863 } else if (!status.equals(other.getStatus())) {
866 if (extensionDefinitions == null) {
867 if (other.getUnknownSchemaNodes() != null) {
870 } else if (!extensionDefinitions.equals(other
871 .getUnknownSchemaNodes())) {
874 if (position == null) {
875 if (other.getPosition() != null) {
878 } else if (!position.equals(other.getPosition())) {
885 public String toString() {
886 return Bit.class.getSimpleName() + "[name="
887 + qname.getLocalName() + ", position=" + position + "]";
893 * Parse orderedby statement.
896 * Ordered_by_stmtContext
897 * @return true, if orderedby contains value 'user' or false otherwise
899 public static boolean parseUserOrdered(Ordered_by_stmtContext childNode) {
900 boolean result = false;
901 for (int j = 0; j < childNode.getChildCount(); j++) {
902 ParseTree orderArg = childNode.getChild(j);
903 if (orderArg instanceof Ordered_by_argContext) {
904 String orderStr = stringFromNode(orderArg);
905 if (orderStr.equals("system")) {
907 } else if (orderStr.equals("user")) {
910 logger.warn("Invalid 'orderedby' statement.");
918 * Parse given config context and return true if it contains string 'true',
922 * config context to parse.
923 * @return true if given context contains string 'true', false otherwise
925 public static boolean parseConfig(final Config_stmtContext ctx) {
927 for (int i = 0; i < ctx.getChildCount(); ++i) {
928 final ParseTree configContext = ctx.getChild(i);
929 if (configContext instanceof Config_argContext) {
930 final String value = stringFromNode(configContext);
931 if (value.equals("true")) {
941 * Parse given type body and creates UnknownType definition.
943 * @param typedefQName
944 * qname of current type
947 * @return UnknownType object with constraints from parsed type body
949 public static TypeDefinition<?> parseUnknownTypeBody(QName typedefQName,
950 Type_body_stmtsContext ctx) {
951 UnknownType.Builder ut = new UnknownType.Builder(typedefQName);
954 List<RangeConstraint> rangeStatements = getRangeConstraints(ctx);
955 List<LengthConstraint> lengthStatements = getLengthConstraints(ctx);
956 List<PatternConstraint> patternStatements = getPatternConstraint(ctx);
957 Integer fractionDigits = getFractionDigits(ctx);
959 ut.rangeStatements(rangeStatements);
960 ut.lengthStatements(lengthStatements);
961 ut.patterns(patternStatements);
962 ut.fractionDigits(fractionDigits);
969 * Create TypeDefinition object based on given type name and type body.
976 * current path in schema
983 * @return TypeDefinition object based on parsed values.
985 public static TypeDefinition<?> parseTypeBody(String typeName,
986 Type_body_stmtsContext typeBody, List<String> actualPath,
987 URI namespace, Date revision, String prefix) {
988 TypeDefinition<?> type = null;
990 List<RangeConstraint> rangeStatements = getRangeConstraints(typeBody);
991 Integer fractionDigits = getFractionDigits(typeBody);
992 List<LengthConstraint> lengthStatements = getLengthConstraints(typeBody);
993 List<PatternConstraint> patternStatements = getPatternConstraint(typeBody);
994 List<EnumTypeDefinition.EnumPair> enumConstants = getEnumConstants(typeBody, actualPath, namespace, revision, prefix);
996 if (typeName.equals("decimal64")) {
997 type = YangTypesConverter.javaTypeForBaseYangDecimal64Type(
998 rangeStatements, fractionDigits);
999 } else if (typeName.startsWith("int")) {
1000 type = YangTypesConverter.javaTypeForBaseYangSignedIntegerType(typeName,
1002 } else if(typeName.startsWith("uint")) {
1003 type = YangTypesConverter.javaTypeForBaseYangUnsignedIntegerType(typeName,
1005 } else if (typeName.equals("enumeration")) {
1006 type = new EnumerationType(enumConstants);
1007 } else if (typeName.equals("string")) {
1008 type = new StringType(lengthStatements, patternStatements);
1009 } else if (typeName.equals("bits")) {
1010 type = new BitsType(getBits(typeBody, actualPath, namespace,
1012 } else if (typeName.equals("leafref")) {
1013 final String path = parseLeafrefPath(typeBody);
1014 final boolean absolute = path.startsWith("/");
1015 RevisionAwareXPath xpath = new RevisionAwareXPathImpl(path,
1017 type = new Leafref(xpath);
1018 } else if (typeName.equals("binary")) {
1019 List<Byte> bytes = Collections.emptyList();
1020 type = new BinaryType(bytes, lengthStatements, null);
1021 } else if (typeName.equals("instance-identifier")) {
1022 boolean requireInstance = isRequireInstance(typeBody);
1023 type = new InstanceIdentifier(null, requireInstance);
1028 private static String parseLeafrefPath(Type_body_stmtsContext ctx) {
1029 for (int i = 0; i < ctx.getChildCount(); i++) {
1030 ParseTree child = ctx.getChild(i);
1031 if (child instanceof Leafref_specificationContext) {
1032 for (int j = 0; j < child.getChildCount(); j++) {
1033 ParseTree leafRefSpec = child.getChild(j);
1034 if (leafRefSpec instanceof Path_stmtContext) {
1035 return stringFromNode(leafRefSpec);
1044 * Internal helper method for parsing Must_stmtContext.
1048 * @return an array of strings with following fields: [0] must text [1]
1049 * description [2] reference
1051 public static String[] parseMust(YangParser.Must_stmtContext ctx) {
1052 String[] params = new String[3];
1054 String mustText = "";
1055 String description = null;
1056 String reference = null;
1057 for (int i = 0; i < ctx.getChildCount(); ++i) {
1058 ParseTree child = ctx.getChild(i);
1059 if (child instanceof StringContext) {
1060 final StringContext context = (StringContext) child;
1061 for (int j = 0; j < context.getChildCount(); j++) {
1062 String mustPart = context.getChild(j).getText();
1064 mustText += mustPart
1065 .substring(0, mustPart.length() - 1);
1069 mustText += mustPart.substring(1);
1072 } else if (child instanceof Description_stmtContext) {
1073 description = stringFromNode(child);
1074 } else if (child instanceof Reference_stmtContext) {
1075 reference = stringFromNode(child);
1078 params[0] = mustText;
1079 params[1] = description;
1080 params[2] = reference;
1086 * Parse given tree and set constraints to given builder.
1089 * Context to search.
1090 * @param constraintsBuilder
1091 * ConstraintsBuilder to fill.
1093 public static void parseConstraints(ParseTree ctx,
1094 ConstraintsBuilder constraintsBuilder) {
1095 for (int i = 0; i < ctx.getChildCount(); ++i) {
1096 final ParseTree childNode = ctx.getChild(i);
1097 if (childNode instanceof Max_elements_stmtContext) {
1098 Integer max = Integer.valueOf(stringFromNode(childNode));
1099 constraintsBuilder.setMinElements(max);
1100 } else if (childNode instanceof Min_elements_stmtContext) {
1101 Integer min = Integer.valueOf(stringFromNode(childNode));
1102 constraintsBuilder.setMinElements(min);
1103 } else if (childNode instanceof Must_stmtContext) {
1104 String[] mustParams = parseMust((Must_stmtContext) childNode);
1105 constraintsBuilder.addMustDefinition(mustParams[0],
1106 mustParams[1], mustParams[2]);
1107 } else if (childNode instanceof Mandatory_stmtContext) {
1108 for (int j = 0; j < childNode.getChildCount(); j++) {
1109 ParseTree mandatoryTree = ctx.getChild(j);
1110 if (mandatoryTree instanceof Mandatory_argContext) {
1111 Boolean mandatory = Boolean
1112 .valueOf(stringFromNode(mandatoryTree));
1113 constraintsBuilder.setMandatory(mandatory);
1116 } else if (childNode instanceof When_stmtContext) {
1117 constraintsBuilder.addWhenCondition(stringFromNode(childNode));
1123 * Parse given context and return yin value.
1124 * @param ctx context to parse
1125 * @return true if value is 'true', false otherwise
1127 public static boolean parseYinValue(Argument_stmtContext ctx) {
1128 boolean yinValue = false;
1130 for(int j = 0; j < ctx.getChildCount(); j++) {
1131 ParseTree yin = ctx.getChild(j);
1132 if(yin instanceof Yin_element_stmtContext) {
1133 for(int k = 0; k < yin.getChildCount(); k++) {
1134 ParseTree yinArg = yin.getChild(k);
1135 if(yinArg instanceof Yin_element_argContext) {
1136 String yinString = stringFromNode(yinArg);
1137 if(yinString.equals("true")) {