BUG 2854 : Do not add empty read write transactions to the replicable journal
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / rest / impl / XmlMapper.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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 package org.opendaylight.controller.sal.rest.impl;
9
10 import com.google.common.base.Optional;
11 import javax.activation.UnsupportedDataTypeException;
12 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
13 import org.opendaylight.yangtools.yang.data.api.codec.LeafrefCodec;
14 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
15 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlCodecProvider;
16 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlDocumentUtils;
17 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
18 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.util.Leafref;
21 import org.w3c.dom.Document;
22
23 /**
24  * @deprecated class will be removed for lithium release
25  */
26 @Deprecated
27 public class XmlMapper {
28     private static final LeafrefCodecImpl LEAFREF_DEFAULT_CODEC = new LeafrefCodecImpl(
29             Optional.<LeafrefTypeDefinition> absent());
30
31     private static class LeafrefCodecImpl extends TypeDefinitionAwareCodec<Object, LeafrefTypeDefinition> implements
32             LeafrefCodec<String> {
33
34         protected LeafrefCodecImpl(final Optional<LeafrefTypeDefinition> typeDef) {
35             super(typeDef, Object.class);
36         }
37
38         @Override
39         public String serialize(final Object data) {
40             return String.valueOf(data);
41         }
42
43         @Override
44         public Object deserialize(final String data) {
45             return data;
46         }
47     }
48
49     private static class XmlCodecProviderImpl implements XmlCodecProvider {
50         @Override
51         public TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codecFor(final TypeDefinition<?> baseType) {
52             final TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codec = TypeDefinitionAwareCodec
53                     .from(baseType);
54
55             if (codec == null) {
56                 if (baseType instanceof Leafref) {
57                     return LEAFREF_DEFAULT_CODEC;
58                 }
59             }
60             return codec;
61         }
62     }
63
64     private static final XmlCodecProvider XML_CODEC_PROVIDER_IMPL = new XmlCodecProviderImpl();
65
66     public Document write(final CompositeNode data, final DataNodeContainer schema) throws UnsupportedDataTypeException {
67         return XmlDocumentUtils.toDocument(data, schema, XML_CODEC_PROVIDER_IMPL);
68     }
69 }