a183e47639e846c88321df40ccb0fa01202a39eb
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / StackedYangInstanceIdentifier.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.api;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verify;
12 import static java.util.Objects.requireNonNull;
13
14 import com.google.common.collect.Iterables;
15 import com.google.common.collect.Lists;
16 import java.io.IOException;
17 import java.io.ObjectInputStream;
18 import java.io.ObjectOutputStream;
19 import java.lang.reflect.Field;
20 import java.security.AccessController;
21 import java.security.PrivilegedAction;
22 import java.util.ArrayList;
23 import java.util.List;
24 import org.eclipse.jdt.annotation.NonNull;
25
26 final class StackedYangInstanceIdentifier extends YangInstanceIdentifier implements Cloneable {
27     private static final long serialVersionUID = 1L;
28     private static final Field PARENT_FIELD;
29
30     static {
31         final Field f;
32         try {
33             f = StackedYangInstanceIdentifier.class.getDeclaredField("parent");
34         } catch (NoSuchFieldException | SecurityException e) {
35             throw new ExceptionInInitializerError(e);
36         }
37
38         AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
39             f.setAccessible(true);
40             return null;
41         });
42
43         PARENT_FIELD = f;
44     }
45
46     private final @NonNull YangInstanceIdentifier parent;
47     private final @NonNull PathArgument pathArgument;
48
49     private transient volatile StackedPathArguments pathArguments;
50     private transient volatile StackedReversePathArguments reversePathArguments;
51
52     StackedYangInstanceIdentifier(final YangInstanceIdentifier parent, final PathArgument pathArgument,
53             final int hash) {
54         super(hash);
55         this.parent = requireNonNull(parent);
56         this.pathArgument = requireNonNull(pathArgument);
57     }
58
59     @Override
60     public StackedYangInstanceIdentifier clone() {
61         try {
62             return (StackedYangInstanceIdentifier) super.clone();
63         } catch (CloneNotSupportedException e) {
64             throw new IllegalStateException("clone() should be supported", e);
65         }
66     }
67
68     @Override
69     public @NonNull YangInstanceIdentifier getParent() {
70         return parent;
71     }
72
73     @Override
74     public YangInstanceIdentifier getAncestor(final int depth) {
75         checkArgument(depth >= 0, "Steps cannot be negative");
76
77         // Calculate how far up our FixedYangInstanceIdentifier ancestor is
78         int stackedDepth = 1;
79         YangInstanceIdentifier wlk = getParent();
80         while (wlk instanceof StackedYangInstanceIdentifier) {
81             wlk = wlk.getParent();
82             stackedDepth++;
83         }
84
85         // Guaranteed to come from FixedYangInstanceIdentifier
86         final int fixedDepth = wlk.getPathArguments().size();
87         if (fixedDepth >= depth) {
88             return wlk.getAncestor(depth);
89         }
90
91         // Calculate our depth and check argument
92         final int ourDepth = stackedDepth + fixedDepth;
93         checkArgument(depth <= ourDepth, "Depth %s exceeds maximum depth %s", depth, ourDepth);
94
95         // Requested depth is covered by the stack, traverse up for specified number of steps
96         final int toWalk = ourDepth - depth;
97         YangInstanceIdentifier result = this;
98         for (int i = 0; i < toWalk; ++i) {
99             result = result.getParent();
100         }
101
102         return result;
103     }
104
105     @Override
106     public boolean isEmpty() {
107         return false;
108     }
109
110     @Override
111     public List<PathArgument> getPathArguments() {
112         StackedPathArguments ret = tryPathArguments();
113         if (ret == null) {
114             final List<PathArgument> stack = new ArrayList<>();
115             YangInstanceIdentifier current = this;
116             do {
117                 verify(current instanceof StackedYangInstanceIdentifier);
118                 final StackedYangInstanceIdentifier stacked = (StackedYangInstanceIdentifier) current;
119                 stack.add(stacked.getLastPathArgument());
120                 current = stacked.getParent();
121             } while (current.tryPathArguments() == null);
122
123             ret = new StackedPathArguments(current, Lists.reverse(stack));
124             pathArguments = ret;
125         }
126
127         return ret;
128     }
129
130     @Override
131     public List<PathArgument> getReversePathArguments() {
132         StackedReversePathArguments ret = tryReversePathArguments();
133         if (ret == null) {
134             ret = new StackedReversePathArguments(this);
135             reversePathArguments = ret;
136         }
137         return ret;
138     }
139
140     @Override
141     public PathArgument getLastPathArgument() {
142         return pathArgument;
143     }
144
145     @Override
146     StackedPathArguments tryPathArguments() {
147         return pathArguments;
148     }
149
150     @Override
151     StackedReversePathArguments tryReversePathArguments() {
152         return reversePathArguments;
153     }
154
155     @Override
156     YangInstanceIdentifier createRelativeIdentifier(final int skipFromRoot) {
157         // TODO: can we optimize this one?
158         return YangInstanceIdentifier.create(Iterables.skip(getPathArguments(), skipFromRoot));
159     }
160
161     @Override
162     boolean pathArgumentsEqual(final YangInstanceIdentifier other) {
163         if (other instanceof StackedYangInstanceIdentifier) {
164             final StackedYangInstanceIdentifier stacked = (StackedYangInstanceIdentifier) other;
165             return pathArgument.equals(stacked.pathArgument) && parent.equals(stacked.parent);
166         }
167         return super.pathArgumentsEqual(other);
168     }
169
170     private void readObject(final ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
171         inputStream.defaultReadObject();
172
173         final FixedYangInstanceIdentifier p = (FixedYangInstanceIdentifier) inputStream.readObject();
174         try {
175             PARENT_FIELD.set(this, p);
176         } catch (IllegalArgumentException | IllegalAccessException e) {
177             throw new IOException("Failed to set parent", e);
178         }
179     }
180
181     private void writeObject(final ObjectOutputStream outputStream) throws IOException {
182         outputStream.defaultWriteObject();
183
184         final FixedYangInstanceIdentifier p;
185         if (parent instanceof FixedYangInstanceIdentifier) {
186             p = (FixedYangInstanceIdentifier) parent;
187         } else {
188             p = FixedYangInstanceIdentifier.create(parent.getPathArguments(), parent.hashCode());
189         }
190         outputStream.writeObject(p);
191     }
192
193     @Override
194     public YangInstanceIdentifier toOptimized() {
195         return FixedYangInstanceIdentifier.create(getPathArguments());
196     }
197 }