Merge "Bug 1430: Obtain config params from config system"
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / xml / codec / XmlDocumentUtils.java
1 /*
2  * Copyright (c) 2013 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.xml.codec;
9
10 import com.google.common.base.Function;
11 import com.google.common.base.Objects;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.base.Strings;
15 import com.google.common.collect.ImmutableList;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
18 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
19 import org.opendaylight.yangtools.yang.data.api.Node;
20 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
21 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
22 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
23 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlCodecProvider;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
25 import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
26 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.util.InstanceIdentifierType;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.w3c.dom.Attr;
40 import org.w3c.dom.Document;
41 import org.w3c.dom.Element;
42 import org.w3c.dom.NodeList;
43
44 import javax.activation.UnsupportedDataTypeException;
45 import javax.xml.parsers.DocumentBuilder;
46 import javax.xml.parsers.DocumentBuilderFactory;
47 import javax.xml.parsers.ParserConfigurationException;
48 import javax.xml.stream.XMLOutputFactory;
49 import javax.xml.stream.XMLStreamException;
50 import javax.xml.stream.XMLStreamWriter;
51 import javax.xml.transform.dom.DOMResult;
52 import java.net.URI;
53 import java.util.ArrayList;
54 import java.util.Collection;
55 import java.util.List;
56
57 import static com.google.common.base.Preconditions.checkState;
58
59 public class XmlDocumentUtils {
60   private static class ElementWithSchemaContext {
61     Element element;
62     SchemaContext schemaContext;
63
64     ElementWithSchemaContext(final Element element,final SchemaContext schemaContext) {
65       this.schemaContext = schemaContext;
66       this.element = element;
67     }
68
69     Element getElement() {
70       return element;
71     }
72
73     SchemaContext getSchemaContext() {
74       return schemaContext;
75     }
76   }
77
78   public static final QName OPERATION_ATTRIBUTE_QNAME = QName.create(URI.create("urn:ietf:params:xml:ns:netconf:base:1.0"), null, "operation");
79   private static final Logger logger = LoggerFactory.getLogger(XmlDocumentUtils.class);
80   private static final XMLOutputFactory FACTORY = XMLOutputFactory.newFactory();
81
82   /**
83    * Converts Data DOM structure to XML Document for specified XML Codec Provider and corresponding
84    * Data Node Container schema. The CompositeNode data parameter enters as root of Data DOM tree and will
85    * be transformed to root in XML Document. Each element of Data DOM tree is compared against specified Data
86    * Node Container Schema and transformed accordingly.
87    *
88    * @param data Data DOM root element
89    * @param schema Data Node Container Schema
90    * @param codecProvider XML Codec Provider
91    * @return new instance of XML Document
92    * @throws javax.activation.UnsupportedDataTypeException
93    */
94   public static Document toDocument(final CompositeNode data, final DataNodeContainer schema, final XmlCodecProvider codecProvider)
95       throws UnsupportedDataTypeException {
96     Preconditions.checkNotNull(data);
97     Preconditions.checkNotNull(schema);
98
99     if (!(schema instanceof ContainerSchemaNode || schema instanceof ListSchemaNode)) {
100       throw new UnsupportedDataTypeException("Schema can be ContainerSchemaNode or ListSchemaNode. Other types are not supported yet.");
101     }
102
103     final DOMResult result = new DOMResult(getDocument());
104     try {
105       final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(result);
106       XmlStreamUtils.create(codecProvider).writeDocument(writer, data, (SchemaNode)schema);
107       writer.close();
108       return (Document)result.getNode();
109     } catch (XMLStreamException e) {
110       logger.error("Failed to serialize data {}", data, e);
111       return null;
112     }
113   }
114
115   public static Document getDocument() {
116     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
117     Document doc = null;
118     try {
119       DocumentBuilder bob = dbf.newDocumentBuilder();
120       doc = bob.newDocument();
121     } catch (ParserConfigurationException e) {
122       throw new RuntimeException(e);
123     }
124     return doc;
125   }
126
127
128   public static QName qNameFromElement(final Element xmlElement) {
129     String namespace = xmlElement.getNamespaceURI();
130     String localName = xmlElement.getLocalName();
131     return QName.create(namespace != null ? URI.create(namespace) : null, null, localName);
132   }
133
134   private static Node<?> toNodeWithSchema(final Element xmlElement, final DataSchemaNode schema, final XmlCodecProvider codecProvider,final SchemaContext schemaCtx) {
135     checkQName(xmlElement, schema.getQName());
136     if (schema instanceof DataNodeContainer) {
137       return toCompositeNodeWithSchema(xmlElement, schema.getQName(), (DataNodeContainer) schema, schemaCtx);
138     } else if (schema instanceof LeafSchemaNode) {
139       return toSimpleNodeWithType(xmlElement, (LeafSchemaNode) schema, codecProvider,schemaCtx);
140     } else if (schema instanceof LeafListSchemaNode) {
141       return toSimpleNodeWithType(xmlElement, (LeafListSchemaNode) schema, codecProvider,schemaCtx);
142     }
143     return null;
144   }
145
146
147
148   private static Node<?> toSimpleNodeWithType(final Element xmlElement, final LeafSchemaNode schema,
149                                               final XmlCodecProvider codecProvider,final SchemaContext schemaCtx) {
150     TypeDefinitionAwareCodec<? extends Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
151     String text = xmlElement.getTextContent();
152     Object value = null;
153     if (codec != null) {
154       logger.debug("toSimpleNodeWithType: found codec, deserializing text {}", text);
155       value = codec.deserialize(text);
156     }
157
158     final TypeDefinition<?> baseType = XmlUtils.resolveBaseTypeFrom(schema.getType());
159     if (baseType instanceof InstanceIdentifierType) {
160       logger.debug("toSimpleNodeWithType: base type of node is instance identifier, deserializing element", xmlElement);
161       value = InstanceIdentifierForXmlCodec.deserialize(xmlElement,schemaCtx);
162
163     } else if(baseType instanceof IdentityrefTypeDefinition){
164       logger.debug("toSimpleNodeWithType: base type of node is IdentityrefTypeDefinition, deserializing element", xmlElement);
165       value = InstanceIdentifierForXmlCodec.toIdentity(xmlElement.getTextContent(), xmlElement, schemaCtx);
166
167     }
168
169     if (value == null) {
170       logger.debug("toSimpleNodeWithType: no type found for element, returning just the text string value of element {}", xmlElement);
171       value = xmlElement.getTextContent();
172     }
173
174     Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
175     return new SimpleNodeTOImpl<>(schema.getQName(), null, value, modifyAction.orNull());
176   }
177
178   private static Node<?> toSimpleNodeWithType(final Element xmlElement, final LeafListSchemaNode schema,
179                                               final XmlCodecProvider codecProvider,final SchemaContext schemaCtx) {
180     TypeDefinitionAwareCodec<? extends Object, ? extends TypeDefinition<?>> codec = codecProvider.codecFor(schema.getType());
181     String text = xmlElement.getTextContent();
182     Object value = null;
183     if (codec != null) {
184       logger.debug("toSimpleNodeWithType: found codec, deserializing text {}", text);
185       value = codec.deserialize(text);
186     }
187
188     final TypeDefinition<?> baseType = XmlUtils.resolveBaseTypeFrom(schema.getType());
189     if (baseType instanceof InstanceIdentifierType) {
190       logger.debug("toSimpleNodeWithType: base type of node is instance identifier, deserializing element", xmlElement);
191       value = InstanceIdentifierForXmlCodec.deserialize(xmlElement,schemaCtx);
192     }
193
194     if (value == null) {
195       logger.debug("toSimpleNodeWithType: no type found for element, returning just the text string value of element {}", xmlElement);
196       value = xmlElement.getTextContent();
197     }
198
199     Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
200     return new SimpleNodeTOImpl<>(schema.getQName(), null, value, modifyAction.orNull());
201   }
202
203   private static Node<?> toCompositeNodeWithSchema(final Element xmlElement, final QName qName, final DataNodeContainer schema,
204                                                    final SchemaContext schemaCtx) {
205     List<Node<?>> values = toDomNodes(xmlElement, Optional.fromNullable(schema.getChildNodes()),schemaCtx);
206     Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
207     return ImmutableCompositeNode.create(qName, values, modifyAction.orNull());
208   }
209
210   private static Optional<ModifyAction> getModifyOperationFromAttributes(final Element xmlElement) {
211     Attr attributeNodeNS = xmlElement.getAttributeNodeNS(OPERATION_ATTRIBUTE_QNAME.getNamespace().toString(), OPERATION_ATTRIBUTE_QNAME.getLocalName());
212     if(attributeNodeNS == null) {
213       return Optional.absent();
214     }
215
216     ModifyAction action = ModifyAction.fromXmlValue(attributeNodeNS.getValue());
217     Preconditions.checkArgument(action.isOnElementPermitted(), "Unexpected operation %s on %s", action, xmlElement);
218
219     return Optional.of(action);
220   }
221
222   private static void checkQName(final Element xmlElement, final QName qName) {
223     checkState(Objects.equal(xmlElement.getNamespaceURI(), qName.getNamespace().toString()));
224     checkState(qName.getLocalName().equals(xmlElement.getLocalName()));
225   }
226
227   public static final Optional<DataSchemaNode> findFirstSchema(final QName qname, final Collection<DataSchemaNode> dataSchemaNode) {
228     if (dataSchemaNode != null && !dataSchemaNode.isEmpty() && qname != null) {
229       for (DataSchemaNode dsn : dataSchemaNode) {
230         if (qname.isEqualWithoutRevision(dsn.getQName())) {
231           return Optional.<DataSchemaNode> of(dsn);
232         } else if (dsn instanceof ChoiceNode) {
233           for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
234             Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
235             if (foundDsn != null && foundDsn.isPresent()) {
236               return foundDsn;
237             }
238           }
239         }
240       }
241     }
242     return Optional.absent();
243   }
244
245   private static Node<?> toDomNode(Element element) {
246     QName qname = qNameFromElement(element);
247
248     ImmutableList.Builder<Node<?>> values = ImmutableList.<Node<?>> builder();
249     NodeList nodes = element.getChildNodes();
250     boolean isSimpleObject = true;
251     String value = null;
252     for (int i = 0; i < nodes.getLength(); i++) {
253       org.w3c.dom.Node child = nodes.item(i);
254       if (child instanceof Element) {
255         isSimpleObject = false;
256         values.add(toDomNode((Element) child));
257       }
258       if (isSimpleObject && child instanceof org.w3c.dom.Text) {
259         value = element.getTextContent();
260         if (!Strings.isNullOrEmpty(value)) {
261           isSimpleObject = true;
262         }
263       }
264     }
265     if (isSimpleObject) {
266       return new SimpleNodeTOImpl<>(qname, null, value);
267     }
268     return ImmutableCompositeNode.create(qname, values.build());
269   }
270
271   public static List<Node<?>> toDomNodes(final Element element, final Optional<Collection<DataSchemaNode>> context,SchemaContext schemaCtx) {
272     return forEachChild(element.getChildNodes(),schemaCtx, new Function<ElementWithSchemaContext, Optional<Node<?>>>() {
273
274       @Override
275       public Optional<Node<?>> apply(ElementWithSchemaContext input) {
276         if (context.isPresent()) {
277           QName partialQName = qNameFromElement(input.getElement());
278           Optional<DataSchemaNode> schemaNode = XmlDocumentUtils.findFirstSchema(partialQName, context.get());
279           if (schemaNode.isPresent()) {
280             return Optional.<Node<?>> fromNullable(//
281                 toNodeWithSchema(input.getElement(), schemaNode.get(), XmlDocumentUtils.defaultValueCodecProvider(),input.getSchemaContext()));
282           }
283         }
284         return Optional.<Node<?>> fromNullable(toDomNode(input.getElement()));
285       }
286
287     });
288
289   }
290
291   private static final <T> List<T> forEachChild(final NodeList nodes, final SchemaContext schemaContext, final Function<ElementWithSchemaContext, Optional<T>> forBody) {
292     final int l = nodes.getLength();
293     if (l == 0) {
294       return ImmutableList.of();
295     }
296
297     final List<T> list = new ArrayList<>(l);
298     for (int i = 0; i < l; i++) {
299       org.w3c.dom.Node child = nodes.item(i);
300       if (child instanceof Element) {
301         Optional<T> result = forBody.apply(new ElementWithSchemaContext((Element) child,schemaContext));
302         if (result.isPresent()) {
303           list.add(result.get());
304         }
305       }
306     }
307     return ImmutableList.copyOf(list);
308   }
309
310   public static final XmlCodecProvider defaultValueCodecProvider() {
311     return XmlUtils.DEFAULT_XML_CODEC_PROVIDER;
312   }
313 }