+ private static void addOrReplaceNode(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
+ final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
+
+ StatementDefinition refineSubstatementDef = refineSubstatementCtx.getPublicDefinition();
+ StatementDefinition refineTargetNodeDef = refineTargetNodeCtx.getPublicDefinition();
+
+ SourceException.throwIf(!isSupportedRefineTarget(refineSubstatementCtx, refineTargetNodeCtx),
+ refineSubstatementCtx.getStatementSourceReference(),
+ "Error in module '%s' in the refine of uses '%s': can not perform refine of '%s' for the target '%s'.",
+ refineSubstatementCtx.getRoot().getStatementArgument(),
+ refineSubstatementCtx.getParentContext().getStatementArgument(),
+ refineSubstatementCtx.getPublicDefinition(), refineTargetNodeCtx.getPublicDefinition());
+
+ if (isAllowedToAddByRefine(refineSubstatementDef)) {
+ refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
+ } else {
+ refineTargetNodeCtx.removeStatementFromEffectiveSubstatements(refineSubstatementDef);
+ refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
+ }
+ }
+
+ private static final Set<Rfc6020Mapping> ALLOWED_TO_ADD_BY_REFINE_DEF_SET = ImmutableSet.of(Rfc6020Mapping.MUST);
+
+ private static boolean isAllowedToAddByRefine(final StatementDefinition publicDefinition) {
+ return ALLOWED_TO_ADD_BY_REFINE_DEF_SET.contains(publicDefinition);
+ }
+
+ private static boolean isSupportedRefineSubstatement(final StatementContextBase<?, ?, ?> refineSubstatementCtx) {
+ Collection<?> supportedRefineSubstatements = refineSubstatementCtx.getFromNamespace(
+ ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_REFINE_SUBSTATEMENTS);
+
+ return supportedRefineSubstatements == null || supportedRefineSubstatements.isEmpty()
+ || supportedRefineSubstatements.contains(refineSubstatementCtx.getPublicDefinition())
+ || StmtContextUtils.isUnknownStatement(refineSubstatementCtx);
+ }
+
+ private static boolean isSupportedRefineTarget(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
+ final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
+
+ Collection<?> supportedRefineTargets = YangValidationBundles.SUPPORTED_REFINE_TARGETS.get(
+ refineSubstatementCtx.getPublicDefinition());
+
+ return supportedRefineTargets == null || supportedRefineTargets.isEmpty()
+ || supportedRefineTargets.contains(refineTargetNodeCtx.getPublicDefinition());
+ }