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