bea54a8217a2c50d510ee20b45419dab65e62583
[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.StmtContext;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
23 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
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) throws SourceException {
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) throws SourceException {
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) throws SourceException {
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                 if (sourceSubStatement.getPublicDefinition().getDeclaredRepresentationClass()
99                         .equals(MandatoryStatement.class)) {
100                     throw new IllegalArgumentException(
101                             String.format(
102                                     "An augment cannot add node '%s' because it is mandatory and in module different from target",
103                                     sourceCtx.rawStatementArgument()));
104                 }
105             }
106         }
107
108         for (final StatementContextBase<?, ?, ?> subStatement : targetSubStatements) {
109
110             final boolean sourceIsDataNode = DataDefinitionStatement.class.isAssignableFrom(sourceCtx
111                     .getPublicDefinition().getDeclaredRepresentationClass());
112             final boolean targetIsDataNode = DataDefinitionStatement.class.isAssignableFrom(subStatement
113                     .getPublicDefinition().getDeclaredRepresentationClass());
114             boolean qNamesEqual = sourceIsDataNode && targetIsDataNode
115                     && Objects.equals(sourceCtx.getStatementArgument(), subStatement.getStatementArgument());
116
117             if (qNamesEqual) {
118                 throw new IllegalStateException(String.format(
119                         "An augment cannot add node named '%s' because this name is already used in target",
120                         sourceCtx.rawStatementArgument()));
121             }
122         }
123     }
124
125     private static final Set<Rfc6020Mapping> NOCOPY_DEV_SET = ImmutableSet.of(Rfc6020Mapping.USES);
126
127     public static boolean needToCopyByAugment(final StmtContext<?, ?, ?> stmtContext) {
128         return !NOCOPY_DEV_SET.contains(stmtContext.getPublicDefinition());
129     }
130
131     private static final Set<Rfc6020Mapping> REUSED_DEF_SET = ImmutableSet.of(Rfc6020Mapping.TYPEDEF);
132
133     public static boolean isReusedByAugment(final StmtContext<?, ?, ?> stmtContext) {
134         return REUSED_DEF_SET.contains(stmtContext.getPublicDefinition());
135     }
136
137     static boolean isSupportedAugmentTarget(final StatementContextBase<?, ?, ?> substatementCtx) {
138
139         /*
140          * :TODO Substatement must be allowed augment target type e.g. Container, etc... and must be not for example
141          * grouping, identity etc. It is problem in case when more than one substatements have the same QName, for
142          * example Grouping and Container are siblings and they have the same QName. We must find the Container and the
143          * Grouping must be ignored as disallowed augment target.
144          */
145
146         Collection<?> allowedAugmentTargets = substatementCtx.getFromNamespace(ValidationBundlesNamespace.class,
147                 ValidationBundleType.SUPPORTED_AUGMENT_TARGETS);
148
149         // if no allowed target is returned we consider all targets allowed
150         return allowedAugmentTargets == null || allowedAugmentTargets.isEmpty()
151                 || allowedAugmentTargets.contains(substatementCtx.getPublicDefinition());
152     }
153 }