BUG-1473: update model.util.InstanceIdentifier users
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / utils / 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.remote.rpc.utils;
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     if (schema.getType() instanceof InstanceIdentifierType) {
189       logger.debug("toSimpleNodeWithType: base type of node is instance identifier, deserializing element", xmlElement);
190       value = InstanceIdentifierForXmlCodec.deserialize(xmlElement,schemaCtx);
191     }
192
193     if (value == null) {
194       logger.debug("toSimpleNodeWithType: no type found for element, returning just the text string value of element {}", xmlElement);
195       value = xmlElement.getTextContent();
196     }
197
198     Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
199     return new SimpleNodeTOImpl<>(schema.getQName(), null, value, modifyAction.orNull());
200   }
201
202   private static Node<?> toCompositeNodeWithSchema(final Element xmlElement, final QName qName, final DataNodeContainer schema,
203                                                    final SchemaContext schemaCtx) {
204     List<Node<?>> values = toDomNodes(xmlElement, Optional.fromNullable(schema.getChildNodes()),schemaCtx);
205     Optional<ModifyAction> modifyAction = getModifyOperationFromAttributes(xmlElement);
206     return ImmutableCompositeNode.create(qName, values, modifyAction.orNull());
207   }
208
209   private static Optional<ModifyAction> getModifyOperationFromAttributes(final Element xmlElement) {
210     Attr attributeNodeNS = xmlElement.getAttributeNodeNS(OPERATION_ATTRIBUTE_QNAME.getNamespace().toString(), OPERATION_ATTRIBUTE_QNAME.getLocalName());
211     if(attributeNodeNS == null) {
212       return Optional.absent();
213     }
214
215     ModifyAction action = ModifyAction.fromXmlValue(attributeNodeNS.getValue());
216     Preconditions.checkArgument(action.isOnElementPermitted(), "Unexpected operation %s on %s", action, xmlElement);
217
218     return Optional.of(action);
219   }
220
221   private static void checkQName(final Element xmlElement, final QName qName) {
222     checkState(Objects.equal(xmlElement.getNamespaceURI(), qName.getNamespace().toString()));
223     checkState(qName.getLocalName().equals(xmlElement.getLocalName()));
224   }
225
226   public static final Optional<DataSchemaNode> findFirstSchema(final QName qname, final Collection<DataSchemaNode> dataSchemaNode) {
227     if (dataSchemaNode != null && !dataSchemaNode.isEmpty() && qname != null) {
228       for (DataSchemaNode dsn : dataSchemaNode) {
229         if (qname.isEqualWithoutRevision(dsn.getQName())) {
230           return Optional.<DataSchemaNode> of(dsn);
231         } else if (dsn instanceof ChoiceNode) {
232           for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
233             Optional<DataSchemaNode> foundDsn = findFirstSchema(qname, choiceCase.getChildNodes());
234             if (foundDsn != null && foundDsn.isPresent()) {
235               return foundDsn;
236             }
237           }
238         }
239       }
240     }
241     return Optional.absent();
242   }
243
244   private static Node<?> toDomNode(Element element) {
245     QName qname = qNameFromElement(element);
246
247     ImmutableList.Builder<Node<?>> values = ImmutableList.<Node<?>> builder();
248     NodeList nodes = element.getChildNodes();
249     boolean isSimpleObject = true;
250     String value = null;
251     for (int i = 0; i < nodes.getLength(); i++) {
252       org.w3c.dom.Node child = nodes.item(i);
253       if (child instanceof Element) {
254         isSimpleObject = false;
255         values.add(toDomNode((Element) child));
256       }
257       if (isSimpleObject && child instanceof org.w3c.dom.Text) {
258         value = element.getTextContent();
259         if (!Strings.isNullOrEmpty(value)) {
260           isSimpleObject = true;
261         }
262       }
263     }
264     if (isSimpleObject) {
265       return new SimpleNodeTOImpl<>(qname, null, value);
266     }
267     return ImmutableCompositeNode.create(qname, values.build());
268   }
269
270   public static List<Node<?>> toDomNodes(final Element element, final Optional<Collection<DataSchemaNode>> context,SchemaContext schemaCtx) {
271     return forEachChild(element.getChildNodes(),schemaCtx, new Function<ElementWithSchemaContext, Optional<Node<?>>>() {
272
273       @Override
274       public Optional<Node<?>> apply(ElementWithSchemaContext input) {
275         if (context.isPresent()) {
276           QName partialQName = qNameFromElement(input.getElement());
277           Optional<DataSchemaNode> schemaNode = XmlDocumentUtils.findFirstSchema(partialQName, context.get());
278           if (schemaNode.isPresent()) {
279             return Optional.<Node<?>> fromNullable(//
280                 toNodeWithSchema(input.getElement(), schemaNode.get(), XmlDocumentUtils.defaultValueCodecProvider(),input.getSchemaContext()));
281           }
282         }
283         return Optional.<Node<?>> fromNullable(toDomNode(input.getElement()));
284       }
285
286     });
287
288   }
289
290   private static final <T> List<T> forEachChild(final NodeList nodes, final SchemaContext schemaContext, final Function<ElementWithSchemaContext, Optional<T>> forBody) {
291     final int l = nodes.getLength();
292     if (l == 0) {
293       return ImmutableList.of();
294     }
295
296     final List<T> list = new ArrayList<>(l);
297     for (int i = 0; i < l; i++) {
298       org.w3c.dom.Node child = nodes.item(i);
299       if (child instanceof Element) {
300         Optional<T> result = forBody.apply(new ElementWithSchemaContext((Element) child,schemaContext));
301         if (result.isPresent()) {
302           list.add(result.get());
303         }
304       }
305     }
306     return ImmutableList.copyOf(list);
307   }
308
309   public static final XmlCodecProvider defaultValueCodecProvider() {
310     return XmlUtils.DEFAULT_XML_CODEC_PROVIDER;
311   }
312 }