aboutsummaryrefslogtreecommitdiffstats
path: root/common/src/main/java/org/openecomp/mso/yangDecoder/transform/impl/YangDataTransformNN2XMLServiceImpl.java
blob: d147a803ccc554e7c647e15d717f198c900ed62a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package org.openecomp.mso.yangDecoder.transform.impl;

import com.google.common.base.Charsets;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.DomUtils;
import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.parser.DomToNormalizedNodeParserFactory;
import org.opendaylight.yangtools.yang.model.api.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLOutputFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static org.openecomp.mso.yangDecoder.transform.impl.JsonParserStream.getWrapSchemaNode;

public class YangDataTransformNN2XMLServiceImpl {
    private static final XMLOutputFactory XML_FACTORY;
    private static final DocumentBuilderFactory BUILDERFACTORY;
    static {
        XML_FACTORY = XMLOutputFactory.newFactory();
        XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setCoalescing(true);
        factory.setIgnoringElementContentWhitespace(true);
        factory.setIgnoringComments(true);
        BUILDERFACTORY = factory;
    }
    BindingNormalizedNodeSerializer mappingservice;
    SchemaContext schemaContext;
    private static final Logger LOG = LoggerFactory.getLogger(YangDataTransformNN2XMLServiceImpl.class);

    public YangDataTransformNN2XMLServiceImpl(BindingNormalizedNodeSerializer mappingservice, SchemaContext context ) {

        this.schemaContext = context;
        this.mappingservice=mappingservice;
    }

    public String transformNNToString(NormalizedNode nn)
    {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(YangOdlNNC2XMLImpl.getXMLHeader());
        new NormalizedNodeNavigator(new NormalizedNodePrinter(stringBuilder)).navigate(null, nn);
        return stringBuilder.toString();
    }


    public static DataSchemaNode getSchemaNodebyNs(final SchemaContext context, final String ns, final String childNodeName) {
        for (Module module : context.getModules()) {
            if (module.getNamespace().toString().equals(ns)) {
                DataSchemaNode found = findChildNode(module.getChildNodes(), childNodeName);
                Preconditions.checkState(found != null, "Unable to find %s", childNodeName);
                return found;
            }
        }
        throw new IllegalStateException("Unable to find child node " + childNodeName);
    }
    private static DataSchemaNode findChildNode(final Iterable<DataSchemaNode> children, final String name) {
        List<DataNodeContainer> containers = Lists.newArrayList();

        for (DataSchemaNode dataSchemaNode : children) {
            if (dataSchemaNode.getQName().getLocalName().equals(name)) {
                return dataSchemaNode;
            }
            if (dataSchemaNode instanceof DataNodeContainer) {
                containers.add((DataNodeContainer) dataSchemaNode);
            } else if (dataSchemaNode instanceof ChoiceSchemaNode) {
                containers.addAll(((ChoiceSchemaNode) dataSchemaNode).getCases());
            }
        }

        for (DataNodeContainer container : containers) {
            DataSchemaNode retVal = findChildNode(container.getChildNodes(), name);
            if (retVal != null) {
                return retVal;
            }
        }

        return null;
    }
    public NormalizedNode transformNNFromString(String sxml) throws Exception {
        InputStream in_nocode = new ByteArrayInputStream(sxml.getBytes(Charsets.UTF_8));//.getBytes("UTF-8")
         //xml2nn
            final Document docxml = readXmlToDocument(in_nocode);
            Element element0 = docxml.getDocumentElement();
            String localname = element0.getNodeName();
            String ns = element0.getAttribute("xmlns");
           // final ContainerSchemaNode containerNodex = (ContainerSchemaNode)null;
           final DataSchemaNode dsn=getSchemaNodebyNs(schemaContext, ns, localname);
            //cn.getNodeType().getNamespace().toString(), cn.getNodeType().getLocalName());
        DomToNormalizedNodeParserFactory parserFactory =DomToNormalizedNodeParserFactory.getInstance(DomUtils.defaultValueCodecProvider(), schemaContext);
        NormalizedNode parsed = null;
        final List<Element> elements = Collections.singletonList(docxml.getDocumentElement());
        if (dsn instanceof ContainerSchemaNode) {
            parsed= parserFactory.getContainerNodeParser().parse(elements, (ContainerSchemaNode)dsn);
        }else   if (dsn instanceof ListSchemaNode) {
            final ListSchemaNode casted = (ListSchemaNode) dsn;
            parsed=parserFactory.getMapEntryNodeParser().parse(elements, casted);
        }
        return parsed;

    }



