Refactor DOM{Action,Rpc}Implementation
[mdsal.git] / dom / mdsal-dom-api / src / main / java / org / opendaylight / mdsal / dom / api / DOMActionInstance.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.collect.ImmutableSet;
15 import java.util.Objects;
16 import java.util.Set;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.yangtools.concepts.Immutable;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
22
23 /**
24  * An action which is subject to availability.
25  */
26 public final class DOMActionInstance implements Immutable {
27     private final Set<DOMDataTreeIdentifier> dataTrees;
28     private final Absolute type;
29
30     private DOMActionInstance(final Absolute type, final ImmutableSet<DOMDataTreeIdentifier> dataTrees) {
31         this.type = requireNonNull(type);
32         this.dataTrees = requireNonNull(dataTrees);
33         checkArgument(!dataTrees.isEmpty());
34     }
35
36     public static DOMActionInstance of(final Absolute type, final Set<DOMDataTreeIdentifier> dataTrees) {
37         return new DOMActionInstance(type, ImmutableSet.copyOf(dataTrees));
38     }
39
40     public static DOMActionInstance of(final Absolute type, final DOMDataTreeIdentifier... dataTrees) {
41         return new DOMActionInstance(type, ImmutableSet.copyOf(dataTrees));
42     }
43
44     public static DOMActionInstance of(final Absolute type, final LogicalDatastoreType datastore,
45             final YangInstanceIdentifier path) {
46         return new DOMActionInstance(type, ImmutableSet.of(DOMDataTreeIdentifier.of(datastore, path)));
47     }
48
49     /**
50      * Return the set of data trees on which this action is available. These identifiers are required to point
51      * to concrete items, i.e. they may not be wildcards. Identifiers which return an empty
52      * {@link DOMDataTreeIdentifier#getRootIdentifier()} are considered to match all items in that particular datastore
53      * and are expected to be treated as lower-priority alternatives to exact matches.
54      *
55      * @return Set of trees on which this action is available.
56      */
57     public Set<DOMDataTreeIdentifier> getDataTrees() {
58         return dataTrees;
59     }
60
61     /**
62      * Return the action type, i.e. the absolute schema node identifier of the action.
63      *
64      * @return action type.
65      */
66     public Absolute getType() {
67         return type;
68     }
69
70     @Override
71     public int hashCode() {
72         return Objects.hash(getType(), dataTrees);
73     }
74
75     @Override
76     public boolean equals(final @Nullable Object obj) {
77         return this == obj || obj instanceof DOMActionInstance other && getType().equals(other.type)
78             && dataTrees.equals(other.dataTrees);
79     }
80
81     @Override
82     public String toString() {
83         return MoreObjects.toStringHelper(this).add("type", type).add("dataTrees", dataTrees).toString();
84     }
85 }