private JSONObject processChoiceNode(final ChoiceSchemaNode choiceNode, final String moduleName, final JSONObject models, final SchemaContext schemaContext) throws JSONException, IOException { Set<ChoiceCaseNode> cases = choiceNode.getCases(); JSONArray choiceProps = new JSONArray(); for (ChoiceCaseNode choiceCase : cases) { String choiceName = choiceCase.getQName().getLocalName(); JSONObject choiceProp = processChildren(choiceCase.getChildNodes(), choiceCase.getQName(), moduleName, models, schemaContext); JSONObject choiceObj = new JSONObject(); choiceObj.put(choiceName, choiceProp); choiceObj.put(TYPE_KEY, OBJECT_TYPE); choiceProps.put(choiceObj); } JSONObject oneOfProps = new JSONObject(); oneOfProps.put(ONE_OF_KEY, choiceProps); oneOfProps.put(TYPE_KEY, OBJECT_TYPE); return oneOfProps; }
public static Optional<ChoiceCaseNode> findInstantiatedCase(final ChoiceSchemaNode instantiatedChoice, final ChoiceCaseNode originalDefinition) { ChoiceCaseNode potential = instantiatedChoice.getCaseNodeByName(originalDefinition.getQName()); if(originalDefinition.equals(potential)) { return Optional.of(potential); } if (potential != null) { SchemaNode potentialRoot = SchemaNodeUtils.getRootOriginalIfPossible(potential); if (originalDefinition.equals(potentialRoot)) { return Optional.of(potential); } } // We try to find case by name, then lookup its root definition // and compare it with original definition // This solves case, if choice was inside grouping // which was used in different module and thus namespaces are // different, but local names are still same. // // Still we need to check equality of definition, because local name is not // sufficient to uniquelly determine equality of cases // potential = instantiatedChoice.getCaseNodeByName(originalDefinition.getQName().getLocalName()); if(potential != null && (originalDefinition.equals(SchemaNodeUtils.getRootOriginalIfPossible(potential)))) { return Optional.of(potential); } return Optional.absent(); }
/** * Since each case statement within a module must provide unique child nodes, it is allowed to wrap * the actual configuration with a container node with name equal to case name. * * @param choiceCaseNode state or configuration case statement * @return either choiceCaseNode or its only child container */ private static <HAS_CHILDREN_AND_QNAME extends DataNodeContainer & SchemaNode> HAS_CHILDREN_AND_QNAME getDataNodeContainer(final ChoiceCaseNode choiceCaseNode) { Collection<DataSchemaNode> childNodes = choiceCaseNode.getChildNodes(); if (childNodes.size() == 1) { DataSchemaNode onlyChild = childNodes.iterator().next(); if (onlyChild instanceof ContainerSchemaNode) { ContainerSchemaNode onlyContainer = (ContainerSchemaNode) onlyChild; if (Objects.equals(onlyContainer.getQName().getLocalName(), choiceCaseNode.getQName().getLocalName())) { // the actual configuration is inside dummy container return (HAS_CHILDREN_AND_QNAME) onlyContainer; } } } return (HAS_CHILDREN_AND_QNAME) choiceCaseNode; }
public static CharSequence writeChoiceCaseNode(final ChoiceCaseNode choiceCaseNode) { StringConcatenation _builder = new StringConcatenation(); _builder.append("case "); QName _qName = choiceCaseNode.getQName(); String _localName = _qName.getLocalName(); _builder.append(_localName, ""); _builder.append(" {"); _builder.newLineIfNotEmpty(); { Collection<DataSchemaNode> _childNodes = choiceCaseNode.getChildNodes(); for(final DataSchemaNode childNode : _childNodes) { _builder.append(" "); CharSequence _writeDataSchemaNode = YangTemplate.writeDataSchemaNode(childNode); _builder.append(_writeDataSchemaNode, " "); _builder.newLineIfNotEmpty(); } } _builder.append("}"); _builder.newLine(); return _builder; }