    public NormalizedNode transformNotificationNNfromString(String sxml, final InstanceIdentifierContext<?> iicontext) throws Exception
    {
        SchemaNode schemaNode = getWrapSchemaNode(iicontext.getSchemaNode());
        InputStream in_nocode = new ByteArrayInputStream(sxml.getBytes(Charsets.UTF_8));//.getBytes("UTF-8")
        //xml2nn
        final Document docxml = readXmlToDocument(in_nocode);
        Element element0 = docxml.getDocumentElement();
        String localname = element0.getNodeName();
        String ns = element0.getAttribute("xmlns");
        // final ContainerSchemaNode containerNodex = (ContainerSchemaNode)null;
        //  final DataSchemaNode dsn=(DataSchemaNode)schemaNode;/*getSchemaNodebyNs(schemaContext, ns, localname);*/
        //cn.getNodeType().getNamespace().toString(), cn.getNodeType().getLocalName());
        DomToNormalizedNodeParserFactory parserFactory =DomToNormalizedNodeParserFactory.getInstance(DomUtils.defaultValueCodecProvider(), schemaContext);
        NormalizedNode parsed = null;
        final List<Element> elements = Collections.singletonList(docxml.getDocumentElement());
        if (schemaNode instanceof ContainerSchemaNode) {
            parsed= parserFactory.getContainerNodeParser().parse(elements, (ContainerSchemaNode)schemaNode);
        }else   if (schemaNode instanceof ListSchemaNode) {
            final ListSchemaNode casted = (ListSchemaNode) schemaNode;
            parsed=parserFactory.getMapEntryNodeParser().parse(elements, casted);
        }
        return parsed;
    }
    public NormalizedNode transformNotificationStringtoNN(String sxml,String notficationName) throws Exception {
        final InstanceIdentifierContext<?> iicontext= ControllerContext.getInstance().toInstanceIdentifier(notficationName);
         return transformNotificationNNfromString(sxml,iicontext);
    }
    public Map.Entry<DataSchemaNode, NormalizedNode>  transformRpcNNEntryfromString(String sxml, final InstanceIdentifierContext<?> iirpccontext) throws Exception{
        InputStream in_nocode = new ByteArrayInputStream(sxml.getBytes(Charsets.UTF_8));//.getBytes("UTF-8")
        //xml2nn
        final Document docxml = readXmlToDocument(in_nocode);
        Element element0 = docxml.getDocumentElement();
        String localname = element0.getNodeName();
        String ns = element0.getAttribute("xmlns");
        DataSchemaNode schemaNode;
        final SchemaNode schemaNode0 = iirpccontext.getSchemaNode();
        boolean isInput = false;
        if (schemaNode0 instanceof RpcDefinition) {
            if (docxml.getDocumentElement().getLocalName().contains("output")) {
                schemaNode = ((RpcDefinition) schemaNode0).getOutput();
                isInput = false;
            } else {
                schemaNode = ((RpcDefinition) schemaNode0).getInput();
                isInput = true;
            }

        } else if (docxml instanceof DataSchemaNode) {
            schemaNode = (DataSchemaNode) docxml;
        } else {
            throw new IllegalStateException("Unknow SchemaNode");
        }

        final List<YangInstanceIdentifier.PathArgument> iiToDataList = new ArrayList<>();
        InstanceIdentifierContext<? extends SchemaNode> outIIContext;
        // final ContainerSchemaNode containerNodex = (ContainerSchemaNode)null;
        //  final DataSchemaNode dsn=(DataSchemaNode)schemaNode;/*getSchemaNodebyNs(schemaContext, ns, localname);*/
        //cn.getNodeType().getNamespace().toString(), cn.getNodeType().getLocalName());
        DomToNormalizedNodeParserFactory parserFactory =DomToNormalizedNodeParserFactory.getInstance(DomUtils.defaultValueCodecProvider(), schemaContext);
        NormalizedNode parsed = null;
        final List<Element> elements = Collections.singletonList(docxml.getDocumentElement());
        if (schemaNode instanceof ContainerSchemaNode) {
            parsed= parserFactory.getContainerNodeParser().parse(elements, (ContainerSchemaNode)schemaNode);
        }else   if (schemaNode instanceof ListSchemaNode) {
            final ListSchemaNode casted = (ListSchemaNode) schemaNode;
            parsed=parserFactory.getMapEntryNodeParser().parse(elements, casted);
        }
        return new SimpleEntry<DataSchemaNode, NormalizedNode>(schemaNode,parsed);
    }
    public NormalizedNode transformRpcNNfromString(String sxml, final InstanceIdentifierContext<?> iirpccontext) throws Exception{

        InputStream in_nocode = new ByteArrayInputStream(sxml.getBytes(Charsets.UTF_8));//.getBytes("UTF-8")
        //xml2nn
        final Document docxml = readXmlToDocument(in_nocode);
        Element element0 = docxml.getDocumentElement();
        String localname = element0.getNodeName();
        String ns = element0.getAttribute("xmlns");
        DataSchemaNode schemaNode;
        final SchemaNode schemaNode0 = iirpccontext.getSchemaNode();
        boolean isInput = false;
        if (schemaNode0 instanceof RpcDefinition) {
            if (docxml.getDocumentElement().getLocalName().contains("output")) {
                schemaNode = ((RpcDefinition) schemaNode0).getOutput();
                isInput = false;
            } else {
                schemaNode = ((RpcDefinition) schemaNode0).getInput();
                isInput = true;
            }

        } else if (docxml instanceof DataSchemaNode) {
            schemaNode = (DataSchemaNode) docxml;
        } else {
            throw new IllegalStateException("Unknow SchemaNode");
        }

        final List<YangInstanceIdentifier.PathArgument> iiToDataList = new ArrayList<>();
        InstanceIdentifierContext<? extends SchemaNode> outIIContext;
        // final ContainerSchemaNode containerNodex = (ContainerSchemaNode)null;
        //  final DataSchemaNode dsn=(DataSchemaNode)schemaNode;/*getSchemaNodebyNs(schemaContext, ns, localname);*/
        //cn.getNodeType().getNamespace().toString(), cn.getNodeType().getLocalName());
        DomToNormalizedNodeParserFactory parserFactory =DomToNormalizedNodeParserFactory.getInstance(DomUtils.defaultValueCodecProvider(), schemaContext);
        NormalizedNode parsed = null;
        final List<Element> elements = Collections.singletonList(docxml.getDocumentElement());
        if (schemaNode instanceof ContainerSchemaNode) {
            parsed= parserFactory.getContainerNodeParser().parse(elements, (ContainerSchemaNode)schemaNode);
        }else   if (schemaNode instanceof ListSchemaNode) {
            final ListSchemaNode casted = (ListSchemaNode) schemaNode;
            parsed=parserFactory.getMapEntryNodeParser().parse(elements, casted);
        }
        return parsed;
    }
    public NormalizedNode transformRpcNNfromString(String sxml, String rpcName) throws Exception {
        final InstanceIdentifierContext<?> iicontext= ControllerContext.getInstance().toInstanceIdentifier(rpcName);
        return this.transformRpcNNfromString(sxml,iicontext);

    }
    public static Document readXmlToDocument(final InputStream xmlContent) throws IOException, SAXException {
        final DocumentBuilder dBuilder;
        try {
            dBuilder = BUILDERFACTORY.newDocumentBuilder();
        } catch (final ParserConfigurationException e) {
            throw new RuntimeException("Failed to parse XML document", e);
        }
        final Document doc = dBuilder.parse(xmlContent);

        doc.getDocumentElement().normalize();
        return doc;
    }

}