Java Code Examples for org.opendaylight.yangtools.yang.common.QName#getLocalName()

The following examples show how to use org.opendaylight.yangtools.yang.common.QName#getLocalName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PathSegment.java    From yang2swagger with Eclipse Public License 1.0 6 votes vote down vote up
protected String generateName(QName paramName, Set<String> existingNames) {
    String name = paramName.getLocalName();
    if(! existingNames.contains(name)) return name;
    name = this.name + "-" + name;

    if(! existingNames.contains(name)) return name;

    name = moduleName + "-" + name;

    if(! existingNames.contains(name)) return name;

    //brute-force
    final String tmpName = name;

    return IntStream.range(1, 102)
            .mapToObj(i -> tmpName + i)
            .filter(n -> !existingNames.contains(n))
            .findFirst().orElseThrow(IllegalStateException::new);
}
 
Example 2
Source File: RpcContainerSchemaNode.java    From yang2swagger with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public DataSchemaNode getDataChildByName(QName name) {
    switch (name.getLocalName()) {
        case "input":
            return rpcDefinition.getInput();
        case "output":
            return rpcDefinition.getOutput();
        default:
            return null;
    }
}
 
Example 3
Source File: SchemaUtils.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Find child data schema node identified by its QName within a provided schema node. This method performs lookup
 * in the namespace of all leafs, leaf-lists, lists, containers, choices, rpcs, actions, notifications, anydatas
 * and anyxmls according to RFC6050/RFC7950 section 6.2.1.
 *
 * @param node
 *            schema node
 * @param qname
 *            QName
 * @return data child schema node
 * @throws IllegalArgumentException
 *             if the schema node does not allow children
 */
public static @Nullable SchemaNode findDataChildSchemaByQName(final SchemaNode node, final QName qname) {
    if (node instanceof DataNodeContainer) {
        SchemaNode child = ((DataNodeContainer) node).getDataChildByName(qname);
        if (child == null && node instanceof SchemaContext) {
            child = tryFind(((SchemaContext) node).getOperations(), qname).orElse(null);
        }
        if (child == null && node instanceof NotificationNodeContainer) {
            child = tryFind(((NotificationNodeContainer) node).getNotifications(), qname).orElse(null);
        }
        if (child == null && node instanceof ActionNodeContainer) {
            child = tryFind(((ActionNodeContainer) node).getActions(), qname).orElse(null);
        }

        return child;
    }
    if (node instanceof ChoiceSchemaNode) {
        return ((ChoiceSchemaNode) node).findCase(qname).orElse(null);
    }
    if (node instanceof OperationDefinition) {
        switch (qname.getLocalName()) {
            case "input":
                return ((OperationDefinition) node).getInput();
            case "output":
                return ((OperationDefinition) node).getOutput();
            default:
                return null;
        }
    }

    throw new IllegalArgumentException(String.format("Schema node %s does not allow children.", node));
}
 
Example 4
Source File: XMLStreamNormalizedNodeStreamWriter.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public final void metadata(final ImmutableMap<QName, Object> attributes) throws IOException {
    for (final Entry<QName, Object> entry : attributes.entrySet()) {
        final QName qname = entry.getKey();
        final String namespace = qname.getNamespace().toString();
        final String localName = qname.getLocalName();
        final Object value = entry.getValue();

        // FIXME: remove this handling once we have complete mapping to metadata
        try {
            if (namespace.isEmpty()) {
                // Legacy attribute, which is expected to be a String
                StreamWriterFacade.warnLegacyAttribute(localName);
                if (!(value instanceof String)) {
                    if (BROKEN_ATTRIBUTES.add(localName)) {
                        LOG.warn("Unbound annotation {} does not have a String value, ignoring it. Please fix the "
                                + "source of this annotation either by formatting it to a String or removing its "
                                + "use", localName, new Throwable("Call stack"));
                    }
                    LOG.debug("Ignoring annotation {} value {}", localName, value);
                } else {
                    facade.writeAttribute(localName, (String) value);
                    continue;
                }
            } else {
                final String prefix = facade.getPrefix(qname.getNamespace(), namespace);
                final String attrValue = encodeAnnotationValue(facade, qname, value);
                facade.writeAttribute(prefix, namespace, localName, attrValue);
            }
        } catch (final XMLStreamException e) {
            throw new IOException("Unable to emit attribute " + qname, e);
        }
    }
}
 
Example 5
Source File: XMLStreamWriterUtils.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@VisibleForTesting
static String encode(final @NonNull ValueWriter writer, final @NonNull QName qname, final QNameModule parent)
        throws XMLStreamException {
    //in case parent is present and same as element namespace write value without namespace
    if (qname.getNamespace().equals(parent.getNamespace())) {
        return qname.getLocalName();
    }

    final String ns = qname.getNamespace().toString();
    final String prefix = "x";
    writer.writeNamespace(prefix, ns);
    return prefix + ':' + qname.getLocalName();
}
 
Example 6
Source File: OperationAsContainer.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public final Optional<DataSchemaNode> findDataChildByName(final QName name) {
    if (!name.getModule().equals(getQName().getModule())) {
        return Optional.empty();
    }

    switch (name.getLocalName()) {
        case "input":
            return Optional.of(delegate.getInput());
        case "output":
            return Optional.of(delegate.getOutput());
        default:
            return Optional.empty();
    }
}
 
Example 7
Source File: ContainerSchemaNodes.java    From yangtools with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Optional<DataSchemaNode> findDataChildByName(final QName name) {
    switch (name.getLocalName()) {
        case "input":
            return Optional.of(rpcDefinition.getInput());
        case "output":
            return Optional.of(rpcDefinition.getOutput());
        default:
            return Optional.empty();
    }
}
 
