+ // FIXME: This is doing correct put for container and list children, not sure if this will work for choice case
+ private void putData(final DOMDataReadWriteTransaction rwTransaction, final LogicalDatastoreType datastore,
+ final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext,
+ final String insert, final String point) {
+ if (insert == null) {
+ makePut(rwTransaction, datastore, path, payload, schemaContext);
+ return;
+ }
+
+ final DataSchemaNode schemaNode = checkListAndOrderedType(schemaContext, path);
+ checkItemDoesNotExists(rwTransaction, datastore, path);
+ switch (insert) {
+ case "first":
+ if (schemaNode instanceof ListSchemaNode) {
+ final OrderedMapNode readList =
+ (OrderedMapNode) this.readConfigurationData(path.getParent());
+ if (readList == null || readList.getValue().isEmpty()) {
+ simplePut(datastore, path, rwTransaction, schemaContext, payload);
+ } else {
+ rwTransaction.delete(datastore, path.getParent());
+ simplePut(datastore, path, rwTransaction, schemaContext, payload);
+ makePut(rwTransaction, datastore, path.getParent(), readList, schemaContext);
+ }
+ } else {
+ final OrderedLeafSetNode<?> readLeafList =
+ (OrderedLeafSetNode<?>) readConfigurationData(path.getParent());
+ if (readLeafList == null || readLeafList.getValue().isEmpty()) {
+ simplePut(datastore, path, rwTransaction, schemaContext, payload);
+ } else {
+ rwTransaction.delete(datastore, path.getParent());
+ simplePut(datastore, path, rwTransaction, schemaContext, payload);
+ makePut(rwTransaction, datastore, path.getParent(), readLeafList,
+ schemaContext);
+ }
+ }
+ break;
+ case "last":
+ simplePut(datastore, path, rwTransaction, schemaContext, payload);
+ break;
+ case "before":
+ if (schemaNode instanceof ListSchemaNode) {
+ final OrderedMapNode readList =
+ (OrderedMapNode) this.readConfigurationData(path.getParent());
+ if (readList == null || readList.getValue().isEmpty()) {
+ simplePut(datastore, path, rwTransaction, schemaContext, payload);
+ } else {
+ insertWithPointListPut(rwTransaction, datastore, path, payload, schemaContext, point,
+ readList, true);
+ }
+ } else {
+ final OrderedLeafSetNode<?> readLeafList =
+ (OrderedLeafSetNode<?>) readConfigurationData(path.getParent());
+ if (readLeafList == null || readLeafList.getValue().isEmpty()) {
+ simplePut(datastore, path, rwTransaction, schemaContext, payload);
+ } else {
+ insertWithPointLeafListPut(rwTransaction, datastore, path, payload, schemaContext, point,
+ readLeafList, true);
+ }
+ }
+ break;
+ case "after":
+ if (schemaNode instanceof ListSchemaNode) {
+ final OrderedMapNode readList =
+ (OrderedMapNode) this.readConfigurationData(path.getParent());
+ if (readList == null || readList.getValue().isEmpty()) {
+ simplePut(datastore, path, rwTransaction, schemaContext, payload);
+ } else {
+ insertWithPointListPut(rwTransaction, datastore, path, payload, schemaContext, point,
+ readList, false);
+ }
+ } else {
+ final OrderedLeafSetNode<?> readLeafList =
+ (OrderedLeafSetNode<?>) readConfigurationData(path.getParent());
+ if (readLeafList == null || readLeafList.getValue().isEmpty()) {
+ simplePut(datastore, path, rwTransaction, schemaContext, payload);
+ } else {
+ insertWithPointLeafListPut(rwTransaction, datastore, path, payload, schemaContext, point,
+ readLeafList, false);
+ }
+ }
+ break;
+ default:
+ throw new RestconfDocumentedException(
+ "Used bad value of insert parameter. Possible values are first, last, before or after, but was: "
+ + insert);
+ }
+ }
+
+ private static void insertWithPointLeafListPut(final DOMDataWriteTransaction tx,
+ final LogicalDatastoreType datastore, final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload,
+ final SchemaContext schemaContext, final String point, final OrderedLeafSetNode<?> readLeafList,
+ final boolean before) {
+ tx.delete(datastore, path.getParent());
+ final InstanceIdentifierContext<?> instanceIdentifier =
+ ControllerContext.getInstance().toInstanceIdentifier(point);
+ int index1 = 0;
+ for (final LeafSetEntryNode<?> nodeChild : readLeafList.getValue()) {
+ if (nodeChild.getIdentifier().equals(instanceIdentifier.getInstanceIdentifier().getLastPathArgument())) {
+ break;
+ }
+ index1++;
+ }
+ if (!before) {
+ index1++;
+ }
+ int index2 = 0;
+ final NormalizedNode<?, ?> emptySubtree =
+ ImmutableNodes.fromInstanceId(schemaContext, path.getParent());
+ tx.merge(datastore, YangInstanceIdentifier.create(emptySubtree.getIdentifier()), emptySubtree);
+ for (final LeafSetEntryNode<?> nodeChild : readLeafList.getValue()) {
+ if (index2 == index1) {
+ simplePut(datastore, path, tx, schemaContext, payload);
+ }
+ final YangInstanceIdentifier childPath = path.getParent().node(nodeChild.getIdentifier());
+ tx.put(datastore, childPath, nodeChild);
+ index2++;
+ }
+ }
+
+ private static void insertWithPointListPut(final DOMDataWriteTransaction tx, final LogicalDatastoreType datastore,
+ final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext,
+ final String point, final OrderedMapNode readList, final boolean before) {
+ tx.delete(datastore, path.getParent());
+ final InstanceIdentifierContext<?> instanceIdentifier =
+ ControllerContext.getInstance().toInstanceIdentifier(point);
+ int index1 = 0;
+ for (final MapEntryNode mapEntryNode : readList.getValue()) {
+ if (mapEntryNode.getIdentifier().equals(instanceIdentifier.getInstanceIdentifier().getLastPathArgument())) {
+ break;
+ }
+ index1++;
+ }
+ if (!before) {
+ index1++;
+ }
+ int index2 = 0;
+ final NormalizedNode<?, ?> emptySubtree =
+ ImmutableNodes.fromInstanceId(schemaContext, path.getParent());
+ tx.merge(datastore, YangInstanceIdentifier.create(emptySubtree.getIdentifier()), emptySubtree);
+ for (final MapEntryNode mapEntryNode : readList.getValue()) {
+ if (index2 == index1) {
+ simplePut(datastore, path, tx, schemaContext, payload);
+ }
+ final YangInstanceIdentifier childPath = path.getParent().node(mapEntryNode.getIdentifier());
+ tx.put(datastore, childPath, mapEntryNode);
+ index2++;
+ }
+ }
+
+ private static void makePut(final DOMDataWriteTransaction tx, final LogicalDatastoreType datastore,
+ final YangInstanceIdentifier path, final NormalizedNode<?, ?> payload, final SchemaContext schemaContext) {
+ if (payload instanceof MapNode) {
+ final NormalizedNode<?, ?> emptySubtree = ImmutableNodes.fromInstanceId(schemaContext, path);
+ tx.merge(datastore, YangInstanceIdentifier.create(emptySubtree.getIdentifier()), emptySubtree);
+ ensureParentsByMerge(datastore, path, tx, schemaContext);
+ for (final MapEntryNode child : ((MapNode) payload).getValue()) {
+ final YangInstanceIdentifier childPath = path.node(child.getIdentifier());
+ tx.put(datastore, childPath, child);
+ }
+ } else {
+ simplePut(datastore, path, tx, schemaContext, payload);
+ }
+ }
+
+ private static void simplePut(final LogicalDatastoreType datastore, final YangInstanceIdentifier path,
+ final DOMDataWriteTransaction tx, final SchemaContext schemaContext, final NormalizedNode<?, ?> payload) {
+ ensureParentsByMerge(datastore, path, tx, schemaContext);
+ tx.put(datastore, path, payload);
+ }
+
+ private static CheckedFuture<Void, TransactionCommitFailedException> deleteDataViaTransaction(
+ final DOMDataReadWriteTransaction readWriteTransaction, final LogicalDatastoreType datastore,