74851932c7447e992b9669463204d284b6c7b2d1
[yangtools.git] / yang / 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.model.api.YangStmtMapping;
12 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
13 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.RequireInstanceEffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.InstanceIdentifierSpecification;
16 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
17 import org.opendaylight.yangtools.yang.model.util.type.InstanceIdentifierTypeBuilder;
18 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
19 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
23
24 final class InstanceIdentifierSpecificationSupport extends BaseStatementSupport<String,
25         InstanceIdentifierSpecification, EffectiveStatement<String, InstanceIdentifierSpecification>> {
26     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
27         YangStmtMapping.TYPE)
28         .addOptional(YangStmtMapping.REQUIRE_INSTANCE)
29         .build();
30
31     InstanceIdentifierSpecificationSupport() {
32         super(YangStmtMapping.TYPE);
33     }
34
35     @Override
36     public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
37         return value;
38     }
39
40     @Override
41     protected SubstatementValidator getSubstatementValidator() {
42         return SUBSTATEMENT_VALIDATOR;
43     }
44
45     @Override
46     protected InstanceIdentifierSpecification createDeclared(
47             final StmtContext<String, InstanceIdentifierSpecification, ?> ctx,
48             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
49         return new RegularInstanceIdentifierSpecification(ctx.getRawArgument(), substatements);
50     }
51
52     @Override
53     protected InstanceIdentifierSpecification createEmptyDeclared(
54             final StmtContext<String, InstanceIdentifierSpecification, ?> ctx) {
55         return new EmptyIdentifierSpecification(ctx.getRawArgument());
56     }
57
58     @Override
59     protected EffectiveStatement<String, InstanceIdentifierSpecification> createEffective(
60             final Current<String, InstanceIdentifierSpecification> stmt,
61             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
62         final InstanceIdentifierTypeBuilder builder = RestrictedTypes.newInstanceIdentifierBuilder(
63             BaseTypes.instanceIdentifierType(), stmt.getSchemaPath());
64
65         // TODO: we could do better here for empty substatements, but its really splitting hairs
66         for (EffectiveStatement<?, ?> subStmt : substatements) {
67             if (subStmt instanceof RequireInstanceEffectiveStatement) {
68                 builder.setRequireInstance(((RequireInstanceEffectiveStatement)subStmt).argument());
69             }
70         }
71
72         return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
73     }
74 }