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.base.Function;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ForwardingObject;
13 import com.google.common.collect.ImmutableSet;
14 import java.io.IOException;
15 import java.util.HashMap;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
23 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
27 * Stateless Normalized Node Stream Writer decorator, which performs QName translation.
29 * This class serves as base for Normalized Node Stream Writer decorators with option to transform
30 * QNames by user-implemented {@link #transform(QName)} function.
33 public abstract class QNameTransformingStreamWriter extends ForwardingObject implements NormalizedNodeStreamWriter {
35 // FIXME: Probably use loading cache to decrease memory
37 protected abstract NormalizedNodeStreamWriter delegate();
40 * Returns decorator, which uses supplied function to transform QNames.
42 * @param delegate Underlying normalized node stream writer
43 * @param transformation Transformation function, function is required to return non-null
45 * @return decorator, which uses supplied function to transform QNames.
47 public static NormalizedNodeStreamWriter fromFunction(final NormalizedNodeStreamWriter delegate,
48 final Function<QName, QName> transformation) {
49 return new QNameTransformingStreamWriter() {
52 protected NormalizedNodeStreamWriter delegate() {
57 protected QName transform(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(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(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 implemention 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(NodeIdentifier name, Object value) throws IOException, IllegalArgumentException {
104 delegate().leafNode(transform(name), value);
108 public void startLeafSet(NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
109 delegate().startLeafSet(transform(name), childSizeHint);
113 public void leafSetEntryNode(Object value) throws IOException, IllegalArgumentException {
114 delegate().leafSetEntryNode(value);
118 public void startContainerNode(NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
119 delegate().startContainerNode(transform(name), childSizeHint);
123 public void startUnkeyedList(NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
124 delegate().startUnkeyedList(transform(name), childSizeHint);
128 public void startUnkeyedListItem(NodeIdentifier name, int childSizeHint) throws IOException, IllegalStateException {
129 delegate().startUnkeyedListItem(transform(name), childSizeHint);
133 public void startMapNode(NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
134 delegate().startMapNode(transform(name), childSizeHint);
138 public void startMapEntryNode(NodeIdentifierWithPredicates identifier, int childSizeHint) throws IOException,
139 IllegalArgumentException {
140 delegate().startMapEntryNode(transform(identifier), childSizeHint);
144 public void startOrderedMapNode(NodeIdentifier name, int childSizeHint) throws IOException,
145 IllegalArgumentException {
146 delegate().startOrderedMapNode(transform(name), childSizeHint);
150 public void startChoiceNode(NodeIdentifier name, int childSizeHint) throws IOException, IllegalArgumentException {
151 delegate().startChoiceNode(transform(name), childSizeHint);
155 public void startAugmentationNode(AugmentationIdentifier identifier) throws IOException, IllegalArgumentException {
156 delegate().startAugmentationNode(transform(identifier));
160 public void anyxmlNode(NodeIdentifier name, Object value) throws IOException, IllegalArgumentException {
161 delegate().anyxmlNode(transform(name), value);
165 public void endNode() throws IOException, IllegalStateException {
166 delegate().endNode();
170 public void close() throws IOException {
175 public void flush() throws IOException {
179 private NodeIdentifier transform(NodeIdentifier name) {
180 return new NodeIdentifier(transform(name.getNodeType()));
183 private AugmentationIdentifier transform(AugmentationIdentifier identifier) {
184 ImmutableSet.Builder<QName> builder = ImmutableSet.builder();
185 for (QName original : identifier.getPossibleChildNames()) {
186 builder.add(transform(original));
188 return new AugmentationIdentifier(builder.build());
191 private NodeIdentifierWithPredicates transform(NodeIdentifierWithPredicates identifier) {
192 Map<QName, Object> keyValues = new HashMap<>();
193 for (Map.Entry<QName, Object> original : identifier.getKeyValues().entrySet()) {
194 keyValues.put(transform(original.getKey()), original.getValue());
196 return new NodeIdentifierWithPredicates(transform(identifier.getNodeType()), keyValues);