/* * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020; import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.google.common.base.Splitter; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import java.math.BigDecimal; import java.math.BigInteger; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Set; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.SchemaPath; import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint; import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint; import org.opendaylight.yangtools.yang.model.util.UnresolvedNumber; import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException; import org.opendaylight.yangtools.yang.parser.spi.meta.QNameCacheNamespace; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.LengthConstraintEffectiveImpl; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type.RangeConstraintEffectiveImpl; /** * util class for manipulating YANG base and extended types implementation */ public final class TypeUtils { public static final String BINARY = "binary"; public static final String BITS = "bits"; public static final String BOOLEAN = "boolean"; public static final String DECIMAL64 = "decimal64"; public static final String EMPTY = "empty"; public static final String ENUMERATION = "enumeration"; public static final String IDENTITY_REF = "identityref"; public static final String INSTANCE_IDENTIFIER = "instance-identifier"; public static final String INT8 = "int8"; public static final String INT16 = "int16"; public static final String INT32 = "int32"; public static final String INT64 = "int64"; public static final String LEAF_REF = "leafref"; public static final String STRING = "string"; public static final String UINT8 = "uint8"; public static final String UINT16 = "uint16"; public static final String UINT32 = "uint32"; public static final String UINT64 = "uint64"; public static final String UNION = "union"; private static final Set BUILT_IN_TYPES = ImmutableSet.of(BINARY, BITS, BOOLEAN, DECIMAL64, EMPTY, ENUMERATION, IDENTITY_REF, INSTANCE_IDENTIFIER, INT8, INT16, INT32, INT64, LEAF_REF, STRING, UINT8, UINT16, UINT32, UINT64, UNION); private static final Set TYPE_BODY_STMTS = ImmutableSet.of( DECIMAL64, ENUMERATION, LEAF_REF, IDENTITY_REF, BITS, UNION); private static final Splitter PIPE_SPLITTER = Splitter.on('|').trimResults(); private static final Splitter TWO_DOTS_SPLITTER = Splitter.on("..").trimResults(); private TypeUtils() { } private static BigDecimal yangConstraintToBigDecimal(final Number number) { if (UnresolvedNumber.max().equals(number)) { return RangeStatementImpl.YANG_MAX_NUM; } if (UnresolvedNumber.min().equals(number)) { return RangeStatementImpl.YANG_MIN_NUM; } return new BigDecimal(number.toString()); } private static int compareNumbers(final Number n1, final Number n2) { final BigDecimal num1 = yangConstraintToBigDecimal(n1); final BigDecimal num2 = yangConstraintToBigDecimal(n2); return new BigDecimal(num1.toString()).compareTo(new BigDecimal(num2.toString())); } private static Number parseIntegerConstraintValue(final StmtContext ctx, final String value) { if ("max".equals(value)) { return UnresolvedNumber.max(); } if ("min".equals(value)) { return UnresolvedNumber.min(); } try { return new BigInteger(value); } catch (NumberFormatException e) { throw new SourceException(String.format("Value %s is not a valid integer", value), ctx.getStatementSourceReference(), e); } } private static Number parseDecimalConstraintValue(final StmtContext ctx, final String value) { if ("max".equals(value)) { return UnresolvedNumber.max(); } if ("min".equals(value)) { return UnresolvedNumber.min(); } try { return value.indexOf('.') != -1 ? new BigDecimal(value) : new BigInteger(value); } catch (NumberFormatException e) { throw new SourceException(String.format("Value %s is not a valid decimal number", value), ctx.getStatementSourceReference(), e); } } public static List parseRangeListFromString(final StmtContext ctx, final String rangeArgument) { Optional description = Optional.absent(); Optional reference = Optional.absent(); List rangeConstraints = new ArrayList<>(); for (final String singleRange : PIPE_SPLITTER.split(rangeArgument)) { final Iterator boundaries = TWO_DOTS_SPLITTER.splitToList(singleRange).iterator(); final Number min = parseDecimalConstraintValue(ctx, boundaries.next()); final Number max; if (boundaries.hasNext()) { max = parseDecimalConstraintValue(ctx, boundaries.next()); // if min larger than max then error InferenceException.throwIf(compareNumbers(min, max) == 1, ctx.getStatementSourceReference(), "Range constraint %s has descending order of boundaries; should be ascending", singleRange); SourceException.throwIf(boundaries.hasNext(), ctx.getStatementSourceReference(), "Wrong number of boundaries in range constraint %s", singleRange); } else { max = min; } // some of intervals overlapping if (rangeConstraints.size() > 1 && compareNumbers(min, Iterables.getLast(rangeConstraints).getMax()) != 1) { throw new InferenceException(ctx.getStatementSourceReference(), "Some of the ranges in %s are not disjoint", rangeArgument); } rangeConstraints.add(new RangeConstraintEffectiveImpl(min, max, description, reference)); } return rangeConstraints; } public static List parseLengthListFromString(final StmtContext ctx, final String lengthArgument) { Optional description = Optional.absent(); Optional reference = Optional.absent(); List lengthConstraints = new ArrayList<>(); for (final String singleRange : PIPE_SPLITTER.split(lengthArgument)) { final Iterator boundaries = TWO_DOTS_SPLITTER.splitToList(singleRange).iterator(); final Number min = parseIntegerConstraintValue(ctx, boundaries.next()); final Number max; if (boundaries.hasNext()) { max = parseIntegerConstraintValue(ctx, boundaries.next()); // if min larger than max then error Preconditions.checkArgument(compareNumbers(min, max) != 1, "Length constraint %s has descending order of boundaries; should be ascending. Statement source at %s", singleRange, ctx.getStatementSourceReference()); Preconditions.checkArgument(!boundaries.hasNext(), "Wrong number of boundaries in length constraint %s. Statement source at %s", singleRange, ctx.getStatementSourceReference()); } else { max = min; } // some of intervals overlapping if (lengthConstraints.size() > 1 && compareNumbers(min, Iterables.getLast(lengthConstraints).getMax()) != 1) { throw new InferenceException(ctx.getStatementSourceReference(), "Some of the length ranges in %s are not disjoint", lengthArgument); } lengthConstraints.add(new LengthConstraintEffectiveImpl(min, max, description, reference)); } return lengthConstraints; } public static boolean isYangTypeBodyStmtString(final String typeName) { return TYPE_BODY_STMTS.contains(typeName); } public static boolean isYangBuiltInTypeString(final String typeName) { return BUILT_IN_TYPES.contains(typeName); } public static SchemaPath typeEffectiveSchemaPath(final StmtContext stmtCtx) { final SchemaPath path = stmtCtx.getSchemaPath().get(); final QName qname = stmtCtx.getFromNamespace(QNameCacheNamespace.class, QName.create(path.getParent().getLastComponent(), path.getLastComponent().getLocalName())); return path.getParent().createChild(qname); } }