Merge "Bug 2820 - LLDP TLV support and testing"
[controller.git] / opendaylight / md-sal / sal-dom-api / src / main / java / org / opendaylight / controller / md / sal / dom / api / DOMDataTreeIdentifier.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
3  * This program and the accompanying materials are made available under the
4  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5  * and is available at http://www.eclipse.org/legal/epl-v10.html
6  */
7 package org.opendaylight.controller.md.sal.dom.api;
8
9 import com.google.common.base.MoreObjects;
10 import com.google.common.base.Preconditions;
11 import java.io.Serializable;
12 import java.util.Iterator;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.yangtools.concepts.Immutable;
16 import org.opendaylight.yangtools.concepts.Path;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19
20 /**
21  * A unique identifier for a particular subtree. It is composed of the logical
22  * data store type and the instance identifier of the root node.
23  */
24 public final class DOMDataTreeIdentifier implements Immutable, Path<DOMDataTreeIdentifier>, Serializable, Comparable<DOMDataTreeIdentifier> {
25     private static final long serialVersionUID = 1L;
26     private final YangInstanceIdentifier rootIdentifier;
27     private final LogicalDatastoreType datastoreType;
28
29     public DOMDataTreeIdentifier(final LogicalDatastoreType datastoreType, final YangInstanceIdentifier rootIdentifier) {
30         this.datastoreType = Preconditions.checkNotNull(datastoreType);
31         this.rootIdentifier = Preconditions.checkNotNull(rootIdentifier);
32     }
33
34     /**
35      * Return the logical data store type.
36      *
37      * @return Logical data store type. Guaranteed to be non-null.
38      */
39     public @Nonnull LogicalDatastoreType getDatastoreType() {
40         return datastoreType;
41     }
42
43     /**
44      * Return the {@link YangInstanceIdentifier} of the root node.
45      *
46      * @return Instance identifier corresponding to the root node.
47      */
48     public @Nonnull YangInstanceIdentifier getRootIdentifier() {
49         return rootIdentifier;
50     }
51
52     @Override
53     public boolean contains(final DOMDataTreeIdentifier other) {
54         return datastoreType == other.datastoreType && rootIdentifier.contains(other.rootIdentifier);
55     }
56
57     @Override
58     public int hashCode() {
59         final int prime = 31;
60         int result = 1;
61         result = prime * result + datastoreType.hashCode();
62         result = prime * result + rootIdentifier.hashCode();
63         return result;
64     }
65
66     @Override
67     public boolean equals(final Object obj) {
68         if (this == obj) {
69             return true;
70         }
71         if (!(obj instanceof DOMDataTreeIdentifier)) {
72             return false;
73         }
74         DOMDataTreeIdentifier other = (DOMDataTreeIdentifier) obj;
75         if (datastoreType != other.datastoreType) {
76             return false;
77         }
78         return rootIdentifier.equals(other.rootIdentifier);
79     }
80
81     @Override
82     public int compareTo(final DOMDataTreeIdentifier o) {
83         int i = datastoreType.compareTo(o.datastoreType);
84         if (i != 0) {
85             return i;
86         }
87
88         final Iterator<PathArgument> mi = rootIdentifier.getPathArguments().iterator();
89         final Iterator<PathArgument> oi = o.rootIdentifier.getPathArguments().iterator();
90
91         while (mi.hasNext()) {
92             if (!oi.hasNext()) {
93                 return 1;
94             }
95
96             final PathArgument ma = mi.next();
97             final PathArgument oa = oi.next();
98             i = ma.compareTo(oa);
99             if (i != 0) {
100                 return i;
101             }
102         }
103
104         return oi.hasNext() ? -1 : 0;
105     }
106
107     @Override
108     public String toString() {
109         return MoreObjects.toStringHelper(this).add("datastore", datastoreType).add("root", rootIdentifier).toString();
110     }
111 }