Sonar issues clean-up
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / AugmentStatementImpl.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 static org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.EFFECTIVE_MODEL;
11
12 import java.util.Collection;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15 import javax.annotation.Nonnull;
16
17 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.DataDefinitionStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
28 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
29 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
30 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.AugmentEffectiveStatementImpl;
31
32 public class AugmentStatementImpl extends AbstractDeclaredStatement<SchemaNodeIdentifier> implements AugmentStatement {
33
34     private static final Logger LOG = LoggerFactory.getLogger(AugmentStatementImpl.class);
35
36     protected AugmentStatementImpl(StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> context) {
37         super(context);
38     }
39
40     public static class Definition extends AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> {
41
42         public Definition() {
43             super(Rfc6020Mapping.AUGMENT);
44         }
45
46         @Override
47         public SchemaNodeIdentifier parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) throws SourceException {
48             return SchemaNodeIdentifier.create(AugmentUtils.parseAugmentPath(ctx, value), Utils.isXPathAbsolute(value));
49         }
50
51         @Override
52         public AugmentStatement createDeclared(StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
53             return new AugmentStatementImpl(ctx);
54         }
55
56         @Override
57         public EffectiveStatement<SchemaNodeIdentifier, AugmentStatement> createEffective(
58                 StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
59             return new AugmentEffectiveStatementImpl(ctx);
60         }
61
62         @Override
63         public void onFullDefinitionDeclared(
64                 final StmtContext.Mutable<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode)
65                 throws SourceException {
66
67             final ModelActionBuilder augmentAction = augmentNode.newInferenceAction(EFFECTIVE_MODEL);
68             final ModelActionBuilder.Prerequisite<StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>>> sourceCtxPrereq = augmentAction
69                     .requiresCtx(augmentNode, ModelProcessingPhase.FULL_DECLARATION);
70
71             augmentAction.apply(new ModelActionBuilder.InferenceAction() {
72
73                 @Override
74                 public void apply() throws InferenceException {
75
76                     final StatementContextBase<?, ?, ?> augmentTargetCtx = AugmentUtils.getAugmentTargetCtx(augmentNode);
77                     final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) sourceCtxPrereq.get();
78
79                     try {
80                         AugmentUtils.copyFromSourceToTarget(augmentSourceCtx, augmentTargetCtx);
81                     } catch (SourceException e) {
82                         LOG.warn(e.getMessage(), e);
83                     }
84                 }
85
86                 @Override
87                 public void prerequisiteFailed(final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed)
88                         throws InferenceException {
89                     if (failed.contains(augmentAction)) {
90                         throw new InferenceException("Augment action failed", augmentNode.getStatementSourceReference());
91                     }
92                 }
93             });
94         }
95     }
96
97     @Nonnull
98     @Override
99     public SchemaNodeIdentifier getTargetNode() {
100         return argument();
101     }
102
103     @Override
104     public Collection<? extends DataDefinitionStatement> getDataDefinitions() {
105         return allDeclared(DataDefinitionStatement.class);
106     }
107 }