Merge branch 'master' of ../controller
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / config / ConfigStatementSupport.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.rfc7950.stmt.config;
9
10 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
11 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
12 import org.opendaylight.yangtools.yang.model.api.stmt.ConfigStatement;
13 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.ArgumentUtils;
14 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
15 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
17
18 public final class ConfigStatementSupport extends
19     AbstractStatementSupport<Boolean, ConfigStatement, EffectiveStatement<Boolean, ConfigStatement>> {
20     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
21         YangStmtMapping.CONFIG).build();
22     private static final ConfigStatementSupport INSTANCE = new ConfigStatementSupport();
23
24     private ConfigStatementSupport() {
25         super(YangStmtMapping.CONFIG);
26     }
27
28     public static ConfigStatementSupport getInstance() {
29         return INSTANCE;
30     }
31
32     @Override
33     public Boolean parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
34         return ArgumentUtils.parseBoolean(ctx, value);
35     }
36
37     @Override
38     public ConfigStatement createDeclared(final StmtContext<Boolean, ConfigStatement, ?> ctx) {
39         final ConfigStatement ret = new ConfigStatementImpl(ctx);
40
41         if (EmptyConfigStatement.FALSE.equals(ret)) {
42             return EmptyConfigStatement.FALSE;
43         } else if (EmptyConfigStatement.TRUE.equals(ret)) {
44             return EmptyConfigStatement.TRUE;
45         } else {
46             return ret;
47         }
48     }
49
50     @Override
51     public EffectiveStatement<Boolean, ConfigStatement> createEffective(
52             final StmtContext<Boolean, ConfigStatement, EffectiveStatement<Boolean, ConfigStatement>> ctx) {
53         final EffectiveStatement<Boolean, ConfigStatement> ret = new ConfigEffectiveStatementImpl(ctx);
54         final ConfigStatement declared = ret.getDeclared();
55         if (declared instanceof EmptyConfigStatement && ret.effectiveSubstatements().isEmpty()) {
56             return ((EmptyConfigStatement)declared).toEffective();
57         }
58         return ret;
59     }
60
61     @Override
62     public String internArgument(final String rawArgument) {
63         return ArgumentUtils.internBoolean(rawArgument);
64     }
65
66     @Override
67     protected SubstatementValidator getSubstatementValidator() {
68         return SUBSTATEMENT_VALIDATOR;
69     }
70 }