public YangInstanceIdentifier toLegacy(final YangInstanceIdentifier normalized) throws DataNormalizationException { ImmutableList.Builder<PathArgument> legacyArgs = ImmutableList.builder(); DataNormalizationOperation<?> currentOp = operation; for (PathArgument normalizedArg : normalized.getPathArguments()) { currentOp = currentOp.getChild(normalizedArg); if (!currentOp.isMixin()) { legacyArgs.add(normalizedArg); } } return YangInstanceIdentifier.create(legacyArgs.build()); }
private static DataNormalizationOperation<?> fromSchemaAndQNameChecked(final DataNodeContainer schema, final QName child) throws DataNormalizationException { final Optional<DataSchemaNode> potential = findChildSchemaNode(schema, child); if (!potential.isPresent()) { throw new DataNormalizationException(String.format( "Supplied QName %s is not valid according to schema %s, potential children nodes: %s", child, schema,schema.getChildNodes())); } final DataSchemaNode result = potential.get(); // We try to look up if this node was added by augmentation if (schema instanceof DataSchemaNode && result.isAugmenting()) { return fromAugmentation(schema, (AugmentationTarget) schema, result); } return fromDataSchemaNode(result); }
public DataNormalizer(final SchemaContext ctx) { operation = DataNormalizationOperation.from(ctx); }
public YangInstanceIdentifier toNormalized(final YangInstanceIdentifier legacy) { ImmutableList.Builder<PathArgument> normalizedArgs = ImmutableList.builder(); DataNormalizationOperation<?> currentOp = operation; Iterator<PathArgument> arguments = legacy.getPathArguments().iterator(); try { while (arguments.hasNext()) { PathArgument legacyArg = arguments.next(); currentOp = currentOp.getChild(legacyArg); checkArgument(currentOp != null, "Legacy Instance Identifier %s is not correct. Normalized Instance Identifier so far %s", legacy, normalizedArgs.build()); while (currentOp.isMixin()) { normalizedArgs.add(currentOp.getIdentifier()); currentOp = currentOp.getChild(legacyArg.getNodeType()); } normalizedArgs.add(legacyArg); } } catch (DataNormalizationException e) { throw new IllegalArgumentException(String.format("Failed to normalize path %s", legacy), e); } return YangInstanceIdentifier.create(normalizedArgs.build()); }
/** * Returns an default object according to YANG schema for supplied path. * * @param path DOM Path * @return Node with defaults set on. */ public NormalizedNode<?, ?> getDefaultNodeFor(final YangInstanceIdentifier path) { final Iterator<PathArgument> iterator = path.getPathArguments().iterator(); DataNormalizationOperation<?> currentOp = legacyToNormalized.getRootOperation(); while (iterator.hasNext()) { final PathArgument currentArg = iterator.next(); try { currentOp = currentOp.getChild(currentArg); } catch (final DataNormalizationException e) { throw new IllegalArgumentException(String.format("Invalid child encountered in path %s", path), e); } } return currentOp.createDefault(path.getLastPathArgument()); }
public DataNormalizationOperation<?> getOperation(final YangInstanceIdentifier legacy) throws DataNormalizationException { DataNormalizationOperation<?> currentOp = operation; for (PathArgument pathArgument : legacy.getPathArguments()) { currentOp = currentOp.getChild(pathArgument); } return currentOp; }
public boolean isNodeMixin(final YangInstanceIdentifier path) { final DataNormalizationOperation<?> operation; try { operation = dataNormalizer.getOperation(path); } catch (final DataNormalizationException e) { throw new RestconfDocumentedException("Data normalizer failed. Normalization isn't possible", e); } return operation.isMixin(); }
public static DataNormalizationOperation<?> fromDataSchemaNode(final DataSchemaNode potential) { if (potential instanceof ContainerSchemaNode) { return new ContainerNormalization((ContainerSchemaNode) potential); } else if (potential instanceof ListSchemaNode) { return fromListSchemaNode((ListSchemaNode) potential); } else if (potential instanceof LeafSchemaNode) { return new LeafNormalization((LeafSchemaNode) potential); } else if (potential instanceof ChoiceSchemaNode) { return new ChoiceNodeNormalization((ChoiceSchemaNode) potential); } else if (potential instanceof LeafListSchemaNode) { return fromLeafListSchemaNode((LeafListSchemaNode) potential); } else if (potential instanceof AnyXmlSchemaNode) { return new AnyXmlNormalization((AnyXmlSchemaNode) potential); } return null; }
private static ChoiceSchemaNode findChoice(final Iterable<ChoiceSchemaNode> choices, final QName child) { ChoiceSchemaNode foundChoice = null; choiceLoop: for (final ChoiceSchemaNode choice : choices) { for (final CaseSchemaNode caze : choice.getCases().values()) { if (findChildSchemaNode(caze, child).isPresent()) { foundChoice = choice; break choiceLoop; } } } return foundChoice; }
private static Optional<DataSchemaNode> findChildSchemaNode(final DataNodeContainer parent, final QName child) { DataSchemaNode potential = parent.getDataChildByName(child); if (potential == null) { final Iterable<ChoiceSchemaNode> choices = FluentIterable.from(parent.getChildNodes()) .filter(ChoiceSchemaNode.class); potential = findChoice(choices, child); } return Optional.fromNullable(potential); }
/** * Returns a DataNormalizationOperation for provided child node. * * <p> * If supplied child is added by Augmentation this operation returns * a DataNormalizationOperation for augmentation, * otherwise returns a DataNormalizationOperation for child as * call for {@link #fromDataSchemaNode(DataSchemaNode)}. */ private static DataNormalizationOperation<?> fromAugmentation(final DataNodeContainer parent, final AugmentationTarget parentAug, final DataSchemaNode child) { AugmentationSchemaNode augmentation = null; for (final AugmentationSchemaNode aug : parentAug.getAvailableAugmentations()) { final DataSchemaNode potential = aug.getDataChildByName(child.getQName()); if (potential != null) { augmentation = aug; break; } } if (augmentation != null) { return new AugmentationNormalization(augmentation, parent); } else { return fromDataSchemaNode(child); } }
@Override protected void ensureParentsByMerge(final LogicalDatastoreType store, final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier normalizedPath, final InstanceIdentifier<?> path) { List<PathArgument> currentArguments = new ArrayList<>(); DataNormalizationOperation<?> currentOp = getCodec().getDataNormalizer().getRootOperation(); Iterator<PathArgument> iterator = normalizedPath.getPathArguments().iterator(); while (iterator.hasNext()) { PathArgument currentArg = iterator.next(); try { currentOp = currentOp.getChild(currentArg); } catch (DataNormalizationException e) { throw new IllegalArgumentException(String.format("Invalid child encountered in path %s", path), e); } currentArguments.add(currentArg); org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier currentPath = org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.create( currentArguments); getDelegate().merge(store, currentPath, currentOp.createDefault(currentArg)); } }
@Override protected final void ensureParentsByMerge(final LogicalDatastoreType store, final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier normalizedPath, final InstanceIdentifier<?> path) { List<PathArgument> currentArguments = new ArrayList<>(); DataNormalizationOperation<?> currentOp = getCodec().getDataNormalizer().getRootOperation(); Iterator<PathArgument> iterator = normalizedPath.getPathArguments().iterator(); while (iterator.hasNext()) { PathArgument currentArg = iterator.next(); try { currentOp = currentOp.getChild(currentArg); } catch (DataNormalizationException e) { throw new IllegalArgumentException(String.format("Invalid child encountered in path %s", path), e); } currentArguments.add(currentArg); org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier currentPath = org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.create( currentArguments); final Boolean exists; try { exists = getDelegate().exists(store, currentPath).checkedGet(); } catch (ReadFailedException e) { LOG.error("Failed to read pre-existing data from store {} path {}", store, currentPath, e); throw new IllegalStateException("Failed to read pre-existing data", e); } if (!exists && iterator.hasNext()) { getDelegate().merge(store, currentPath, currentOp.createDefault(currentArg)); } } }