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 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | |
| 29 | /** |
| 30 | * An object that is used to locate a resource in a network. |
| 31 | * A ResourcePath represents a path that is hierarchical and composed of a sequence |
| 32 | * of elementary resources that are not globally identifiable. A ResourcePath can be a globally |
| 33 | * unique resource identifier. |
| 34 | * |
| 35 | * Users of this class must keep the semantics of resources regarding the hierarchical structure. |
| 36 | * For example, resource path, Link:1/VLAN ID:100, is valid, but resource path, VLAN ID:100/Link:1 |
| 37 | * is not valid because a link is not a sub-component of a VLAN ID. |
| 38 | */ |
| 39 | @Beta |
| 40 | public final class ResourcePath { |
| 41 | |
| 42 | private final List<Object> resources; |
| 43 | |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 44 | public static final ResourcePath ROOT = new ResourcePath(ImmutableList.of()); |
| 45 | |
| 46 | public static ResourcePath child(ResourcePath parent, Object child) { |
| 47 | ImmutableList<Object> components = ImmutableList.builder() |
| 48 | .addAll(parent.components()) |
| 49 | .add(child) |
| 50 | .build(); |
| 51 | return new ResourcePath(components); |
| 52 | } |
| 53 | |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 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(Object... components) { |
| 60 | this(Arrays.asList(components)); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Creates an resource path from the specified components. |
| 65 | * |
| 66 | * @param components components of the path. The order represents hierarchical structure of the resource. |
| 67 | */ |
| 68 | public ResourcePath(List<Object> components) { |
| 69 | checkNotNull(components); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 70 | |
| 71 | this.resources = ImmutableList.copyOf(components); |
| 72 | } |
| 73 | |
| 74 | // for serialization |
| 75 | private ResourcePath() { |
| 76 | this.resources = null; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns the components of this resource path. |
| 81 | * |
| 82 | * @return the components of this resource path |
| 83 | */ |
| 84 | public List<Object> components() { |
| 85 | return resources; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns the parent resource path of this instance. |
| 90 | * E.g. if this path is Link:1/VLAN ID:100, the return value is the resource path for Link:1. |
| 91 | * |
| 92 | * @return the parent resource path of this instance. |
| 93 | * If there is no parent, empty instance will be returned. |
| 94 | */ |
| 95 | public Optional<ResourcePath> parent() { |
Sho SHIMIZU | 0112078 | 2015-08-21 15:48:43 -0700 | [diff] [blame] | 96 | if (!isRoot()) { |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 97 | return Optional.of(new ResourcePath(resources.subList(0, resources.size() - 1))); |
| 98 | } |
| 99 | |
| 100 | return Optional.empty(); |
| 101 | } |
| 102 | |
| 103 | /** |
Sho SHIMIZU | 0112078 | 2015-08-21 15:48:43 -0700 | [diff] [blame] | 104 | * Returns true if the path represents root. |
| 105 | * |
| 106 | * @return true if the path represents root, false otherwise. |
| 107 | */ |
| 108 | public boolean isRoot() { |
| 109 | return resources.size() == 0; |
| 110 | } |
| 111 | |
| 112 | /** |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 113 | * Returns the last component of this instance. |
| 114 | * |
| 115 | * @return the last component of this instance. |
| 116 | * The return value is equal to the last object of {@code components()}. |
| 117 | */ |
| 118 | public Object lastComponent() { |
| 119 | int last = resources.size() - 1; |
| 120 | return resources.get(last); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public int hashCode() { |
| 125 | return resources.hashCode(); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public boolean equals(Object obj) { |
| 130 | if (this == obj) { |
| 131 | return true; |
| 132 | } |
| 133 | if (!(obj instanceof ResourcePath)) { |
| 134 | return false; |
| 135 | } |
| 136 | final ResourcePath that = (ResourcePath) obj; |
| 137 | return Objects.equals(this.resources, that.resources); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public String toString() { |
| 142 | return MoreObjects.toStringHelper(this) |
| 143 | .add("resources", resources) |
| 144 | .toString(); |
| 145 | } |
| 146 | } |