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