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