summaryrefslogtreecommitdiffstats
path: root/netconf/restconf/restconf-common/src/main/java/org/opendaylight/restconf/common/context/InstanceIdentifierContext.java
blob: 81577a0caa168f237a9953e29af87fc97fd77230 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.restconf.common.context;

import static com.google.common.base.Verify.verify;
import static java.util.Objects.requireNonNull;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Iterables;
import java.util.List;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.mdsal.dom.api.DOMMountPoint;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
import org.opendaylight.yangtools.yang.model.api.SchemaNode;
import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;

public abstract class InstanceIdentifierContext {
    private static final class Root extends InstanceIdentifierContext {
        private final @NonNull EffectiveModelContext context;

        Root(final EffectiveModelContext context, final DOMMountPoint mountPoint) {
            super(context, mountPoint);
            this.context = requireNonNull(context);
        }

        @Override
        public EffectiveModelContext getSchemaContext() {
            return context;
        }

        @Override
        public YangInstanceIdentifier getInstanceIdentifier() {
            return YangInstanceIdentifier.empty();
        }

        @Override
        public Inference inference() {
            return SchemaInferenceStack.of(context).toInference();
        }

        @Override
        InstanceIdentifierContext createWithConcapt(final List<PathArgument> concatArgs) {
            return new DataPath(context, getMountPoint(), SchemaInferenceStack.of(context),
                YangInstanceIdentifier.create(concatArgs));
        }
    }

    private static final class DataPath extends InstanceIdentifierContext {
        private final @NonNull YangInstanceIdentifier path;
        private final @NonNull SchemaInferenceStack stack;

        private DataPath(final SchemaNode schemaNode, final DOMMountPoint mountPoint,
                final SchemaInferenceStack stack, final YangInstanceIdentifier path) {
            super(schemaNode, mountPoint);
            this.stack = requireNonNull(stack);
            this.path = requireNonNull(path);
        }

        static @NonNull DataPath of(final EffectiveModelContext context, final YangInstanceIdentifier path,
                final DOMMountPoint mountPoint) {
            final var nodeAndStack = DataSchemaContextTree.from(context).enterPath(path).orElseThrow();
            return new DataPath(nodeAndStack.node().getDataSchemaNode(), mountPoint, nodeAndStack.stack(), path);
        }

        @Override
        public YangInstanceIdentifier getInstanceIdentifier() {
            return path;
        }

        @Override
        public Inference inference() {
            return stack.toInference();
        }

        @Override
        @NonNull
        InstanceIdentifierContext createWithConcapt(final List<PathArgument> concatArgs) {
            final var newInstanceIdentifier = YangInstanceIdentifier.create(
                Iterables.concat(path.getPathArguments(), concatArgs));
            return new DataPath(getSchemaNode(), getMountPoint(), stack, newInstanceIdentifier);
        }
    }

    private static final class WithoutDataPath extends InstanceIdentifierContext {
        private final @NonNull SchemaInferenceStack stack;

        private WithoutDataPath(final SchemaNode schemaNode, final DOMMountPoint mountPoint,
                final SchemaInferenceStack stack) {
            super(schemaNode, mountPoint);
            this.stack = requireNonNull(stack);
        }

        @Override
        public Inference inference() {
            return stack.toInference();
        }

        @Override
        public @Nullable YangInstanceIdentifier getInstanceIdentifier() {
            return null;
        }

        @Override
        InstanceIdentifierContext createWithConcapt(final List<PathArgument> concatArgs) {
            return this;
        }
    }

    private final @NonNull SchemaNode schemaNode;
    private final @Nullable DOMMountPoint mountPoint;

    InstanceIdentifierContext(final SchemaNode schemaNode, final DOMMountPoint mountPoint) {
        this.schemaNode = requireNonNull(schemaNode);
        this.mountPoint = mountPoint;
    }

    public static @NonNull InstanceIdentifierContext ofLocalRoot(final EffectiveModelContext context) {
        return new Root(context, null);
    }

    @VisibleForTesting
    public static @NonNull InstanceIdentifierContext ofLocalPath(final EffectiveModelContext context,
            final YangInstanceIdentifier path) {
        return DataPath.of(context, path, null);
    }

    // Legacy bierman02 invokeRpc()
    public static @NonNull InstanceIdentifierContext ofRpcInput(final EffectiveModelContext context,
            // FIXME: this this method really needed?
            final RpcDefinition rpc, final @Nullable DOMMountPoint mountPoint) {
        final var stack = SchemaInferenceStack.of(context);
        stack.enterSchemaTree(rpc.getQName());
        stack.enterSchemaTree(rpc.getInput().getQName());
        return new WithoutDataPath(rpc, mountPoint, stack);
    }

