c873ddbfbc6d6bace69312c57c7a08b571a86428
[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.collect.ImmutableList.Builder;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.Collection;
13 import java.util.List;
14 import java.util.Objects;
15 import java.util.Set;
16 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
17 import org.opendaylight.yangtools.yang.model.api.stmt.DataDefinitionStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.UsesStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace;
26 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
27 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
28
29 // FIXME: Move this to the AugmentStatementDefinition#ApplyAction
30 public final class AugmentUtils {
31     private AugmentUtils() {
32     }
33
34     public static void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceCtx,
35             final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
36         copyDeclaredStmts(sourceCtx, targetCtx);
37         copyEffectiveStmts(sourceCtx, targetCtx);
38     }
39
40     // FIXME: Declared statements should not be copied.
41     private static void copyDeclaredStmts(final StatementContextBase<?, ?, ?> sourceCtx,
42             final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
43
44         final List<StatementContextBase<?, ?, ?>> subStatements = new Builder<StatementContextBase<?, ?, ?>>()
45                 .addAll(targetCtx.declaredSubstatements()).addAll(targetCtx.effectiveSubstatements()).build();
46         boolean sourceAndTargetInSameModule = Utils.getRootModuleQName(sourceCtx).equals(
47                 Utils.getRootModuleQName(targetCtx));
48
49         TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition().getDeclaredRepresentationClass()
50                 .equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION : TypeOfCopy.ADDED_BY_AUGMENTATION;
51
52         for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.declaredSubstatements()) {
53             if (needToCopyByAugment(originalStmtCtx)) {
54                 validateNodeCanBeCopiedByAugment(originalStmtCtx, subStatements, sourceAndTargetInSameModule);
55
56                 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
57                 targetCtx.addEffectiveSubstatement(copy);
58             } else if (isReusedByAugment(originalStmtCtx)) {
59                 targetCtx.addEffectiveSubstatement(originalStmtCtx);
60             }
61         }
62     }
63
64     private static void copyEffectiveStmts(final StatementContextBase<?, ?, ?> sourceCtx,
65             final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
66
67         final List<StatementContextBase<?, ?, ?>> subStatements = new Builder<StatementContextBase<?, ?, ?>>()
68                 .addAll(targetCtx.declaredSubstatements()).addAll(targetCtx.effectiveSubstatements()).build();
69         boolean sourceAndTargetInSameModule = Utils.getRootModuleQName(sourceCtx).equals(
70                 Utils.getRootModuleQName(targetCtx));
71
72         TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition().getDeclaredRepresentationClass()
73                 .equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION : TypeOfCopy.ADDED_BY_AUGMENTATION;
74
75         for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.effectiveSubstatements()) {
76             if (needToCopyByAugment(originalStmtCtx)) {
77                 validateNodeCanBeCopiedByAugment(originalStmtCtx, subStatements, sourceAndTargetInSameModule);
78
79                 StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
80                 targetCtx.addEffectiveSubstatement(copy);
81             } else if (isReusedByAugment(originalStmtCtx)) {
82                 targetCtx.addEffectiveSubstatement(originalStmtCtx);
83             }
84         }
85     }
86
87     private static void validateNodeCanBeCopiedByAugment(final StatementContextBase<?, ?, ?> sourceCtx,
88             final List<StatementContextBase<?, ?, ?>> targetSubStatements, final boolean sourceAndTargetInSameModule) {
89
90         if (sourceCtx.getPublicDefinition().getDeclaredRepresentationClass().equals(WhenStatement.class)) {
91             return;
92         }
93
94         if (!sourceAndTargetInSameModule) {
95             final List<StatementContextBase<?, ?, ?>> sourceSubStatements = new Builder<StatementContextBase<?, ?, ?>>()
96                     .addAll(sourceCtx.declaredSubstatements()).addAll(sourceCtx.effectiveSubstatements()).build();
97
98             for (final StatementContextBase<?, ?, ?> sourceSubStatement : sourceSubStatements) {
99                 if (sourceSubStatement.getPublicDefinition().getDeclaredRepresentationClass()
100                         .equals(MandatoryStatement.class)) {
101                     throw new InferenceException(
102                             String.format(
103                                     "An augment cannot add node '%s' because it is mandatory and in module different from target",
104                                     sourceCtx.rawStatementArgument()), sourceCtx.getStatementSourceReference());
105                 }
106             }
107         }
108
109         for (final StatementContextBase<?, ?, ?> subStatement : targetSubStatements) {
110
111             final boolean sourceIsDataNode = DataDefinitionStatement.class.isAssignableFrom(sourceCtx
112                     .getPublicDefinition().getDeclaredRepresentationClass());
113             final boolean targetIsDataNode = DataDefinitionStatement.class.isAssignableFrom(subStatement
114                     .getPublicDefinition().getDeclaredRepresentationClass());
115             boolean qNamesEqual = sourceIsDataNode && targetIsDataNode
116                     && Objects.equals(sourceCtx.getStatementArgument(), subStatement.getStatementArgument());
117
118             if (qNamesEqual) {
119                 throw new InferenceException(String.format(
120                         "An augment cannot add node named '%s' because this name is already used in target",
121                         sourceCtx.rawStatementArgument()), sourceCtx.getStatementSourceReference());
122             }
123         }
124     }
125
126     private static final Set<Rfc6020Mapping> NOCOPY_DEV_SET = ImmutableSet.of(Rfc6020Mapping.USES);
127
128     public static boolean needToCopyByAugment(final StmtContext<?, ?, ?> stmtContext) {
129         return !NOCOPY_DEV_SET.contains(stmtContext.getPublicDefinition());
130     }
131
132     private static final Set<Rfc6020Mapping> REUSED_DEF_SET = ImmutableSet.of(Rfc6020Mapping.TYPEDEF);
133
134     public static boolean isReusedByAugment(final StmtContext<?, ?, ?> stmtContext) {
135         return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
136     }
137
138     static boolean isSupportedAugmentTarget(final StatementContextBase<?, ?, ?> substatementCtx) {
139
140         /*
141          * :TODO Substatement must be allowed augment target type e.g. Container, etc... and must be not for example
142          * grouping, identity etc. It is problem in case when more than one substatements have the same QName, for
143          * example Grouping and Container are siblings and they have the same QName. We must find the Container and the
144          * Grouping must be ignored as disallowed augment target.
145          */
146
147         Collection<?> allowedAugmentTargets = substatementCtx.getFromNamespace(ValidationBundlesNamespace.class,
148                 ValidationBundleType.SUPPORTED_AUGMENT_TARGETS);
149
150         // if no allowed target is returned we consider all targets allowed
151         return allowedAugmentTargets == null || allowedAugmentTargets.isEmpty()
152                 || allowedAugmentTargets.contains(substatementCtx.getPublicDefinition());
153     }
154 }