private static Optional<String> getValueOfSimpleNode(final NormalizedNode<? extends YangInstanceIdentifier.PathArgument, ?> node) { final Object value = node.getValue(); return value == null || Strings.isNullOrEmpty(value.toString()) ? Optional.<String>absent() : Optional.of(value.toString().trim()); }
@Override protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) { if (normalizedNode instanceof AnyXmlNode) { return valueCodec.deserialize(normalizedNode.getValue()); } return null; }
public static RouteDistinguisher extractRouteDistinguisher(final DataContainerNode<?> route, final NodeIdentifier rdNid) { final NormalizedNode<?, ?> rdNode = NormalizedNodes.findNode(route, rdNid).orNull(); if (rdNode != null) { return parseRouteDistinguisher(rdNode.getValue()); } return null; } }
private String getCurrentOwner(YangInstanceIdentifier entityId) { Optional<NormalizedNode<?, ?>> optionalEntityOwner = getDataStore().readNode(entityId.node(ENTITY_OWNER_QNAME)); if(optionalEntityOwner.isPresent()){ return optionalEntityOwner.get().getValue().toString(); } return null; }
private static void toStringTree(final StringBuilder builder, final NormalizedNode<?, ?> node, final int offset) { final String prefix = Strings.repeat(" ", offset); builder.append(prefix).append(toStringTree(node.getIdentifier())); if (node instanceof NormalizedNodeContainer<?, ?, ?>) { final NormalizedNodeContainer<?, ?, ?> container = (NormalizedNodeContainer<?, ?, ?>) node; builder.append(" {\n"); for (NormalizedNode<?, ?> child : container.getValue()) { toStringTree(builder, child, offset + STRINGTREE_INDENT); } builder.append(prefix).append('}'); } else { builder.append(' ').append(node.getValue()); } builder.append('\n'); }
private static void toStringTree(final StringBuilder builder, final NormalizedNode<?, ?> node, final int offset) { final String prefix = Strings.repeat(" ", offset); builder.append(prefix).append(toStringTree(node.getIdentifier())); if (node instanceof NormalizedNodeContainer<?, ?, ?>) { final NormalizedNodeContainer<?, ?, ?> container = (NormalizedNodeContainer<?, ?, ?>) node; builder.append(" {\n"); for (NormalizedNode<?, ?> child : container.getValue()) { toStringTree(builder, child, offset + STRINGTREE_INDENT); } builder.append(prefix).append('}'); } else { builder.append(' ').append(node.getValue()); } builder.append('\n'); }
private void validateLeafRefNodeData(final NormalizedNode<?, ?> leaf, final LeafRefContext referencingCtx, final ModificationType modificationType, final YangInstanceIdentifier current) { final Set<Object> values = computeValues(root, createPath(referencingCtx.getAbsoluteLeafRefTargetPath()), current); if (values.contains(leaf.getValue())) { LOG.debug("Operation [{}] validate data of LEAFREF node: name[{}] = value[{}] {}", modificationType, referencingCtx.getNodeName(), leaf.getValue(), SUCCESS); return; } LOG.debug("Operation [{}] validate data of LEAFREF node: name[{}] = value[{}] {}", modificationType, referencingCtx.getNodeName(), leaf.getValue(), FAILED); LOG.debug("Invalid leafref value [{}] allowed values {} of LEAFREF node: {} leafRef target path: {}", leaf.getValue(), values, leaf.getNodeType(), referencingCtx.getAbsoluteLeafRefTargetPath()); errorsMessages.add(String.format("Invalid leafref value [%s] allowed values %s of LEAFREF node: %s leafRef " + "target path: %s", leaf.getValue(), values, leaf.getNodeType(), referencingCtx.getAbsoluteLeafRefTargetPath())); }
private void validateLeafRefNodeData(final NormalizedNode<?, ?> leaf, final LeafRefContext referencingCtx, final ModificationType modificationType, final YangInstanceIdentifier current) { final Set<Object> values = computeValues(root, createPath(referencingCtx.getAbsoluteLeafRefTargetPath()), current); if (values.contains(leaf.getValue())) { LOG.debug("Operation [{}] validate data of LEAFREF node: name[{}] = value[{}] {}", modificationType, referencingCtx.getNodeName(), leaf.getValue(), SUCCESS); return; } LOG.debug("Operation [{}] validate data of LEAFREF node: name[{}] = value[{}] {}", modificationType, referencingCtx.getNodeName(), leaf.getValue(), FAILED); LOG.debug("Invalid leafref value [{}] allowed values {} of LEAFREF node: {} leafRef target path: {}", leaf.getValue(), values, leaf.getNodeType(), referencingCtx.getAbsoluteLeafRefTargetPath()); errorsMessages.add(String.format("Invalid leafref value [%s] allowed values %s of LEAFREF node: %s leafRef " + "target path: %s", leaf.getValue(), values, leaf.getNodeType(), referencingCtx.getAbsoluteLeafRefTargetPath())); }
@Override public void visitNode(int level, String parentPath, NormalizedNode<?, ?> normalizedNode) { System.out.println(spaces((level) * 4) + normalizedNode.getClass().toString() + ":" + normalizedNode.getIdentifier()); if(normalizedNode instanceof LeafNode || normalizedNode instanceof LeafSetEntryNode){ System.out.println(spaces((level) * 4) + " parentPath = " + parentPath); System.out.println(spaces((level) * 4) + " key = " + normalizedNode.getClass().toString() + ":" + normalizedNode.getIdentifier()); System.out.println(spaces((level) * 4) + " value = " + normalizedNode.getValue()); } } }
@Override final NormalizedNode<?, ?> create(final PathArgument first, final Iterator<PathArgument> others, final Optional<NormalizedNode<?, ?>> deepestChild, final Optional<Entry<QName, ModifyAction>> operation) { final NormalizedNodeAttrBuilder<? extends PathArgument, Object, ? extends NormalizedNode<? extends PathArgument, Object>> builder = getBuilder(first); if (deepestChild.isPresent()) { builder.withValue(deepestChild.get().getValue()); } addModifyOpIfPresent(operation, builder); return builder.build(); }
private SimpleRoutingPolicy getSimpleRoutingPolicy(final DataTreeCandidateNode rootNode) { final DataTreeCandidateNode statusChange = rootNode.getModifiedChild(SIMPLE_ROUTING_POLICY_NID); if (statusChange != null) { final Optional<NormalizedNode<?, ?>> maybePeerStatus = statusChange.getDataAfter(); if (maybePeerStatus.isPresent()) { return SimpleRoutingPolicy.valueOf(BindingMapping.getClassName((String) (maybePeerStatus.get()).getValue())); } } return null; }
@Override final NormalizedNode<?, ?> create(final PathArgument first, final Iterator<PathArgument> others, final Optional<NormalizedNode<?, ?>> deepestChild, final Optional<Entry<QName, ModifyAction>> operation) { final NormalizedNodeAttrBuilder<? extends PathArgument, Object, ? extends NormalizedNode<? extends PathArgument, Object>> builder = getBuilder(first); if (deepestChild.isPresent()) { builder.withValue(deepestChild.get().getValue()); } addModifyOpIfPresent(operation, builder); return builder.build(); }
@Override protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) { if (normalizedNode instanceof LeafNode<?>) { return valueCodec.deserialize(normalizedNode.getValue()); } if (normalizedNode instanceof LeafSetNode<?>) { @SuppressWarnings("unchecked") final Collection<LeafSetEntryNode<Object>> domValues = ((LeafSetNode<Object>) normalizedNode).getValue(); final List<Object> result = new ArrayList<>(domValues.size()); for (final LeafSetEntryNode<Object> valueNode : domValues) { result.add(valueCodec.deserialize(valueNode.getValue())); } return result; } return null; }
@Override protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) { if (normalizedNode instanceof LeafNode<?>) { return valueCodec.deserialize(normalizedNode.getValue()); } if (normalizedNode instanceof LeafSetNode<?>) { @SuppressWarnings("unchecked") final Collection<LeafSetEntryNode<Object>> domValues = ((LeafSetNode<Object>) normalizedNode).getValue(); final List<Object> result = new ArrayList<>(domValues.size()); for (final LeafSetEntryNode<Object> valueNode : domValues) { result.add(valueCodec.deserialize(valueNode.getValue())); } return result; } return null; }
private static NormalizedNode<?, ?> getNodeReferencedByLeafref(final RevisionAwareXPath xpath, final NormalizedNodeContext currentNodeContext, final SchemaContext schemaContext, final TypedDataSchemaNode correspondingSchemaNode, final Object nodeValue) { final NormalizedNode<?, ?> referencedNode = xpath.isAbsolute() ? getNodeReferencedByAbsoluteLeafref(xpath, currentNodeContext, schemaContext, correspondingSchemaNode) : getNodeReferencedByRelativeLeafref(xpath, currentNodeContext, schemaContext, correspondingSchemaNode); if (referencedNode instanceof LeafSetNode) { return getReferencedLeafSetEntryNode((LeafSetNode<?>) referencedNode, nodeValue); } if (referencedNode instanceof LeafNode && referencedNode.getValue().equals(nodeValue)) { return referencedNode; } return null; }
private static NormalizedNode<?, ?> getNodeReferencedByLeafref(final RevisionAwareXPath xpath, final NormalizedNodeContext currentNodeContext, final SchemaContext schemaContext, final TypedDataSchemaNode correspondingSchemaNode, final Object nodeValue) { final NormalizedNode<?, ?> referencedNode = xpath.isAbsolute() ? getNodeReferencedByAbsoluteLeafref(xpath, currentNodeContext, schemaContext, correspondingSchemaNode) : getNodeReferencedByRelativeLeafref(xpath, currentNodeContext, schemaContext, correspondingSchemaNode); if (referencedNode instanceof LeafSetNode) { return getReferencedLeafSetEntryNode((LeafSetNode<?>) referencedNode, nodeValue); } if (referencedNode instanceof LeafNode && referencedNode.getValue().equals(nodeValue)) { return referencedNode; } return null; }
private List<Ipv6Prefixes> extractPrefixes(final Collection<MapEntryNode> routes) { final List<Ipv6Prefixes> prefs = new ArrayList<>(routes.size()); for (final MapEntryNode route : routes) { final String prefix = (String) NormalizedNodes.findNode(route, routePrefixIdentifier()).get().getValue(); prefs.add(new Ipv6PrefixesBuilder().setPathId(PathIdUtil.buildPathId(route, routePathIdNid())).setPrefix(new Ipv6Prefix(prefix)).build()); } return prefs; } }
private List<Ipv4Prefixes> extractPrefixes(final Collection<MapEntryNode> routes) { final List<Ipv4Prefixes> prefs = new ArrayList<>(routes.size()); for (final MapEntryNode route : routes) { final String prefix = (String) NormalizedNodes.findNode(route, routePrefixIdentifier()).get().getValue(); final Ipv4PrefixesBuilder prefixBuilder = new Ipv4PrefixesBuilder().setPrefix(new Ipv4Prefix(prefix)); prefixBuilder.setPathId(PathIdUtil.buildPathId(route, routePathIdNid())); prefs.add(prefixBuilder.build()); } return prefs; }
private static Object enumValueFunction(final Context context, final List<?> args) throws FunctionCallException { if (!args.isEmpty()) { throw new FunctionCallException("enum-value() takes one argument: node-set nodes."); } Verify.verify(context instanceof NormalizedNodeContext, "Unhandled context %s", context.getClass()); final NormalizedNodeContext currentNodeContext = (NormalizedNodeContext) context; final TypedDataSchemaNode correspondingSchemaNode = getCorrespondingTypedSchemaNode(currentNodeContext); final TypeDefinition<?> nodeType = correspondingSchemaNode.getType(); if (!(nodeType instanceof EnumTypeDefinition)) { return DOUBLE_NAN; } final Object nodeValue = currentNodeContext.getNode().getValue(); if (!(nodeValue instanceof String)) { return DOUBLE_NAN; } final EnumTypeDefinition enumerationType = (EnumTypeDefinition) nodeType; final String enumName = (String) nodeValue; return getEnumValue(enumerationType, enumName); }
@Test public void testSerializeDeserializeAnyXmlNode() throws Exception { final ByteArrayInputStream is = new ByteArrayInputStream("<xml><data/></xml>".getBytes(Charset.defaultCharset())); final Document parse = UntrustedXML.newDocumentBuilder().parse(is); final AnyXmlNode anyXmlNode = Builders.anyXmlBuilder() .withNodeIdentifier(id("anyXmlNode")) .withValue(new DOMSource(parse)) .build(); final byte[] bytes = SerializationUtils.serializeNormalizedNode(anyXmlNode); final NormalizedNode<?, ?> deserialized = SerializationUtils.deserializeNormalizedNode(bytes); final DOMSource value = (DOMSource) deserialized.getValue(); final Diff diff = XMLUnit.compareXML((Document) anyXmlNode.getValue().getNode(), value.getNode().getOwnerDocument()); Assert.assertTrue(diff.toString(), diff.similar()); }