2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.operations;
10 import java.util.List;
12 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
13 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
14 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
15 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
16 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
18 import com.google.common.base.Optional;
19 import com.google.common.collect.Iterables;
20 import com.google.common.collect.Lists;
22 final class LeafSetNodeModification implements Modification<LeafListSchemaNode, LeafSetNode<?>> {
25 public Optional<LeafSetNode<?>> modify(LeafListSchemaNode schema, Optional<LeafSetNode<?>> actual,
26 Optional<LeafSetNode<?>> modification, OperationStack operationStack) throws DataModificationException {
28 List<LeafSetEntryNode<?>> resultNodes = Lists.newArrayList();
29 if(actual.isPresent()) {
30 Iterables.addAll(resultNodes, actual.get().getValue());
33 // TODO implement ordering for modification nodes
35 for (LeafSetEntryNode<?> leafListModification : modification.get().getValue()) {
36 operationStack.enteringNode(leafListModification);
38 switch (operationStack.getCurrentOperation()) {
41 DataModificationException.DataExistsException.check(schema.getQName(), actual, leafListModification);
44 if (contains(actual, leafListModification) == false) {
45 resultNodes.add(leafListModification);
50 DataModificationException.DataMissingException.check(schema.getQName(), actual, leafListModification);
53 if (resultNodes.contains(leafListModification)) {
54 resultNodes.remove(leafListModification);
62 throw new UnsupportedOperationException(String.format("Unable to perform operation: %s on: %s, unknown", operationStack.getCurrentOperation(), schema));
65 operationStack.exitingNode(leafListModification);
67 return build(schema, resultNodes);
70 private Optional<LeafSetNode<?>> build(LeafListSchemaNode schemaNode, List<LeafSetEntryNode<?>> resultNodes) {
71 if(resultNodes.isEmpty())
72 return Optional.absent();
74 ListNodeBuilder<Object, LeafSetEntryNode<Object>> b = Builders.leafSetBuilder(schemaNode);
75 for (LeafSetEntryNode<?> resultNode : resultNodes) {
76 // FIXME: can we do something about this SuppressWarnings?
77 @SuppressWarnings("unchecked")
78 final LeafSetEntryNode<Object> child = (LeafSetEntryNode<Object>) resultNode;
82 return Optional.<LeafSetNode<?>>of(b.build());
85 private static boolean contains(Optional<LeafSetNode<?>> actual, LeafSetEntryNode<?> leafListModification) {
86 boolean contains = actual.isPresent();
87 contains &= actual.get().getChild(leafListModification.getIdentifier()).isPresent();