Update binding-dom adaptation to remove AugmentationNode
[mdsal.git] / binding / mdsal-binding-dom-codec-api / src / main / java / org / opendaylight / mdsal / binding / dom / codec / api / BindingNormalizedNodeSerializer.java
1 /*
2  * Copyright (c) 2014 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.mdsal.binding.dom.codec.api;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.ImmutableSet;
15 import java.time.Instant;
16 import java.util.Map.Entry;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
20 import org.opendaylight.yangtools.yang.binding.Action;
21 import org.opendaylight.yangtools.yang.binding.Augmentation;
22 import org.opendaylight.yangtools.yang.binding.BaseNotification;
23 import org.opendaylight.yangtools.yang.binding.DataContainer;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.binding.Notification;
27 import org.opendaylight.yangtools.yang.binding.RpcInput;
28 import org.opendaylight.yangtools.yang.binding.RpcOutput;
29 import org.opendaylight.yangtools.yang.common.YangConstants;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
33 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
37
38 /**
39  * Serialization service, which provides two-way serialization between Java Binding Data representation and
40  * NormalizedNode representation.
41  */
42 public interface BindingNormalizedNodeSerializer {
43     /**
44      * Result of a {@link BindingNormalizedNodeSerializer#toNormalizedNode(InstanceIdentifier, DataObject)}. Since the
45      * Binding {@link Augmentation} does not have an exact equivalent, there are two specializations of this class:
46      * {@link NodeResult} and {@link AugmentationResult}.
47      */
48     sealed interface NormalizedResult {
49         /**
50          * Return the {@link YangInstanceIdentifier} path of this result.
51          *
52          * @return A {@link YangInstanceIdentifier}
53          */
54         @NonNull YangInstanceIdentifier path();
55     }
56
57     /**
58      * A {@link NormalizedResult} for an {@link Augmentation}.
59      *
60      * @param path A YangInstanceIdentifier identifying the parent of this augmentation
61      * @param possibleChildren {@link PathArgument}s of each possible child
62      * @param children Augmentation children
63      */
64     record AugmentationResult(
65             @NonNull YangInstanceIdentifier path,
66             @NonNull ImmutableSet<PathArgument> possibleChildren,
67             @NonNull ImmutableList<DataContainerChild> children) implements NormalizedResult {
68         public AugmentationResult {
69             requireNonNull(path);
70             requireNonNull(possibleChildren);
71             requireNonNull(children);
72         }
73     }
74
75     record NodeResult(@NonNull YangInstanceIdentifier path, @NonNull NormalizedNode node) implements NormalizedResult {
76         public NodeResult {
77             requireNonNull(path);
78             requireNonNull(node);
79         }
80     }
81
82     /**
83      * Translates supplied Binding Instance Identifier into NormalizedNode instance identifier.
84      *
85      * @param binding Binding Instance Identifier
86      * @return DOM Instance Identifier
87      * @throws IllegalArgumentException If supplied Instance Identifier is not valid.
88      */
89     // FIXME: MDSAL-525: reconcile this with BindingInstanceIdentifierCodec
90     @NonNull YangInstanceIdentifier toYangInstanceIdentifier(@NonNull InstanceIdentifier<?> binding);
91
92     /**
93      * Translates supplied YANG Instance Identifier into Binding instance identifier.
94      *
95      * @param dom YANG Instance Identifier
96      * @return Binding Instance Identifier, or null if the instance identifier is not representable.
97      */
98     // FIXME: MDSAL-525: reconcile this with BindingInstanceIdentifierCodec
99     <T extends DataObject> @Nullable InstanceIdentifier<T> fromYangInstanceIdentifier(
100             @NonNull YangInstanceIdentifier dom);
101
102     /**
103      * Translates supplied Binding Instance Identifier and data into NormalizedNode representation.
104      *
105      * @param path Binding Instance Identifier pointing to data
106      * @param data Data object representing data
107      * @return {@link NormalizedResult} representation
108      * @throws IllegalArgumentException If supplied Instance Identifier is not valid.
109      */
110     <T extends DataObject> @NonNull NormalizedResult toNormalizedNode(InstanceIdentifier<T> path, T data);
111
112     /**
113      * Translates supplied YANG Instance Identifier and NormalizedNode into Binding data.
114      *
115      * @param path Binding Instance Identifier
116      * @param data NormalizedNode representing data
117      * @return DOM Instance Identifier
118      */
119     @Nullable Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode(@NonNull YangInstanceIdentifier path,
120             NormalizedNode data);
121
122     /**
123      * Translates supplied NormalizedNode Notification into Binding data.
124      *
125      * @param path Schema Path of Notification, schema path is absolute, and consists of Notification QName.
126      * @param data NormalizedNode representing data
127      * @return Binding representation of Notification
128      */
129     @NonNull BaseNotification fromNormalizedNodeNotification(@NonNull Absolute path, @NonNull ContainerNode data);
130
131     /**
132      * Translates supplied NormalizedNode Notification into Binding data, optionally taking an instant
133      * when the notification was generated.
134      *
135      * @param path Schema Path of Notification, schema path is absolute, and consists of Notification QName.
136      * @param data NormalizedNode representing data
137      * @param eventInstant optional instant when the event was generated
138      * @return Binding representation of Notification
139      */
140     @Beta
141     @NonNull BaseNotification fromNormalizedNodeNotification(@NonNull Absolute path, @NonNull ContainerNode data,
142             @Nullable Instant eventInstant);
143
144     /**
145      * Translates supplied NormalizedNode RPC input or output into Binding data.
146      *
147      * @param containerPath Container path (RPC type + input/output)
148      * @param data NormalizedNode representing data
149      * @return Binding representation of RPC data
150      */
151     @Nullable DataObject fromNormalizedNodeRpcData(@NonNull Absolute containerPath, @NonNull ContainerNode data);
152
153     /**
154      * Translates supplied ContainerNode action input.
155      *
156      * @param action Binding action class
157      * @param input ContainerNode representing data
158      * @return Binding representation of action input
159      * @throws NullPointerException if any of the arguments is null
160      */
161     @Beta
162     <T extends RpcInput> @NonNull T fromNormalizedNodeActionInput(
163             @NonNull Class<? extends Action<?, ?, ?>> action, @NonNull ContainerNode input);
164
165     /**
166      * Translates supplied ContainerNode action output.
167      *
168      * @param action Binding action class
169      * @param output ContainerNode representing data
170      * @return Binding representation of action output
171      * @throws NullPointerException if any of the arguments is null
172      */
173     @Beta
174     <T extends RpcOutput> @NonNull T fromNormalizedNodeActionOutput(
175             @NonNull Class<? extends Action<?, ?, ?>> action, @NonNull ContainerNode output);
176
177     /**
178      * Translates supplied Binding Notification or output into NormalizedNode notification.
179      *
180      * @param data {@link Notification} representing notification data
181      * @return NormalizedNode representation of notification
182      */
183     @NonNull ContainerNode toNormalizedNodeNotification(@NonNull Notification<?> data);
184
185     /**
186      * Translates supplied Binding Notification or output into NormalizedNode notification.
187      *
188      * @param path schema node identifier of the notification
189      * @param data {@link BaseNotification} representing notification data
190      * @return NormalizedNode representation of notification
191      */
192     @NonNull ContainerNode toNormalizedNodeNotification(@NonNull Absolute path, @NonNull BaseNotification data);
193
194     /**
195      * Translates supplied Binding RPC input or output into NormalizedNode data.
196      *
197      * @param data NormalizedNode representing rpc data
198      * @return NormalizedNode representation of rpc data
199      */
200     @NonNull ContainerNode toNormalizedNodeRpcData(@NonNull DataContainer data);
201
202     /**
203      * Lazily translates supplied Binding action input into NormalizedNode data.
204      *
205      * @param action Binding action class
206      * @param input Binding action input
207      * @return NormalizedNode representation of action input
208      * @throws NullPointerException if any of the arguments is null
209      */
210     @Beta
211     @NonNull BindingLazyContainerNode<RpcInput> toLazyNormalizedNodeActionInput(
212             @NonNull Class<? extends Action<?, ?, ?>> action, @NonNull NodeIdentifier identifier,
213                     @NonNull RpcInput input);
214
215     /**
216      * Lazily translates supplied Binding action input into NormalizedNode data.
217      *
218      * @param action Binding action class
219      * @param input Binding action input
220      * @return NormalizedNode representation of action input
221      * @throws NullPointerException if any of the arguments is null
222      */
223     @Beta default @NonNull BindingLazyContainerNode<RpcInput> toLazyNormalizedNodeActionInput(
224             @NonNull final Class<? extends Action<?, ?, ?>> action, @NonNull final RpcInput input) {
225         return toLazyNormalizedNodeActionInput(action,
226             new NodeIdentifier(YangConstants.operationInputQName(BindingReflections.getQNameModule(action))), input);
227     }
228
229     /**
230      * Translates supplied Binding action input into NormalizedNode data.
231      *
232      * @param action Binding action class
233      * @param input Binding action input
234      * @return NormalizedNode representation of action input
235      * @throws NullPointerException if any of the arguments is null
236      */
237     @Beta default @NonNull ContainerNode toNormalizedNodeActionInput(
238             @NonNull final Class<? extends Action<?, ?, ?>> action, @NonNull final RpcInput input) {
239         return toLazyNormalizedNodeActionInput(action,
240             new NodeIdentifier(YangConstants.operationInputQName(BindingReflections.getQNameModule(action))), input)
241                 .getDelegate();
242     }
243
244     /**
245      * Lazily translates supplied Binding action output into NormalizedNode data.
246      *
247      * @param action Binding action class
248      * @param output Binding action output
249      * @return NormalizedNode representation of action output
250      */
251     @Beta
252     @NonNull BindingLazyContainerNode<RpcOutput> toLazyNormalizedNodeActionOutput(
253             @NonNull Class<? extends Action<?, ?, ?>> action, @NonNull NodeIdentifier identifier,
254                     @NonNull RpcOutput output);
255
256     /**
257      * Lazily translates supplied Binding action output into NormalizedNode data.
258      *
259      * @param action Binding action class
260      * @param output Binding action output
261      * @return NormalizedNode representation of action output
262      */
263     @Beta default @NonNull BindingLazyContainerNode<RpcOutput> toLazyNormalizedNodeActionOutput(
264             @NonNull final Class<? extends Action<?, ?, ?>> action, @NonNull final RpcOutput output) {
265         return toLazyNormalizedNodeActionOutput(action,
266             new NodeIdentifier(YangConstants.operationInputQName(BindingReflections.getQNameModule(action))), output);
267     }
268
269     /**
270      * Translates supplied Binding action output into NormalizedNode data.
271      *
272      * @param output Binding action output
273      * @return NormalizedNode representation of action output
274      */
275     @Beta default @NonNull ContainerNode toNormalizedNodeActionOutput(
276             @NonNull final Class<? extends Action<?, ?, ?>> action, @NonNull final RpcOutput output) {
277         return toLazyNormalizedNodeActionOutput(action,
278             new NodeIdentifier(YangConstants.operationInputQName(BindingReflections.getQNameModule(action))), output)
279                 .getDelegate();
280     }
281 }