2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.transform;
10 import com.google.common.collect.ForwardingObject;
11 import com.google.common.collect.ImmutableSet;
12 import java.io.IOException;
13 import java.util.HashMap;
15 import java.util.function.Function;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.QNameModule;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
22 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
26 * Stateless Normalized Node Stream Writer decorator, which performs QName translation.
28 * This class serves as base for Normalized Node Stream Writer decorators with option to transform
29 * QNames by user-implemented {@link #transform(QName)} function.
32 public abstract class QNameTransformingStreamWriter extends ForwardingObject implements NormalizedNodeStreamWriter {
34 // FIXME: Probably use loading cache to decrease memory
36 protected abstract NormalizedNodeStreamWriter delegate();
39 * Returns decorator, which uses supplied function to transform QNames.
41 * @param delegate Underlying normalized node stream writer
42 * @param transformation Transformation function, function is required to return non-null
44 * @return decorator, which uses supplied function to transform QNames.
46 public static NormalizedNodeStreamWriter fromFunction(final NormalizedNodeStreamWriter delegate,
47 final Function<QName, QName> transformation) {
48 return new QNameTransformingStreamWriter() {
51 protected NormalizedNodeStreamWriter delegate() {
57 protected QName transform(@Nonnull final QName key) {
58 return transformation.apply(key);
65 * Returns decorator, which uses supplied map to transform QNames.
67 * QNames not present in map are left unchanged.
69 * @param delegate Underlying normalized node stream writer
70 * @param mapping Immutable map which represent mapping from original to new values.
71 * @return decorator, which uses supplied mapping to transform QNames.
73 public static NormalizedNodeStreamWriter createQNameReplacing(final NormalizedNodeStreamWriter delegate,
74 final Map<QName, QName> mapping) {
75 return fromFunction(delegate, new QNameReplacementFunction(mapping));
79 * Returns decorator, which uses supplied map to transform QNameModules.
81 * QNameModules not present in map are left unchanged.
83 * @param delegate Underlying normalized node stream writer
84 * @param mapping Immutable map which represent mapping from original to new values.
85 * @return decorator, which uses supplied mapping to transform QNameModules.
87 public static NormalizedNodeStreamWriter createQNameModuleReplacing(final NormalizedNodeStreamWriter delegate,
88 final Map<QNameModule, QNameModule> mapping) {
89 return fromFunction(delegate, new QNameModuleReplacementFunction(mapping));
93 * Transforms a QName to new mapping.
95 * NOTE: If QName should be unchanged implementation needs to return original QName.
97 * @param key QName to transform.
98 * @return Returns new value of QName.
100 protected abstract @Nonnull QName transform(@Nonnull QName key);
103 public void leafNode(final NodeIdentifier name, final Object value) throws IOException {
104 delegate().leafNode(transform(name), value);
108 public void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
109 delegate().startLeafSet(transform(name), childSizeHint);
113 public void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
114 delegate().startOrderedLeafSet(transform(name), childSizeHint);
118 public void leafSetEntryNode(final QName name, final Object value) throws IOException {
119 delegate().leafSetEntryNode(transform(name), value);
123 public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
124 delegate().startContainerNode(transform(name), childSizeHint);
128 public void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IOException {
129 delegate().startUnkeyedList(transform(name), childSizeHint);
133 public void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IOException {
134 delegate().startUnkeyedListItem(transform(name), childSizeHint);
138 public void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
139 delegate().startMapNode(transform(name), childSizeHint);
143 public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint) throws IOException {
144 delegate().startMapEntryNode(transform(identifier), childSizeHint);
148 public void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
149 delegate().startOrderedMapNode(transform(name), childSizeHint);
153 public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
154 delegate().startChoiceNode(transform(name), childSizeHint);
158 public void startAugmentationNode(final AugmentationIdentifier identifier) throws IOException {
159 delegate().startAugmentationNode(transform(identifier));
163 public void anyxmlNode(final NodeIdentifier name, final Object value) throws IOException {
164 delegate().anyxmlNode(transform(name), value);
168 public void startYangModeledAnyXmlNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
169 delegate().startYangModeledAnyXmlNode(transform(name), childSizeHint);
173 public void endNode() throws IOException {
174 delegate().endNode();
178 public void close() throws IOException {
183 public void flush() throws IOException {
187 private NodeIdentifier transform(final NodeIdentifier name) {
188 return new NodeIdentifier(transform(name.getNodeType()));
191 private AugmentationIdentifier transform(final AugmentationIdentifier identifier) {
192 ImmutableSet.Builder<QName> builder = ImmutableSet.builder();
193 for (QName original : identifier.getPossibleChildNames()) {
194 builder.add(transform(original));
196 return new AugmentationIdentifier(builder.build());
199 private NodeIdentifierWithPredicates transform(final NodeIdentifierWithPredicates identifier) {
200 Map<QName, Object> keyValues = new HashMap<>();
201 for (Map.Entry<QName, Object> original : identifier.getKeyValues().entrySet()) {
202 keyValues.put(transform(original.getKey()), original.getValue());
204 return new NodeIdentifierWithPredicates(transform(identifier.getNodeType()), keyValues);