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;
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 ForwardingObject implements NormalizedNodeStreamWriter {
33 // FIXME: Probably use loading cache to decrease memory
35 protected abstract NormalizedNodeStreamWriter delegate();
38 * Returns decorator, which uses supplied function to transform QNames.
40 * @param delegate Underlying normalized node stream writer
41 * @param transformation Transformation function, function is required to return non-null
43 * @return decorator, which uses supplied function to transform QNames.
45 public static NormalizedNodeStreamWriter fromFunction(final NormalizedNodeStreamWriter delegate,
46 final Function<QName, QName> transformation) {
47 return new QNameTransformingStreamWriter() {
50 protected NormalizedNodeStreamWriter delegate() {
56 protected QName transform(@Nonnull final QName key) {
57 return transformation.apply(key);
64 * Returns decorator, which uses supplied map to transform QNames. QNames not present in map are left unchanged.
66 * @param delegate Underlying normalized node stream writer
67 * @param mapping Immutable map which represent mapping from original to new values.
68 * @return decorator, which uses supplied mapping to transform QNames.
70 public static NormalizedNodeStreamWriter createQNameReplacing(final NormalizedNodeStreamWriter delegate,
71 final Map<QName, QName> mapping) {
72 return fromFunction(delegate, new QNameReplacementFunction(mapping));
76 * Returns decorator, which uses supplied map to transform QNameModules. QNameModules not present in map are left
79 * @param delegate Underlying normalized node stream writer
80 * @param mapping Immutable map which represent mapping from original to new values.
81 * @return decorator, which uses supplied mapping to transform QNameModules.
83 public static NormalizedNodeStreamWriter createQNameModuleReplacing(final NormalizedNodeStreamWriter delegate,
84 final Map<QNameModule, QNameModule> mapping) {
85 return fromFunction(delegate, new QNameModuleReplacementFunction(mapping));
89 public void leafNode(final NodeIdentifier name, final Object value) throws IOException {
90 delegate().leafNode(transform(name), value);
94 public void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
95 delegate().startLeafSet(transform(name), childSizeHint);
99 public void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
100 delegate().startOrderedLeafSet(transform(name), childSizeHint);
104 public void leafSetEntryNode(final QName name, final Object value) throws IOException {
105 delegate().leafSetEntryNode(transform(name), value);
109 public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
110 delegate().startContainerNode(transform(name), childSizeHint);
114 public void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IOException {
115 delegate().startUnkeyedList(transform(name), childSizeHint);
119 public void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IOException {
120 delegate().startUnkeyedListItem(transform(name), childSizeHint);
124 public void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
125 delegate().startMapNode(transform(name), childSizeHint);
129 public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint)
131 delegate().startMapEntryNode(transform(identifier), childSizeHint);
135 public void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
136 delegate().startOrderedMapNode(transform(name), childSizeHint);
140 public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
141 delegate().startChoiceNode(transform(name), childSizeHint);
145 public void startAugmentationNode(final AugmentationIdentifier identifier) throws IOException {
146 delegate().startAugmentationNode(transform(identifier));
150 public void anyxmlNode(final NodeIdentifier name, final Object value) throws IOException {
151 delegate().anyxmlNode(transform(name), value);
155 public void startYangModeledAnyXmlNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
156 delegate().startYangModeledAnyXmlNode(transform(name), childSizeHint);
160 public void endNode() throws IOException {
161 delegate().endNode();
165 public void close() throws IOException {
170 public void flush() throws IOException {
175 * Transforms a QName to new mapping.
178 * NOTE: If QName should be unchanged implementation needs to return original QName.
180 * @param key QName to transform.
181 * @return Returns new value of QName.
183 protected abstract @Nonnull QName transform(@Nonnull QName key);
185 private NodeIdentifier transform(final NodeIdentifier name) {
186 return new NodeIdentifier(transform(name.getNodeType()));
189 private AugmentationIdentifier transform(final AugmentationIdentifier identifier) {
190 ImmutableSet.Builder<QName> builder = ImmutableSet.builder();
191 for (QName original : identifier.getPossibleChildNames()) {
192 builder.add(transform(original));
194 return new AugmentationIdentifier(builder.build());
197 private NodeIdentifierWithPredicates transform(final NodeIdentifierWithPredicates identifier) {
198 Map<QName, Object> keyValues = new HashMap<>();
199 for (Map.Entry<QName, Object> original : identifier.getKeyValues().entrySet()) {
200 keyValues.put(transform(original.getKey()), original.getValue());
202 return new NodeIdentifierWithPredicates(transform(identifier.getNodeType()), keyValues);