Add RFC6241 parser support
[yangtools.git] / yang / rfc6241-parser-support / src / main / java / org / opendaylight / yangtools / rfc6241 / parser / GetFilterElementAttributesStatementSupport.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, 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.rfc6241.parser;
9
10 import com.google.common.annotations.Beta;
11 import org.opendaylight.yangtools.rfc6241.model.api.GetFilterElementAttributesEffectiveStatement;
12 import org.opendaylight.yangtools.rfc6241.model.api.GetFilterElementAttributesSchemaNode;
13 import org.opendaylight.yangtools.rfc6241.model.api.GetFilterElementAttributesStatement;
14 import org.opendaylight.yangtools.rfc6241.model.api.NetconfStatements;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
17 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
18 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
19 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.UnknownEffectiveStatementBase;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractVoidStatementSupport;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 @Beta
29 public final class GetFilterElementAttributesStatementSupport
30     extends AbstractVoidStatementSupport<GetFilterElementAttributesStatement,
31         GetFilterElementAttributesEffectiveStatement> {
32
33     private static final class Declared extends AbstractDeclaredStatement<Void>
34             implements GetFilterElementAttributesStatement {
35         Declared(final StmtContext<Void, ?, ?> context) {
36             super(context);
37         }
38
39         @Override
40         public Void getArgument() {
41             return null;
42         }
43     }
44
45     private static final class Effective
46             extends UnknownEffectiveStatementBase<Void, GetFilterElementAttributesStatement>
47             implements GetFilterElementAttributesEffectiveStatement, GetFilterElementAttributesSchemaNode {
48
49         private final SchemaPath path;
50
51         Effective(final StmtContext<Void, GetFilterElementAttributesStatement, ?> ctx) {
52             super(ctx);
53             path = ctx.coerceParentContext().getSchemaPath().get().createChild(
54                 ctx.getPublicDefinition().getStatementName());
55         }
56
57         @Override
58         public QName getQName() {
59             return path.getLastComponent();
60         }
61
62         @Override
63         public SchemaPath getPath() {
64             return path;
65         }
66     }
67
68     private static final Logger LOG = LoggerFactory.getLogger(GetFilterElementAttributesStatementSupport.class);
69     private static final GetFilterElementAttributesStatementSupport INSTANCE =
70             new GetFilterElementAttributesStatementSupport(NetconfStatements.GET_FILTER_ELEMENT_ATTRIBUTES);
71
72     private final SubstatementValidator validator;
73
74     GetFilterElementAttributesStatementSupport(final StatementDefinition definition) {
75         super(definition);
76         this.validator = SubstatementValidator.builder(definition).build();
77     }
78
79     public static GetFilterElementAttributesStatementSupport getInstance() {
80         return INSTANCE;
81     }
82
83     @Override
84     public GetFilterElementAttributesStatement createDeclared(
85             final StmtContext<Void, GetFilterElementAttributesStatement, ?> ctx) {
86         return new Declared(ctx);
87     }
88
89     @Override
90     public GetFilterElementAttributesEffectiveStatement createEffective(final StmtContext<Void,
91             GetFilterElementAttributesStatement, GetFilterElementAttributesEffectiveStatement> ctx) {
92         return new Effective(ctx);
93     }
94
95     @Override
96     protected SubstatementValidator getSubstatementValidator() {
97         return validator;
98     }
99
100     @Override
101     public void onFullDefinitionDeclared(final Mutable<Void, GetFilterElementAttributesStatement,
102             GetFilterElementAttributesEffectiveStatement> stmt) {
103         super.onFullDefinitionDeclared(stmt);
104         stmt.setIsSupportedToBuildEffective(computeSupported(stmt));
105     }
106
107     private static boolean computeSupported(final StmtContext<?, ?, ?> stmt) {
108         final StmtContext<?, ?, ?> parent = stmt.getParentContext();
109         if (parent == null) {
110             LOG.debug("No parent, ignoring get-filter-element-attributes statement");
111             return false;
112         }
113         if (parent.getPublicDefinition() != YangStmtMapping.ANYXML) {
114             LOG.debug("Parent is not an anyxml node, ignoring get-filter-element-attributes statement");
115             return false;
116         }
117         if (!"filter".equals(parent.rawStatementArgument())) {
118             LOG.debug("Parent is not named 'filter', ignoring get-filter-element-attributes statement");
119             return false;
120         }
121
122         final StmtContext<?, ?, ?> grandParent = parent.getParentContext();
123         if (grandParent == null) {
124             LOG.debug("No grandparent, ignoring get-filter-element-attributes statement");
125             return false;
126         }
127         if (grandParent.getPublicDefinition() != YangStmtMapping.INPUT) {
128             LOG.debug("Grandparent is not an input node, ignoring get-filter-element-attributes statement");
129             return false;
130         }
131
132         final StmtContext<?, ?, ?> greatGrandParent = grandParent.getParentContext();
133         if (greatGrandParent == null) {
134             LOG.debug("No grandparent, ignoring get-filter-element-attributes statement");
135             return false;
136         }
137         if (greatGrandParent.getPublicDefinition() != YangStmtMapping.RPC) {
138             LOG.debug("Grandparent is not an RPC node, ignoring get-filter-element-attributes statement");
139             return false;
140         }
141
142         switch (greatGrandParent.rawStatementArgument()) {
143             case "get":
144             case "get-config":
145                 return true;
146             default:
147                 LOG.debug("Great-grandparent is not named 'get' nor 'get-config, ignoring get-filter-element-attributes"
148                     + " statement");
149                 return false;
150         }
151     }
152 }