Populate xpath/ hierarchy
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / DefaultReactors.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.impl;
9
10 import com.google.common.annotations.Beta;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.opendaylight.yangtools.odlext.parser.AugmentIdentifierStatementSupport;
13 import org.opendaylight.yangtools.odlext.parser.ContextInstanceStatementSupport;
14 import org.opendaylight.yangtools.odlext.parser.ContextReferenceStatementSupport;
15 import org.opendaylight.yangtools.odlext.parser.InstanceTargetStatementSupport;
16 import org.opendaylight.yangtools.odlext.parser.RpcContextReferenceStatementSupport;
17 import org.opendaylight.yangtools.openconfig.parser.EncryptedValueStatementSupport;
18 import org.opendaylight.yangtools.openconfig.parser.HashedValueStatementSupport;
19 import org.opendaylight.yangtools.rfc6241.parser.GetFilterElementAttributesStatementSupport;
20 import org.opendaylight.yangtools.rfc6536.parser.DefaultDenyAllStatementSupport;
21 import org.opendaylight.yangtools.rfc6536.parser.DefaultDenyWriteStatementSupport;
22 import org.opendaylight.yangtools.rfc6643.parser.AliasStatementSupport;
23 import org.opendaylight.yangtools.rfc6643.parser.DefValStatementSupport;
24 import org.opendaylight.yangtools.rfc6643.parser.DisplayHintStatementSupport;
25 import org.opendaylight.yangtools.rfc6643.parser.ImpliedStatementSupport;
26 import org.opendaylight.yangtools.rfc6643.parser.MaxAccessStatementSupport;
27 import org.opendaylight.yangtools.rfc6643.parser.OidStatementSupport;
28 import org.opendaylight.yangtools.rfc6643.parser.SubIdStatementSupport;
29 import org.opendaylight.yangtools.rfc7952.parser.AnnotationStatementSupport;
30 import org.opendaylight.yangtools.rfc8040.parser.YangDataArgumentNamespace;
31 import org.opendaylight.yangtools.rfc8040.parser.YangDataStatementSupport;
32 import org.opendaylight.yangtools.rfc8528.parser.MountPointStatementSupport;
33 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
34 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.CustomCrossSourceStatementReactorBuilder;
35 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
37 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
38 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.Builder;
39 import org.opendaylight.yangtools.yang.xpath.api.YangXPathParserFactory;
40
41 /**
42  * Utility class for instantiating default-configured {@link CrossSourceStatementReactor}s.
43  *
44  * @author Robert Varga
45  */
46 @Beta
47 public final class DefaultReactors {
48     private static final class DefaultReactor {
49         // Thread-safe lazy init
50         static final @NonNull CrossSourceStatementReactor INSTANCE = defaultReactorBuilder().build();
51     }
52
53     private DefaultReactors() {
54         // Hidden on purpose
55     }
56
57     /**
58      * Get a shared default-configured reactor instance. This instance is configured to handle both RFC6020 and RFC7950,
59      * as well as
60      * <ul>
61      * <li>RFC6536's default-deny-{all,write} extensions</li>
62      * <li>RFC7952's annotation extension</li>
63      * <li>RFC8040's yang-data extension</li>
64      * <li>OpenConfig extensions</li>
65      * <li>OpenDaylight extensions</li>
66      * </ul>
67      *
68      * @return a shared default-configured reactor instance.
69      */
70     public static @NonNull CrossSourceStatementReactor defaultReactor() {
71         return DefaultReactor.INSTANCE;
72     }
73
74     /**
75      * Return a baseline CrossSourceStatementReactor {@link Builder}. The builder is initialized to the equivalent
76      * of the reactor returned via {@link #defaultReactor()}, but can be further customized before use.
77      *
78      * @return A populated CrossSourceStatementReactor builder.
79      */
80     public static @NonNull CustomCrossSourceStatementReactorBuilder defaultReactorBuilder() {
81         return defaultReactorBuilder(YangParserConfiguration.DEFAULT);
82     }
83
84     /**
85      * Return a baseline CrossSourceStatementReactor {@link Builder}. The builder is initialized to the equivalent
86      * of the reactor returned via {@link #defaultReactor()}, but can be further customized before use.
87      *
88      * @param config parser configuration
89      * @return A populated CrossSourceStatementReactor builder.
90      */
91     public static @NonNull CustomCrossSourceStatementReactorBuilder defaultReactorBuilder(
92             final YangParserConfiguration config) {
93         return addExtensions(RFC7950Reactors.defaultReactorBuilder(config), config);
94     }
95
96     /**
97      * Return a baseline CrossSourceStatementReactor {@link Builder}. The builder is initialized to the equivalent
98      * of the reactor returned via {@link #defaultReactor()}, but can be further customized before use.
99      *
100      * @return A populated CrossSourceStatementReactor builder.
101      */
102     public static @NonNull CustomCrossSourceStatementReactorBuilder defaultReactorBuilder(
103             final YangXPathParserFactory xpathFactory) {
104         return defaultReactorBuilder(xpathFactory, YangParserConfiguration.DEFAULT);
105     }
106
107     /**
108      * Return a baseline CrossSourceStatementReactor {@link Builder}. The builder is initialized to the equivalent
109      * of the reactor returned via {@link #defaultReactor()}, but can be further customized before use.
110      *
111      * @param config parser configuration
112      * @return A populated CrossSourceStatementReactor builder.
113      */
114     public static @NonNull CustomCrossSourceStatementReactorBuilder defaultReactorBuilder(
115             final YangXPathParserFactory xpathFactory, final YangParserConfiguration config) {
116         return addExtensions(RFC7950Reactors.defaultReactorBuilder(xpathFactory, config), config);
117     }
118
119     private static @NonNull CustomCrossSourceStatementReactorBuilder addExtensions(
120             final @NonNull CustomCrossSourceStatementReactorBuilder builder, final YangParserConfiguration config) {
121         return builder
122                 // OpenDaylight extensions
123                 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION,
124                     new AugmentIdentifierStatementSupport(config))
125                 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION, new ContextInstanceStatementSupport(config))
126                 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION,
127                     new ContextReferenceStatementSupport(config))
128                 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION, new InstanceTargetStatementSupport(config))
129                 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION,
130                     new RpcContextReferenceStatementSupport(config))
131
132                 // RFC6241 get-filter-element-attributes support
133                 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION,
134                     new GetFilterElementAttributesStatementSupport(config))
135
136                 // RFC6536 default-deny-{all,write} support
137                 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION,
138                     new DefaultDenyAllStatementSupport(config))
139                 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION,
140                     new DefaultDenyWriteStatementSupport(config))
141
142                 // RFC6643 extensions
143                 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION, new DisplayHintStatementSupport(config))
144                 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION, new MaxAccessStatementSupport(config))
145                 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION, new DefValStatementSupport(config))
146                 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION, new ImpliedStatementSupport(config))
147                 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION, new AliasStatementSupport(config))
148                 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION, new OidStatementSupport(config))
149                 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION, new SubIdStatementSupport(config))
150
151                 // RFC7952 annotation support
152                 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION, new AnnotationStatementSupport(config))
153
154                 // RFC8040 yang-data support
155                 .addNamespaceSupport(ModelProcessingPhase.FULL_DECLARATION, YangDataArgumentNamespace.BEHAVIOUR)
156                 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION, new YangDataStatementSupport(config))
157
158                 // RFC8528 yang-data support
159                 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION, new MountPointStatementSupport(config))
160
161                 // OpenConfig extensions support (except openconfig-version)
162                 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION,
163                     new EncryptedValueStatementSupport(config))
164                 .addStatementSupport(ModelProcessingPhase.FULL_DECLARATION,
165                     new HashedValueStatementSupport(config));
166     }
167 }