blob: 57fab9dd567ed00151f55acbacbcec59e6213f3f [file] [log] [blame]
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -04001/*
2 * Copyright 2017-present 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
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;
50
51 private PiActionGroup(PiActionGroupId id, Type type, ImmutableSet<PiActionGroupMember> members) {
52 this.id = id;
53 this.type = type;
54 this.members = members;
55 }
56
57 /**
58 * Returns the identifier of this action group.
59 *
60 * @return action group identifier
61 */
62 public PiActionGroupId id() {
63 return id;
64 }
65
66 /**
67 * Returns the type of this action group.
68 *
69 * @return action group type
70 */
71 public Type type() {
72 return type;
73 }
74
75 /**
76 * Returns the members of this action group.
77 *
78 * @return collection of action members.
79 */
80 public Collection<PiActionGroupMember> members() {
81 return members;
82 }
83
84 @Override
85 public boolean equals(Object o) {
86 if (this == o) {
87 return true;
88 }
89 if (o == null || getClass() != o.getClass()) {
90 return false;
91 }
92 PiActionGroup that = (PiActionGroup) o;
93 return id == that.id &&
94 Objects.equal(type, that.type) &&
95 Objects.equal(members, that.members);
96 }
97
98 @Override
99 public int hashCode() {
100 return Objects.hashCode(id, type, members);
101 }
102
103 @Override
104 public String toString() {
105 return MoreObjects.toStringHelper(this)
106 .add("groupId", id)
107 .add("type", type)
108 .add("members", members)
109 .toString();
110 }
111
112 /**
113 * Returns a new builder of action groups.
114 *
115 * @return action group builder
116 */
117 public static Builder builder() {
118 return new Builder();
119 }
120
121 /**
122 * Builder of action groups.
123 */
124 public static final class Builder {
125
126 private PiActionGroupId id;
127 private Type type;
128 private Map<PiActionGroupMemberId, PiActionGroupMember> members = Maps.newHashMap();
129
130 private Builder() {
131 // hides constructor.
132 }
133
134 /**
135 * Sets the identifier of this action group.
136 *
137 * @param id action group identifier
138 * @return this
139 */
140 public Builder withId(PiActionGroupId id) {
141 this.id = id;
142 return this;
143 }
144
145 /**
146 * Sets the type of this action group.
147 *
148 * @param type action group type
149 * @return this
150 */
151 public Builder withType(Type type) {
152 this.type = type;
153 return this;
154 }
155
156 /**
157 * Adds one member to this action group.
158 *
159 * @param member action group member
160 * @return this
161 */
162 public Builder addMember(PiActionGroupMember member) {
163 members.put(member.id(), member);
164 return this;
165 }
166
167 /**
168 * Adds many members to this action group.
169 *
170 * @param members action group members
171 * @return this
172 */
173 public Builder addMembers(Collection<PiActionGroupMember> members) {
174 members.forEach(this::addMember);
175 return this;
176 }
177
178 /**
179 * Creates a new action group.
180 *
181 * @return action group
182 */
183 public PiActionGroup build() {
184 checkNotNull(id);
185 checkNotNull(type);
186 checkArgument(members.size() > 0, "Members cannot be empty");
187 return new PiActionGroup(id, type, ImmutableSet.copyOf(members.values()));
188 }
189 }
190}