Remove ImportResolutionMode.SEMVER_LATEST
[yangtools.git] / parser / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / InstanceIdentifierSpecificationSupport.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.collect.ImmutableList;
11 import org.opendaylight.yangtools.yang.common.QName;
12 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
13 import org.opendaylight.yangtools.yang.model.api.meta.DeclarationReference;
14 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.RequireInstanceEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.InstanceIdentifierSpecification;
18 import org.opendaylight.yangtools.yang.model.ri.type.BaseTypes;
19 import org.opendaylight.yangtools.yang.model.ri.type.InstanceIdentifierTypeBuilder;
20 import org.opendaylight.yangtools.yang.model.ri.type.RestrictedTypes;
21 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.BoundStmtCtx;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
25
26 final class InstanceIdentifierSpecificationSupport
27         extends AbstractTypeSupport<InstanceIdentifierSpecification> {
28     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
29         SubstatementValidator.builder(YangStmtMapping.TYPE).addOptional(YangStmtMapping.REQUIRE_INSTANCE).build();
30
31     InstanceIdentifierSpecificationSupport(final YangParserConfiguration config) {
32         super(config, SUBSTATEMENT_VALIDATOR);
33     }
34
35     @Override
36     protected InstanceIdentifierSpecification createDeclared(final BoundStmtCtx<QName> ctx,
37             final ImmutableList<DeclaredStatement<?>> substatements) {
38         return substatements.isEmpty() ? new EmptyIdentifierSpecification(ctx.getRawArgument(), ctx.getArgument())
39             : new RegularInstanceIdentifierSpecification(ctx.getRawArgument(), ctx.getArgument(), substatements);
40     }
41
42     @Override
43     protected InstanceIdentifierSpecification attachDeclarationReference(final InstanceIdentifierSpecification stmt,
44             final DeclarationReference reference) {
45         return new RefInstanceIdentifierSpecification(stmt, reference);
46     }
47
48     @Override
49     protected EffectiveStatement<QName, InstanceIdentifierSpecification> createEffective(
50             final Current<QName, InstanceIdentifierSpecification> stmt,
51             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
52         final InstanceIdentifierTypeBuilder builder = RestrictedTypes.newInstanceIdentifierBuilder(
53             BaseTypes.instanceIdentifierType(), stmt.argumentAsTypeQName());
54
55         // TODO: we could do better here for empty substatements, but its really splitting hairs
56         for (EffectiveStatement<?, ?> subStmt : substatements) {
57             if (subStmt instanceof RequireInstanceEffectiveStatement) {
58                 builder.setRequireInstance(((RequireInstanceEffectiveStatement)subStmt).argument());
59             }
60         }
61
62         return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
63     }
64 }