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