blob: 3fa16afb11d0a7ac8c40c096743292a7fc2130ba [file] [log] [blame]
Sho SHIMIZUabd849c2015-07-14 09:14:12 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Sho SHIMIZUabd849c2015-07-14 09:14:12 -07003 *
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 */
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080016package org.onosproject.net.resource;
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070017
18import com.google.common.annotations.Beta;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070019import com.google.common.base.MoreObjects;
20
21import java.util.Objects;
22
23import static com.google.common.base.Preconditions.checkNotNull;
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070024
25/**
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070026 * Represents allocation of resource which is identified by the specifier.
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070027 */
28@Beta
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070029public class ResourceAllocation {
30
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080031 private final Resource resource;
Naoki Shiotabd1974c2016-04-29 18:44:17 -070032 private final ResourceConsumerId consumerId;
33
34 /**
35 * Creates an instance with the specified subject, resource and consumerId.
36 *
37 * @param resource resource of the subject
38 * @param consumerId consumer ID of this resource
39 */
40 public ResourceAllocation(Resource resource, ResourceConsumerId consumerId) {
41 this.resource = checkNotNull(resource);
42 this.consumerId = checkNotNull(consumerId);
43 }
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070044
45 /**
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070046 * Creates an instance with the specified subject, resource and consumer.
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070047 *
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070048 * @param resource resource of the subject
HIGUCHI Yuta11d16092015-12-04 23:35:43 -080049 * @param consumer consumer of this resource
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070050 */
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080051 public ResourceAllocation(Resource resource, ResourceConsumer consumer) {
Naoki Shiotabd1974c2016-04-29 18:44:17 -070052 this(resource, checkNotNull(consumer).consumerId());
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070053 }
54
55 // for serialization
56 private ResourceAllocation() {
57 this.resource = null;
Naoki Shiotabd1974c2016-04-29 18:44:17 -070058 this.consumerId = null;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070059 }
60
61 /**
62 * Returns the specifier of the resource this allocation uses.
63 *
64 * @return the specifier of the resource this allocation uses
65 */
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080066 public Resource resource() {
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070067 return resource;
68 }
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070069
70 /**
Naoki Shiotabd1974c2016-04-29 18:44:17 -070071 * Returns ID of the consumer of this resource.
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070072 *
Naoki Shiotabd1974c2016-04-29 18:44:17 -070073 * @return ID of the consumer of this resource
Sho SHIMIZUabd849c2015-07-14 09:14:12 -070074 */
Naoki Shiotabd1974c2016-04-29 18:44:17 -070075 public ResourceConsumerId consumerId() {
76 return consumerId;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070077 }
78
79 @Override
80 public int hashCode() {
Naoki Shiotabd1974c2016-04-29 18:44:17 -070081 return Objects.hash(resource, consumerId);
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070082 }
83
84 @Override
85 public boolean equals(Object obj) {
86 if (this == obj) {
87 return true;
88 }
89 if (!(obj instanceof ResourceAllocation)) {
90 return false;
91 }
92 final ResourceAllocation that = (ResourceAllocation) obj;
93 return Objects.equals(this.resource, that.resource)
Naoki Shiotabd1974c2016-04-29 18:44:17 -070094 && Objects.equals(this.consumerId, that.consumerId);
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070095 }
96
97 @Override
98 public String toString() {
99 return MoreObjects.toStringHelper(this)
100 .add("resource", resource)
Naoki Shiotabd1974c2016-04-29 18:44:17 -0700101 .add("consumerId", consumerId)
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700102 .toString();
103 }
Sho SHIMIZUabd849c2015-07-14 09:14:12 -0700104}