Peel DataSchemaNode-only users of InstanceIdentifierContext
[netconf.git] / restconf / restconf-common / src / main / java / org / opendaylight / restconf / common / context / InstanceIdentifierContext.java
1 /*
2  * Copyright (c) 2014 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.restconf.common.context;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.Iterables;
13 import java.util.List;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.model.api.ContainerLike;
20 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
23 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
24
25 public final class InstanceIdentifierContext {
26     private final YangInstanceIdentifier instanceIdentifier;
27     private final SchemaNode schemaNode;
28     private final DOMMountPoint mountPoint;
29     private final EffectiveModelContext schemaContext;
30
31     private InstanceIdentifierContext(final EffectiveModelContext context, final DOMMountPoint mountPoint) {
32         instanceIdentifier = YangInstanceIdentifier.empty();
33         schemaContext = requireNonNull(context);
34         schemaNode = schemaContext;
35         this.mountPoint = mountPoint;
36     }
37
38     private InstanceIdentifierContext(final EffectiveModelContext context, final RpcDefinition rpc,
39             final DOMMountPoint mountPoint) {
40         instanceIdentifier = null;
41         schemaContext = requireNonNull(context);
42         schemaNode = requireNonNull(rpc);
43         this.mountPoint = mountPoint;
44     }
45
46     private InstanceIdentifierContext(final EffectiveModelContext context, final ContainerLike rpcInputOutput,
47             final DOMMountPoint mountPoint) {
48         instanceIdentifier = null;
49         schemaContext = requireNonNull(context);
50         schemaNode = requireNonNull(rpcInputOutput);
51         this.mountPoint = mountPoint;
52     }
53
54     @Deprecated(forRemoval = true)
55     public InstanceIdentifierContext(final YangInstanceIdentifier instanceIdentifier, final SchemaNode schemaNode,
56             final DOMMountPoint mountPoint, final EffectiveModelContext context) {
57         this.instanceIdentifier = instanceIdentifier;
58         this.schemaNode = schemaNode;
59         this.mountPoint = mountPoint;
60         schemaContext = context;
61     }
62
63     public static @NonNull InstanceIdentifierContext ofLocalRoot(final EffectiveModelContext context) {
64         return new InstanceIdentifierContext(context, null);
65     }
66
67     // Legacy bierman02 invokeRpc()
68     public static @NonNull InstanceIdentifierContext ofLocalRpc(final EffectiveModelContext context,
69         // FIXME: this this method really needed?
70             final RpcDefinition rpc) {
71         return new InstanceIdentifierContext(context, rpc, null);
72     }
73
74     // Invocations of various identifier-less details
75     public static @NonNull InstanceIdentifierContext ofDataSchemaNode(final EffectiveModelContext context,
76             final DataSchemaNode schemaNode) {
77         return new InstanceIdentifierContext(null, requireNonNull(schemaNode), null, requireNonNull(context));
78     }
79
80     // Invocations of various identifier-less details, potentially having a mount point
81     public static @NonNull InstanceIdentifierContext ofDataSchemaNode(final EffectiveModelContext context,
82             final DataSchemaNode schemaNode, final @Nullable DOMMountPoint mountPoint) {
83         return new InstanceIdentifierContext(null, requireNonNull(schemaNode), mountPoint, requireNonNull(context));
84     }
85
86     public static @NonNull InstanceIdentifierContext ofLocalRpcInput(final EffectiveModelContext context,
87             // FIXME: this this method really needed?
88             final RpcDefinition rpc) {
89         return new InstanceIdentifierContext(context, rpc.getInput(), null);
90     }
91
92     public static @NonNull InstanceIdentifierContext ofLocalRpcOutput(final EffectiveModelContext context,
93             // FIXME: we want to re-validate this, so might as well take a QName
94             final RpcDefinition rpc) {
95         return new InstanceIdentifierContext(context, rpc, null);
96     }
97
98     public static @NonNull InstanceIdentifierContext ofMountPointRoot(final DOMMountPoint mountPoint,
99             final EffectiveModelContext mountContext) {
100         return new InstanceIdentifierContext(mountContext, requireNonNull(mountPoint));
101     }
102
103     public static @NonNull InstanceIdentifierContext ofMountPointRpcOutput(final DOMMountPoint mountPoint,
104             final EffectiveModelContext mountContext, final RpcDefinition rpc) {
105         return new InstanceIdentifierContext(mountContext, rpc, requireNonNull(mountPoint));
106     }
107
108     // FIXME: what the heck are the callers of this doing?!
109     public @NonNull InstanceIdentifierContext withConcatenatedArgs(final List<PathArgument> concatArgs) {
110         if (concatArgs.isEmpty()) {
111             return this;
112         }
113         final var newInstanceIdentifier = YangInstanceIdentifier.create(
114             Iterables.concat(instanceIdentifier.getPathArguments(), concatArgs));
115         return new InstanceIdentifierContext(newInstanceIdentifier, schemaNode, mountPoint, schemaContext);
116     }
117
118     public YangInstanceIdentifier getInstanceIdentifier() {
119         return instanceIdentifier;
120     }
121
122     public SchemaNode getSchemaNode() {
123         return schemaNode;
124     }
125
126     public DOMMountPoint getMountPoint() {
127         return mountPoint;
128     }
129
130     public EffectiveModelContext getSchemaContext() {
131         return schemaContext;
132     }
133 }