Binding2 runtime - Codecs impl - cache
[mdsal.git] / binding2 / mdsal-binding2-dom-codec / src / main / java / org / opendaylight / mdsal / binding / javav2 / dom / codec / impl / context / base / DataContainerCodecContext.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
9 package org.opendaylight.mdsal.binding.javav2.dom.codec.impl.context.base;
10
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.Optional;
13 import com.google.common.collect.ImmutableCollection;
14 import com.google.common.collect.ImmutableSet;
15 import java.io.IOException;
16 import java.util.List;
17 import javax.annotation.Nonnull;
18 import javax.annotation.Nullable;
19 import org.opendaylight.mdsal.binding.javav2.dom.codec.api.codecs.BindingNormalizedNodeCachingCodec;
20 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.MissingSchemaException;
21 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.cache.CachingNormalizedNodeCodec;
22 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.codecs.NonCachingCodec;
23 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.serializer.BindingToNormalizedStreamWriter;
24 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeArgument;
25 import org.opendaylight.mdsal.binding.javav2.spec.base.TreeNode;
26 import org.opendaylight.mdsal.binding.javav2.spec.runtime.BindingStreamEventWriter;
27 import org.opendaylight.mdsal.binding.javav2.spec.runtime.TreeNodeSerializer;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.common.QNameModule;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
34 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
35 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
36
37 @Beta
38 public abstract class DataContainerCodecContext<D extends TreeNode, T> extends NodeCodecContext<D> {
39
40     private final DataContainerCodecPrototype<T> prototype;
41     private volatile TreeNodeSerializer eventStreamSerializer;
42
43     protected DataContainerCodecContext(final DataContainerCodecPrototype<T> prototype) {
44         this.prototype = prototype;
45     }
46
47     @Nonnull
48     @Override
49     public final T getSchema() {
50         return prototype.getSchema();
51     }
52
53     protected final QNameModule namespace() {
54         return prototype.getNamespace();
55     }
56
57     protected final CodecContextFactory factory() {
58         return prototype.getFactory();
59     }
60
61     @Override
62     public YangInstanceIdentifier.PathArgument getDomPathArgument() {
63         return prototype.getYangArg();
64     }
65
66     /**
67      * Returns nested node context using supplied YANG Instance Identifier
68      *
69      * @param arg Yang Instance Identifier Argument
70      * @return Context of child
71      * @throws IllegalArgumentException If supplied argument does not represent valid child.
72      */
73     @Nonnull
74     @Override
75     public abstract NodeCodecContext<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument arg);
76
77     /**
78      * Returns nested node context using supplied Binding Instance Identifier
79      * and adds YANG instance identifiers to supplied list.
80      *
81      * @param arg Binding Instance Identifier Argument
82      * @return Context of child or null if supplied {@code arg} does not represent valid child.
83      * @throws IllegalArgumentException If supplied argument does not represent valid child.
84      */
85     @SuppressWarnings("unchecked")
86     @Nonnull
87     @Override
88     public DataContainerCodecContext<?,?> bindingPathArgumentChild(@Nonnull final TreeArgument<?> arg,
89             final List<PathArgument> builder) {
90         final DataContainerCodecContext<?,?> child = streamChild((Class<? extends TreeNode>) arg.getType());
91         if (builder != null) {
92             child.addYangPathArgument(arg,builder);
93         }
94         return child;
95     }
96
97     /**
98      * Returns de-serialized Binding Path Argument from YANG instance identifier.
99      *
100      * @param domArg input path argument
101      * @return returns binding path argument
102      */
103     @SuppressWarnings("rawtypes")
104     protected TreeArgument getBindingPathArgument(final YangInstanceIdentifier.PathArgument domArg) {
105         return bindingArg();
106     }
107
108     @SuppressWarnings("rawtypes")
109     protected final TreeArgument bindingArg() {
110         return prototype.getBindingArg();
111     }
112
113     @Nonnull
114     @SuppressWarnings("unchecked")
115     @Override
116     public final Class<D> getBindingClass() {
117         return Class.class.cast(prototype.getBindingClass());
118     }
119
120     /**
121      * Returns child context as if it was walked by
122      * {@link BindingStreamEventWriter}. This means that to enter case, one
123      * must issue getChild(ChoiceClass).getChild(CaseClass).
124      *
125      * @param childClass input child class
126      * @return Context of child node or null, if supplied class is not subtree child
127      * @throws IllegalArgumentException If supplied child class is not valid in specified context.
128      */
129     @Nonnull
130     @Override
131     public abstract <DV extends TreeNode> DataContainerCodecContext<DV,?> streamChild(@Nonnull final Class<DV> childClass) throws IllegalArgumentException;
132
133     /**
134      * Returns child context as if it was walked by
135      * {@link BindingStreamEventWriter}. This means that to enter case, one
136      * must issue getChild(ChoiceClass).getChild(CaseClass).
137      *
138      * @param childClass input child class
139      * @return Context of child or Optional absent is supplied class is not applicable in context.
140      */
141     @Override
142     public abstract <DV extends TreeNode> Optional<DataContainerCodecContext<DV, ?>> possibleStreamChild(@Nonnull
143         final Class<DV> childClass);
144
145     @Override
146     public String toString() {
147         return getClass().getSimpleName() + " [" + prototype.getBindingClass() + "]";
148     }
149
150     @Nonnull
151     @Override
152     public BindingNormalizedNodeCachingCodec<D> createCachingCodec(
153             @Nonnull final ImmutableCollection<Class<? extends TreeNode>> cacheSpecifier) {
154         if (cacheSpecifier.isEmpty()) {
155             return new NonCachingCodec<D>(this);
156         }
157         return new CachingNormalizedNodeCodec<D>(this, ImmutableSet.copyOf(cacheSpecifier));
158     }
159
160     BindingStreamEventWriter createWriter(final NormalizedNodeStreamWriter domWriter) {
161         return BindingToNormalizedStreamWriter.create(this, domWriter);
162     }
163
164     @Nonnull
165     protected final <V> V childNonNull(@Nullable final V nullable, final YangInstanceIdentifier.PathArgument child,
166             final String message, final Object... args) {
167         if (nullable != null) {
168             return nullable;
169         }
170         MissingSchemaException.checkModulePresent(factory().getRuntimeContext().getSchemaContext(), child);
171         throw IncorrectNestingException.create(message, args);
172     }
173
174     @Nonnull
175     protected final <V> V childNonNull(@Nullable final V nullable, final QName child, final String message,
176             final Object... args) {
177         if (nullable != null) {
178             return nullable;
179         }
180         MissingSchemaException.checkModulePresent(factory().getRuntimeContext().getSchemaContext(), child);
181         throw IncorrectNestingException.create(message, args);
182     }
183
184     @Nonnull
185     protected final <V> V childNonNull(@Nullable final V nullable, final Class<?> childClass, final String message,
186             final Object... args) {
187         if (nullable != null) {
188             return nullable;
189         }
190         MissingSchemaForClassException.check(factory().getRuntimeContext(), childClass);
191         MissingClassInLoadingStrategyException.check(factory().getRuntimeContext().getStrategy(), childClass);
192         throw IncorrectNestingException.create(message, args);
193     }
194
195     public TreeNodeSerializer eventStreamSerializer() {
196         if(eventStreamSerializer == null) {
197             eventStreamSerializer = factory().getEventStreamSerializer(getBindingClass());
198         }
199         return eventStreamSerializer;
200     }
201
202     @Nonnull
203     @Override
204     public NormalizedNode<?, ?> serialize(@Nonnull final D data) {
205         final NormalizedNodeResult result = new NormalizedNodeResult();
206         // We create DOM stream writer which produces normalized nodes
207         final NormalizedNodeStreamWriter domWriter = ImmutableNormalizedNodeStreamWriter.from(result);
208         writeAsNormalizedNode(data, domWriter);
209         return result.getResult();
210     }
211
212     @Override
213     public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
214         try {
215             eventStreamSerializer().serialize(data, createWriter(writer));
216         } catch (final IOException e) {
217             throw new IllegalStateException("Failed to serialize Binding DTO",e);
218         }
219     }
220
221 }