1 package org.openecomp.mso.yangDecoder.transform.impl;
3 import com.google.common.base.Charsets;
4 import com.google.common.base.Preconditions;
5 import com.google.common.collect.Lists;
6 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
7 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
8 import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer;
9 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
10 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
11 import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.DomUtils;
12 import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.parser.DomToNormalizedNodeParserFactory;
13 import org.opendaylight.yangtools.yang.model.api.*;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16 import org.w3c.dom.Document;
17 import org.w3c.dom.Element;
18 import org.xml.sax.SAXException;
20 import javax.xml.parsers.DocumentBuilder;
21 import javax.xml.parsers.DocumentBuilderFactory;
22 import javax.xml.parsers.ParserConfigurationException;
23 import javax.xml.stream.XMLOutputFactory;
24 import java.io.ByteArrayInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.util.AbstractMap.SimpleEntry;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
33 import static org.openecomp.mso.yangDecoder.transform.impl.JsonParserStream.getWrapSchemaNode;
35 public class YangDataTransformNN2XMLServiceImpl {
36 private static final XMLOutputFactory XML_FACTORY;
37 private static final DocumentBuilderFactory BUILDERFACTORY;
39 XML_FACTORY = XMLOutputFactory.newFactory();
40 XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);
41 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
42 factory.setNamespaceAware(true);
43 factory.setCoalescing(true);
44 factory.setIgnoringElementContentWhitespace(true);
45 factory.setIgnoringComments(true);
46 BUILDERFACTORY = factory;
48 BindingNormalizedNodeSerializer mappingservice;
49 SchemaContext schemaContext;
50 private static final Logger LOG = LoggerFactory.getLogger(YangDataTransformNN2XMLServiceImpl.class);
52 public YangDataTransformNN2XMLServiceImpl(BindingNormalizedNodeSerializer mappingservice, SchemaContext context ) {
54 this.schemaContext = context;
55 this.mappingservice=mappingservice;
58 public String transformNNToString(NormalizedNode nn)
60 StringBuilder stringBuilder = new StringBuilder();
61 stringBuilder.append(YangOdlNNC2XMLImpl.getXMLHeader());
62 new NormalizedNodeNavigator(new NormalizedNodePrinter(stringBuilder)).navigate(null, nn);
63 return stringBuilder.toString();
67 public static DataSchemaNode getSchemaNodebyNs(final SchemaContext context, final String ns, final String childNodeName) {
68 for (Module module : context.getModules()) {
69 if (module.getNamespace().toString().equals(ns)) {
70 DataSchemaNode found = findChildNode(module.getChildNodes(), childNodeName);
71 Preconditions.checkState(found != null, "Unable to find %s", childNodeName);
75 throw new IllegalStateException("Unable to find child node " + childNodeName);
77 private static DataSchemaNode findChildNode(final Iterable<DataSchemaNode> children, final String name) {
78 List<DataNodeContainer> containers = Lists.newArrayList();
80 for (DataSchemaNode dataSchemaNode : children) {
81 if (dataSchemaNode.getQName().getLocalName().equals(name)) {
82 return dataSchemaNode;
84 if (dataSchemaNode instanceof DataNodeContainer) {
85 containers.add((DataNodeContainer) dataSchemaNode);
86 } else if (dataSchemaNode instanceof ChoiceSchemaNode) {
87 containers.addAll(((ChoiceSchemaNode) dataSchemaNode).getCases());
91 for (DataNodeContainer container : containers) {
92 DataSchemaNode retVal = findChildNode(container.getChildNodes(), name);
100 public NormalizedNode transformNNFromString(String sxml) throws Exception {
101 InputStream in_nocode = new ByteArrayInputStream(sxml.getBytes(Charsets.UTF_8));//.getBytes("UTF-8")
103 final Document docxml = readXmlToDocument(in_nocode);
104 Element element0 = docxml.getDocumentElement();
105 String localname = element0.getNodeName();
106 String ns = element0.getAttribute("xmlns");
107 // final ContainerSchemaNode containerNodex = (ContainerSchemaNode)null;
108 final DataSchemaNode dsn=getSchemaNodebyNs(schemaContext, ns, localname);
109 //cn.getNodeType().getNamespace().toString(), cn.getNodeType().getLocalName());
110 DomToNormalizedNodeParserFactory parserFactory =DomToNormalizedNodeParserFactory.getInstance(DomUtils.defaultValueCodecProvider(), schemaContext);
111 NormalizedNode parsed = null;
112 final List<Element> elements = Collections.singletonList(docxml.getDocumentElement());
113 if (dsn instanceof ContainerSchemaNode) {
114 parsed= parserFactory.getContainerNodeParser().parse(elements, (ContainerSchemaNode)dsn);
115 }else if (dsn instanceof ListSchemaNode) {
116 final ListSchemaNode casted = (ListSchemaNode) dsn;
117 parsed=parserFactory.getMapEntryNodeParser().parse(elements, casted);
125 public NormalizedNode transformNotificationNNfromString(String sxml, final InstanceIdentifierContext<?> iicontext) throws Exception
127 SchemaNode schemaNode = getWrapSchemaNode(iicontext.getSchemaNode());
128 InputStream in_nocode = new ByteArrayInputStream(sxml.getBytes(Charsets.UTF_8));//.getBytes("UTF-8")
130 final Document docxml = readXmlToDocument(in_nocode);
131 Element element0 = docxml.getDocumentElement();
132 String localname = element0.getNodeName();
133 String ns = element0.getAttribute("xmlns");
134 // final ContainerSchemaNode containerNodex = (ContainerSchemaNode)null;
135 // final DataSchemaNode dsn=(DataSchemaNode)schemaNode;/*getSchemaNodebyNs(schemaContext, ns, localname);*/
136 //cn.getNodeType().getNamespace().toString(), cn.getNodeType().getLocalName());
137 DomToNormalizedNodeParserFactory parserFactory =DomToNormalizedNodeParserFactory.getInstance(DomUtils.defaultValueCodecProvider(), schemaContext);
138 NormalizedNode parsed = null;
139 final List<Element> elements = Collections.singletonList(docxml.getDocumentElement());
140 if (schemaNode instanceof ContainerSchemaNode) {
141 parsed= parserFactory.getContainerNodeParser().parse(elements, (ContainerSchemaNode)schemaNode);
142 }else if (schemaNode instanceof ListSchemaNode) {
143 final ListSchemaNode casted = (ListSchemaNode) schemaNode;
144 parsed=parserFactory.getMapEntryNodeParser().parse(elements, casted);
148 public NormalizedNode transformNotificationStringtoNN(String sxml,String notficationName) throws Exception {
149 final InstanceIdentifierContext<?> iicontext= ControllerContext.getInstance().toInstanceIdentifier(notficationName);
150 return transformNotificationNNfromString(sxml,iicontext);
152 public Map.Entry<DataSchemaNode, NormalizedNode> transformRpcNNEntryfromString(String sxml, final InstanceIdentifierContext<?> iirpccontext) throws Exception{
153 InputStream in_nocode = new ByteArrayInputStream(sxml.getBytes(Charsets.UTF_8));//.getBytes("UTF-8")
155 final Document docxml = readXmlToDocument(in_nocode);
156 Element element0 = docxml.getDocumentElement();
157 String localname = element0.getNodeName();
158 String ns = element0.getAttribute("xmlns");
159 DataSchemaNode schemaNode;
160 final SchemaNode schemaNode0 = iirpccontext.getSchemaNode();
161 boolean isInput = false;
162 if (schemaNode0 instanceof RpcDefinition) {
163 if (docxml.getDocumentElement().getLocalName().contains("output")) {
164 schemaNode = ((RpcDefinition) schemaNode0).getOutput();
167 schemaNode = ((RpcDefinition) schemaNode0).getInput();
171 } else if (docxml instanceof DataSchemaNode) {
172 schemaNode = (DataSchemaNode) docxml;
174 throw new IllegalStateException("Unknow SchemaNode");
177 final List<YangInstanceIdentifier.PathArgument> iiToDataList = new ArrayList<>();
178 InstanceIdentifierContext<? extends SchemaNode> outIIContext;
179 // final ContainerSchemaNode containerNodex = (ContainerSchemaNode)null;
180 // final DataSchemaNode dsn=(DataSchemaNode)schemaNode;/*getSchemaNodebyNs(schemaContext, ns, localname);*/
181 //cn.getNodeType().getNamespace().toString(), cn.getNodeType().getLocalName());
182 DomToNormalizedNodeParserFactory parserFactory =DomToNormalizedNodeParserFactory.getInstance(DomUtils.defaultValueCodecProvider(), schemaContext);
183 NormalizedNode parsed = null;
184 final List<Element> elements = Collections.singletonList(docxml.getDocumentElement());
185 if (schemaNode instanceof ContainerSchemaNode) {
186 parsed= parserFactory.getContainerNodeParser().parse(elements, (ContainerSchemaNode)schemaNode);
187 }else if (schemaNode instanceof ListSchemaNode) {
188 final ListSchemaNode casted = (ListSchemaNode) schemaNode;
189 parsed=parserFactory.getMapEntryNodeParser().parse(elements, casted);
191 return new SimpleEntry<DataSchemaNode, NormalizedNode>(schemaNode,parsed);
193 public NormalizedNode transformRpcNNfromString(String sxml, final InstanceIdentifierContext<?> iirpccontext) throws Exception{
195 InputStream in_nocode = new ByteArrayInputStream(sxml.getBytes(Charsets.UTF_8));//.getBytes("UTF-8")
197 final Document docxml = readXmlToDocument(in_nocode);
198 Element element0 = docxml.getDocumentElement();
199 String localname = element0.getNodeName();
200 String ns = element0.getAttribute("xmlns");
201 DataSchemaNode schemaNode;
202 final SchemaNode schemaNode0 = iirpccontext.getSchemaNode();
203 boolean isInput = false;
204 if (schemaNode0 instanceof RpcDefinition) {
205 if (docxml.getDocumentElement().getLocalName().contains("output")) {
206 schemaNode = ((RpcDefinition) schemaNode0).getOutput();
209 schemaNode = ((RpcDefinition) schemaNode0).getInput();
213 } else if (docxml instanceof DataSchemaNode) {
214 schemaNode = (DataSchemaNode) docxml;
216 throw new IllegalStateException("Unknow SchemaNode");
219 final List<YangInstanceIdentifier.PathArgument> iiToDataList = new ArrayList<>();
220 InstanceIdentifierContext<? extends SchemaNode> outIIContext;
221 // final ContainerSchemaNode containerNodex = (ContainerSchemaNode)null;
222 // final DataSchemaNode dsn=(DataSchemaNode)schemaNode;/*getSchemaNodebyNs(schemaContext, ns, localname);*/
223 //cn.getNodeType().getNamespace().toString(), cn.getNodeType().getLocalName());
224 DomToNormalizedNodeParserFactory parserFactory =DomToNormalizedNodeParserFactory.getInstance(DomUtils.defaultValueCodecProvider(), schemaContext);
225 NormalizedNode parsed = null;
226 final List<Element> elements = Collections.singletonList(docxml.getDocumentElement());
227 if (schemaNode instanceof ContainerSchemaNode) {
228 parsed= parserFactory.getContainerNodeParser().parse(elements, (ContainerSchemaNode)schemaNode);
229 }else if (schemaNode instanceof ListSchemaNode) {
230 final ListSchemaNode casted = (ListSchemaNode) schemaNode;
231 parsed=parserFactory.getMapEntryNodeParser().parse(elements, casted);
235 public NormalizedNode transformRpcNNfromString(String sxml, String rpcName) throws Exception {
236 final InstanceIdentifierContext<?> iicontext= ControllerContext.getInstance().toInstanceIdentifier(rpcName);
237 return this.transformRpcNNfromString(sxml,iicontext);
240 public static Document readXmlToDocument(final InputStream xmlContent) throws IOException, SAXException {
241 final DocumentBuilder dBuilder;
243 dBuilder = BUILDERFACTORY.newDocumentBuilder();
244 } catch (final ParserConfigurationException e) {
245 throw new RuntimeException("Failed to parse XML document", e);
247 final Document doc = dBuilder.parse(xmlContent);
249 doc.getDocumentElement().normalize();