2 * Copyright (c) 2014 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.impl.schema.tree;
10 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
11 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
12 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
13 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
14 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
16 import com.google.common.base.Optional;
18 public abstract class RootModificationApplyOperation implements ModificationApplyOperation {
21 public Optional<ModificationApplyOperation> getChild(final PathArgument child) {
22 return getDelegate().getChild(child);
26 public final void checkApplicable(final YangInstanceIdentifier path, final NodeModification modification, final Optional<TreeNode> current)
27 throws DataValidationFailedException {
28 getDelegate().checkApplicable(path, modification, current);
32 public final Optional<TreeNode> apply(final ModifiedNode modification, final Optional<TreeNode> currentMeta, final Version version) {
33 return getDelegate().apply(modification, currentMeta, version);
37 public boolean equals(final Object obj) {
38 return getDelegate().equals(obj);
42 public int hashCode() {
43 return getDelegate().hashCode();
47 public String toString() {
48 return getDelegate().toString();
52 public void verifyStructure(final ModifiedNode modification) throws IllegalArgumentException {
53 getDelegate().verifyStructure(modification);
56 abstract ModificationApplyOperation getDelegate();
58 public abstract RootModificationApplyOperation snapshot();
60 public abstract void upgradeIfPossible();
64 public static RootModificationApplyOperation from(final ModificationApplyOperation resolver) {
65 if(resolver instanceof RootModificationApplyOperation) {
66 return ((RootModificationApplyOperation) resolver).snapshot();
68 return new NotUpgradable(resolver);
71 private static final class Upgradable extends RootModificationApplyOperation {
73 private final LatestOperationHolder holder;
74 private ModificationApplyOperation delegate;
77 public Upgradable(final LatestOperationHolder holder, final ModificationApplyOperation delegate) {
79 this.delegate = delegate;
84 public void upgradeIfPossible() {
85 ModificationApplyOperation holderCurrent = holder.getCurrent();
86 if(holderCurrent != delegate) {
87 // FIXME: Allow update only if there is addition of models, not removals.
88 delegate = holderCurrent;
94 ModificationApplyOperation getDelegate() {
99 public RootModificationApplyOperation snapshot() {
100 return new Upgradable(holder,getDelegate());
105 private static final class NotUpgradable extends RootModificationApplyOperation {
107 private final ModificationApplyOperation delegate;
109 public NotUpgradable(final ModificationApplyOperation delegate) {
110 this.delegate = delegate;
114 public ModificationApplyOperation getDelegate() {
119 public void upgradeIfPossible() {
124 public RootModificationApplyOperation snapshot() {
129 public static class LatestOperationHolder {
131 private ModificationApplyOperation current = new AlwaysFailOperation();
133 public ModificationApplyOperation getCurrent() {
137 public void setCurrent(final ModificationApplyOperation newApplyOper) {
138 current = newApplyOper;
141 public RootModificationApplyOperation newSnapshot() {
142 return new Upgradable(this,current);