Merge "BUG 1082 Migrate sal-rest-connector to Async Data Broker API"
[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 public class XmlMapper {
24     private static final LeafrefCodecImpl LEAFREF_DEFAULT_CODEC = new LeafrefCodecImpl(
25             Optional.<LeafrefTypeDefinition> absent());
26
27     private static class LeafrefCodecImpl extends TypeDefinitionAwareCodec<Object, LeafrefTypeDefinition> implements
28             LeafrefCodec<String> {
29
30         protected LeafrefCodecImpl(Optional<LeafrefTypeDefinition> typeDef) {
31             super(typeDef, Object.class);
32         }
33
34         @Override
35         public String serialize(Object data) {
36             return String.valueOf(data);
37         }
38
39         @Override
40         public Object deserialize(String data) {
41             return data;
42         }
43     }
44
45     private static class XmlCodecProviderImpl implements XmlCodecProvider {
46         @Override
47         public TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codecFor(TypeDefinition<?> baseType) {
48             TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codec = TypeDefinitionAwareCodec
49                     .from(baseType);
50
51             if (codec == null) {
52                 if (baseType instanceof Leafref) {
53                     return LEAFREF_DEFAULT_CODEC;
54                 }
55             }
56             return codec;
57         }
58     }
59
60     private static final XmlCodecProvider XML_CODEC_PROVIDER_IMPL = new XmlCodecProviderImpl();
61
62     public Document write(CompositeNode data, DataNodeContainer schema) throws UnsupportedDataTypeException {
63         return XmlDocumentUtils.toDocument(data, schema, XML_CODEC_PROVIDER_IMPL);
64     }
65 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.