TypeStatement takes a QName argument
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / TypeStatementRFC7950Support.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.collect.ImmutableMap;
12 import java.util.Optional;
13 import org.opendaylight.yangtools.yang.common.Uint32;
14 import org.opendaylight.yangtools.yang.model.api.stmt.BitEffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.EnumEffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.ValueEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
19 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
21 import org.opendaylight.yangtools.yang.model.api.type.TypeDefinitions;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25
26 /**
27  * Class providing necessary support for processing YANG 1.1 Type statement.
28  */
29 @Beta
30 public final class TypeStatementRFC7950Support extends AbstractTypeStatementSupport {
31     private static final ImmutableMap<String, StatementSupport<?, ?, ?>> ARGUMENT_SPECIFIC_SUPPORTS = ImmutableMap.of(
32         TypeDefinitions.LEAFREF.getLocalName(), new LeafrefSpecificationRFC7950Support(),
33         TypeDefinitions.IDENTITYREF.getLocalName(), new IdentityRefSpecificationRFC7950Support());
34     private static final TypeStatementRFC7950Support INSTANCE = new TypeStatementRFC7950Support();
35
36     private TypeStatementRFC7950Support() {
37         // Hidden
38     }
39
40     public static TypeStatementRFC7950Support getInstance() {
41         return INSTANCE;
42     }
43
44     @Override
45     public boolean hasArgumentSpecificSupports() {
46         return !ARGUMENT_SPECIFIC_SUPPORTS.isEmpty() || super.hasArgumentSpecificSupports();
47     }
48
49     @Override
50     public StatementSupport<?, ?, ?> getSupportSpecificForArgument(final String argument) {
51         final StatementSupport<?, ?, ?> potential = ARGUMENT_SPECIFIC_SUPPORTS.get(argument);
52         return potential != null ? potential : super.getSupportSpecificForArgument(argument);
53     }
54
55     @Override
56     Bit addRestrictedBit(final EffectiveStmtCtx stmt, final BitsTypeDefinition base, final BitEffectiveStatement bit) {
57         // FIXME: this looks like a duplicate of BitsSpecificationEffectiveStatement
58         final Optional<Uint32> declaredPosition = bit.getDeclaredPosition();
59         final Uint32 effectivePos;
60         if (declaredPosition.isEmpty()) {
61             effectivePos = getBaseTypeBitPosition(bit.argument(), base, stmt);
62         } else {
63             effectivePos = declaredPosition.get();
64         }
65
66         return EffectiveTypeUtil.buildBit(bit, effectivePos);
67     }
68
69     @Override
70     EnumPair addRestrictedEnum(final EffectiveStmtCtx stmt, final EnumTypeDefinition base,
71             final EnumEffectiveStatement enumStmt) {
72         final EnumEffectiveStatement enumSubStmt = enumStmt;
73         final Optional<Integer> declaredValue =
74                 enumSubStmt.findFirstEffectiveSubstatementArgument(ValueEffectiveStatement.class);
75         final int effectiveValue;
76         if (declaredValue.isEmpty()) {
77             effectiveValue = getBaseTypeEnumValue(enumSubStmt.getDeclared().rawArgument(), base, stmt);
78         } else {
79             effectiveValue = declaredValue.orElseThrow();
80         }
81
82         return EffectiveTypeUtil.buildEnumPair(enumSubStmt, effectiveValue);
83     }
84
85     private static Uint32 getBaseTypeBitPosition(final String bitName, final BitsTypeDefinition baseType,
86             final EffectiveStmtCtx stmt) {
87         for (Bit baseTypeBit : baseType.getBits()) {
88             if (bitName.equals(baseTypeBit.getName())) {
89                 return baseTypeBit.getPosition();
90             }
91         }
92
93         throw new SourceException(stmt, "Bit '%s' is not a subset of its base bits type %s.", bitName,
94             baseType.getQName());
95     }
96
97
98     private static int getBaseTypeEnumValue(final String enumName, final EnumTypeDefinition baseType,
99             final EffectiveStmtCtx ctx) {
100         for (EnumPair baseTypeEnumPair : baseType.getValues()) {
101             if (enumName.equals(baseTypeEnumPair.getName())) {
102                 return baseTypeEnumPair.getValue();
103             }
104         }
105
106         throw new SourceException(ctx, "Enum '%s' is not a subset of its base enumeration type %s.", enumName,
107             baseType.getQName());
108     }
109
110 }