Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [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.group; |
| 17 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 21 | import java.util.Objects; |
| 22 | |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 26 | /** |
| 27 | * Default implementation of group description interface. |
| 28 | */ |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 29 | public class DefaultGroupDescription implements GroupDescription { |
| 30 | private final GroupDescription.Type type; |
| 31 | private final GroupBuckets buckets; |
| 32 | private final GroupKey appCookie; |
| 33 | private final ApplicationId appId; |
| 34 | private final DeviceId deviceId; |
| 35 | |
| 36 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 37 | * Constructor to be used by north bound applications. |
| 38 | * NOTE: The caller of this subsystem MUST ensure the appCookie |
| 39 | * provided in this API is immutable |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 40 | * |
| 41 | * @param deviceId device identifier |
| 42 | * @param type type of the group |
| 43 | * @param buckets immutable list of group bucket |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame^] | 44 | * @param appCookie immutable application cookie of type DefaultGroupKey |
| 45 | * to be associated with the group |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 46 | * @param appId application id |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 47 | */ |
| 48 | public DefaultGroupDescription(DeviceId deviceId, |
| 49 | GroupDescription.Type type, |
| 50 | GroupBuckets buckets, |
| 51 | GroupKey appCookie, |
| 52 | ApplicationId appId) { |
| 53 | this.type = checkNotNull(type); |
| 54 | this.deviceId = checkNotNull(deviceId); |
| 55 | this.buckets = checkNotNull(buckets); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 56 | this.appCookie = appCookie; |
| 57 | this.appId = appId; |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 61 | * Constructor to be used by group subsystem internal components. |
| 62 | * Creates group description object from another object of same type. |
| 63 | * |
| 64 | * @param groupDesc group description object |
| 65 | * |
| 66 | */ |
| 67 | public DefaultGroupDescription(GroupDescription groupDesc) { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 68 | this.type = groupDesc.type(); |
| 69 | this.deviceId = groupDesc.deviceId(); |
| 70 | this.buckets = groupDesc.buckets(); |
| 71 | this.appCookie = groupDesc.appCookie(); |
| 72 | this.appId = groupDesc.appId(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Constructor to be used by group subsystem internal components. |
| 77 | * Creates group description object from the information retrieved |
| 78 | * from data plane. |
| 79 | * |
| 80 | * @param deviceId device identifier |
| 81 | * @param type type of the group |
| 82 | * @param buckets immutable list of group bucket |
| 83 | * |
| 84 | */ |
| 85 | public DefaultGroupDescription(DeviceId deviceId, |
| 86 | GroupDescription.Type type, |
| 87 | GroupBuckets buckets) { |
| 88 | this(deviceId, type, buckets, null, null); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns type of a group object. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 93 | * |
| 94 | * @return GroupType group type |
| 95 | */ |
| 96 | @Override |
| 97 | public GroupDescription.Type type() { |
| 98 | return this.type; |
| 99 | } |
| 100 | |
| 101 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 102 | * Returns device identifier on which this group object is created. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 103 | * |
| 104 | * @return DeviceId device identifier |
| 105 | */ |
| 106 | @Override |
| 107 | public DeviceId deviceId() { |
| 108 | return this.deviceId; |
| 109 | } |
| 110 | |
| 111 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 112 | * Returns application identifier that has created this group object. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 113 | * |
| 114 | * @return ApplicationId application identifier |
| 115 | */ |
| 116 | @Override |
| 117 | public ApplicationId appId() { |
| 118 | return this.appId; |
| 119 | } |
| 120 | |
| 121 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 122 | * Returns application cookie associated with a group object. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 123 | * |
| 124 | * @return GroupKey application cookie |
| 125 | */ |
| 126 | @Override |
| 127 | public GroupKey appCookie() { |
| 128 | return this.appCookie; |
| 129 | } |
| 130 | |
| 131 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 132 | * Returns group buckets of a group. |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 133 | * |
| 134 | * @return GroupBuckets immutable list of group bucket |
| 135 | */ |
| 136 | @Override |
| 137 | public GroupBuckets buckets() { |
| 138 | return this.buckets; |
| 139 | } |
| 140 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 141 | @Override |
| 142 | /* |
| 143 | * The deviceId, type and buckets are used for hash. |
| 144 | * |
| 145 | * (non-Javadoc) |
| 146 | * @see java.lang.Object#equals(java.lang.Object) |
| 147 | */ |
| 148 | public int hashCode() { |
| 149 | return Objects.hash(deviceId, type, buckets); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | /* |
| 154 | * The deviceId, type and buckets should be same. |
| 155 | * |
| 156 | * (non-Javadoc) |
| 157 | * @see java.lang.Object#equals(java.lang.Object) |
| 158 | */ |
| 159 | public boolean equals(Object obj) { |
| 160 | if (this == obj) { |
| 161 | return true; |
| 162 | } |
| 163 | if (obj instanceof DefaultGroupDescription) { |
| 164 | DefaultGroupDescription that = (DefaultGroupDescription) obj; |
| 165 | return Objects.equals(deviceId, that.deviceId) && |
| 166 | Objects.equals(type, that.type) && |
| 167 | Objects.equals(buckets, that.buckets); |
| 168 | |
| 169 | } |
| 170 | return false; |
| 171 | } |
| 172 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 173 | @Override |
| 174 | public String toString() { |
| 175 | return toStringHelper(this) |
| 176 | .add("deviceId", deviceId) |
| 177 | .add("type", type) |
| 178 | .add("buckets", buckets) |
| 179 | .add("appId", appId) |
| 180 | .toString(); |
| 181 | } |
Srikanth Vavilapalli | 56db94f | 2015-01-22 22:30:17 -0800 | [diff] [blame] | 182 | } |