    public static @NonNull InstanceIdentifierContext ofRpcOutput(final EffectiveModelContext context,
            // FIXME: this this method really needed?
            final RpcDefinition rpc, final @Nullable DOMMountPoint mountPoint) {
        final var stack = SchemaInferenceStack.of(context);
        stack.enterSchemaTree(rpc.getQName());
        stack.enterSchemaTree(rpc.getOutput().getQName());
        return new WithoutDataPath(rpc, mountPoint, stack);
    }

    // Invocations of various identifier-less details
    public static @NonNull InstanceIdentifierContext ofStack(final SchemaInferenceStack stack) {
        return ofStack(stack, null);
    }

    // Invocations of various identifier-less details, potentially having a mount point
    public static @NonNull InstanceIdentifierContext ofStack(final SchemaInferenceStack stack,
            final @Nullable DOMMountPoint mountPoint) {
        final SchemaNode schemaNode;
        if (!stack.isEmpty()) {
            final var stmt = stack.currentStatement();
            verify(stmt instanceof SchemaNode, "Unexpected statement %s", stmt);
            schemaNode = (SchemaNode) stmt;
        } else {
            schemaNode = stack.getEffectiveModelContext();
        }

        return new WithoutDataPath(schemaNode, mountPoint, stack);
    }

    public static @NonNull InstanceIdentifierContext ofLocalRpcInput(final EffectiveModelContext context,
            // FIXME: this this method really needed?
            final RpcDefinition rpc) {
        final var stack = SchemaInferenceStack.of(context);
        stack.enterSchemaTree(rpc.getQName());
        stack.enterSchemaTree(rpc.getInput().getQName());
        return new WithoutDataPath(rpc.getInput(), null, stack);
    }

    public static @NonNull InstanceIdentifierContext ofLocalRpcOutput(final EffectiveModelContext context,
            // FIXME: we want to re-validate this, so might as well take a QName
            final RpcDefinition rpc) {
        final var stack = SchemaInferenceStack.of(context);
        stack.enterSchemaTree(rpc.getQName());
        stack.enterSchemaTree(rpc.getOutput().getQName());
        return new WithoutDataPath(rpc, null, stack);
    }

    public static @NonNull InstanceIdentifierContext ofPath(final SchemaInferenceStack stack,
            final SchemaNode schemaNode, final YangInstanceIdentifier path,
            final @Nullable DOMMountPoint mountPoint) {
        return new DataPath(schemaNode, mountPoint, stack, path);
    }

    public static @NonNull InstanceIdentifierContext ofMountPointRoot(final DOMMountPoint mountPoint,
            final EffectiveModelContext mountContext) {
        return new Root(mountContext, requireNonNull(mountPoint));
    }

    @VisibleForTesting
    public static @NonNull InstanceIdentifierContext ofMountPointPath(final DOMMountPoint mountPoint,
            final EffectiveModelContext context, final YangInstanceIdentifier path) {
        return DataPath.of(context, path, requireNonNull(mountPoint));
    }

    public static @NonNull InstanceIdentifierContext ofMountPointRpcOutput(final DOMMountPoint mountPoint,
            final EffectiveModelContext mountContext, final RpcDefinition rpc) {
        final var stack = SchemaInferenceStack.of(mountContext);
        stack.enterSchemaTree(rpc.getQName());
        stack.enterSchemaTree(rpc.getOutput().getQName());
        return new WithoutDataPath(rpc, requireNonNull(mountPoint), stack);
    }

    // FIXME: what the heck are the callers of this doing?!
    public final @NonNull InstanceIdentifierContext withConcatenatedArgs(final List<PathArgument> concatArgs) {
        return concatArgs.isEmpty() ? this : createWithConcapt(concatArgs);
    }

    abstract @NonNull InstanceIdentifierContext createWithConcapt(List<PathArgument> concatArgs);

    public final @NonNull SchemaNode getSchemaNode() {
        return schemaNode;
    }

    public final @Nullable DOMMountPoint getMountPoint() {
        return mountPoint;
    }

    public @NonNull EffectiveModelContext getSchemaContext() {
        return inference().getEffectiveModelContext();
    }

    public abstract @NonNull Inference inference();

    public abstract @Nullable YangInstanceIdentifier getInstanceIdentifier();
}