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