2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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/epl-v10.html
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
10 import com.google.common.collect.ImmutableMap;
11 import com.google.common.collect.Maps;
12 import java.util.List;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
17 import org.opendaylight.yangtools.yang.model.api.type.TypeDefinitions;
18 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractQNameStatementSupport;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
24 * Abstract base of all type-related statement support classes.
26 abstract class AbstractTypeSupport<T extends TypeStatement>
27 extends AbstractQNameStatementSupport<T, EffectiveStatement<QName, T>> {
28 private static final ImmutableMap<String, QName> BUILTIN_TYPES = Maps.uniqueIndex(List.of(
29 TypeDefinitions.BINARY,
31 TypeDefinitions.BOOLEAN,
32 TypeDefinitions.DECIMAL64,
33 TypeDefinitions.EMPTY,
34 TypeDefinitions.ENUMERATION,
35 TypeDefinitions.IDENTITYREF,
36 TypeDefinitions.INSTANCE_IDENTIFIER,
38 TypeDefinitions.INT16,
39 TypeDefinitions.INT32,
40 TypeDefinitions.INT64,
41 TypeDefinitions.LEAFREF,
42 TypeDefinitions.STRING,
43 TypeDefinitions.UINT8,
44 TypeDefinitions.UINT16,
45 TypeDefinitions.UINT32,
46 TypeDefinitions.UINT64,
47 TypeDefinitions.UNION),
50 AbstractTypeSupport(final YangParserConfiguration config) {
51 super(YangStmtMapping.TYPE, StatementPolicy.exactReplica(), config);
55 public final String internArgument(final String rawArgument) {
57 return (builtin = BUILTIN_TYPES.get(rawArgument)) != null ? builtin.getLocalName() : rawArgument;
61 public final QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
62 // RFC7950 section 7.3 states that:
64 // The name of the type MUST NOT be one of the YANG built-in types.
66 // Therefore, if the string matches one of built-in types, it cannot legally refer to a typedef. Hence consult
67 // built in types and if it's not there parse it as a node identifier.
68 final String rawArgument = ctx.getRawArgument();
69 final QName builtin = BUILTIN_TYPES.get(rawArgument);
70 return builtin != null ? builtin : StmtContextUtils.parseNodeIdentifier(ctx, rawArgument);