blob: 3ffb2c26853d5b59666e2436c9751040ac9a8071 [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
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070044 * @param appCookie immutable application cookie of type DefaultGroupKey
45 * to be associated with the group
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080046 * @param appId application id
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080047 */
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 Vavilapalli45c27c82015-01-30 12:57:56 -080056 this.appCookie = appCookie;
57 this.appId = appId;
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080058 }
59
60 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -080061 * 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 Vavilapalli45c27c82015-01-30 12:57:56 -080068 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 Vavilapalli0599d512015-01-30 12:57:56 -080089 }
90
91 /**
92 * Returns type of a group object.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -080093 *
94 * @return GroupType group type
95 */
96 @Override
97 public GroupDescription.Type type() {
98 return this.type;
99 }
100
101 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800102 * Returns device identifier on which this group object is created.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800103 *
104 * @return DeviceId device identifier
105 */
106 @Override
107 public DeviceId deviceId() {
108 return this.deviceId;
109 }
110
111 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800112 * Returns application identifier that has created this group object.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800113 *
114 * @return ApplicationId application identifier
115 */
116 @Override
117 public ApplicationId appId() {
118 return this.appId;
119 }
120
121 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800122 * Returns application cookie associated with a group object.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800123 *
124 * @return GroupKey application cookie
125 */
126 @Override
127 public GroupKey appCookie() {
128 return this.appCookie;
129 }
130
131 /**
Srikanth Vavilapalli0599d512015-01-30 12:57:56 -0800132 * Returns group buckets of a group.
Srikanth Vavilapalli56db94f2015-01-22 22:30:17 -0800133 *
134 * @return GroupBuckets immutable list of group bucket
135 */
136 @Override
137 public GroupBuckets buckets() {
138 return this.buckets;
139 }
140
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800141 @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 Vavilapallied12ae52015-02-09 14:43:19 -0800173 @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 Vavilapalli56db94f2015-01-22 22:30:17 -0800182}