blob: 1de4d612f4e85996ccfe17c9c6015ecc481cdbc9 [file] [log] [blame]
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -04001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -04003 *
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
17package org.onosproject.net.pi.runtime;
18
19import com.google.common.annotations.Beta;
20import com.google.common.base.MoreObjects;
21import com.google.common.base.Objects;
22import com.google.common.collect.ImmutableSet;
23import com.google.common.collect.Maps;
24
25import java.util.Collection;
26import java.util.Map;
27
28import static com.google.common.base.Preconditions.checkArgument;
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * Action group of a protocol-independent pipeline.
33 */
34@Beta
35public final class PiActionGroup {
36
37 /**
38 * Type of action group.
39 */
40 public enum Type {
41 /**
42 * Load-balancing among different members in a group.
43 */
44 SELECT
45 }
46
47 private final PiActionGroupId id;
48 private final Type type;
49 private final ImmutableSet<PiActionGroupMember> members;
Yi Tseng82512da2017-08-16 19:46:36 -070050 private final PiActionProfileId piActionProfileId;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040051
Yi Tseng82512da2017-08-16 19:46:36 -070052 private PiActionGroup(PiActionGroupId id, Type type,
53 ImmutableSet<PiActionGroupMember> members,
54 PiActionProfileId piActionProfileId) {
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040055 this.id = id;
56 this.type = type;
57 this.members = members;
Yi Tseng82512da2017-08-16 19:46:36 -070058 this.piActionProfileId = piActionProfileId;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040059 }
60
61 /**
62 * Returns the identifier of this action group.
63 *
64 * @return action group identifier
65 */
66 public PiActionGroupId id() {
67 return id;
68 }
69
70 /**
71 * Returns the type of this action group.
72 *
73 * @return action group type
74 */
75 public Type type() {
76 return type;
77 }
78
79 /**
80 * Returns the members of this action group.
81 *
82 * @return collection of action members.
83 */
84 public Collection<PiActionGroupMember> members() {
85 return members;
86 }
87
Yi Tseng82512da2017-08-16 19:46:36 -070088 /**
89 * Gets identifier of the action profile.
90 *
91 * @return action profile id
92 */
93 public PiActionProfileId actionProfileId() {
94 return piActionProfileId;
95 }
96
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -040097 @Override
98 public boolean equals(Object o) {
99 if (this == o) {
100 return true;
101 }
Yi Tseng82512da2017-08-16 19:46:36 -0700102 if (o == null || !(o instanceof PiActionGroup)) {
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400103 return false;
104 }
105 PiActionGroup that = (PiActionGroup) o;
Yi Tseng82512da2017-08-16 19:46:36 -0700106 return Objects.equal(id, that.id) &&
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400107 Objects.equal(type, that.type) &&
Yi Tseng82512da2017-08-16 19:46:36 -0700108 Objects.equal(members, that.members) &&
109 Objects.equal(piActionProfileId, that.piActionProfileId);
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400110 }
111
112 @Override
113 public int hashCode() {
114 return Objects.hashCode(id, type, members);
115 }
116
117 @Override
118 public String toString() {
119 return MoreObjects.toStringHelper(this)
120 .add("groupId", id)
121 .add("type", type)
122 .add("members", members)
Yi Tseng82512da2017-08-16 19:46:36 -0700123 .add("piActionProfileId", piActionProfileId)
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400124 .toString();
125 }
126
127 /**
128 * Returns a new builder of action groups.
129 *
130 * @return action group builder
131 */
132 public static Builder builder() {
133 return new Builder();
134 }
135
136 /**
137 * Builder of action groups.
138 */
139 public static final class Builder {
140
141 private PiActionGroupId id;
142 private Type type;
143 private Map<PiActionGroupMemberId, PiActionGroupMember> members = Maps.newHashMap();
Yi Tseng82512da2017-08-16 19:46:36 -0700144 private PiActionProfileId piActionProfileId;
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400145
146 private Builder() {
147 // hides constructor.
148 }
149
150 /**
151 * Sets the identifier of this action group.
152 *
153 * @param id action group identifier
154 * @return this
155 */
156 public Builder withId(PiActionGroupId id) {
157 this.id = id;
158 return this;
159 }
160
161 /**
162 * Sets the type of this action group.
163 *
164 * @param type action group type
165 * @return this
166 */
167 public Builder withType(Type type) {
168 this.type = type;
169 return this;
170 }
171
172 /**
173 * Adds one member to this action group.
174 *
175 * @param member action group member
176 * @return this
177 */
178 public Builder addMember(PiActionGroupMember member) {
179 members.put(member.id(), member);
180 return this;
181 }
182
183 /**
184 * Adds many members to this action group.
185 *
186 * @param members action group members
187 * @return this
188 */
189 public Builder addMembers(Collection<PiActionGroupMember> members) {
190 members.forEach(this::addMember);
191 return this;
192 }
193
194 /**
Yi Tseng82512da2017-08-16 19:46:36 -0700195 * Sets the identifier of the action profile.
196 *
197 * @param piActionProfileId the identifier of the action profile
198 * @return this
199 */
200 public Builder withActionProfileId(PiActionProfileId piActionProfileId) {
201 this.piActionProfileId = piActionProfileId;
202 return this;
203 }
204
205 /**
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400206 * Creates a new action group.
207 *
208 * @return action group
209 */
210 public PiActionGroup build() {
211 checkNotNull(id);
212 checkNotNull(type);
Yi Tseng82512da2017-08-16 19:46:36 -0700213 checkArgument(!members.isEmpty(), "Members cannot be empty");
214 checkNotNull(piActionProfileId);
215 return new PiActionGroup(id, type,
216 ImmutableSet.copyOf(members.values()),
217 piActionProfileId);
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400218 }
219 }
220}