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