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