Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.net.newresource; |
| 17 | |
| 18 | import com.google.common.annotations.Beta; |
| 19 | import com.google.common.base.MoreObjects; |
| 20 | import com.google.common.collect.ImmutableList; |
| 21 | |
| 22 | import java.util.Arrays; |
| 23 | import java.util.List; |
| 24 | import java.util.Objects; |
| 25 | import java.util.Optional; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkArgument; |
| 28 | import static com.google.common.base.Preconditions.checkNotNull; |
| 29 | |
| 30 | /** |
| 31 | * An object that is used to locate a resource in a network. |
| 32 | * A ResourcePath represents a path that is hierarchical and composed of a sequence |
| 33 | * of elementary resources that are not globally identifiable. A ResourcePath can be a globally |
| 34 | * unique resource identifier. |
| 35 | * |
| 36 | * Users of this class must keep the semantics of resources regarding the hierarchical structure. |
| 37 | * For example, resource path, Link:1/VLAN ID:100, is valid, but resource path, VLAN ID:100/Link:1 |
| 38 | * is not valid because a link is not a sub-component of a VLAN ID. |
| 39 | */ |
| 40 | @Beta |
| 41 | public final class ResourcePath { |
| 42 | |
| 43 | private final List<Object> resources; |
| 44 | |
| 45 | /** |
| 46 | * Creates an resource path from the specified components. |
| 47 | * |
| 48 | * @param components components of the path. The order represents hierarchical structure of the resource. |
| 49 | */ |
| 50 | public ResourcePath(Object... components) { |
| 51 | this(Arrays.asList(components)); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Creates an resource path from the specified components. |
| 56 | * |
| 57 | * @param components components of the path. The order represents hierarchical structure of the resource. |
| 58 | */ |
| 59 | public ResourcePath(List<Object> components) { |
| 60 | checkNotNull(components); |
| 61 | checkArgument(components.size() > 0); |
| 62 | |
| 63 | this.resources = ImmutableList.copyOf(components); |
| 64 | } |
| 65 | |
| 66 | // for serialization |
| 67 | private ResourcePath() { |
| 68 | this.resources = null; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns the components of this resource path. |
| 73 | * |
| 74 | * @return the components of this resource path |
| 75 | */ |
| 76 | public List<Object> components() { |
| 77 | return resources; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Returns the parent resource path of this instance. |
| 82 | * E.g. if this path is Link:1/VLAN ID:100, the return value is the resource path for Link:1. |
| 83 | * |
| 84 | * @return the parent resource path of this instance. |
| 85 | * If there is no parent, empty instance will be returned. |
| 86 | */ |
| 87 | public Optional<ResourcePath> parent() { |
| 88 | if (resources.size() >= 2) { |
| 89 | return Optional.of(new ResourcePath(resources.subList(0, resources.size() - 1))); |
| 90 | } |
| 91 | |
| 92 | return Optional.empty(); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns the last component of this instance. |
| 97 | * |
| 98 | * @return the last component of this instance. |
| 99 | * The return value is equal to the last object of {@code components()}. |
| 100 | */ |
| 101 | public Object lastComponent() { |
| 102 | int last = resources.size() - 1; |
| 103 | return resources.get(last); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public int hashCode() { |
| 108 | return resources.hashCode(); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public boolean equals(Object obj) { |
| 113 | if (this == obj) { |
| 114 | return true; |
| 115 | } |
| 116 | if (!(obj instanceof ResourcePath)) { |
| 117 | return false; |
| 118 | } |
| 119 | final ResourcePath that = (ResourcePath) obj; |
| 120 | return Objects.equals(this.resources, that.resources); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public String toString() { |
| 125 | return MoreObjects.toStringHelper(this) |
| 126 | .add("resources", resources) |
| 127 | .toString(); |
| 128 | } |
| 129 | } |