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
|
/*
* Copyright (c) 2016 Cisco and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fd.honeycomb.notification.impl;
import java.io.IOException;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.dom.DOMResult;
import org.opendaylight.controller.config.util.xml.XmlUtil;
import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
import org.opendaylight.netconf.notifications.NetconfNotification;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
import org.opendaylight.yangtools.yang.data.impl.codec.xml.XMLStreamNormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public final class TranslationUtil {
public TranslationUtil() {}
private static final Logger LOG = LoggerFactory.getLogger(TranslationUtil.class);
private static final XMLOutputFactory XML_FACTORY;
static {
XML_FACTORY = XMLOutputFactory.newFactory();
XML_FACTORY.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);
}
/**
* Transform {@link org.opendaylight.mdsal.dom.api.DOMNotification} into an XML based {@link NetconfNotification}.
*/
public static NetconfNotification notificationToXml(final DOMNotification domNotification, final SchemaContext ctx) {
LOG.trace("Transforming notification: {} into XML", domNotification.getType());
final SchemaPath type = domNotification.getType();
final QName notificationQName = type.getLastComponent();
final DOMResult result = prepareDomResultForRpcRequest(notificationQName);
try {
writeNormalizedRpc(domNotification, result, type, ctx);
} catch (final XMLStreamException | IOException | IllegalStateException e) {
LOG.warn("Unable to transform notification: {} into XML", domNotification.getType(), e);
throw new IllegalArgumentException("Unable to serialize " + type, e);
}
final Document node = result.getNode().getOwnerDocument();
return new NetconfNotification(node);
}
private static DOMResult prepareDomResultForRpcRequest(final QName notificationQName) {
final Document document = XmlUtil.newDocument();
final Element notificationElement =
document.createElementNS(notificationQName.getNamespace().toString(), notificationQName.getLocalName());
document.appendChild(notificationElement);
return new DOMResult(notificationElement);
}
private static void writeNormalizedRpc(final DOMNotification normalized, final DOMResult result,
final SchemaPath schemaPath, final SchemaContext baseNetconfCtx)
throws IOException, XMLStreamException {
final XMLStreamWriter writer = XML_FACTORY.createXMLStreamWriter(result);
try {
try (final NormalizedNodeStreamWriter normalizedNodeStreamWriter =
XMLStreamNormalizedNodeStreamWriter.create(writer, baseNetconfCtx, schemaPath)) {
try (final NormalizedNodeWriter normalizedNodeWriter =
NormalizedNodeWriter.forStreamWriter(normalizedNodeStreamWriter)) {
for (DataContainerChild<?, ?> dataContainerChild : normalized.getBody().getValue()) {
normalizedNodeWriter.write(dataContainerChild);
}
normalizedNodeWriter.flush();
}
}
} finally {
try {
writer.close();
} catch (final Exception e) {
LOG.warn("Unable to close resource properly. Ignoring", e);
}
}
}
}
|