Example 8
Source File: LeafRefPathParserImpl.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
private static QNameWithPredicate adaptChildStep(final QNameStep step, final QNameModule localModule) {
    final QName qname = resolve(step.getQName(), localModule);
    final Set<YangExpr> predicates = step.getPredicates();
    if (predicates.isEmpty()) {
        return new SimpleQNameWithPredicate(qname);
    }

    final QNameWithPredicateBuilder builder = new QNameWithPredicateBuilder(qname.getModule(),
        qname.getLocalName());

    for (YangExpr pred : predicates) {
        final QNamePredicateBuilder predBuilder = new QNamePredicateBuilder();

        if (pred instanceof YangBinaryExpr) {
            final YangBinaryExpr eqPred = (YangBinaryExpr) pred;
            checkState(eqPred.getOperator() == YangBinaryOperator.EQUALS);

            final YangExpr left = eqPred.getLeftExpr();
            checkState(left instanceof YangQNameExpr, "Unsupported left expression %s", left);
            predBuilder.setIdentifier(resolve(((YangQNameExpr) left).getQName(), localModule));

            final YangExpr right = eqPred.getRightExpr();
            if (right instanceof YangPathExpr) {
                final YangPathExpr rightPath = (YangPathExpr) right;
                final YangExpr filter = rightPath.getFilterExpr();
                if (filter instanceof YangFunctionCallExpr) {
                    checkState(YangFunction.CURRENT.getIdentifier().equals(
                        ((YangFunctionCallExpr) filter).getName()));
                } else {
                    throw new IllegalStateException("Unhandled filter " + filter);
                }

                final Relative location = rightPath.getLocationPath()
                        .orElseThrow(() -> new IllegalStateException("Missing locationPath in " + rightPath));
                predBuilder.setPathKeyExpression(LeafRefPath.create(
                    createPathSteps(localModule, location.getSteps()), false));
            } else {
                throw new UnsupportedOperationException("Not implemented for " + right);
            }
        }

        builder.addQNamePredicate(predBuilder.build());
    }

    return builder.build();
}
 
Example 9
Source File: UnknownEffectiveStatementBase.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public String toString() {
    final QName type = getNodeType();
    return String.valueOf(type.getNamespace()) + ":" + type.getLocalName() + " " + nodeParameter;
}
 
Example 10
Source File: AbstractListStatementSupport.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
@Override
protected final ListEffectiveStatement createEffective(
        final StmtContext<QName, ListStatement, ListEffectiveStatement> ctx,
        final ListStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
    final SchemaPath path = ctx.getSchemaPath().get();
    final ListSchemaNode original = (ListSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective)
            .orElse(null);

    final ImmutableList<QName> keyDefinition;
    final KeyEffectiveStatement keyStmt = findFirstStatement(substatements, KeyEffectiveStatement.class);
    if (keyStmt != null) {
        final List<QName> keyDefinitionInit = new ArrayList<>(keyStmt.argument().size());
        final Set<QName> possibleLeafQNamesForKey = new HashSet<>();
        for (final EffectiveStatement<?, ?> effectiveStatement : substatements) {
            if (effectiveStatement instanceof LeafSchemaNode) {
                possibleLeafQNamesForKey.add(((LeafSchemaNode) effectiveStatement).getQName());
            }
        }
        for (final QName keyQName : keyStmt.argument()) {
            if (!possibleLeafQNamesForKey.contains(keyQName)) {
                throw new InferenceException(ctx.getStatementSourceReference(),
                    "Key '%s' misses node '%s' in list '%s'", keyStmt.getDeclared().rawArgument(),
                    keyQName.getLocalName(), ctx.getStatementArgument());
            }
            keyDefinitionInit.add(keyQName);
        }

        keyDefinition = ImmutableList.copyOf(keyDefinitionInit);
    } else {
        keyDefinition = ImmutableList.of();
    }

    final boolean configuration = ctx.isConfiguration();
    final int flags = new FlagsBuilder()
            .setHistory(ctx.getCopyHistory())
            .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
            .setConfiguration(configuration)
            .setUserOrdered(findFirstArgument(substatements, OrderedByEffectiveStatement.class, Ordering.SYSTEM)
                .equals(Ordering.USER))
            .toFlags();
    if (configuration && keyDefinition.isEmpty() && isInstantied(ctx)) {
        warnConfigList(ctx);
    }

    final Optional<ElementCountConstraint> elementCountConstraint =
            EffectiveStmtUtils.createElementCountConstraint(substatements);
    return original == null && !elementCountConstraint.isPresent()
            ? new EmptyListEffectiveStatement(declared, path, flags, ctx, substatements, keyDefinition)
                    : new RegularListEffectiveStatement(declared, path, flags, ctx, substatements, keyDefinition,
                        elementCountConstraint.orElse(null), original);
}
 
Example 11
Source File: ConverterNamespaceContext.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
@NonNull String jaxenQName(final QName qname) {
    return reverse().convert(qname.getModule()) + ':' + qname.getLocalName();
}
 
Example 12
Source File: QNameCodecUtil.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
public static String encodeQName(final QName qname, final Function<QNameModule, String> moduleToPrefix) {
    final String prefix = moduleToPrefix.apply(qname.getModule());
    checkArgument(prefix != null, "Cannot allocated prefix for %s", qname);
    return prefix.isEmpty() ? qname.getLocalName() : prefix + ":" + qname.getLocalName();
}