+ private def dispatch DataSchemaNode getSchemaChildNode(DataNodeContainer parentSchemaNode, CompositeNodeWrapper data) {
+ return parentSchemaNode?.getDataChildByName(data.localName)
+ }
+
+ private def InstanceIdWithSchemaNode addLastIdentifierFromData(InstanceIdWithSchemaNode identifierWithSchemaNode, CompositeNode data, DataSchemaNode schemaOfData) {
+ val iiOriginal = identifierWithSchemaNode?.instanceIdentifier
+ var InstanceIdentifierBuilder iiBuilder = null
+ if (iiOriginal === null) {
+ iiBuilder = InstanceIdentifier.builder
+ } else {
+ iiBuilder = InstanceIdentifier.builder(iiOriginal)
+ }
+
+ if (schemaOfData instanceof ListSchemaNode) {
+ iiBuilder.nodeWithKey(schemaOfData.QName, (schemaOfData as ListSchemaNode).resolveKeysFromData(data))
+ } else {
+ iiBuilder.node(schemaOfData.QName)
+ }
+ return new InstanceIdWithSchemaNode(iiBuilder.toInstance, schemaOfData, identifierWithSchemaNode?.mountPoint)
+ }
+
+ private def resolveKeysFromData(ListSchemaNode listNode, CompositeNode dataNode) {
+ val keyValues = new HashMap<QName, Object>();
+ for (key : listNode.keyDefinition) {
+ val dataNodeKeyValueObject = dataNode.getSimpleNodesByName(key.localName)?.head?.value
+ if (dataNodeKeyValueObject === null) {
+ throw new ResponseException(BAD_REQUEST, "Data contains list \"" + dataNode.nodeType.localName + "\" which does not contain key: \"" + key.localName + "\"")
+ }
+ keyValues.put(key, dataNodeKeyValueObject);
+ }
+ return keyValues
+ }
+
+ private def CompositeNode normalizeNode(CompositeNode node, DataSchemaNode schema, InstanceIdentifier mountPoint) {
+ if (schema !== null && !schema.containerOrList) {
+ throw new ResponseException(BAD_REQUEST, "Root element has to be container or list yang datatype.");
+ }
+ if (node instanceof CompositeNodeWrapper) {
+ if ((node as CompositeNodeWrapper).changeAllowed) {
+ normalizeNode(node as CompositeNodeWrapper, schema, null, mountPoint)
+ }
+ return (node as CompositeNodeWrapper).unwrap()
+ }
+ return node
+ }
+
+ private def isContainerOrList(DataSchemaNode schemaNode) {
+ return (schemaNode instanceof ContainerSchemaNode) || (schemaNode instanceof ListSchemaNode)
+ }
+
+ private def void normalizeNode(NodeWrapper<?> nodeBuilder, DataSchemaNode schema, QName previousAugment,
+ InstanceIdentifier mountPoint) {
+ if (schema === null) {
+ throw new ResponseException(BAD_REQUEST,
+ "Data has bad format.\n\"" + nodeBuilder.localName + "\" does not exist in yang schema.");
+ }
+ var validQName = schema.QName
+ var currentAugment = previousAugment;
+ if (schema.augmenting) {
+ currentAugment = schema.QName
+ } else if (previousAugment !== null && schema.QName.namespace !== previousAugment.namespace) {
+ validQName = QName.create(currentAugment, schema.QName.localName);
+ }
+ var moduleName = controllerContext.findModuleNameByNamespace(validQName.namespace);
+ if (moduleName === null && mountPoint !== null && !mountPoint.path.empty) {
+ moduleName = controllerContext.findModuleByNamespace(validQName.namespace, mountPoint)?.name
+ }
+ if (nodeBuilder.namespace === null || nodeBuilder.namespace == validQName.namespace ||
+ nodeBuilder.namespace.toString == moduleName) {
+ nodeBuilder.qname = validQName
+ } else {
+ throw new ResponseException(BAD_REQUEST,
+ "Data has bad format.\nIf data is in XML format then namespace for \"" + nodeBuilder.localName +
+ "\" should be \"" + schema.QName.namespace + "\".\nIf data is in Json format then module name for \"" +
+ nodeBuilder.localName + "\" should be \"" + moduleName + "\".");
+ }
+
+ if (nodeBuilder instanceof CompositeNodeWrapper) {
+ val List<NodeWrapper<?>> children = (nodeBuilder as CompositeNodeWrapper).getValues
+ for (child : children) {
+ normalizeNode(child,
+ findFirstSchemaByLocalName(child.localName, (schema as DataNodeContainer).childNodes),
+ currentAugment, mountPoint)
+ }
+ if(schema instanceof ListSchemaNode) {
+ val listKeys = (schema as ListSchemaNode).keyDefinition
+ for (listKey : listKeys) {
+ var foundKey = false
+ for (child : children) {
+ if (child.unwrap.nodeType.localName == listKey.localName) {
+ foundKey = true;
+ }
+ }
+ if (!foundKey) {
+ throw new ResponseException(BAD_REQUEST,
+ "Missing key in URI \"" + listKey.localName + "\" of list \"" + schema.QName.localName + "\"")
+ }
+ }
+ }
+ } else if (nodeBuilder instanceof SimpleNodeWrapper) {
+ val simpleNode = (nodeBuilder as SimpleNodeWrapper)
+ val value = simpleNode.value
+ var inputValue = value;
+
+ if (schema.typeDefinition instanceof IdentityrefTypeDefinition) {
+ if (value instanceof String) {
+ inputValue = new IdentityValuesDTO(validQName.namespace.toString, value as String, null)
+ } // else value is instance of ValuesDTO
+ }
+
+ val outputValue = RestCodec.from(schema.typeDefinition)?.deserialize(inputValue);
+ simpleNode.setValue(outputValue)
+ } else if (nodeBuilder instanceof EmptyNodeWrapper) {
+ val emptyNodeBuilder = nodeBuilder as EmptyNodeWrapper
+ if (schema instanceof LeafSchemaNode) {
+ emptyNodeBuilder.setComposite(false);
+ } else if (schema instanceof ContainerSchemaNode) {
+
+ // FIXME: Add presence check
+ emptyNodeBuilder.setComposite(true);
+ }
+ }
+ }
+
+ private def dispatch TypeDefinition<?> typeDefinition(LeafSchemaNode node) {
+ var baseType = node.type
+ while (baseType.baseType !== null) {
+ baseType = baseType.baseType;
+ }
+ baseType
+ }
+
+ private def dispatch TypeDefinition<?> typeDefinition(LeafListSchemaNode node) {
+ var TypeDefinition<?> baseType = node.type
+ while (baseType.baseType !== null) {
+ baseType = baseType.baseType;
+ }
+ baseType
+ }
+
+ private def DataSchemaNode findFirstSchemaByLocalName(String localName, Set<DataSchemaNode> schemas) {
+ for (schema : schemas) {
+ if (schema instanceof ChoiceNode) {
+ for (caze : (schema as ChoiceNode).cases) {
+ val result = findFirstSchemaByLocalName(localName, caze.childNodes)
+ if (result !== null) {
+ return result
+ }
+ }
+ } else {
+ val result = schemas.findFirst[n|n.QName.localName.equals(localName)]
+ if (result !== null) {
+ return result;
+
+ }
+ }
+ }
+ return null
+ }
+
+}