use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.
the class SimulatedGetConfig method loadInitialConfigXMLFile.
private static DataList loadInitialConfigXMLFile(final File file) {
LOG.info("Loading initial config xml file: {}", file.getName());
DataList configData = new DataList();
try {
Element element = XmlUtil.readXmlToElement(file);
XmlElement xmlElement = XmlElement.fromDomElement(element);
List<XmlElement> xmlElementList = xmlElement.getChildElements();
configData.setConfigList(xmlElementList);
} catch (IOException e) {
LOG.info("IO exception loading xml file: {} ", e.getMessage());
} catch (SAXException e) {
LOG.info("SAXException {}", e.getMessage());
}
return configData;
}
use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.
the class EXIParameters method fromXmlElement.
public static EXIParameters fromXmlElement(final XmlElement root) throws UnsupportedOption {
final CodingMode coding;
final NodeList alignmentElements = root.getElementsByTagName(EXI_PARAMETER_ALIGNMENT);
if (alignmentElements.getLength() > 0) {
final Element alignmentElement = (Element) alignmentElements.item(0);
final String alignmentTextContent = alignmentElement.getTextContent().trim();
switch(alignmentTextContent) {
case EXI_PARAMETER_BYTE_ALIGNED:
coding = CodingMode.BYTE_PACKED;
break;
case EXI_PARAMETER_COMPRESSED:
coding = CodingMode.COMPRESSION;
break;
case EXI_PARAMETER_PRE_COMPRESSION:
coding = CodingMode.PRE_COMPRESSION;
break;
case EXI_PARAMETER_BIT_PACKED:
coding = CodingMode.BIT_PACKED;
break;
default:
LOG.warn("Unexpected value in alignmentTextContent: {} , using default value", alignmentTextContent);
coding = CodingMode.BIT_PACKED;
break;
}
} else {
coding = CodingMode.BIT_PACKED;
}
final FidelityOptions fidelity = FidelityOptions.createDefault();
final NodeList fidelityElements = root.getElementsByTagName(EXI_PARAMETER_FIDELITY);
if (fidelityElements.getLength() > 0) {
final Element fidelityElement = (Element) fidelityElements.item(0);
fidelity.setFidelity(FidelityOptions.FEATURE_DTD, fidelityElement.getElementsByTagName(EXI_FIDELITY_DTD).getLength() > 0);
fidelity.setFidelity(FidelityOptions.FEATURE_LEXICAL_VALUE, fidelityElement.getElementsByTagName(EXI_FIDELITY_LEXICAL_VALUES).getLength() > 0);
fidelity.setFidelity(FidelityOptions.FEATURE_COMMENT, fidelityElement.getElementsByTagName(EXI_FIDELITY_COMMENTS).getLength() > 0);
fidelity.setFidelity(FidelityOptions.FEATURE_PI, fidelityElement.getElementsByTagName(EXI_FIDELITY_PIS).getLength() > 0);
fidelity.setFidelity(FidelityOptions.FEATURE_PREFIX, fidelityElement.getElementsByTagName(EXI_FIDELITY_PREFIXES).getLength() > 0);
}
final EXISchema schema;
final NodeList schemaElements = root.getElementsByTagName(EXI_PARAMETER_SCHEMAS);
if (schemaElements.getLength() > 0) {
final Element schemaElement = (Element) schemaElements.item(0);
final String schemaName = schemaElement.getTextContent().trim();
schema = EXISchema.forOption(schemaName);
checkArgument(schema != null, "Unsupported schema name %s", schemaName);
} else {
schema = EXISchema.NONE;
}
return new EXIParameters(coding, fidelity, schema);
}
use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.
the class NetconfHelloMessage method isHelloMessage.
private static boolean isHelloMessage(final Document document) {
final XmlElement element = XmlElement.fromDomElement(document.getDocumentElement());
if (!HELLO_TAG.equals(element.getName())) {
return false;
}
final Optional<String> optNamespace = element.getNamespaceOptionally();
// accept even if hello has no namespace
return optNamespace.isEmpty() || XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0.equals(optNamespace.get());
}
use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.
the class NetconfMessageTransformer method toNotification.
@Override
public synchronized DOMNotification toNotification(final NetconfMessage message) {
final Entry<Instant, XmlElement> stripped = NetconfMessageTransformUtil.stripNotification(message);
final QName notificationNoRev;
try {
notificationNoRev = QName.create(stripped.getValue().getNamespace(), stripped.getValue().getName()).withoutRevision();
} catch (final MissingNameSpaceException e) {
throw new IllegalArgumentException("Unable to parse notification " + message + ", cannot find namespace", e);
}
Collection<? extends NotificationDefinition> notificationDefinitions = mappedNotifications.get(notificationNoRev);
Element element = stripped.getValue().getDomElement();
NestedNotificationInfo nestedNotificationInfo = null;
if (notificationDefinitions.isEmpty()) {
// check if notification is nested notification
Optional<NestedNotificationInfo> nestedNotificationOptional = findNestedNotification(message, element);
if (nestedNotificationOptional.isPresent()) {
nestedNotificationInfo = nestedNotificationOptional.get();
notificationDefinitions = Collections.singletonList(nestedNotificationInfo.notificationDefinition);
element = (Element) nestedNotificationInfo.notificationNode;
}
}
Preconditions.checkArgument(notificationDefinitions.size() > 0, "Unable to parse notification %s, unknown notification. Available notifications: %s", notificationDefinitions, mappedNotifications.keySet());
final NotificationDefinition mostRecentNotification = getMostRecentNotification(notificationDefinitions);
final ContainerNode content;
try {
final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
final XmlParserStream xmlParser = XmlParserStream.create(writer, mountContext, SchemaInferenceStack.ofInstantiatedPath(mountContext.getEffectiveModelContext(), mostRecentNotification.getPath()).toInference(), strictParsing);
xmlParser.traverse(new DOMSource(element));
content = (ContainerNode) resultHolder.getResult();
} catch (XMLStreamException | URISyntaxException | IOException | SAXException | UnsupportedOperationException e) {
throw new IllegalArgumentException(String.format("Failed to parse notification %s", element), e);
}
if (nestedNotificationInfo != null) {
return new NetconfDeviceTreeNotification(content, // FIXME: improve this to cache the path
mostRecentNotification.getPath().asAbsolute(), stripped.getKey(), nestedNotificationInfo.domDataTreeIdentifier);
}
return new NetconfDeviceNotification(content, stripped.getKey());
}
use of org.opendaylight.netconf.api.xml.XmlElement in project netconf by opendaylight.
the class NetconfMessageTransformer method parseResult.
private NormalizedNode parseResult(final NetconfMessage message, final OperationDefinition operationDefinition) {
final Optional<XmlElement> okResponseElement = XmlElement.fromDomDocument(message.getDocument()).getOnlyChildElementWithSameNamespaceOptionally("ok");
if (operationDefinition.getOutput().getChildNodes().isEmpty()) {
Preconditions.checkArgument(okResponseElement.isPresent(), "Unexpected content in response of rpc: %s, %s", operationDefinition.getQName(), message);
return null;
} else {
if (okResponseElement.isPresent()) {
LOG.debug("Received response <ok/> for RPC with defined Output");
return null;
}
Element element = message.getDocument().getDocumentElement();
try {
final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
final XmlParserStream xmlParser = XmlParserStream.create(writer, mountContext, // FIXME: we should have a cached inference here
SchemaInferenceStack.ofInstantiatedPath(mountContext.getEffectiveModelContext(), operationDefinition.getOutput().getPath()).toInference(), strictParsing);
xmlParser.traverse(new DOMSource(element));
return resultHolder.getResult();
} catch (XMLStreamException | URISyntaxException | IOException | SAXException e) {
throw new IllegalArgumentException(String.format("Failed to parse RPC response %s", element), e);
}
}
}
Aggregations