Migrate QName-based declared statements
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / list / AbstractListStatementSupport.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.list;
9
10 import com.google.common.collect.ImmutableList;
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Optional;
15 import java.util.Set;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
18 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
21 import org.opendaylight.yangtools.yang.model.api.Status;
22 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
23 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
24 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.KeyEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.ListStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.OrderedByEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
30 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
31 import org.opendaylight.yangtools.yang.parser.rfc7950.namespace.ChildSchemaNodeNamespace;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseQNameStatementSupport;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
34 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStmtUtils;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
39 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 abstract class AbstractListStatementSupport extends BaseQNameStatementSupport<ListStatement, ListEffectiveStatement> {
44     private static final Logger LOG = LoggerFactory.getLogger(AbstractListStatementSupport.class);
45
46     AbstractListStatementSupport() {
47         super(YangStmtMapping.LIST);
48     }
49
50     @Override
51     public final QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
52         return StmtContextUtils.parseIdentifier(ctx, value);
53     }
54
55     @Override
56     public final void onStatementAdded(final Mutable<QName, ListStatement, ListEffectiveStatement> stmt) {
57         stmt.coerceParentContext().addToNs(ChildSchemaNodeNamespace.class, stmt.coerceStatementArgument(), stmt);
58     }
59
60     @Override
61     protected ListStatement createDeclared(final StmtContext<QName, ListStatement, ?> ctx,
62             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
63         return new RegularListStatement(ctx.coerceStatementArgument(), substatements);
64     }
65
66     @Override
67     protected ListStatement createEmptyDeclared(final StmtContext<QName, ListStatement, ?> ctx) {
68         return new EmptyListStatement(ctx.coerceStatementArgument());
69     }
70
71     @Override
72     protected final ListEffectiveStatement createEffective(
73             final StmtContext<QName, ListStatement, ListEffectiveStatement> ctx,
74             final ListStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
75         final StatementSourceReference ref = ctx.getStatementSourceReference();
76         final SchemaPath path = ctx.getSchemaPath().get();
77         final ListSchemaNode original = (ListSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective)
78                 .orElse(null);
79
80         final ImmutableList<QName> keyDefinition;
81         final KeyEffectiveStatement keyStmt = findFirstStatement(substatements, KeyEffectiveStatement.class);
82         if (keyStmt != null) {
83             final List<QName> keyDefinitionInit = new ArrayList<>(keyStmt.argument().size());
84             final Set<QName> possibleLeafQNamesForKey = new HashSet<>();
85             for (final EffectiveStatement<?, ?> effectiveStatement : substatements) {
86                 if (effectiveStatement instanceof LeafSchemaNode) {
87                     possibleLeafQNamesForKey.add(((LeafSchemaNode) effectiveStatement).getQName());
88                 }
89             }
90             for (final SchemaNodeIdentifier key : keyStmt.argument()) {
91                 final QName keyQName = key.getLastComponent();
92
93                 if (!possibleLeafQNamesForKey.contains(keyQName)) {
94                     throw new InferenceException(ref, "Key '%s' misses node '%s' in list '%s'",
95                         keyStmt.getDeclared().rawArgument(), keyQName.getLocalName(), ctx.getStatementArgument());
96                 }
97                 keyDefinitionInit.add(keyQName);
98             }
99
100             keyDefinition = ImmutableList.copyOf(keyDefinitionInit);
101         } else {
102             keyDefinition = ImmutableList.of();
103         }
104
105         final boolean configuration = ctx.isConfiguration();
106         final int flags = new FlagsBuilder()
107                 .setHistory(ctx.getCopyHistory())
108                 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
109                 .setConfiguration(configuration)
110                 .setUserOrdered(findFirstArgument(substatements, OrderedByEffectiveStatement.class, "system")
111                     .equals("user"))
112                 .toFlags();
113         if (configuration && keyDefinition.isEmpty() && !inGrouping(ctx)) {
114             LOG.info("Configuration list {} does not define any keys in violation of RFC7950 section 7.8.2. While "
115                     + " this is fine with OpenDaylight, it can cause interoperability issues with other systems "
116                     + "[at {}]", ctx.getStatementArgument(), ref);
117         }
118
119         final Optional<ElementCountConstraint> elementCountConstraint =
120                 EffectiveStmtUtils.createElementCountConstraint(substatements);
121         return original == null && !elementCountConstraint.isPresent()
122                 ? new EmptyListEffectiveStatement(declared, path, flags, ctx, substatements, keyDefinition)
123                         : new RegularListEffectiveStatement(declared, path, flags, ctx, substatements, keyDefinition,
124                             elementCountConstraint.orElse(null), original);
125     }
126
127     private static boolean inGrouping(final StmtContext<?, ?, ?> ctx) {
128         StmtContext<?, ?, ?> parent = ctx.getParentContext();
129         while (parent != null) {
130             if (parent.getPublicDefinition() == YangStmtMapping.GROUPING) {
131                 return true;
132             }
133             parent = parent.getParentContext();
134         }
135         return false;
136     }
137
138     @Override
139     protected final ListEffectiveStatement createEmptyEffective(
140             final StmtContext<QName, ListStatement, ListEffectiveStatement> ctx, final ListStatement declared) {
141         return createEffective(ctx, declared, ImmutableList.of());
142     }
143 }