blob: 385a5d6a49400ea6cc15825a532c4bc6cbf47558 [file] [log] [blame]
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -08001/*
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 */
16package org.onosproject.net.group;
17
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080018import static com.google.common.base.MoreObjects.toStringHelper;
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080019import static com.google.common.base.Preconditions.checkNotNull;
20
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080021import java.util.Objects;
22
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080023import org.onosproject.core.ApplicationId;
24import org.onosproject.net.DeviceId;
25
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080026/**
27 * Default implementation of group description interface.
28 */
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080029public 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 Vavilapalli0599d512015-01-30 12:57:56 -080037 * 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 Vavilapalli56db94f2015-01-22 22:30:17 -080040 *
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 Vavilapalli56db94f2015-01-22 22:30:17 -080046 */
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 Vavilapalli45c27c82015-01-30 12:57:56 -080055 this.appCookie = appCookie;
56 this.appId = appId;
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080057 }
58
59 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080060 * 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 Vavilapalli45c27c82015-01-30 12:57:56 -080067 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 Vavilapalli0599d512015-01-30 12:57:56 -080088 }
89
90 /**
91 * Returns type of a group object.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080092 *
93 * @return GroupType group type
94 */
95 @Override
96 public GroupDescription.Type type() {
97 return this.type;
98 }
99
100 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800101 * Returns device identifier on which this group object is created.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800102 *
103 * @return DeviceId device identifier
104 */
105 @Override
106 public DeviceId deviceId() {
107 return this.deviceId;
108 }
109
110 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800111 * Returns application identifier that has created this group object.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800112 *
113 * @return ApplicationId application identifier
114 */
115 @Override
116 public ApplicationId appId() {
117 return this.appId;
118 }
119
120 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800121 * Returns application cookie associated with a group object.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800122 *
123 * @return GroupKey application cookie
124 */
125 @Override
126 public GroupKey appCookie() {
127 return this.appCookie;
128 }
129
130 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800131 * Returns group buckets of a group.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800132 *
133 * @return GroupBuckets immutable list of group bucket
134 */
135 @Override
136 public GroupBuckets buckets() {
137 return this.buckets;
138 }
139
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800140 @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 Vavilapallied12ae52015-02-09 14:43:19 -0800172 @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 Vavilapalli56db94f2015-01-22 22:30:17 -0800181}