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.ImmutableSet;
11 import java.io.IOException;
12 import java.util.HashMap;
14 import java.util.function.Function;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
21 import org.opendaylight.yangtools.yang.data.api.schema.stream.ForwardingNormalizedNodeStreamWriter;
22 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
25 * 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.
31 public abstract class QNameTransformingStreamWriter extends ForwardingNormalizedNodeStreamWriter {
33 // FIXME: Probably use loading cache to decrease memory
36 * Returns decorator, which uses supplied function to transform QNames.
38 * @param delegate Underlying normalized node stream writer
39 * @param transformation Transformation function, function is required to return non-null
41 * @return decorator, which uses supplied function to transform QNames.
43 public static NormalizedNodeStreamWriter fromFunction(final NormalizedNodeStreamWriter delegate,
44 final Function<QName, QName> transformation) {
45 return new QNameTransformingStreamWriter() {
48 protected NormalizedNodeStreamWriter delegate() {
54 protected QName transform(@Nonnull final QName key) {
55 return transformation.apply(key);
62 * Returns decorator, which uses supplied map to transform QNames. QNames not present in map are left unchanged.
64 * @param delegate Underlying normalized node stream writer
65 * @param mapping Immutable map which represent mapping from original to new values.
66 * @return decorator, which uses supplied mapping to transform QNames.
68 public static NormalizedNodeStreamWriter createQNameReplacing(final NormalizedNodeStreamWriter delegate,
69 final Map<QName, QName> mapping) {
70 return fromFunction(delegate, new QNameReplacementFunction(mapping));
74 * Returns decorator, which uses supplied map to transform QNameModules. QNameModules not present in map are left
77 * @param delegate Underlying normalized node stream writer
78 * @param mapping Immutable map which represent mapping from original to new values.
79 * @return decorator, which uses supplied mapping to transform QNameModules.
81 public static NormalizedNodeStreamWriter createQNameModuleReplacing(final NormalizedNodeStreamWriter delegate,
82 final Map<QNameModule, QNameModule> mapping) {
83 return fromFunction(delegate, new QNameModuleReplacementFunction(mapping));
87 public void leafNode(final NodeIdentifier name, final Object value) throws IOException {
88 super.leafNode(transform(name), value);
92 public void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
93 super.startLeafSet(transform(name), childSizeHint);
97 public void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
98 super.startOrderedLeafSet(transform(name), childSizeHint);
102 public void leafSetEntryNode(final QName name, final Object value) throws IOException {
103 super.leafSetEntryNode(transform(name), value);
107 public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
108 super.startContainerNode(transform(name), childSizeHint);
112 public void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IOException {
113 super.startUnkeyedList(transform(name), childSizeHint);
117 public void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IOException {
118 super.startUnkeyedListItem(transform(name), childSizeHint);
122 public void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
123 super.startMapNode(transform(name), childSizeHint);
127 public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint)
129 super.startMapEntryNode(transform(identifier), childSizeHint);
133 public void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
134 super.startOrderedMapNode(transform(name), childSizeHint);
138 public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
139 super.startChoiceNode(transform(name), childSizeHint);
143 public void startAugmentationNode(final AugmentationIdentifier identifier) throws IOException {
144 super.startAugmentationNode(transform(identifier));
148 public void anyxmlNode(final NodeIdentifier name, final Object value) throws IOException {
149 super.anyxmlNode(transform(name), value);
153 public void startYangModeledAnyXmlNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
154 super.startYangModeledAnyXmlNode(transform(name), childSizeHint);
158 * Transforms a QName to new mapping.
161 * NOTE: If QName should be unchanged implementation needs to return original QName.
163 * @param key QName to transform.
164 * @return Returns new value of QName.
166 protected abstract @Nonnull QName transform(@Nonnull QName key);
168 private NodeIdentifier transform(final NodeIdentifier name) {
169 final QName original = name.getNodeType();
170 final QName transformed = transform(original);
171 return transformed == original ? name : new NodeIdentifier(transformed);
174 private AugmentationIdentifier transform(final AugmentationIdentifier identifier) {
175 ImmutableSet.Builder<QName> builder = ImmutableSet.builder();
176 for (QName original : identifier.getPossibleChildNames()) {
177 builder.add(transform(original));
179 return new AugmentationIdentifier(builder.build());
182 private NodeIdentifierWithPredicates transform(final NodeIdentifierWithPredicates identifier) {
183 Map<QName, Object> keyValues = new HashMap<>();
184 for (Map.Entry<QName, Object> original : identifier.getKeyValues().entrySet()) {
185 keyValues.put(transform(original.getKey()), original.getValue());
187 return new NodeIdentifierWithPredicates(transform(identifier.getNodeType()), keyValues);