Add forwarding transactions to binding v1
[mdsal.git] / dom / mdsal-dom-api / src / main / java / org / opendaylight / mdsal / dom / api / DOMOperationInstance.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.mdsal.dom.api;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.base.MoreObjects;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import com.google.common.collect.ImmutableSet;
16 import java.util.Objects;
17 import java.util.Set;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.yangtools.concepts.Immutable;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
24
25 /**
26  * An operation (RPC or action) which is subject to availability. This is a common superclass for {@link Action} and
27  * {@link Rpc}.
28  *
29  * @param <T> type of operation type
30  */
31 public abstract class DOMOperationInstance<T> implements Immutable {
32     public static final class Action extends DOMOperationInstance<SchemaPath> {
33         private final Set<DOMDataTreeIdentifier> dataTrees;
34
35         Action(final SchemaPath type, final Set<DOMDataTreeIdentifier> dataTrees) {
36             super(type);
37             this.dataTrees = ImmutableSet.copyOf(dataTrees);
38             checkArgument(!dataTrees.isEmpty());
39         }
40
41         /**
42          * Return the set of data trees on which this action is available. These identifiers are required to point
43          * to concrete items, i.e. they may not be wildcards.
44          *
45          * @return Set of trees on which this action is available.
46          */
47         public Set<DOMDataTreeIdentifier> getDataTrees() {
48             return dataTrees;
49         }
50
51         @Override
52         public int hashCode() {
53             return Objects.hash(getType(), dataTrees);
54         }
55
56         @Override
57         public boolean equals(final @Nullable Object obj) {
58             if (this == obj) {
59                 return true;
60             }
61             if (!(obj instanceof Action)) {
62                 return false;
63             }
64             final Action other = (Action) obj;
65             return getType().equals(other.getType()) && dataTrees.equals(other.dataTrees);
66         }
67
68         @Override
69         ToStringHelper addToStringAttributes(final ToStringHelper helper) {
70             return helper.add("dataTrees", dataTrees);
71         }
72     }
73
74     public static final class Rpc extends DOMOperationInstance<QName> {
75         Rpc(final QName type) {
76             super(type);
77         }
78
79         @Override
80         public int hashCode() {
81             return getType().hashCode();
82         }
83
84         @Override
85         public boolean equals(final @Nullable Object obj) {
86             return this == obj || obj instanceof Rpc && getType().equals(((Rpc) obj).getType());
87         }
88     }
89
90     private final T type;
91
92     DOMOperationInstance(final T type) {
93         this.type = requireNonNull(type);
94     }
95
96     public static Action actionOf(final SchemaPath type, final Set<DOMDataTreeIdentifier> dataTrees) {
97         return new Action(type, dataTrees);
98     }
99
100     public static Action actionOf(final SchemaPath type, final DOMDataTreeIdentifier... dataTrees) {
101         return actionOf(type, ImmutableSet.copyOf(dataTrees));
102     }
103
104     public static Action actionOf(final SchemaPath type, final LogicalDatastoreType datastore,
105             final YangInstanceIdentifier path) {
106         return actionOf(type, ImmutableSet.of(new DOMDataTreeIdentifier(datastore, path)));
107     }
108
109     public static Rpc rpcOf(final QName type) {
110         return new Rpc(type);
111     }
112
113     /**
114      * Return the operation type.
115      *
116      * @return operation type.
117      */
118     public final T getType() {
119         return type;
120     }
121
122     @Override
123     public abstract int hashCode();
124
125     @Override
126     public abstract boolean equals(@Nullable Object obj);
127
128     @Override
129     public final String toString() {
130         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
131     }
132
133     ToStringHelper addToStringAttributes(final ToStringHelper helper) {
134         return helper.add("type", type);
135     }
136 }