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