9bcedcb22ba870bcc39e483e09da1346247017fb
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / AugmentUtils.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.stmt.rfc6020;
9
10 import com.google.common.base.Verify;
11 import com.google.common.collect.ImmutableList.Builder;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.Collection;
14 import java.util.List;
15 import java.util.Objects;
16 import java.util.Set;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
19 import org.opendaylight.yangtools.yang.model.api.stmt.DataDefinitionStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace;
28 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
29 import org.opendaylight.yangtools.yang.parser.stmt.reactor.RootStatementContext;
30 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
31
32 // FIXME: Move this to the AugmentStatementDefinition#ApplyAction
33 public final class AugmentUtils {
34     private AugmentUtils() {
35     }
36
37     public static void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceCtx,
38             final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
39         copyDeclaredStmts(sourceCtx, targetCtx);
40         copyEffectiveStmts(sourceCtx, targetCtx);
41     }
42
43     // FIXME: Declared statements should not be copied.
44     private static void copyDeclaredStmts(final StatementContextBase<?, ?, ?> sourceCtx,
45             final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
46
47         TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition().getDeclaredRepresentationClass()
48                 .equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION : TypeOfCopy.ADDED_BY_AUGMENTATION;
49
50         for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.declaredSubstatements()) {
51             if (needToCopyByAugment(originalStmtCtx)) {
52                 validateNodeCanBeCopiedByAugment(originalStmtCtx, targetCtx);
53
54                 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
55                 targetCtx.addEffectiveSubstatement(copy);
56             } else if (isReusedByAugment(originalStmtCtx)) {
57                 targetCtx.addEffectiveSubstatement(originalStmtCtx);
58             }
59         }
60     }
61
62     private static void copyEffectiveStmts(final StatementContextBase<?, ?, ?> sourceCtx,
63             final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
64         TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition().getDeclaredRepresentationClass()
65                 .equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION : TypeOfCopy.ADDED_BY_AUGMENTATION;
66
67         for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.effectiveSubstatements()) {
68             if (needToCopyByAugment(originalStmtCtx)) {
69                 validateNodeCanBeCopiedByAugment(originalStmtCtx, targetCtx);
70
71                 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
72                 targetCtx.addEffectiveSubstatement(copy);
73             } else if (isReusedByAugment(originalStmtCtx)) {
74                 targetCtx.addEffectiveSubstatement(originalStmtCtx);
75             }
76         }
77     }
78
79     private static void validateNodeCanBeCopiedByAugment(final StatementContextBase<?, ?, ?> sourceCtx,
80             final StatementContextBase<?, ?, ?> targetCtx) {
81
82         if (sourceCtx.getPublicDefinition().getDeclaredRepresentationClass().equals(WhenStatement.class)) {
83             return;
84         }
85
86         if (reguiredCheckOfMandatoryNodes(sourceCtx, targetCtx)) {
87             final List<StatementContextBase<?, ?, ?>> sourceSubStatements = new Builder<StatementContextBase<?, ?, ?>>()
88                     .addAll(sourceCtx.declaredSubstatements()).addAll(sourceCtx.effectiveSubstatements()).build();
89
90             for (final StatementContextBase<?, ?, ?> sourceSubStatement : sourceSubStatements) {
91                 InferenceException.throwIf(MandatoryStatement.class.equals(
92                     sourceSubStatement.getPublicDefinition().getDeclaredRepresentationClass()),
93                     sourceCtx.getStatementSourceReference(),
94                     "An augment cannot add node '%s' because it is mandatory and in module different from target",
95                     sourceCtx.rawStatementArgument());
96             }
97         }
98
99         final List<StatementContextBase<?, ?, ?>> targetSubStatements = new Builder<StatementContextBase<?, ?, ?>>()
100                 .addAll(targetCtx.declaredSubstatements()).addAll(targetCtx.effectiveSubstatements()).build();
101
102         for (final StatementContextBase<?, ?, ?> subStatement : targetSubStatements) {
103
104             final boolean sourceIsDataNode = DataDefinitionStatement.class.isAssignableFrom(sourceCtx
105                     .getPublicDefinition().getDeclaredRepresentationClass());
106             final boolean targetIsDataNode = DataDefinitionStatement.class.isAssignableFrom(subStatement
107                     .getPublicDefinition().getDeclaredRepresentationClass());
108             boolean qNamesEqual = sourceIsDataNode && targetIsDataNode
109                     && Objects.equals(sourceCtx.getStatementArgument(), subStatement.getStatementArgument());
110
111             InferenceException.throwIf(qNamesEqual, sourceCtx.getStatementSourceReference(),
112                 "An augment cannot add node named '%s' because this name is already used in target",
113                 sourceCtx.rawStatementArgument());
114         }
115     }
116
117     private static boolean reguiredCheckOfMandatoryNodes(StatementContextBase<?, ?, ?> sourceCtx,
118             StatementContextBase<?, ?, ?> targetCtx) {
119         /*
120          * If the statement argument is not QName, it cannot be mandatory statement,
121          * therefore return false and skip mandatory nodes validation
122          */
123         if(!(sourceCtx.getStatementArgument() instanceof QName)) {
124             return false;
125         }
126         QName sourceStmtQName = (QName) sourceCtx.getStatementArgument();
127
128         RootStatementContext<?, ?, ?> root = targetCtx.getRoot();
129         do {
130             Verify.verify(targetCtx.getStatementArgument() instanceof QName,
131                     "Argument of augment target statement must be QName.");
132             QName targetStmtQName = (QName) targetCtx.getStatementArgument();
133             /*
134              * If target is from another module, return true and perform
135              * mandatory nodes validation
136              */
137             if (!Utils.belongsToTheSameModule(targetStmtQName, sourceStmtQName)) {
138                 return true;
139             } else {
140                 /*
141                  * If target or one of its parent is a presence container from
142                  * the same module, return false and skip mandatory nodes
143                  * validation
144                  */
145                 if (Utils.isPresenceContainer(targetCtx)) {
146                     return false;
147                 }
148             }
149         } while ((targetCtx = targetCtx.getParentContext()) != root);
150
151         /*
152          * All target node's parents belong to the same module as source node,
153          * therefore return false and skip mandatory nodes validation.
154          */
155         return false;
156     }
157
158     private static final Set<Rfc6020Mapping> NOCOPY_DEF_SET = ImmutableSet.of(Rfc6020Mapping.USES, Rfc6020Mapping.WHEN,
159             Rfc6020Mapping.DESCRIPTION, Rfc6020Mapping.REFERENCE, Rfc6020Mapping.STATUS);
160
161     public static boolean needToCopyByAugment(final StmtContext<?, ?, ?> stmtContext) {
162         return !NOCOPY_DEF_SET.contains(stmtContext.getPublicDefinition());
163     }
164
165     private static final Set<Rfc6020Mapping> REUSED_DEF_SET = ImmutableSet.of(Rfc6020Mapping.TYPEDEF);
166
167     public static boolean isReusedByAugment(final StmtContext<?, ?, ?> stmtContext) {
168         return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
169     }
170
171     static boolean isSupportedAugmentTarget(final StatementContextBase<?, ?, ?> substatementCtx) {
172
173         /*
174          * :TODO Substatement must be allowed augment target type e.g. Container, etc... and must not be for example
175          * grouping, identity etc. It is problem in case when more than one substatements have the same QName, for
176          * example Grouping and Container are siblings and they have the same QName. We must find the Container and the
177          * Grouping must be ignored as disallowed augment target.
178          */
179
180         Collection<?> allowedAugmentTargets = substatementCtx.getFromNamespace(ValidationBundlesNamespace.class,
181                 ValidationBundleType.SUPPORTED_AUGMENT_TARGETS);
182
183         // if no allowed target is returned we consider all targets allowed
184         return allowedAugmentTargets == null || allowedAugmentTargets.isEmpty()
185                 || allowedAugmentTargets.contains(substatementCtx.getPublicDefinition());
186     }
187 }