X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frest%2Fimpl%2FXmlMapper.java;h=e76e1962a058bea7c0bdb8a22b6a2c7285e785cd;hp=a3d658e7bfec43bca4dc743a460cab8bd6cea9de;hb=405ea7ce68d22bd3d2501857c5253793b581b086;hpb=85ed33d7b90013a393fedcf4947307fbc7248fc2 diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/XmlMapper.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/XmlMapper.java index a3d658e7bf..e76e1962a0 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/XmlMapper.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/XmlMapper.java @@ -1,142 +1,67 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ package org.opendaylight.controller.sal.rest.impl; -import java.util.Set; - import javax.activation.UnsupportedDataTypeException; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; -import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO; -import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO.IdentityValue; -import org.opendaylight.controller.sal.restconf.impl.RestCodec; -import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.CompositeNode; -import org.opendaylight.yangtools.yang.data.api.Node; -import org.opendaylight.yangtools.yang.data.api.SimpleNode; -import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode; -import org.opendaylight.yangtools.yang.model.api.ChoiceNode; -import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; +import org.opendaylight.yangtools.yang.data.api.codec.LeafrefCodec; +import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec; +import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlCodecProvider; +import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlDocumentUtils; import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; -import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; -import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode; -import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; -import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; -import org.opendaylight.yangtools.yang.model.api.YangNode; -import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition; +import org.opendaylight.yangtools.yang.model.util.Leafref; import org.w3c.dom.Document; -import org.w3c.dom.Element; -import com.google.common.base.Preconditions; +import com.google.common.base.Optional; public class XmlMapper { - - private final Logger logger = LoggerFactory.getLogger(XmlMapper.class); + private static final LeafrefCodecImpl LEAFREF_DEFAULT_CODEC = new LeafrefCodecImpl( + Optional. absent()); - public Document write(CompositeNode data, DataNodeContainer schema) throws UnsupportedDataTypeException { - Preconditions.checkNotNull(data); - Preconditions.checkNotNull(schema); + private static class LeafrefCodecImpl extends TypeDefinitionAwareCodec implements + LeafrefCodec { - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); - Document doc = null; - try { - DocumentBuilder bob = dbf.newDocumentBuilder(); - doc = bob.newDocument(); - } catch (ParserConfigurationException e) { - return null; + protected LeafrefCodecImpl(Optional typeDef) { + super(typeDef, Object.class); } - if (schema instanceof ContainerSchemaNode || schema instanceof ListSchemaNode) { - doc.appendChild(translateToXmlAndReturnRootElement(doc, data, schema)); - return doc; - } else { - throw new UnsupportedDataTypeException( - "Schema can be ContainerSchemaNode or ListSchemaNode. Other types are not supported yet."); + @Override + public String serialize(Object data) { + return String.valueOf(data); } - } - private Element translateToXmlAndReturnRootElement(Document doc, Node data, YangNode schema) - throws UnsupportedDataTypeException { - QName dataType = data.getNodeType(); - Element itemEl = doc.createElementNS(dataType.getNamespace().toString(), dataType.getLocalName()); - if (data instanceof SimpleNode) { - if (schema instanceof LeafListSchemaNode) { - writeValueOfNodeByType(itemEl, (SimpleNode) data, ((LeafListSchemaNode) schema).getType(), (DataSchemaNode) schema); - } else if (schema instanceof LeafSchemaNode) { - writeValueOfNodeByType(itemEl, (SimpleNode) data, ((LeafSchemaNode) schema).getType(), (DataSchemaNode) schema); - } else { - Object value = data.getValue(); - if (value != null) { - itemEl.setTextContent(String.valueOf(value)); - } - } - } else { // CompositeNode - for (Node child : ((CompositeNode) data).getChildren()) { - DataSchemaNode childSchema = null; - if(schema != null){ - childSchema = findFirstSchemaForNode(child, ((DataNodeContainer) schema).getChildNodes()); - if (logger.isDebugEnabled()) { - if (childSchema == null) { - logger.debug("Probably the data node \"" + ((child == null) ? "" : child.getNodeType().getLocalName()) - + "\" is not conform to schema"); - } - } - } - itemEl.appendChild(translateToXmlAndReturnRootElement(doc, child, childSchema)); - } + @Override + public Object deserialize(String data) { + return data; } - return itemEl; } - private void writeValueOfNodeByType(Element element, SimpleNode node, TypeDefinition type, DataSchemaNode schema) { - - TypeDefinition baseType = RestUtil.resolveBaseTypeFrom(type); + private static class XmlCodecProviderImpl implements XmlCodecProvider { + @Override + public TypeDefinitionAwareCodec> codecFor(TypeDefinition baseType) { + TypeDefinitionAwareCodec> codec = TypeDefinitionAwareCodec + .from(baseType); - if (baseType instanceof IdentityrefTypeDefinition) { - if (node.getValue() instanceof QName) { - IdentityValuesDTO valueDTO = (IdentityValuesDTO) RestCodec.from(type).serialize(node.getValue()); - IdentityValue value = valueDTO.getValuesWithNamespaces().get(0); - String prefix = "x"; - if (value.getPrefix() != null && !value.getPrefix().isEmpty()) { - prefix = value.getPrefix(); + if (codec == null) { + if (baseType instanceof Leafref) { + return LEAFREF_DEFAULT_CODEC; } - element.setAttribute("xmlns:" + prefix, value.getNamespace()); - element.setTextContent(prefix + ":" + value.getValue()); - } else { - logger.debug("Value of " + baseType.getQName().getNamespace() + ":" - + baseType.getQName().getLocalName() + " is not instance of " + QName.class + " but is " + node.getValue().getClass()); - element.setTextContent(String.valueOf(node.getValue())); - } - } else { - if (node.getValue() != null) { - String value = String.valueOf(RestCodec.from(baseType).serialize(node.getValue())); - if (value.equals("null")) { - value = String.valueOf(node.getValue()); - } - element.setTextContent(value); } + return codec; } } - private DataSchemaNode findFirstSchemaForNode(Node node, Set dataSchemaNode) { - if (dataSchemaNode != null && node != null) { - for (DataSchemaNode dsn : dataSchemaNode) { - if (node.getNodeType().getLocalName().equals(dsn.getQName().getLocalName())) { - return dsn; - } else if (dsn instanceof ChoiceNode) { - for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) { - DataSchemaNode foundDsn = findFirstSchemaForNode(node, choiceCase.getChildNodes()); - if (foundDsn != null) { - return foundDsn; - } - } - } - } - } - return null; - } + private static final XmlCodecProvider XML_CODEC_PROVIDER_IMPL = new XmlCodecProviderImpl(); + public Document write(CompositeNode data, DataNodeContainer schema) throws UnsupportedDataTypeException { + return XmlDocumentUtils.toDocument(data, schema, XML_CODEC_PROVIDER_IMPL); + } }