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