private String leafrefToDef(final LeafSchemaNode parentNode, final LeafrefTypeDefinition leafrefType, final String defaultValue) { Preconditions.checkArgument(leafrefType != null, "Leafref Type Definition reference cannot be NULL!"); Preconditions.checkArgument(leafrefType.getPathStatement() != null, "The Path Statement for Leafref Type Definition cannot be NULL!"); final RevisionAwareXPath xpath = leafrefType.getPathStatement(); final String strXPath = xpath.toString(); if (strXPath != null) { if (strXPath.indexOf('[') == -1) { final Module module = findParentModule(schemaContext, parentNode); if (module != null) { final SchemaNode dataNode; if (xpath.isAbsolute()) { dataNode = findDataSchemaNode(schemaContext, module, xpath); } else { dataNode = findDataSchemaNodeForRelativeXPath(schemaContext, module, parentNode, xpath); } final String result = getTypeDefaultConstruction((LeafSchemaNode) dataNode, defaultValue); return result; } } else { return "new java.lang.Object()"; } } return null; }
final SchemaContext schemaContext, final SchemaNode schema) { RevisionAwareXPath pathStatement = typeDefinition.getPathStatement(); pathStatement = new RevisionAwareXPathImpl(stripConditionsFromXPathString(pathStatement), pathStatement.isAbsolute()); Module parentModule = findParentModuleOfReferencingType(schemaContext, baseSchema); dataSchemaNode = (DataSchemaNode) SchemaContextUtil.findDataSchemaNode(schemaContext, parentModule, pathStatement); } else { Module parentModule = findParentModule(schemaContext, schema); dataSchemaNode = (DataSchemaNode) SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext, parentModule, schema, pathStatement); final TypeDefinition<?> targetTypeDefinition = typeDefinition(dataSchemaNode); return getBaseTypeForLeafRef((LeafrefTypeDefinition) targetTypeDefinition, schemaContext, dataSchemaNode);
private GroupingDefinition findUsedGrouping(final UsesNode uses) { final SchemaNode targetGrouping = findNodeInSchemaContext(schemaContext, uses.getGroupingPath() .getPathFromRoot()); if (targetGrouping instanceof GroupingDefinition) { return (GroupingDefinition) targetGrouping; } throw new IllegalArgumentException("Failed to resolve used grouping for " + uses); }
/** * Find "proper" qname of unknown node in case it comes from a grouping */ private static QName findQNameFromGrouping(final DataNodeContainer subtree, final SchemaContext ctx, final UnknownSchemaNode unknownSchemaNode, final String localIdentityName) { QName identityQName = null; for (UsesNode usesNode : subtree.getUses()) { SchemaNode dataChildByName = SchemaContextUtil.findDataSchemaNode(ctx, usesNode.getGroupingPath()); Module m = SchemaContextUtil.findParentModule(ctx, dataChildByName); List<UnknownSchemaNode> unknownSchemaNodes = dataChildByName.getUnknownSchemaNodes(); if(Collections2.transform(unknownSchemaNodes, UNKNOWN_NODE_TO_STRING).contains(UNKNOWN_NODE_TO_STRING.apply(unknownSchemaNode))) { identityQName = QName.create(dataChildByName.getQName(), localIdentityName); } } return identityQName; }
/** * Returns base type for {@code typeDefinition} which belongs to module specified via {@code qname}. This handle * the case when leafref type isn't specified as type substatement of leaf or leaf-list but is defined in other * module as typedef which is then imported to referenced module. * * <p> * Because {@code typeDefinition} is definied via typedef statement, only absolute path is meaningful. */ public static TypeDefinition<?> getBaseTypeForLeafRef(final LeafrefTypeDefinition typeDefinition, final SchemaContext schemaContext, final QName qname) { final RevisionAwareXPath pathStatement = typeDefinition.getPathStatement(); final RevisionAwareXPath strippedPathStatement = new RevisionAwareXPathImpl( stripConditionsFromXPathString(pathStatement), pathStatement.isAbsolute()); if (!strippedPathStatement.isAbsolute()) { return null; } final Optional<Module> parentModule = schemaContext.findModule(qname.getModule()); checkArgument(parentModule.isPresent(), "Failed to find parent module for %s", qname); final DataSchemaNode dataSchemaNode = (DataSchemaNode) SchemaContextUtil.findDataSchemaNode(schemaContext, parentModule.get(), strippedPathStatement); final TypeDefinition<?> targetTypeDefinition = typeDefinition(dataSchemaNode); if (targetTypeDefinition instanceof LeafrefTypeDefinition) { return getBaseTypeForLeafRef((LeafrefTypeDefinition) targetTypeDefinition, schemaContext, dataSchemaNode); } return targetTypeDefinition; }
/** * Searches for generated TO for <code>searchedTypeDef</code> type definition * in {@link #genTypeDefsContextMap genTypeDefsContextMap}. * * @param searchedTypeName string with name of <code>searchedTypeDef</code> * @return generated TO for <code>searchedTypeDef</code> or <code>null</code> it it doesn't exist */ private Type findGenTO(final String searchedTypeName, final SchemaNode parentNode) { final Module typeModule = findParentModule(schemaContext, parentNode); if (typeModule != null && typeModule.getName() != null) { final Map<Optional<Revision>, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get( typeModule.getName()); final Map<String, Type> genTOs = modulesByDate.get(typeModule.getRevision()); if (genTOs != null) { return genTOs.get(searchedTypeName); } } return null; }
@Override public Collection<NormalizedNodeUpdate> normalizedUpdates(@Nonnull final YangInstanceIdentifier topLevelIdentifier, @Nonnull final Map.Entry<YangInstanceIdentifier.PathArgument, DataContainerChild<? extends YangInstanceIdentifier.PathArgument, ?>> entry) { final ContainerSchemaNode containerSchemaNode = (ContainerSchemaNode) SchemaContextUtil .findDataSchemaNode(ctx, getSchemaPath(topLevelIdentifier, entry)); if (containerSchemaNode.isPresenceContainer()) { LOG.debug("Processing {} as presence container", topLevelIdentifier); // if presence container - create delete right away return ((ContainerNode) entry.getValue()).getValue().stream() .map(containerNode -> new NormalizedNodeUpdate( YangInstanceIdentifier.builder(topLevelIdentifier) .node(containerNode.getIdentifier()).build(), containerNode, null)) .collect(Collectors.toList()); } else { LOG.debug("Processing {} as non-presence container", topLevelIdentifier); // if non-presence - goes deep with base logic return super.normalizedUpdates(topLevelIdentifier, entry); } }
dataNode = SchemaContextUtil.findDataSchemaNode(schemaContext, module, xpath); } else { dataNode = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext, module, unionType, xpath);
@Override TypeDefinition<?> getBaseTypeForLeafRef(final SchemaNode schemaNode, final LeafrefTypeDefinition type) { final TypeDefinition<?> ret = SchemaContextUtil.getBaseTypeForLeafRef(type, schemaContext, schemaNode); return verifyNotNull(ret, "Unable to find base type for leafref node '%s'.", schemaNode.getPath()); }
checkArgument(strXPath.indexOf('[') == -1, "Revision Aware XPath may not contain a condition"); if (nonCondXPath.isAbsolute()) { final List<QName> path = xpathToQNamePath(context, module, strXPath); return pureData.isPresent() ? pureData.get() : findNodeInSchemaContext(context, path);
final Iterable<QName> qnamePath = resolveRelativeXPath(context, module, relativeXPath, actualSchemaNode); return pureData.isPresent() ? pureData.get() : findNodeInSchemaContext(context, qnamePath);
NotificationCodecContext<?> createNotificationDataContext(final Class<?> notificationType) { checkArgument(Notification.class.isAssignableFrom(notificationType)); checkArgument(notificationType.isInterface(), "Supplied class must be interface."); final QName qname = BindingReflections.findQName(notificationType); /** * FIXME: After Lithium cleanup of yang-model-api, use direct call on schema context * to retrieve notification via index. */ final NotificationDefinition schema = SchemaContextUtil.getNotificationSchema(getSchema(), SchemaPath.create(true, qname)); checkArgument(schema != null, "Supplied %s is not valid notification", notificationType); return new NotificationCodecContext<>(notificationType, schema, factory()); }
/** * Searches for generated TO for <code>searchedTypeDef</code> type * definition in {@link #genTypeDefsContextMap genTypeDefsContextMap} * * @param searchedTypeName * string with name of <code>searchedTypeDef</code> * @return generated TO for <code>searchedTypeDef</code> or * <code>null</code> it it doesn't exist */ private Type findGenTO(final String searchedTypeName, final SchemaNode parentNode) { final Module typeModule = findParentModule(schemaContext, parentNode); if (typeModule != null && typeModule.getName() != null) { final Map<Date, Map<String, Type>> modulesByDate = genTypeDefsContextMap.get(typeModule.getName()); final Map<String, Type> genTOs = modulesByDate.get(typeModule.getRevision()); if (genTOs != null) { return genTOs.get(searchedTypeName); } } return null; }
SchemaNode targetSchemaNode = null; targetSchemaNode = findDataSchemaNode(schemaContext, targetPath); if (targetSchemaNode instanceof DataSchemaNode && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) { if (targetSchemaNode instanceof DerivableSchemaNode) {
dataNode = SchemaContextUtil.findDataSchemaNode(schemaContext, module, xpath); } else { dataNode = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext, module, unionType, xpath);
/** * Returns base type for {@code typeDefinition} which belongs to module specified via {@code qname}. This handle * the case when leafref type isn't specified as type substatement of leaf or leaf-list but is defined in other * module as typedef which is then imported to referenced module. * * <p> * Because {@code typeDefinition} is definied via typedef statement, only absolute path is meaningful. */ public static TypeDefinition<?> getBaseTypeForLeafRef(final LeafrefTypeDefinition typeDefinition, final SchemaContext schemaContext, final QName qname) { final RevisionAwareXPath pathStatement = typeDefinition.getPathStatement(); final RevisionAwareXPath strippedPathStatement = new RevisionAwareXPathImpl( stripConditionsFromXPathString(pathStatement), pathStatement.isAbsolute()); if (!strippedPathStatement.isAbsolute()) { return null; } final Optional<Module> parentModule = schemaContext.findModule(qname.getModule()); checkArgument(parentModule.isPresent(), "Failed to find parent module for %s", qname); final DataSchemaNode dataSchemaNode = (DataSchemaNode) SchemaContextUtil.findDataSchemaNode(schemaContext, parentModule.get(), strippedPathStatement); final TypeDefinition<?> targetTypeDefinition = typeDefinition(dataSchemaNode); if (targetTypeDefinition instanceof LeafrefTypeDefinition) { return getBaseTypeForLeafRef((LeafrefTypeDefinition) targetTypeDefinition, schemaContext, dataSchemaNode); } return targetTypeDefinition; }
private void addKeyValue(final HashMap<QName, Object> map, final DataSchemaNode node, final String uriValue, final DOMMountPoint mountPoint) { Preconditions.checkNotNull(uriValue); Preconditions.checkArgument((node instanceof LeafSchemaNode)); final String urlDecoded = urlPathArgDecode(uriValue); TypeDefinition<?> typedef = ((LeafSchemaNode) node).getType(); final TypeDefinition<?> baseType = RestUtil.resolveBaseTypeFrom(typedef); if (baseType instanceof LeafrefTypeDefinition) { typedef = SchemaContextUtil.getBaseTypeForLeafRef((LeafrefTypeDefinition) baseType, globalSchema, node); } Codec<Object, Object> codec = RestCodec.from(typedef, mountPoint); Object decoded = codec.deserialize(urlDecoded); String additionalInfo = ""; if (decoded == null) { if ((baseType instanceof IdentityrefTypeDefinition)) { decoded = toQName(urlDecoded, null); additionalInfo = "For key which is of type identityref it should be in format module_name:identity_name."; } } if (decoded == null) { throw new RestconfDocumentedException(uriValue + " from URI can't be resolved. " + additionalInfo, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE); } map.put(node.getQName(), decoded); }
checkArgument(strXPath.indexOf('[') == -1, "Revision Aware XPath may not contain a condition"); if (nonCondXPath.isAbsolute()) { final List<QName> path = xpathToQNamePath(context, module, strXPath); return pureData.isPresent() ? pureData.get() : findNodeInSchemaContext(context, path);
final Iterable<QName> qnamePath = resolveRelativeXPath(context, module, relativeXPath, actualSchemaNode); return pureData.isPresent() ? pureData.get() : findNodeInSchemaContext(context, qnamePath);
private NotificationCodecContext<?> createNotificationDataContext(final Class<?> notificationType) { Preconditions.checkArgument(Notification.class.isAssignableFrom(notificationType)); Preconditions.checkArgument(notificationType.isInterface(), "Supplied class must be interface."); final QName qname = BindingReflections.findQName(notificationType); /** * FIXME: After Lithium cleanup of yang-model-api, use direct call on schema context to retrieve * notification via index. */ final NotificationDefinition schema = SchemaContextUtil.getNotificationSchema(getSchema(), SchemaPath.create(true, qname)); Preconditions.checkArgument(schema != null, "Supplied %s is not valid notification", notificationType); return new NotificationCodecContext<>(notificationType, schema, factory()); }